pthread_condwait.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /****************************************************************************
  2. * sched/pthread/pthread_condwait.c
  3. *
  4. * Copyright (C) 2007-2009, 2012, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <unistd.h>
  40. #include <pthread.h>
  41. #include <sched.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <nuttx/cancelpt.h>
  45. #include "pthread/pthread.h"
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: int pthread_cond_wait
  51. *
  52. * Description:
  53. * A thread can wait for a condition variable to be signalled or broadcast.
  54. *
  55. * Input Parameters:
  56. * None
  57. *
  58. * Returned Value:
  59. * None
  60. *
  61. * Assumptions:
  62. *
  63. ****************************************************************************/
  64. int pthread_cond_wait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex)
  65. {
  66. int status;
  67. int ret;
  68. sinfo("cond=0x%p mutex=0x%p\n", cond, mutex);
  69. /* pthread_cond_wait() is a cancellation point */
  70. (void)enter_cancellation_point();
  71. /* Make sure that non-NULL references were provided. */
  72. if (cond == NULL || mutex == NULL)
  73. {
  74. ret = EINVAL;
  75. }
  76. /* Make sure that the caller holds the mutex */
  77. else if (mutex->pid != (int)getpid())
  78. {
  79. ret = EPERM;
  80. }
  81. else
  82. {
  83. /* Give up the mutex */
  84. sinfo("Give up mutex / take cond\n");
  85. sched_lock();
  86. mutex->pid = -1;
  87. ret = pthread_mutex_give(mutex);
  88. /* Take the semaphore */
  89. status = pthread_sem_take((FAR sem_t *)&cond->sem, false);
  90. if (ret == OK)
  91. {
  92. /* Report the first failure that occurs */
  93. ret = status;
  94. }
  95. sched_unlock();
  96. /* Reacquire the mutex.
  97. *
  98. * When cancellation points are enabled, we need to hold the mutex
  99. * when the pthread is canceled and cleanup handlers, if any, are
  100. * entered.
  101. */
  102. sinfo("Reacquire mutex...\n");
  103. status = pthread_mutex_take(mutex, false);
  104. if (ret == OK)
  105. {
  106. /* Report the first failure that occurs */
  107. ret = status;
  108. }
  109. /* Was all of the above successful? */
  110. if (ret == OK)
  111. {
  112. mutex->pid = getpid();
  113. }
  114. }
  115. leave_cancellation_point();
  116. sinfo("Returning %d\n", ret);
  117. return ret;
  118. }