lock_alloc.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. *
  3. * Copyright (C) 2001-2003 FhG Fokus
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * For a license to use the Kamailio software under conditions
  13. * other than those described here, or to purchase support for this
  14. * software, please contact iptel.org by e-mail at the following addresses:
  15. * [email protected]
  16. *
  17. * Kamailio is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /*!
  27. * \file
  28. * \brief Kamailio core :: Kamailio locking library
  29. * \author andrei
  30. * \ingroup core
  31. * Module: \ref core
  32. *
  33. * WARNING: don't include this directly include instead locking.h!
  34. *
  35. Implements: (see also locking.h)
  36. simple locks:
  37. -------------
  38. gen_lock_t* lock_alloc(); - allocates a lock in shared mem.
  39. void lock_dealloc(gen_lock_t* lock); - deallocates the lock's shared m.
  40. lock sets: [implemented only for FL & SYSV so far]
  41. ----------
  42. gen_lock_set_t* lock_set_alloc(no) - allocs a lock set in shm.
  43. void lock_set_dealloc(gen_lock_set_t* s); - deallocs the lock set shm.
  44. */
  45. #ifndef _lock_alloc_h
  46. #define _lock_alloc_h
  47. /*shm_{malloc, free}*/
  48. #include "mem/mem.h"
  49. #ifdef SHM_MEM
  50. #include "mem/shm_mem.h"
  51. #else
  52. #error "locking requires shared memory support"
  53. #endif
  54. #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
  55. /* simple locks*/
  56. #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
  57. #define lock_dealloc(lock) shm_free((void*)lock)
  58. /* lock sets */
  59. inline static gen_lock_set_t* lock_set_alloc(int n)
  60. {
  61. gen_lock_set_t* ls;
  62. ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t)+n*sizeof(gen_lock_t));
  63. if (ls==0){
  64. LM_CRIT("could not allocate lock_set\n");
  65. }else{
  66. ls->locks=(gen_lock_t*)((char*)ls+sizeof(gen_lock_set_t));
  67. ls->size=n;
  68. }
  69. return ls;
  70. }
  71. #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
  72. #elif defined USE_SYSV_SEM
  73. /*simple locks*/
  74. #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
  75. #define lock_dealloc(lock) shm_free((void*)lock)
  76. /* lock sets */
  77. inline static gen_lock_set_t* lock_set_alloc(int n)
  78. {
  79. gen_lock_set_t* ls;
  80. ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t));
  81. if (ls){
  82. ls->size=n;
  83. ls->semid=-1;
  84. };
  85. return ls;
  86. }
  87. #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
  88. #else
  89. #error "no locking method selected"
  90. #endif
  91. #endif