shm_mq.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*-------------------------------------------------------------------------
  2. *
  3. * shm_mq.h
  4. * single-reader, single-writer shared memory message queue
  5. *
  6. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/storage/shm_mq.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef SHM_MQ_H
  14. #define SHM_MQ_H
  15. #include "postmaster/bgworker.h"
  16. #include "storage/dsm.h"
  17. #include "storage/proc.h"
  18. /* The queue itself, in shared memory. */
  19. struct shm_mq;
  20. typedef struct shm_mq shm_mq;
  21. /* Backend-private state. */
  22. struct shm_mq_handle;
  23. typedef struct shm_mq_handle shm_mq_handle;
  24. /* Descriptors for a single write spanning multiple locations. */
  25. typedef struct
  26. {
  27. const char *data;
  28. Size len;
  29. } shm_mq_iovec;
  30. /* Possible results of a send or receive operation. */
  31. typedef enum
  32. {
  33. SHM_MQ_SUCCESS, /* Sent or received a message. */
  34. SHM_MQ_WOULD_BLOCK, /* Not completed; retry later. */
  35. SHM_MQ_DETACHED /* Other process has detached queue. */
  36. } shm_mq_result;
  37. /*
  38. * Primitives to create a queue and set the sender and receiver.
  39. *
  40. * Both the sender and the receiver must be set before any messages are read
  41. * or written, but they need not be set by the same process. Each must be
  42. * set exactly once.
  43. */
  44. extern shm_mq *shm_mq_create(void *address, Size size);
  45. extern void shm_mq_set_receiver(shm_mq *mq, PGPROC *);
  46. extern void shm_mq_set_sender(shm_mq *mq, PGPROC *);
  47. /* Accessor methods for sender and receiver. */
  48. extern PGPROC *shm_mq_get_receiver(shm_mq *);
  49. extern PGPROC *shm_mq_get_sender(shm_mq *);
  50. /* Set up backend-local queue state. */
  51. extern shm_mq_handle *shm_mq_attach(shm_mq *mq, dsm_segment *seg,
  52. BackgroundWorkerHandle *handle);
  53. /* Associate worker handle with shm_mq. */
  54. extern void shm_mq_set_handle(shm_mq_handle *, BackgroundWorkerHandle *);
  55. /* Break connection. */
  56. extern void shm_mq_detach(shm_mq *);
  57. /* Get the shm_mq from handle. */
  58. extern shm_mq *shm_mq_get_queue(shm_mq_handle *mqh);
  59. /* Send or receive messages. */
  60. extern shm_mq_result shm_mq_send(shm_mq_handle *mqh,
  61. Size nbytes, const void *data, bool nowait);
  62. extern shm_mq_result shm_mq_sendv(shm_mq_handle *mqh,
  63. shm_mq_iovec *iov, int iovcnt, bool nowait);
  64. extern shm_mq_result shm_mq_receive(shm_mq_handle *mqh,
  65. Size *nbytesp, void **datap, bool nowait);
  66. /* Wait for our counterparty to attach to the queue. */
  67. extern shm_mq_result shm_mq_wait_for_attach(shm_mq_handle *mqh);
  68. /* Smallest possible queue. */
  69. extern PGDLLIMPORT const Size shm_mq_minimum_size;
  70. #endif /* SHM_MQ_H */