locking.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* $Id$ */
  2. /*
  3. *
  4. * Copyright (C) 2001-2003 Fhg Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*
  28. * ser locking library
  29. *
  30. * 2002-12-16 created by andrei
  31. *
  32. *
  33. Implements:
  34. lock_t* lock_alloc(); - allocates a lock in shared mem.
  35. lock_t* lock_init(lock_t* lock); - inits the lock
  36. void lock_destroy(lock_t* lock); - removes the lock (e.g sysv rmid)
  37. void lock_dealloc(lock_t* lock); - deallocates the lock's shared m.
  38. void lock_get(lock_t* lock); - lock (mutex down)
  39. void lock_release(lock_t* lock); - unlock (mutex up)
  40. */
  41. #ifndef _locking_h
  42. #define _locking_h
  43. #include "mem/mem.h"
  44. #ifdef SHM_MEM
  45. #include "mem/shm_mem.h"
  46. #else
  47. #error "locking requires shared memroy support"
  48. #endif
  49. #ifdef FAST_LOCK
  50. #include "fastlock.h"
  51. typedef fl_lock_t lock_t;
  52. #define lock_alloc() shm_malloc(sizeof(lock_t))
  53. #define lock_destroy(lock) /* do nothing */
  54. #define lock_dealloc(lock) shm_free(lock)
  55. inline static lock_t* lock_init(lock_t* lock)
  56. {
  57. init_lock(*lock);
  58. return lock;
  59. }
  60. #define lock_get(lock) get_lock(lock)
  61. #define lock_release(lock) release_lock(lock)
  62. #elif defined USE_PTHREAD_MUTEX
  63. #include <pthread.h>
  64. typedef pthread_mutex_t lock_t;
  65. #define lock_alloc() shm_malloc(sizeof(lock_t))
  66. #define lock_destroy(lock) /* do nothing */
  67. #define lock_dealloc(lock) shm_free(lock)
  68. inline static lock_t* lock_init(lock_t* lock)
  69. {
  70. if (pthread_mutex_init(lock, 0)==0) return lock;
  71. else return 0;
  72. }
  73. #define lock_get(lock) pthread_mutex_lock(lock)
  74. #define lock_release(lock) pthread_mutex_unlock(lock)
  75. #elif defined USE_POSIX_SEM
  76. #include <semaphore.h>
  77. typedef sem_t lock_t;
  78. #define lock_alloc() shm_malloc(sizeof(lock_t))
  79. #define lock_destroy(lock) /* do nothing */
  80. #define lock_dealloc(lock) shm_free(lock)
  81. inline static lock_t* lock_init(lock_t* lock)
  82. {
  83. if (sem_init(lock, 1, 1)<0) return 0;
  84. return lock;
  85. }
  86. #define lock_get(lock) sem_wait(lock)
  87. #define lock_release(lock) sem_post(lock)
  88. #elif defined USE_SYSV_SEM
  89. #include <sys/ipc.h>
  90. #include <sys/sem.h>
  91. #if ((defined(HAVE_UNION_SEMUN) || defined(__GNU_LIBRARY__) )&& !defined(_SEM_SEMUN_UNDEFINED))
  92. /* union semun is defined by including sem.h */
  93. #else
  94. /* according to X/OPEN we have to define it ourselves */
  95. union semun {
  96. int val; /* value for SETVAL */
  97. struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
  98. unsigned short int *array; /* array for GETALL, SETALL */
  99. struct seminfo *__buf; /* buffer for IPC_INFO */
  100. };
  101. #endif
  102. typedef int lock_t;
  103. #define lock_alloc() shm_malloc(sizeof(lock_t))
  104. #define lock_dealloc(lock) shm_free(lock)
  105. inline static lock_t* lock_init(lock_t* lock)
  106. {
  107. union semun su;
  108. *lock=semget(IPC_PRIVATE, 1, 0700);
  109. if (*lock==-1) return 0;
  110. su.val=1;
  111. if (semctl(*lock, 0, SETVAL, su)==-1){
  112. /* init error*/
  113. return 0;
  114. }
  115. return lock;
  116. }
  117. inline static void lock_destroy(lock_t* lock)
  118. {
  119. semctl(*lock, 0, IPC_RMID, (union semun)(int)0);
  120. }
  121. #define lock_dealloc(lock) shm_free(lock)
  122. inline static void lock_get(lock_t* lock)
  123. {
  124. struct sembuf sop;
  125. sop.sem_num=0;
  126. sop.sem_op=-1; /* down */
  127. sop.sem_flg=0; /*SEM_UNDO*/
  128. semop(*lock, &sop, 1);
  129. }
  130. inline static void lock_release(lock_t* lock)
  131. {
  132. struct sembuf sop;
  133. sop.sem_num=0;
  134. sop.sem_op=1; /* up */
  135. sop.sem_flg=0; /* SEM_UNDO*/
  136. semop(*lock, &sop, 1);
  137. }
  138. #else
  139. #error "no locking method selected"
  140. #endif
  141. #endif