semaphore.c 3.5 KB

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