mohq_locks.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2013 Robert Boisvert
  5. *
  6. * This file is part of the mohqueue module for sip-router, a free SIP server.
  7. *
  8. * The mohqueue module is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * The mohqueue module is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include "mohq.h"
  24. #include "mohq_locks.h"
  25. /**********
  26. * external functions
  27. **********/
  28. /**********
  29. * Change Lock
  30. *
  31. * INPUT:
  32. * Arg (1) = lock pointer
  33. * Arg (2) = exclusive flag
  34. * OUTPUT: 0 if failed
  35. **********/
  36. int mohq_lock_change (mohq_lock *plock, int bexcl)
  37. {
  38. /**********
  39. * o lock memory
  40. * o check set type
  41. * o unlock memory
  42. **********/
  43. int nret = 0;
  44. lock_get (plock->plock);
  45. if (bexcl)
  46. {
  47. if (plock->lock_cnt == 1)
  48. {
  49. plock->lock_cnt = -1;
  50. nret = 1;
  51. }
  52. }
  53. else
  54. {
  55. if (plock->lock_cnt == -1)
  56. {
  57. plock->lock_cnt = 1;
  58. nret = 1;
  59. }
  60. }
  61. lock_release (plock->plock);
  62. return nret;
  63. }
  64. /**********
  65. * Destroy Lock Record
  66. *
  67. * INPUT:
  68. * Arg (1) = lock pointer
  69. * OUTPUT: none
  70. **********/
  71. void mohq_lock_destroy (mohq_lock *plock)
  72. {
  73. lock_destroy (plock->plock);
  74. lock_dealloc (plock->plock);
  75. return;
  76. }
  77. /**********
  78. * Init Lock Record
  79. *
  80. * INPUT:
  81. * Arg (1) = lock pointer
  82. * OUTPUT: 0 if failed
  83. **********/
  84. int mohq_lock_init (mohq_lock *plock)
  85. {
  86. /**********
  87. * alloc memory and initialize
  88. **********/
  89. char *pfncname = "mohq_lock_init: ";
  90. plock->plock = lock_alloc ();
  91. if (!plock->plock)
  92. {
  93. LM_ERR ("%sUnable to allocate lock memory!", pfncname);
  94. return 0;
  95. }
  96. if (!lock_init (plock->plock))
  97. {
  98. LM_ERR ("%sUnable to init lock!", pfncname);
  99. lock_dealloc (plock->plock);
  100. return 0;
  101. }
  102. plock->lock_cnt = 0;
  103. return -1;
  104. }
  105. /**********
  106. * Release Lock
  107. *
  108. * INPUT:
  109. * Arg (1) = lock pointer
  110. * OUTPUT: none
  111. **********/
  112. void mohq_lock_release (mohq_lock *plock)
  113. {
  114. /**********
  115. * o lock memory
  116. * o reduce count
  117. * o unlock memory
  118. **********/
  119. lock_get (plock->plock);
  120. switch (plock->lock_cnt)
  121. {
  122. case -1:
  123. plock->lock_cnt = 0;
  124. break;
  125. case 0:
  126. LM_WARN ("mohq_lock_release: Lock was not set");
  127. break;
  128. default:
  129. plock->lock_cnt--;
  130. break;
  131. }
  132. lock_release (plock->plock);
  133. return;
  134. }
  135. /**********
  136. * Set Lock
  137. *
  138. * INPUT:
  139. * Arg (1) = lock pointer
  140. * Arg (2) = exclusive flag
  141. * Arg (3) = milliseconds to try; 0 = try once
  142. * OUTPUT: 0 if failed
  143. **********/
  144. int mohq_lock_set (mohq_lock *plock, int bexcl, int nms_cnt)
  145. {
  146. int nret = 0;
  147. do
  148. {
  149. /**********
  150. * o lock memory
  151. * o check set type
  152. * o unlock memory
  153. * o sleep if failed
  154. **********/
  155. lock_get (plock->plock);
  156. if (bexcl)
  157. {
  158. if (!plock->lock_cnt)
  159. {
  160. plock->lock_cnt = -1;
  161. nret = 1;
  162. }
  163. }
  164. else
  165. {
  166. if (plock->lock_cnt != -1)
  167. {
  168. plock->lock_cnt++;
  169. nret = 1;
  170. }
  171. }
  172. lock_release (plock->plock);
  173. if (!nret)
  174. { usleep (1); }
  175. }
  176. while (!nret && --nms_cnt >= 0);
  177. return nret;
  178. }