sgx_pthread.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 ocall_pthread_rwlock_init(int *p_ret, void **rwlock, void *attr);
  15. int ocall_pthread_rwlock_destroy(int *p_ret, void **rwlock);
  16. int ocall_pthread_rwlock_rdlock(int *p_ret, void **rwlock);
  17. int ocall_pthread_rwlock_wrlock(int *p_ret, void **rwlock);
  18. int ocall_pthread_rwlock_unlock(int *p_ret, void **rwlock);
  19. int pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr)
  20. {
  21. int ret = -1;
  22. if (ocall_pthread_rwlock_init(&ret, (void **)rwlock, NULL)
  23. != SGX_SUCCESS) {
  24. TRACE_OCALL_FAIL();
  25. return -1;
  26. }
  27. (void)attr;
  28. return ret;
  29. }
  30. int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
  31. {
  32. int ret = -1;
  33. if (ocall_pthread_rwlock_destroy(&ret, (void *)*rwlock) != SGX_SUCCESS) {
  34. TRACE_OCALL_FAIL();
  35. }
  36. return ret;
  37. }
  38. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
  39. {
  40. int ret = -1;
  41. if (ocall_pthread_rwlock_rdlock(&ret, (void*)*rwlock) != SGX_SUCCESS) {
  42. TRACE_OCALL_FAIL();
  43. }
  44. return ret;
  45. }
  46. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
  47. {
  48. int ret = -1;
  49. if (ocall_pthread_rwlock_wrlock(&ret, (void*)*rwlock) != SGX_SUCCESS) {
  50. TRACE_OCALL_FAIL();
  51. }
  52. return ret;
  53. }
  54. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
  55. {
  56. int ret = -1;
  57. if (ocall_pthread_rwlock_unlock(&ret, (void*)*rwlock) != SGX_SUCCESS) {
  58. TRACE_OCALL_FAIL();
  59. }
  60. return ret;
  61. }
  62. #endif /* end of SGX_THREAD_LOCK_INITIALIZER */
  63. #endif