ipc.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*-------------------------------------------------------------------------
  2. *
  3. * ipc.h
  4. * POSTGRES inter-process communication definitions.
  5. *
  6. * This file is misnamed, as it no longer has much of anything directly
  7. * to do with IPC. The functionality here is concerned with managing
  8. * exit-time cleanup for either a postmaster or a backend.
  9. *
  10. *
  11. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  12. * Portions Copyright (c) 1994, Regents of the University of California
  13. *
  14. * src/include/storage/ipc.h
  15. *
  16. *-------------------------------------------------------------------------
  17. */
  18. #ifndef IPC_H
  19. #define IPC_H
  20. typedef void (*pg_on_exit_callback) (int code, Datum arg);
  21. typedef void (*shmem_startup_hook_type) (void);
  22. /*----------
  23. * API for handling cleanup that must occur during either ereport(ERROR)
  24. * or ereport(FATAL) exits from a block of code. (Typical examples are
  25. * undoing transient changes to shared-memory state.)
  26. *
  27. * PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg);
  28. * {
  29. * ... code that might throw ereport(ERROR) or ereport(FATAL) ...
  30. * }
  31. * PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg);
  32. *
  33. * where the cleanup code is in a function declared per pg_on_exit_callback.
  34. * The Datum value "arg" can carry any information the cleanup function
  35. * needs.
  36. *
  37. * This construct ensures that cleanup_function() will be called during
  38. * either ERROR or FATAL exits. It will not be called on successful
  39. * exit from the controlled code. (If you want it to happen then too,
  40. * call the function yourself from just after the construct.)
  41. *
  42. * Note: the macro arguments are multiply evaluated, so avoid side-effects.
  43. *----------
  44. */
  45. #define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg) \
  46. do { \
  47. before_shmem_exit(cleanup_function, arg); \
  48. PG_TRY()
  49. #define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg) \
  50. cancel_before_shmem_exit(cleanup_function, arg); \
  51. PG_CATCH(); \
  52. { \
  53. cancel_before_shmem_exit(cleanup_function, arg); \
  54. cleanup_function (0, arg); \
  55. PG_RE_THROW(); \
  56. } \
  57. PG_END_TRY(); \
  58. } while (0)
  59. /* ipc.c */
  60. extern PGDLLIMPORT bool proc_exit_inprogress;
  61. extern void proc_exit(int code) pg_attribute_noreturn();
  62. extern void shmem_exit(int code);
  63. extern void on_proc_exit(pg_on_exit_callback function, Datum arg);
  64. extern void on_shmem_exit(pg_on_exit_callback function, Datum arg);
  65. extern void before_shmem_exit(pg_on_exit_callback function, Datum arg);
  66. extern void cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg);
  67. extern void on_exit_reset(void);
  68. /* ipci.c */
  69. extern PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook;
  70. extern void CreateSharedMemoryAndSemaphores(bool makePrivate, int port);
  71. #endif /* IPC_H */