semaphore-helper.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* $Id: semaphore-helper.h,v 1.3 2001/03/26 15:00:33 orjanf Exp $
  2. *
  3. * SMP- and interrupt-safe semaphores helper functions. Generic versions, no
  4. * optimizations whatsoever...
  5. *
  6. */
  7. #ifndef _ASM_SEMAPHORE_HELPER_H
  8. #define _ASM_SEMAPHORE_HELPER_H
  9. #include <asm/atomic.h>
  10. #include <linux/errno.h>
  11. #define read(a) ((a)->counter)
  12. #define inc(a) (((a)->counter)++)
  13. #define dec(a) (((a)->counter)--)
  14. #define count_inc(a) ((*(a))++)
  15. /*
  16. * These two _must_ execute atomically wrt each other.
  17. */
  18. static inline void wake_one_more(struct semaphore * sem)
  19. {
  20. atomic_inc(&sem->waking);
  21. }
  22. static inline int waking_non_zero(struct semaphore *sem)
  23. {
  24. unsigned long flags;
  25. int ret = 0;
  26. local_irq_save(flags);
  27. if (read(&sem->waking) > 0) {
  28. dec(&sem->waking);
  29. ret = 1;
  30. }
  31. local_irq_restore(flags);
  32. return ret;
  33. }
  34. static inline int waking_non_zero_interruptible(struct semaphore *sem,
  35. struct task_struct *tsk)
  36. {
  37. int ret = 0;
  38. unsigned long flags;
  39. local_irq_save(flags);
  40. if (read(&sem->waking) > 0) {
  41. dec(&sem->waking);
  42. ret = 1;
  43. } else if (signal_pending(tsk)) {
  44. inc(&sem->count);
  45. ret = -EINTR;
  46. }
  47. local_irq_restore(flags);
  48. return ret;
  49. }
  50. static inline int waking_non_zero_trylock(struct semaphore *sem)
  51. {
  52. int ret = 1;
  53. unsigned long flags;
  54. local_irq_save(flags);
  55. if (read(&sem->waking) <= 0)
  56. inc(&sem->count);
  57. else {
  58. dec(&sem->waking);
  59. ret = 0;
  60. }
  61. local_irq_restore(flags);
  62. return ret;
  63. }
  64. #endif /* _ASM_SEMAPHORE_HELPER_H */