msg_queue.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #ifndef __MSG_QUEUE_H
  26. #define __MSG_QUEUE_H
  27. #include <cds/sync.h>
  28. #include <cds/ref_cntr.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /** \ingroup cds
  33. * @{
  34. *
  35. * \defgroup cds_msg_queue Message Queue
  36. *
  37. * Message queue is a structure useful for sending data between processes.
  38. * It can be synchronized via its own mutex or the synchronization can
  39. * be left on caller. It can use reference counter which is useful
  40. * when accessing dynamicaly allocated queue destroyed by its last user.
  41. *
  42. * \todo To meaningfully use reference counters it is needed to add
  43. * function for adding new reference to message queue.
  44. *
  45. * \todo Introduce message types because it is often needed.
  46. * @{
  47. * */
  48. typedef void (*destroy_function_f)(void *);
  49. /** Structure holding message which can be put
  50. * into message queue.
  51. *
  52. * There is a need to allow destroing the message without knowing its
  53. * internals (destroying message queue with non-processed messages) and thus
  54. * the destroy_function able to fully destroy whole data hold by message must
  55. * be given. It is mostly needed to choose the function manually only for
  56. * complex data with pointers which content need to be freed too.
  57. * For simple structures it is set automaticaly during the message creation.
  58. */
  59. typedef struct _mq_message_t {
  60. /** pointer to data hold by the message */
  61. void *data;
  62. /** length of data hold by message */
  63. int data_len;
  64. /** pointer to next message in the queue - is used only when
  65. * message is in the queue */
  66. struct _mq_message_t *next;
  67. /** pointer to destroy function */
  68. destroy_function_f destroy_function;
  69. enum {
  70. message_allocated_with_data, /**< data are allocated together with message structure */
  71. message_holding_data_ptr /**< message holds only pointer to data */
  72. } allocation_style; /**< member describing the manner of data storage */
  73. char data_buf[1]; /**< data buffer used when data are allocated together with message */
  74. } mq_message_t;
  75. /** Message queue flag meaning that internal queue mutex is used for
  76. * synchronization. It is set during message queue initialization via \ref
  77. * msg_queue_init or \ref msg_queue_init_ex. */
  78. #define MQ_USE_MUTEX 1
  79. /** Message queue flag meaning that reference counters are used.
  80. * To set this flag is needed to call \ref msg_queue_init_ref_cnt with
  81. * non-NULL group parameter. */
  82. #define MQ_USE_REF_CNTR 2
  83. /** Message queue structure.
  84. * Never access its members directly (they may change), always
  85. * use interface functions!
  86. */
  87. typedef struct msg_queue {
  88. /** Reference counter. Need not to be used.
  89. * If you want to use reference counter to message queue you have
  90. * to call \ref msg_queue_init_ref_cnt function with not-NULL reference
  91. * counter group.*/
  92. reference_counter_data_t ref;
  93. /** Pointer to the first message in the queue. Messages are hold in
  94. * one way linked list, each message contains pointer to next one. */
  95. mq_message_t *first;
  96. /** Pointer to last message in the queue to speed up appending messages
  97. * into the queue.*/
  98. mq_message_t *last;
  99. /** Queue mutex - might not be initialized, depends on initialization
  100. * parameters */
  101. cds_mutex_t q_mutex;
  102. /** flags - see MQ_xxx constants */
  103. unsigned int flags;
  104. } msg_queue_t;
  105. /** Macro for accessing message data.
  106. * It is better to use this macro than accessing internal members of
  107. * the structure. */
  108. #define get_message_data(msg) (msg ? msg->data: NULL)
  109. /** Macro for determining message data length.
  110. * It is better to use this macro than accessing internal members of
  111. * the structure. */
  112. #define get_message_data_len(msg) (msg ? msg->data_len: 0)
  113. /** The space for data is allocated in messages data
  114. * (they are automaticaly freed!)! Pointer to allocated
  115. * data bytes is in data variable in the message structure. */
  116. mq_message_t *create_message_ex(int data_len);
  117. /** Creates message holding data allocated using cds_malloc.
  118. * Data must be allocated using cds_malloc or there must be
  119. * set destroy function via \ref set_data_destroy_function
  120. * because they are automaticaly freed by free_message! */
  121. mq_message_t *create_message(void *data, int data_len);
  122. /** Sets function which will be called by free_message to destroy data.
  123. *
  124. * This function may be useful when a complex structure with pointers is added
  125. * as data parameter. */
  126. void set_data_destroy_function(mq_message_t *msg, destroy_function_f func);
  127. /** Initializes message.
  128. * If auto_free set, data must be allocated using cds_malloc and are
  129. * automaticaly freed by free_message (and if msg_queue_destroy called) */
  130. void init_message_ex(mq_message_t *m, void *data, int data_len, destroy_function_f func);
  131. /** Frees the message and data hold by the message. */
  132. void free_message(mq_message_t *msg);
  133. /** Put message into queue. */
  134. int push_message(msg_queue_t *q, mq_message_t *m);
  135. /** Remove message from queue. */
  136. mq_message_t *pop_message(msg_queue_t *q);
  137. /** Tests if message queue holds a message.
  138. * \retval 1 if empty
  139. * \retval 0 if NOT empty. */
  140. int is_msg_queue_empty(msg_queue_t *q);
  141. /** Initializes message queue with a mutex guarding queue operations. */
  142. int msg_queue_init(msg_queue_t *q);
  143. /** Initializes message queue. If synchronize is set it initializes
  144. * a mutex guarding queue operations otherwise the message queue remains
  145. * unsynchronized. */
  146. int msg_queue_init_ex(msg_queue_t *q, int synchronize);
  147. /** Initializes reference counter for given message queue
  148. * \param grp specifies group of reference counters to use. The message
  149. * queue will stop using the reference counter if NULL.
  150. * \param q specifies the message queue */
  151. void msg_queue_init_ref_cnt(msg_queue_t *q, reference_counter_group_t *grp);
  152. /** Destroys message queue if no more references exist.
  153. * This function destroys all message queue internal data but doesn't free
  154. * the message queue itself. It can be useful for staticaly allocated queues
  155. * or when allocated not using cds_malloc. */
  156. void msg_queue_destroy(msg_queue_t *q);
  157. /** Destroys and frees the message queue if no more references exist.
  158. * It uses cds_free for freeing the memory. */
  159. void msg_queue_free(msg_queue_t *q);
  160. /** @}
  161. @} */
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif