semaphore.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/config.h>
  13. #include <linux/sched.h>
  14. #include <linux/module.h>
  15. #include <asm/semaphore.h>
  16. struct sem_waiter {
  17. struct list_head list;
  18. struct task_struct *task;
  19. };
  20. #if SEM_DEBUG
  21. void semtrace(struct semaphore *sem, const char *str)
  22. {
  23. if (sem->debug)
  24. printk("[%d] %s({%d,%d})\n",
  25. current->pid,
  26. str,
  27. sem->counter,
  28. list_empty(&sem->wait_list) ? 0 : 1);
  29. }
  30. #else
  31. #define semtrace(SEM,STR) do { } while(0)
  32. #endif
  33. /*
  34. * wait for a token to be granted from a semaphore
  35. * - entered with lock held and interrupts disabled
  36. */
  37. void __down(struct semaphore *sem, unsigned long flags)
  38. {
  39. struct task_struct *tsk = current;
  40. struct sem_waiter waiter;
  41. semtrace(sem, "Entering __down");
  42. /* set up my own style of waitqueue */
  43. waiter.task = tsk;
  44. get_task_struct(tsk);
  45. list_add_tail(&waiter.list, &sem->wait_list);
  46. /* we don't need to touch the semaphore struct anymore */
  47. spin_unlock_irqrestore(&sem->wait_lock, flags);
  48. /* wait to be given the semaphore */
  49. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  50. for (;;) {
  51. if (list_empty(&waiter.list))
  52. break;
  53. schedule();
  54. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  55. }
  56. tsk->state = TASK_RUNNING;
  57. semtrace(sem, "Leaving __down");
  58. }
  59. EXPORT_SYMBOL(__down);
  60. /*
  61. * interruptibly wait for a token to be granted from a semaphore
  62. * - entered with lock held and interrupts disabled
  63. */
  64. int __down_interruptible(struct semaphore *sem, unsigned long flags)
  65. {
  66. struct task_struct *tsk = current;
  67. struct sem_waiter waiter;
  68. int ret;
  69. semtrace(sem,"Entering __down_interruptible");
  70. /* set up my own style of waitqueue */
  71. waiter.task = tsk;
  72. get_task_struct(tsk);
  73. list_add_tail(&waiter.list, &sem->wait_list);
  74. /* we don't need to touch the semaphore struct anymore */
  75. set_task_state(tsk, TASK_INTERRUPTIBLE);
  76. spin_unlock_irqrestore(&sem->wait_lock, flags);
  77. /* wait to be given the semaphore */
  78. ret = 0;
  79. for (;;) {
  80. if (list_empty(&waiter.list))
  81. break;
  82. if (unlikely(signal_pending(current)))
  83. goto interrupted;
  84. schedule();
  85. set_task_state(tsk, TASK_INTERRUPTIBLE);
  86. }
  87. out:
  88. tsk->state = TASK_RUNNING;
  89. semtrace(sem, "Leaving __down_interruptible");
  90. return ret;
  91. interrupted:
  92. spin_lock_irqsave(&sem->wait_lock, flags);
  93. if (!list_empty(&waiter.list)) {
  94. list_del(&waiter.list);
  95. ret = -EINTR;
  96. }
  97. spin_unlock_irqrestore(&sem->wait_lock, flags);
  98. if (ret == -EINTR)
  99. put_task_struct(current);
  100. goto out;
  101. }
  102. EXPORT_SYMBOL(__down_interruptible);
  103. /*
  104. * release a single token back to a semaphore
  105. * - entered with lock held and interrupts disabled
  106. */
  107. void __up(struct semaphore *sem)
  108. {
  109. struct task_struct *tsk;
  110. struct sem_waiter *waiter;
  111. semtrace(sem,"Entering __up");
  112. /* grant the token to the process at the front of the queue */
  113. waiter = list_entry(sem->wait_list.next, struct sem_waiter, list);
  114. /* We must be careful not to touch 'waiter' after we set ->task = NULL.
  115. * It is an allocated on the waiter's stack and may become invalid at
  116. * any time after that point (due to a wakeup from another source).
  117. */
  118. list_del_init(&waiter->list);
  119. tsk = waiter->task;
  120. mb();
  121. waiter->task = NULL;
  122. wake_up_process(tsk);
  123. put_task_struct(tsk);
  124. semtrace(sem,"Leaving __up");
  125. }
  126. EXPORT_SYMBOL(__up);