semaphore.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* semaphore.c: FR-V semaphores
  2. *
  3. * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from lib/rwsem-spinlock.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/module.h>
  14. #include <asm/semaphore.h>
  15. struct sem_waiter {
  16. struct list_head list;
  17. struct task_struct *task;
  18. };
  19. #if SEMAPHORE_DEBUG
  20. void semtrace(struct semaphore *sem, const char *str)
  21. {
  22. if (sem->debug)
  23. printk("[%d] %s({%d,%d})\n",
  24. current->pid,
  25. str,
  26. sem->counter,
  27. list_empty(&sem->wait_list) ? 0 : 1);
  28. }
  29. #else
  30. #define semtrace(SEM,STR) do { } while(0)
  31. #endif
  32. /*
  33. * wait for a token to be granted from a semaphore
  34. * - entered with lock held and interrupts disabled
  35. */
  36. void __down(struct semaphore *sem, unsigned long flags)
  37. {
  38. struct task_struct *tsk = current;
  39. struct sem_waiter waiter;
  40. semtrace(sem, "Entering __down");
  41. /* set up my own style of waitqueue */
  42. waiter.task = tsk;
  43. get_task_struct(tsk);
  44. list_add_tail(&waiter.list, &sem->wait_list);
  45. /* we don't need to touch the semaphore struct anymore */
  46. spin_unlock_irqrestore(&sem->wait_lock, flags);
  47. /* wait to be given the semaphore */
  48. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  49. for (;;) {
  50. if (list_empty(&waiter.list))
  51. break;
  52. schedule();
  53. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  54. }
  55. tsk->state = TASK_RUNNING;
  56. semtrace(sem, "Leaving __down");
  57. }
  58. EXPORT_SYMBOL(__down);
  59. /*
  60. * interruptibly wait for a token to be granted from a semaphore
  61. * - entered with lock held and interrupts disabled
  62. */
  63. int __down_interruptible(struct semaphore *sem, unsigned long flags)
  64. {
  65. struct task_struct *tsk = current;
  66. struct sem_waiter waiter;
  67. int ret;
  68. semtrace(sem,"Entering __down_interruptible");
  69. /* set up my own style of waitqueue */
  70. waiter.task = tsk;
  71. get_task_struct(tsk);
  72. list_add_tail(&waiter.list, &sem->wait_list);
  73. /* we don't need to touch the semaphore struct anymore */
  74. set_task_state(tsk, TASK_INTERRUPTIBLE);
  75. spin_unlock_irqrestore(&sem->wait_lock, flags);
  76. /* wait to be given the semaphore */
  77. ret = 0;
  78. for (;;) {
  79. if (list_empty(&waiter.list))
  80. break;
  81. if (unlikely(signal_pending(current)))
  82. goto interrupted;
  83. schedule();
  84. set_task_state(tsk, TASK_INTERRUPTIBLE);
  85. }
  86. out:
  87. tsk->state = TASK_RUNNING;
  88. semtrace(sem, "Leaving __down_interruptible");
  89. return ret;
  90. interrupted:
  91. spin_lock_irqsave(&sem->wait_lock, flags);
  92. if (!list_empty(&waiter.list)) {
  93. list_del(&waiter.list);
  94. ret = -EINTR;
  95. }
  96. spin_unlock_irqrestore(&sem->wait_lock, flags);
  97. if (ret == -EINTR)
  98. put_task_struct(current);
  99. goto out;
  100. }
  101. EXPORT_SYMBOL(__down_interruptible);
  102. /*
  103. * release a single token back to a semaphore
  104. * - entered with lock held and interrupts disabled
  105. */
  106. void __up(struct semaphore *sem)
  107. {
  108. struct task_struct *tsk;
  109. struct sem_waiter *waiter;
  110. semtrace(sem,"Entering __up");
  111. /* grant the token to the process at the front of the queue */
  112. waiter = list_entry(sem->wait_list.next, struct sem_waiter, list);
  113. /* We must be careful not to touch 'waiter' after we set ->task = NULL.
  114. * It is an allocated on the waiter's stack and may become invalid at
  115. * any time after that point (due to a wakeup from another source).
  116. */
  117. list_del_init(&waiter->list);
  118. tsk = waiter->task;
  119. mb();
  120. waiter->task = NULL;
  121. wake_up_process(tsk);
  122. put_task_struct(tsk);
  123. semtrace(sem,"Leaving __up");
  124. }
  125. EXPORT_SYMBOL(__up);