sethread_mutex.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2011-2017 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_mutex_init(sgx_thread_mutex_t *mutex, const sgx_thread_mutexattr_t *unused)
  37. {
  38. UNUSED(unused);
  39. CHECK_PARAMETER(mutex);
  40. mutex->m_control = SGX_THREAD_MUTEX_NONRECURSIVE;
  41. mutex->m_refcount = 0;
  42. mutex->m_owner = SGX_THREAD_T_NULL;
  43. mutex->m_lock = SGX_SPINLOCK_INITIALIZER;
  44. QUEUE_INIT(&mutex->m_queue);
  45. return 0;
  46. }
  47. int sgx_thread_mutex_destroy(sgx_thread_mutex_t *mutex)
  48. {
  49. CHECK_PARAMETER(mutex);
  50. SPIN_LOCK(&mutex->m_lock);
  51. if (mutex->m_owner != SGX_THREAD_T_NULL
  52. || QUEUE_FIRST(&mutex->m_queue) != SGX_THREAD_T_NULL) {
  53. SPIN_UNLOCK(&mutex->m_lock);
  54. return EBUSY;
  55. }
  56. mutex->m_control = 0;
  57. mutex->m_refcount = 0;
  58. SPIN_UNLOCK(&mutex->m_lock);
  59. return 0;
  60. }
  61. int sgx_thread_mutex_lock(sgx_thread_mutex_t *mutex)
  62. {
  63. CHECK_PARAMETER(mutex);
  64. sgx_thread_t self = (sgx_thread_t)get_thread_data();
  65. while (1) {
  66. SPIN_LOCK(&mutex->m_lock);
  67. if(mutex->m_control != SGX_THREAD_MUTEX_RECURSIVE
  68. && mutex->m_control != SGX_THREAD_MUTEX_NONRECURSIVE) {
  69. SPIN_UNLOCK(&mutex->m_lock);
  70. return EINVAL;
  71. }
  72. if (mutex->m_control == SGX_THREAD_MUTEX_RECURSIVE
  73. && mutex->m_owner == self) {
  74. mutex->m_refcount++;
  75. SPIN_UNLOCK(&mutex->m_lock);
  76. return 0;
  77. }
  78. if (mutex->m_owner == SGX_THREAD_T_NULL
  79. && (QUEUE_FIRST(&mutex->m_queue) == self
  80. || QUEUE_FIRST(&mutex->m_queue) == SGX_THREAD_T_NULL)) {
  81. if (QUEUE_FIRST(&mutex->m_queue) == self)
  82. QUEUE_REMOVE_HEAD(&mutex->m_queue);
  83. mutex->m_owner = self;
  84. mutex->m_refcount++;
  85. SPIN_UNLOCK(&mutex->m_lock);
  86. return 0;
  87. }
  88. sgx_thread_t waiter = SGX_THREAD_T_NULL;
  89. QUEUE_FOREACH(waiter, &mutex->m_queue) {
  90. if (waiter == self) break;
  91. }
  92. if (waiter == SGX_THREAD_T_NULL)
  93. QUEUE_INSERT_TAIL(&mutex->m_queue, self);
  94. SPIN_UNLOCK(&mutex->m_lock);
  95. int err = 0;
  96. sgx_thread_wait_untrusted_event_ocall(&err, TD2TCS(self));
  97. }
  98. /* NOTREACHED */
  99. }
  100. int sgx_thread_mutex_trylock(sgx_thread_mutex_t *mutex)
  101. {
  102. CHECK_PARAMETER(mutex);
  103. sgx_thread_t self = (sgx_thread_t)get_thread_data();
  104. SPIN_LOCK(&mutex->m_lock);
  105. if(mutex->m_control != SGX_THREAD_MUTEX_RECURSIVE
  106. && mutex->m_control != SGX_THREAD_MUTEX_NONRECURSIVE) {
  107. SPIN_UNLOCK(&mutex->m_lock);
  108. return EINVAL;
  109. }
  110. if (mutex->m_control == SGX_THREAD_MUTEX_RECURSIVE
  111. && mutex->m_owner == self) {
  112. mutex->m_refcount++;
  113. SPIN_UNLOCK(&mutex->m_lock);
  114. return 0;
  115. }
  116. if (mutex->m_owner == SGX_THREAD_T_NULL
  117. && (QUEUE_FIRST(&mutex->m_queue) == self
  118. || QUEUE_FIRST(&mutex->m_queue) == SGX_THREAD_T_NULL)) {
  119. if (QUEUE_FIRST(&mutex->m_queue) == self)
  120. QUEUE_REMOVE_HEAD(&mutex->m_queue);
  121. mutex->m_owner = self;
  122. mutex->m_refcount++;
  123. SPIN_UNLOCK(&mutex->m_lock);
  124. return 0;
  125. }
  126. SPIN_UNLOCK(&mutex->m_lock);
  127. return EBUSY;
  128. }
  129. /* sgx_thread_mutex_unlock_lazy:
  130. * check and modify mutex object, but not wake the pending thread up.
  131. */
  132. int sgx_thread_mutex_unlock_lazy(sgx_thread_mutex_t *mutex, sgx_thread_t *pwaiter)
  133. {
  134. CHECK_PARAMETER(mutex);
  135. sgx_thread_t self = (sgx_thread_t)get_thread_data();
  136. SPIN_LOCK(&mutex->m_lock);
  137. if(mutex->m_control != SGX_THREAD_MUTEX_RECURSIVE
  138. && mutex->m_control != SGX_THREAD_MUTEX_NONRECURSIVE) {
  139. SPIN_UNLOCK(&mutex->m_lock);
  140. return EINVAL;
  141. }
  142. /* if the mutux is not locked by anyone */
  143. if(mutex->m_owner == SGX_THREAD_T_NULL) {
  144. SPIN_UNLOCK(&mutex->m_lock);
  145. return EINVAL;
  146. }
  147. /* if the mutex is locked by another thread */
  148. if (mutex->m_owner != self) {
  149. SPIN_UNLOCK(&mutex->m_lock);
  150. return EPERM;
  151. }
  152. /* the mutex is locked by current thread */
  153. if (--mutex->m_refcount == 0)
  154. mutex->m_owner = SGX_THREAD_T_NULL;
  155. else {
  156. SPIN_UNLOCK(&mutex->m_lock);
  157. return 0;
  158. }
  159. /* Before releasing the mutex, get the first thread,
  160. * the thread should be waked up by the caller.
  161. */
  162. sgx_thread_t waiter = QUEUE_FIRST(&mutex->m_queue);
  163. SPIN_UNLOCK(&mutex->m_lock);
  164. if (pwaiter != NULL) *pwaiter = waiter;
  165. return 0;
  166. }
  167. /* sgx_thread_mutex_unlock:
  168. * invoke sgx_thread_mutex_unlock_lazy, wake the pending thread up.
  169. */
  170. int sgx_thread_mutex_unlock(sgx_thread_mutex_t *mutex)
  171. {
  172. sgx_thread_t waiter = SGX_THREAD_T_NULL;
  173. int ret = sgx_thread_mutex_unlock_lazy(mutex, &waiter);
  174. if (ret != 0) return ret;
  175. if (waiter != SGX_THREAD_T_NULL) /* wake the waiter up*/
  176. sgx_thread_set_untrusted_event_ocall(&ret, TD2TCS(waiter));
  177. return 0;
  178. }