lock_alloc.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * ser locking library
  29. * WARNING: don't include this directly include instead locking.h!
  30. * History:
  31. * --------
  32. * 2003-03-06 created by andrei (contains parts of the original locking.h)
  33. * 2003-03-17 fixed cast warning in shm_free (forced to void*) (andrei)
  34. * 2004-07-28 s/lock_set_t/gen_lock_set_t/ because of a type conflict
  35. * on darwin (andrei)
  36. *
  37. Implements: (see also locking.h)
  38. simple locks:
  39. -------------
  40. gen_lock_t* lock_alloc(); - allocates a lock in shared mem.
  41. void lock_dealloc(gen_lock_t* lock); - deallocates the lock's shared m.
  42. lock sets: [implemented only for FL & SYSV so far]
  43. ----------
  44. gen_lock_set_t* lock_set_alloc(no) - allocs a lock set in shm.
  45. void lock_set_dealloc(gen_lock_set_t* s); - deallocs the lock set shm.
  46. */
  47. #ifndef _lock_alloc_h
  48. #define _lock_alloc_h
  49. /*shm_{malloc, free}*/
  50. #include "mem/mem.h"
  51. #ifdef SHM_MEM
  52. #include "mem/shm_mem.h"
  53. #else
  54. #error "locking requires shared memory support"
  55. #endif
  56. #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
  57. /* simple locks*/
  58. #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
  59. #define lock_dealloc(lock) shm_free((void*)lock)
  60. /* lock sets */
  61. inline static gen_lock_set_t* lock_set_alloc(int n)
  62. {
  63. gen_lock_set_t* ls;
  64. ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t)+n*sizeof(gen_lock_t));
  65. if (ls==0){
  66. LM_CRIT("could not allocate lock_set\n");
  67. }else{
  68. ls->locks=(gen_lock_t*)((char*)ls+sizeof(gen_lock_set_t));
  69. ls->size=n;
  70. }
  71. return ls;
  72. }
  73. #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
  74. #elif defined USE_SYSV_SEM
  75. /*simple locks*/
  76. #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
  77. #define lock_dealloc(lock) shm_free((void*)lock)
  78. /* lock sets */
  79. inline static gen_lock_set_t* lock_set_alloc(int n)
  80. {
  81. gen_lock_set_t* ls;
  82. ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t));
  83. if (ls){
  84. ls->size=n;
  85. ls->semid=-1;
  86. };
  87. return ls;
  88. }
  89. #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
  90. #else
  91. #error "no locking method selected"
  92. #endif
  93. #endif