sgx_pthread.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_api_vmcore.h"
  6. #include "sgx_pthread.h"
  7. #include "sgx_error.h"
  8. #ifndef SGX_DISABLE_WASI
  9. #define TRACE_FUNC() os_printf("undefined %s\n", __FUNCTION__)
  10. #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)
  11. #ifndef SGX_THREAD_LOCK_INITIALIZER /* defined since sgxsdk-2.11 */
  12. /* sgxsdk doesn't support pthread_rwlock related APIs until
  13. version 2.11, we implement them by ourselves. */
  14. int
  15. ocall_pthread_rwlock_init(int *p_ret, void **rwlock, void *attr);
  16. int
  17. ocall_pthread_rwlock_destroy(int *p_ret, void **rwlock);
  18. int
  19. ocall_pthread_rwlock_rdlock(int *p_ret, void **rwlock);
  20. int
  21. ocall_pthread_rwlock_wrlock(int *p_ret, void **rwlock);
  22. int
  23. ocall_pthread_rwlock_unlock(int *p_ret, void **rwlock);
  24. int
  25. pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr)
  26. {
  27. int ret = -1;
  28. if (ocall_pthread_rwlock_init(&ret, (void **)rwlock, NULL) != SGX_SUCCESS) {
  29. TRACE_OCALL_FAIL();
  30. return -1;
  31. }
  32. (void)attr;
  33. return ret;
  34. }
  35. int
  36. pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
  37. {
  38. int ret = -1;
  39. if (ocall_pthread_rwlock_destroy(&ret, (void *)*rwlock) != SGX_SUCCESS) {
  40. TRACE_OCALL_FAIL();
  41. }
  42. return ret;
  43. }
  44. int
  45. pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
  46. {
  47. int ret = -1;
  48. if (ocall_pthread_rwlock_rdlock(&ret, (void *)*rwlock) != SGX_SUCCESS) {
  49. TRACE_OCALL_FAIL();
  50. }
  51. return ret;
  52. }
  53. int
  54. pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
  55. {
  56. int ret = -1;
  57. if (ocall_pthread_rwlock_wrlock(&ret, (void *)*rwlock) != SGX_SUCCESS) {
  58. TRACE_OCALL_FAIL();
  59. }
  60. return ret;
  61. }
  62. int
  63. pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
  64. {
  65. int ret = -1;
  66. if (ocall_pthread_rwlock_unlock(&ret, (void *)*rwlock) != SGX_SUCCESS) {
  67. TRACE_OCALL_FAIL();
  68. }
  69. return ret;
  70. }
  71. #endif /* end of SGX_THREAD_LOCK_INITIALIZER */
  72. #endif