semaphore.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2008 Intel Corporation
  4. * Author: Matthew Wilcox <[email protected]>
  5. *
  6. * Please see kernel/locking/semaphore.c for documentation of these functions
  7. */
  8. #ifndef __LINUX_SEMAPHORE_H
  9. #define __LINUX_SEMAPHORE_H
  10. #include <linux/list.h>
  11. #include <linux/spinlock.h>
  12. /* Please don't access any members of this structure directly */
  13. struct semaphore {
  14. raw_spinlock_t lock;
  15. unsigned int count;
  16. struct list_head wait_list;
  17. };
  18. #define __SEMAPHORE_INITIALIZER(name, n) \
  19. { \
  20. .lock = __RAW_SPIN_LOCK_UNLOCKED((name).lock), \
  21. .count = n, \
  22. .wait_list = LIST_HEAD_INIT((name).wait_list), \
  23. }
  24. #define DEFINE_SEMAPHORE(name) \
  25. struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
  26. static inline void sema_init(struct semaphore *sem, int val)
  27. {
  28. static struct lock_class_key __key;
  29. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
  30. lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
  31. }
  32. extern void down(struct semaphore *sem);
  33. extern int __must_check down_interruptible(struct semaphore *sem);
  34. extern int __must_check down_killable(struct semaphore *sem);
  35. extern int __must_check down_trylock(struct semaphore *sem);
  36. extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
  37. extern void up(struct semaphore *sem);
  38. #endif /* __LINUX_SEMAPHORE_H */