futex.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef _LINUX_FUTEX_H
  2. #define _LINUX_FUTEX_H
  3. #include <linux/sched.h>
  4. /* Second argument to futex syscall */
  5. #define FUTEX_WAIT 0
  6. #define FUTEX_WAKE 1
  7. #define FUTEX_FD 2
  8. #define FUTEX_REQUEUE 3
  9. #define FUTEX_CMP_REQUEUE 4
  10. #define FUTEX_WAKE_OP 5
  11. #define FUTEX_LOCK_PI 6
  12. #define FUTEX_UNLOCK_PI 7
  13. #define FUTEX_TRYLOCK_PI 8
  14. /*
  15. * Support for robust futexes: the kernel cleans up held futexes at
  16. * thread exit time.
  17. */
  18. /*
  19. * Per-lock list entry - embedded in user-space locks, somewhere close
  20. * to the futex field. (Note: user-space uses a double-linked list to
  21. * achieve O(1) list add and remove, but the kernel only needs to know
  22. * about the forward link)
  23. *
  24. * NOTE: this structure is part of the syscall ABI, and must not be
  25. * changed.
  26. */
  27. struct robust_list {
  28. struct robust_list __user *next;
  29. };
  30. /*
  31. * Per-thread list head:
  32. *
  33. * NOTE: this structure is part of the syscall ABI, and must only be
  34. * changed if the change is first communicated with the glibc folks.
  35. * (When an incompatible change is done, we'll increase the structure
  36. * size, which glibc will detect)
  37. */
  38. struct robust_list_head {
  39. /*
  40. * The head of the list. Points back to itself if empty:
  41. */
  42. struct robust_list list;
  43. /*
  44. * This relative offset is set by user-space, it gives the kernel
  45. * the relative position of the futex field to examine. This way
  46. * we keep userspace flexible, to freely shape its data-structure,
  47. * without hardcoding any particular offset into the kernel:
  48. */
  49. long futex_offset;
  50. /*
  51. * The death of the thread may race with userspace setting
  52. * up a lock's links. So to handle this race, userspace first
  53. * sets this field to the address of the to-be-taken lock,
  54. * then does the lock acquire, and then adds itself to the
  55. * list, and then clears this field. Hence the kernel will
  56. * always have full knowledge of all locks that the thread
  57. * _might_ have taken. We check the owner TID in any case,
  58. * so only truly owned locks will be handled.
  59. */
  60. struct robust_list __user *list_op_pending;
  61. };
  62. /*
  63. * Are there any waiters for this robust futex:
  64. */
  65. #define FUTEX_WAITERS 0x80000000
  66. /*
  67. * The kernel signals via this bit that a thread holding a futex
  68. * has exited without unlocking the futex. The kernel also does
  69. * a FUTEX_WAKE on such futexes, after setting the bit, to wake
  70. * up any possible waiters:
  71. */
  72. #define FUTEX_OWNER_DIED 0x40000000
  73. /*
  74. * The rest of the robust-futex field is for the TID:
  75. */
  76. #define FUTEX_TID_MASK 0x3fffffff
  77. /*
  78. * This limit protects against a deliberately circular list.
  79. * (Not worth introducing an rlimit for it)
  80. */
  81. #define ROBUST_LIST_LIMIT 2048
  82. #ifdef __KERNEL__
  83. long do_futex(u32 __user *uaddr, int op, u32 val, unsigned long timeout,
  84. u32 __user *uaddr2, u32 val2, u32 val3);
  85. extern int
  86. handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi);
  87. #ifdef CONFIG_FUTEX
  88. extern void exit_robust_list(struct task_struct *curr);
  89. extern void exit_pi_state_list(struct task_struct *curr);
  90. #else
  91. static inline void exit_robust_list(struct task_struct *curr)
  92. {
  93. }
  94. static inline void exit_pi_state_list(struct task_struct *curr)
  95. {
  96. }
  97. #endif
  98. #endif /* __KERNEL__ */
  99. #define FUTEX_OP_SET 0 /* *(int *)UADDR2 = OPARG; */
  100. #define FUTEX_OP_ADD 1 /* *(int *)UADDR2 += OPARG; */
  101. #define FUTEX_OP_OR 2 /* *(int *)UADDR2 |= OPARG; */
  102. #define FUTEX_OP_ANDN 3 /* *(int *)UADDR2 &= ~OPARG; */
  103. #define FUTEX_OP_XOR 4 /* *(int *)UADDR2 ^= OPARG; */
  104. #define FUTEX_OP_OPARG_SHIFT 8 /* Use (1 << OPARG) instead of OPARG. */
  105. #define FUTEX_OP_CMP_EQ 0 /* if (oldval == CMPARG) wake */
  106. #define FUTEX_OP_CMP_NE 1 /* if (oldval != CMPARG) wake */
  107. #define FUTEX_OP_CMP_LT 2 /* if (oldval < CMPARG) wake */
  108. #define FUTEX_OP_CMP_LE 3 /* if (oldval <= CMPARG) wake */
  109. #define FUTEX_OP_CMP_GT 4 /* if (oldval > CMPARG) wake */
  110. #define FUTEX_OP_CMP_GE 5 /* if (oldval >= CMPARG) wake */
  111. /* FUTEX_WAKE_OP will perform atomically
  112. int oldval = *(int *)UADDR2;
  113. *(int *)UADDR2 = oldval OP OPARG;
  114. if (oldval CMP CMPARG)
  115. wake UADDR2; */
  116. #define FUTEX_OP(op, oparg, cmp, cmparg) \
  117. (((op & 0xf) << 28) | ((cmp & 0xf) << 24) \
  118. | ((oparg & 0xfff) << 12) | (cmparg & 0xfff))
  119. #endif