locking.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. Kamailio locking interface
  2. ============================
  3. 1. Why use it?
  4. --------------
  5. The main reason for creating it was to have a single transparent interface to various locking methods.
  6. For example right now Kamailio uses the following locking methods, depending on their availability on the
  7. target system:
  8. FAST_LOCK - fast inline assembly locks, defined in fast_lock.h. They are currently available for
  9. x86, x86_64, sparc, sparc64, arm , armv6 (no smp mode supported yet), ppc, ppc64, mips, mips64
  10. and alpha . In general if the assembly code exists for a given arhitecture and the compiler
  11. knows inline assembly (for example sun cc does not) FAST_LOCK is prefered. The main
  12. advantage of using FAST_LOCK is very low memory overhead and extremely fast lock/unlock
  13. operations (like 20 times faster then SYSV semaphores on linux & 40 times on solaris).
  14. The only thing that comes close to them are pthread mutexes (which are about 3-4 times slower).
  15. PTHREAD_MUTEX - uses pthread_mutex_lock/unlock. They are quite fast but they work between
  16. processes only on some systems (they do not work on linux).
  17. POSIX_SEM - uses posix semaphores (sem_wait/sem_post). They are slower then the previous
  18. methods but still way faster then SYSV sempahores. Unfortunately they also do not work on
  19. all the systems (e.g. linux).
  20. SYSV_SEM - this is the most portable but also the slowest locking method. Another problem is
  21. that the number of semaphores that can be alocated by a process is limited. One also has to
  22. free them before exiting.
  23. 2. How to use it?
  24. -----------------
  25. First of all you have to include locking.h. Then when compiling the code one or all of FAST_LOCK,
  26. USE_PTHREAD_MUTEX, USE_PTHREAD_SEM or USE_SYSV_SEM must be defined (the Kamailio Makefile.defs takes
  27. care of this, you should need to change it only for new arhitectures or compilers).
  28. locking.h defines 2 new types: gen_lock_t and lock_set_t.
  29. 3. Simple locks
  30. ---------------
  31. The simple locks are simple mutexes. The type is gen_lock_t.
  32. WARNING: do not make any assumptions on gen_lock_t base type, it does not have to be always an int.
  33. Allocation & initialization:
  34. ----------------------------
  35. The locks are allocated with:
  36. gen_lock_t* lock_alloc();
  37. and initialized with:
  38. gen_lock_t* lock_init(gen_lock_t* lock);
  39. Both function return 0 on failure.
  40. The locks must be initialized before use.
  41. A proper alloc/init sequence looks like:
  42. gen_lock_t* lock;
  43. lock=lock_alloc();
  44. if (lock==0) goto error;
  45. if (lock_init(lock)==0){
  46. lock_dealloc(lock);
  47. goto error; /* could not init lock*/
  48. }
  49. ...
  50. Lock allocation can be skipped in some cases: if the lock is already in shared memory you don't need to
  51. allocate it again, you can initialize it directly, but keep in mind that the lock MUST be in shared memory.
  52. Example:
  53. struct s {
  54. int foo;
  55. gen_lock_t lock;
  56. } bar;
  57. bar=shm_malloc(sizeof struct s); /* we allocate it in the shared memory */
  58. if (lock_init(&bar->lock)==0){
  59. /* error initializing the lock */
  60. ...
  61. }
  62. Destroying & deallocating the locks:
  63. ------------------------------------
  64. void lock_destroy(gen_lock_t* lock);
  65. void lock_dealloc(gen_lock_t* lock);
  66. The lock_destroy function must be called first. It removes the resources associated with the
  67. lock, but it does not also free the lock shared memory part. Think of sysv rmid.
  68. Please don't forget to call this function, or you can leave allocated resources in some cases
  69. (e.g sysv semaphores). Be carefull to call it in your module destroy function if you use any
  70. global module locks.
  71. Example:
  72. lock_destroy(lock);
  73. lock_dealloc(lock);
  74. Of course you don't need to call lock_dealloc if your lock was not allocated with lock_alloc.
  75. Locking & unlocking:
  76. --------------------
  77. void lock_get(gen_lock_t* lock); - lock (mutex down)
  78. void lock_release(gen_lock_t* lock); - unlock (mutex up)
  79. int lock_try(gen_lock_t* lock); - tries to lock and returns 0
  80. if succesfull, -1 if not (this is
  81. a non-blocking lock_get())
  82. 4. Lock Sets
  83. ------------
  84. The lock sets are kind of sysv semaphore sets equivalent. The type is lock_set_t.
  85. Use them when you need a lot of mutexes. In some cases they waste less system
  86. resources than arrays of gen_lock_t (e.g. sys v semaphores).
  87. Allocating & initializing:
  88. --------------------------
  89. lock_set_t* lock_set_alloc(int no);
  90. lock_set_t* lock_set_init(lock_set_t* set);
  91. Both functions return 0 on failure.
  92. WARNING: expect the allocation function to fail for large numbers. It depends on the locking
  93. method used & the system available resources (again the sysv semaphores example).
  94. Example:
  95. lock_set_t *lock_set;
  96. lock_set=lock_set_alloc(100);
  97. if (lock_set==0) goto error;
  98. if (lock_set_init(lock_set)==0){
  99. lock_set_dealloc(lock_set);
  100. goto error;
  101. }
  102. or
  103. if ((lock_set=lock_set_alloc(100))==0) || (lock_set_init(lock_set)==0)){
  104. if (lock_set) lock_set_dealloc(lock_set);
  105. goto error;
  106. }
  107. Destroying & deallocating:
  108. --------------------------
  109. void lock_set_destroy(lock_set_t* s);
  110. void lock_set_dealloc(lock_set_t* s);
  111. Again don't forget to "destroy" the locks.
  112. Locking & unlocking:
  113. --------------------
  114. void lock_set_get(lock_set_t* s, int i);
  115. void lock_set_release(lock_set_t* s, int i);
  116. int lock_set_try(lock_set_t* s, int i); - tries to lock the i-th lock
  117. from the set. If succesfull
  118. returns 0, if not -1.
  119. Example:
  120. lock_set_get(lock_set, 2);
  121. /* do something */
  122. lock_set_release(lock_set, 2);
  123. When to use lock_set_t & when to use gen_lock_t
  124. -----------------------------------------------
  125. If you use lots of semaphores and GEN_LOCK_T_PREFERED is undefined then use
  126. lock_set_t. If GEN_LOCK_T_PREFERED is defined you can safely use gen_lock_t
  127. arrays instead.