semaphore.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2008 Intel Corporation
  3. * Author: Matthew Wilcox <willy@linux.intel.com>
  4. *
  5. * Distributed under the terms of the GNU GPL, version 2
  6. *
  7. * Counting semaphores allow up to <n> tasks to acquire the semaphore
  8. * simultaneously.
  9. */
  10. #ifndef __LINUX_SEMAPHORE_H
  11. #define __LINUX_SEMAPHORE_H
  12. #include <linux/list.h>
  13. #include <linux/spinlock.h>
  14. /*
  15. * The spinlock controls access to the other members of the semaphore.
  16. * 'count' is decremented by every task which calls down*() and incremented
  17. * by every call to up(). Thus, if it is positive, it indicates how many
  18. * more tasks may acquire the lock. If it is negative, it indicates how
  19. * many tasks are waiting for the lock. Tasks waiting for the lock are
  20. * kept on the wait_list.
  21. */
  22. struct semaphore {
  23. spinlock_t lock;
  24. int count;
  25. struct list_head wait_list;
  26. };
  27. #define __SEMAPHORE_INITIALIZER(name, n) \
  28. { \
  29. .lock = __SPIN_LOCK_UNLOCKED((name).lock), \
  30. .count = n, \
  31. .wait_list = LIST_HEAD_INIT((name).wait_list), \
  32. }
  33. #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
  34. struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
  35. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
  36. static inline void sema_init(struct semaphore *sem, int val)
  37. {
  38. static struct lock_class_key __key;
  39. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
  40. lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
  41. }
  42. #define init_MUTEX(sem) sema_init(sem, 1)
  43. #define init_MUTEX_LOCKED(sem) sema_init(sem, 0)
  44. /*
  45. * Attempt to acquire the semaphore. If another task is already holding the
  46. * semaphore, sleep until the semaphore is released.
  47. */
  48. extern void down(struct semaphore *sem);
  49. /*
  50. * As down(), except the sleep may be interrupted by a signal. If it is,
  51. * this function will return -EINTR.
  52. */
  53. extern int __must_check down_interruptible(struct semaphore *sem);
  54. /*
  55. * As down_interruptible(), except the sleep may only be interrupted by
  56. * signals which are fatal to this process.
  57. */
  58. extern int __must_check down_killable(struct semaphore *sem);
  59. /*
  60. * As down(), except this function will not sleep. It will return 0 if it
  61. * acquired the semaphore and 1 if the semaphore was contended. This
  62. * function may be called from any context, including interrupt and softirq.
  63. */
  64. extern int __must_check down_trylock(struct semaphore *sem);
  65. /*
  66. * As down(), except this function will return -ETIME if it fails to
  67. * acquire the semaphore within the specified number of jiffies.
  68. */
  69. extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
  70. /*
  71. * Release the semaphore. Unlike mutexes, up() may be called from any
  72. * context and even by tasks which have never called down().
  73. */
  74. extern void up(struct semaphore *sem);
  75. #endif /* __LINUX_SEMAPHORE_H */