sethread_cond.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include "util.h"
  35. #include "sethread_internal.h"
  36. int sgx_thread_cond_init(sgx_thread_cond_t *cond, const sgx_thread_condattr_t *unused)
  37. {
  38. UNUSED(unused);
  39. CHECK_PARAMETER(cond);
  40. cond->m_lock = SGX_SPINLOCK_INITIALIZER;
  41. QUEUE_INIT(&cond->m_queue);
  42. return 0;
  43. }
  44. int sgx_thread_cond_destroy(sgx_thread_cond_t *cond)
  45. {
  46. CHECK_PARAMETER(cond);
  47. SPIN_LOCK(&cond->m_lock);
  48. if (QUEUE_FIRST(&cond->m_queue) != SGX_THREAD_T_NULL) {
  49. SPIN_UNLOCK(&cond->m_lock);
  50. return EBUSY;
  51. }
  52. SPIN_UNLOCK(&cond->m_lock);
  53. return 0;
  54. }
  55. int sgx_thread_cond_wait(sgx_thread_cond_t *cond, sgx_thread_mutex_t *mutex)
  56. {
  57. CHECK_PARAMETER(cond);
  58. CHECK_PARAMETER(mutex);
  59. sgx_thread_t self = (sgx_thread_t)get_thread_data();
  60. SPIN_LOCK(&cond->m_lock);
  61. QUEUE_INSERT_TAIL(&cond->m_queue, self);
  62. sgx_thread_t waiter = SGX_THREAD_T_NULL;
  63. int ret = sgx_thread_mutex_unlock_lazy(mutex, &waiter);
  64. if (ret != 0) {
  65. SPIN_UNLOCK(&cond->m_lock);
  66. return ret;
  67. }
  68. while (1) {
  69. sgx_thread_t tmp = SGX_THREAD_T_NULL;
  70. SPIN_UNLOCK(&cond->m_lock);
  71. /* OPT: if there is a thread waiting on the mutex, wake it in a single OCALL. */
  72. if (waiter == SGX_THREAD_T_NULL) {
  73. sgx_thread_wait_untrusted_event_ocall(&ret, TD2TCS(self));
  74. } else {
  75. sgx_thread_setwait_untrusted_events_ocall(&ret, TD2TCS(waiter), TD2TCS(self));
  76. waiter = SGX_THREAD_T_NULL;
  77. }
  78. SPIN_LOCK(&cond->m_lock);
  79. QUEUE_FOREACH(tmp, &cond->m_queue) {
  80. if (tmp == self) break; /* stop searching and re-wait outside */
  81. }
  82. if (tmp == SGX_THREAD_T_NULL) break; /* current thread isn't in the queue */
  83. }
  84. SPIN_UNLOCK(&cond->m_lock);
  85. sgx_thread_mutex_lock(mutex);
  86. return 0;
  87. }
  88. int sgx_thread_cond_signal(sgx_thread_cond_t *cond)
  89. {
  90. int err = 0;
  91. sgx_thread_t waiter = SGX_THREAD_T_NULL;
  92. CHECK_PARAMETER(cond);
  93. SPIN_LOCK(&cond->m_lock);
  94. if ((waiter = QUEUE_FIRST(&cond->m_queue)) == SGX_THREAD_T_NULL) {
  95. SPIN_UNLOCK(&cond->m_lock);
  96. return 0;
  97. }
  98. QUEUE_REMOVE_HEAD(&cond->m_queue);
  99. SPIN_UNLOCK(&cond->m_lock);
  100. sgx_thread_set_untrusted_event_ocall(&err, TD2TCS(waiter)); /* wake first pending thread */
  101. return 0;
  102. }
  103. int sgx_thread_cond_broadcast(sgx_thread_cond_t *cond)
  104. {
  105. size_t n_waiter = 0; int err = 0;
  106. sgx_thread_t waiter = SGX_THREAD_T_NULL;
  107. const void **waiters = NULL;
  108. CHECK_PARAMETER(cond);
  109. SPIN_LOCK(&cond->m_lock);
  110. QUEUE_COUNT_ALL(waiter, &cond->m_queue, n_waiter);
  111. if (n_waiter == 0) {
  112. SPIN_UNLOCK(&cond->m_lock);
  113. return 0;
  114. }
  115. waiters = (const void **)malloc(n_waiter * sizeof(const void *));
  116. if (waiters == NULL) {
  117. SPIN_UNLOCK(&cond->m_lock);
  118. return ENOMEM;
  119. }
  120. const void **tmp = waiters;
  121. while ((waiter = QUEUE_FIRST(&cond->m_queue)) != SGX_THREAD_T_NULL) {
  122. QUEUE_REMOVE_HEAD(&cond->m_queue); /* remove the pending thread */
  123. *tmp++ = TD2TCS(waiter);
  124. }
  125. SPIN_UNLOCK(&cond->m_lock);
  126. sgx_thread_set_multiple_untrusted_events_ocall(&err, waiters, n_waiter); /* wake all pending threads up */
  127. free(waiters);
  128. return 0;
  129. }