futexlock.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * futex based lock (mutex) implementation (linux 2.6+ only)
  20. * based on Ulrich Drepper implementation in "Futexes Are Tricky"
  21. * (http://people.redhat.com/drepper/futex.pdf)
  22. *
  23. * Implements:
  24. * void futex_get(futex_lock_t* lock); - mutex lock
  25. * void futex_release(futex_lock_t* lock); - unlock
  26. * int futex_try(futex_lock_t* lock); - tries to get lock, returns 0
  27. * on success and !=0 on failure
  28. * (1 or 2)
  29. *
  30. * Config defines:
  31. */
  32. /*
  33. * History:
  34. * --------
  35. * 2007-05-13 created by andrei
  36. * 2007-06-12 added ADAPTIVE_WAIT busy waiting (andrei)
  37. */
  38. #ifndef _futexlock_h
  39. #define _futexlock_h
  40. #include "atomic/atomic_common.h"
  41. #include "atomic/atomic_native.h"
  42. #ifdef HAVE_ASM_INLINE_ATOMIC_OPS
  43. #define HAVE_FUTEX
  44. #include <sys/types.h> /* hack to workaround some type conflicts
  45. between linux-libc-dev andlibc headers
  46. in recent (6.08.2008) x86_64 debian sid
  47. installations */
  48. #include <linux/futex.h>
  49. #include <sys/syscall.h>
  50. #include <unistd.h>
  51. #include "compiler_opt.h"
  52. /* either syscall directly or #include <sys/linux/syscall.h> and use
  53. * sys_futex directly */
  54. #define sys_futex(addr, op, val, timeout, addr2, val3) \
  55. syscall(__NR_futex , (addr), (op), (val), (timeout), (addr2), (val3))
  56. typedef atomic_t futex_lock_t;
  57. /* the mutex has 3 states: 0 - free/unlocked and nobody waiting
  58. * 1 - locked and nobody waiting for it
  59. * 2 - locked w/ 0 or more waiting processes/threads
  60. */
  61. inline static futex_lock_t* futex_init(futex_lock_t* lock)
  62. {
  63. atomic_set(lock, 0);
  64. return lock;
  65. }
  66. inline static void futex_get(futex_lock_t* lock)
  67. {
  68. int v;
  69. #ifdef ADAPTIVE_WAIT
  70. register int i=ADAPTIVE_WAIT_LOOPS;
  71. retry:
  72. #endif
  73. v=atomic_cmpxchg(lock, 0, 1); /* lock if 0 */
  74. if (likely(v==0)){ /* optimize for the uncontended case */
  75. /* success */
  76. membar_enter_lock();
  77. return;
  78. }else if (unlikely(v==2)){ /* if contended, optimize for the one waiter
  79. case */
  80. /* waiting processes/threads => add ourselves to the queue */
  81. do{
  82. sys_futex(&(lock)->val, FUTEX_WAIT, 2, 0, 0, 0);
  83. v=atomic_get_and_set(lock, 2);
  84. }while(v);
  85. }else{
  86. /* v==1 */
  87. #ifdef ADAPTIVE_WAIT
  88. if (i>0){
  89. i--;
  90. goto retry;
  91. }
  92. #endif
  93. v=atomic_get_and_set(lock, 2);
  94. while(v){
  95. sys_futex(&(lock)->val, FUTEX_WAIT, 2, 0, 0, 0);
  96. v=atomic_get_and_set(lock, 2);
  97. }
  98. }
  99. membar_enter_lock();
  100. }
  101. inline static void futex_release(futex_lock_t* lock)
  102. {
  103. int v;
  104. membar_leave_lock();
  105. v=atomic_get_and_set(lock, 0);
  106. if (unlikely(v==2)){ /* optimize for the uncontended case */
  107. sys_futex(&(lock)->val, FUTEX_WAKE, 1, 0, 0, 0);
  108. }
  109. }
  110. static inline int futex_try(futex_lock_t* lock)
  111. {
  112. int c;
  113. c=atomic_cmpxchg(lock, 0, 1);
  114. if (likely(c))
  115. membar_enter_lock();
  116. return c;
  117. }
  118. #else /*HAVE_ASM_INLINE_ATOMIC_OPS*/
  119. #undef USE_FUTEX
  120. #endif /*HAVE_ASM_INLINE_ATOMIC_OPS*/
  121. #endif /* _futexlocks_h*/