futex.h 4.0 KB

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