msg_queue.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include <stdio.h>
  26. #include <cds/msg_queue.h>
  27. #include <cds/memory.h>
  28. #include <cds/ref_cntr.h>
  29. #include <cds/logger.h>
  30. mq_message_t *create_message_ex(int data_len)
  31. {
  32. mq_message_t *m;
  33. if (data_len < 0) data_len = 0;
  34. m = cds_malloc(data_len + sizeof(mq_message_t));
  35. if (!m) return NULL;
  36. m->data_len = data_len;
  37. m->data = (((char *)m) + sizeof(mq_message_t));
  38. m->next = NULL;
  39. m->allocation_style = message_allocated_with_data;
  40. m->destroy_function = NULL;
  41. return m;
  42. }
  43. mq_message_t *create_message(void *data, int data_len)
  44. {
  45. mq_message_t *m;
  46. /* if (data_len < 0) data_len = 0; */
  47. m = cds_malloc(sizeof(mq_message_t));
  48. if (!m) return NULL;
  49. m->data_len = data_len;
  50. m->data = data;
  51. m->next = NULL;
  52. m->allocation_style = message_holding_data_ptr;
  53. m->destroy_function = cds_free_ptr;
  54. return m;
  55. }
  56. void init_message_ex(mq_message_t *m, void *data, int data_len, destroy_function_f func)
  57. {
  58. /* if (data_len < 0) data_len = 0; */
  59. if (!m) return;
  60. m->data_len = data_len;
  61. m->data = data;
  62. m->next = NULL;
  63. m->destroy_function = func;
  64. m->allocation_style = message_holding_data_ptr;
  65. }
  66. void set_data_destroy_function(mq_message_t *msg, destroy_function_f func)
  67. {
  68. if (msg) msg->destroy_function = func;
  69. }
  70. void free_message(mq_message_t *msg)
  71. {
  72. if (msg->destroy_function && msg->data)
  73. msg->destroy_function(msg->data);
  74. switch (msg->allocation_style) {
  75. case message_allocated_with_data:
  76. break;
  77. case message_holding_data_ptr:
  78. /* if (msg->data) cds_free(msg->data); */
  79. break;
  80. }
  81. cds_free(msg);
  82. }
  83. int push_message(msg_queue_t *q, mq_message_t *m)
  84. {
  85. if ((!q) || (!m)) return -1;
  86. m->next = NULL;
  87. if (q->flags & MQ_USE_MUTEX) cds_mutex_lock(&q->q_mutex);
  88. if (q->last) q->last->next = m;
  89. else {
  90. q->first = m;
  91. q->last = m;
  92. }
  93. q->last = m;
  94. if (q->flags & MQ_USE_MUTEX) cds_mutex_unlock(&q->q_mutex);
  95. return 0;
  96. }
  97. int mq_add_to_top(msg_queue_t *q, mq_message_t *m)
  98. {
  99. if ((!q) || (!m)) return -1;
  100. m->next = NULL;
  101. if (q->flags & MQ_USE_MUTEX) cds_mutex_lock(&q->q_mutex);
  102. m->next = q->first;
  103. q->first = m;
  104. if (!q->last) q->last = m;
  105. if (q->flags & MQ_USE_MUTEX) cds_mutex_unlock(&q->q_mutex);
  106. return 0;
  107. }
  108. mq_message_t *pop_message(msg_queue_t *q)
  109. {
  110. mq_message_t *m;
  111. if (!q) return NULL;
  112. if (q->flags & MQ_USE_MUTEX) cds_mutex_lock(&q->q_mutex);
  113. m = q->first;
  114. if (m) {
  115. if (q->first == q->last) {
  116. q->first = NULL;
  117. q->last = NULL;
  118. }
  119. else q->first = m->next;
  120. m->next = NULL;
  121. }
  122. if (q->flags & MQ_USE_MUTEX) cds_mutex_unlock(&q->q_mutex);
  123. return m;
  124. }
  125. int is_msg_queue_empty(msg_queue_t *q)
  126. {
  127. int res = 1;
  128. if (q->flags & MQ_USE_MUTEX) cds_mutex_lock(&q->q_mutex);
  129. if (q->first) res = 0;
  130. if (q->flags & MQ_USE_MUTEX) cds_mutex_unlock(&q->q_mutex);
  131. return res;
  132. }
  133. int msg_queue_init(msg_queue_t *q)
  134. {
  135. return msg_queue_init_ex(q, 1);
  136. }
  137. int msg_queue_init_ex(msg_queue_t *q, int synchronize)
  138. {
  139. if (synchronize) {
  140. cds_mutex_init(&q->q_mutex);
  141. q->flags = MQ_USE_MUTEX;
  142. }
  143. else q->flags = 0;
  144. q->first = NULL;
  145. q->last = NULL;
  146. return 0;
  147. }
  148. /** \internal Destroys all internal data of message queue and
  149. * optionaly frees it if no more references exist. */
  150. static inline void msg_queue_destroy_and_free(msg_queue_t *q, int do_free)
  151. {
  152. mq_message_t *m,*n;
  153. if (!q) return;
  154. if (q->flags & MQ_USE_REF_CNTR) {
  155. if (!remove_reference(&q->ref)) {
  156. /* this was NOT the last reference */
  157. return;
  158. }
  159. }
  160. if (q->flags & MQ_USE_MUTEX) cds_mutex_lock(&q->q_mutex);
  161. m = q->first;
  162. while (m) {
  163. n = m->next;
  164. free_message(m);
  165. m = n;
  166. }
  167. q->first = NULL;
  168. q->last = NULL;
  169. if (q->flags & MQ_USE_MUTEX) {
  170. cds_mutex_unlock(&q->q_mutex);
  171. cds_mutex_destroy(&q->q_mutex);
  172. }
  173. if (do_free) cds_free(q);
  174. }
  175. void msg_queue_destroy(msg_queue_t *q)
  176. {
  177. msg_queue_destroy_and_free(q, 0);
  178. }
  179. void msg_queue_free(msg_queue_t *q)
  180. {
  181. msg_queue_destroy_and_free(q, 1);
  182. }
  183. void msg_queue_init_ref_cnt(msg_queue_t *q, reference_counter_group_t *grp)
  184. {
  185. if (grp) {
  186. init_reference_counter(grp, &q->ref);
  187. q->flags |= MQ_USE_REF_CNTR;
  188. }
  189. else q->flags &= ~MQ_USE_REF_CNTR; /* don't use reference counter */
  190. }