semaphore.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. * Please see kernel/semaphore.c for documentation of these functions
  8. */
  9. #ifndef __LINUX_SEMAPHORE_H
  10. #define __LINUX_SEMAPHORE_H
  11. #include <linux/list.h>
  12. #include <linux/spinlock.h>
  13. /* Please don't access any members of this structure directly */
  14. struct semaphore {
  15. raw_spinlock_t lock;
  16. unsigned int count;
  17. struct list_head wait_list;
  18. };
  19. #define __SEMAPHORE_INITIALIZER(name, n) \
  20. { \
  21. .lock = __RAW_SPIN_LOCK_UNLOCKED((name).lock), \
  22. .count = n, \
  23. .wait_list = LIST_HEAD_INIT((name).wait_list), \
  24. }
  25. #define DEFINE_SEMAPHORE(name) \
  26. struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
  27. static inline void sema_init(struct semaphore *sem, int val)
  28. {
  29. static struct lock_class_key __key;
  30. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
  31. lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
  32. }
  33. extern void down(struct semaphore *sem);
  34. extern int __must_check down_interruptible(struct semaphore *sem);
  35. extern int __must_check down_killable(struct semaphore *sem);
  36. extern int __must_check down_trylock(struct semaphore *sem);
  37. extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
  38. extern void up(struct semaphore *sem);
  39. #endif /* __LINUX_SEMAPHORE_H */