bh_thread.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_thread.h"
  6. #include "bh_assert.h"
  7. #include "bh_log.h"
  8. #include "bh_memory.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/time.h>
  12. static korp_mutex thread_list_lock;
  13. static pthread_key_t thread_local_storage_key[BH_MAX_TLS_NUM];
  14. int _vm_thread_sys_init()
  15. {
  16. unsigned i;
  17. for (i = 0; i < BH_MAX_TLS_NUM; i++)
  18. pthread_key_create(&thread_local_storage_key[i], NULL);
  19. return vm_mutex_init(&thread_list_lock);
  20. }
  21. korp_tid _vm_self_thread()
  22. {
  23. return (korp_tid) pthread_self();
  24. }
  25. void *_vm_tls_get(unsigned idx)
  26. {
  27. bh_assert(idx < BH_MAX_TLS_NUM);
  28. return pthread_getspecific(thread_local_storage_key[idx]);
  29. }
  30. int _vm_tls_put(unsigned idx, void * tls)
  31. {
  32. bh_assert(idx < BH_MAX_TLS_NUM);
  33. pthread_setspecific(thread_local_storage_key[idx], tls);
  34. return BHT_OK;
  35. }
  36. int _vm_mutex_init(korp_mutex *mutex)
  37. {
  38. return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
  39. }
  40. int _vm_recursive_mutex_init(korp_mutex *mutex)
  41. {
  42. int ret;
  43. pthread_mutexattr_t mattr;
  44. bh_assert(mutex);
  45. ret = pthread_mutexattr_init(&mattr);
  46. if (ret)
  47. return BHT_ERROR;
  48. pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE_NP);
  49. ret = pthread_mutex_init(mutex, &mattr);
  50. pthread_mutexattr_destroy(&mattr);
  51. return ret == 0 ? BHT_OK : BHT_ERROR;
  52. }
  53. int _vm_mutex_destroy(korp_mutex *mutex)
  54. {
  55. int ret;
  56. bh_assert(mutex);
  57. ret = pthread_mutex_destroy(mutex);
  58. return ret == 0 ? BHT_OK : BHT_ERROR;
  59. }
  60. /* Returned error (EINVAL, EAGAIN and EDEADLK) from
  61. locking the mutex indicates some logic error present in
  62. the program somewhere.
  63. Don't try to recover error for an existing unknown error.*/
  64. void vm_mutex_lock(korp_mutex *mutex)
  65. {
  66. int ret;
  67. bh_assert(mutex);
  68. ret = pthread_mutex_lock(mutex);
  69. if (0 != ret) {
  70. LOG_FATAL("vm mutex lock failed (ret=%d)!\n", ret);
  71. exit(-1);
  72. }
  73. }
  74. int vm_mutex_trylock(korp_mutex *mutex)
  75. {
  76. int ret;
  77. bh_assert(mutex);
  78. ret = pthread_mutex_trylock(mutex);
  79. return ret == 0 ? BHT_OK : BHT_ERROR;
  80. }
  81. /* Returned error (EINVAL, EAGAIN and EPERM) from
  82. unlocking the mutex indicates some logic error present
  83. in the program somewhere.
  84. Don't try to recover error for an existing unknown error.*/
  85. void vm_mutex_unlock(korp_mutex *mutex)
  86. {
  87. int ret;
  88. bh_assert(mutex);
  89. ret = pthread_mutex_unlock(mutex);
  90. if (0 != ret) {
  91. LOG_FATAL("vm mutex unlock failed (ret=%d)!\n", ret);
  92. exit(-1);
  93. }
  94. }
  95. int _vm_cond_init(korp_cond *cond)
  96. {
  97. bh_assert(cond);
  98. if (pthread_cond_init(cond, NULL) != BHT_OK)
  99. return BHT_ERROR;
  100. return BHT_OK;
  101. }
  102. int _vm_cond_destroy(korp_cond *cond)
  103. {
  104. bh_assert(cond);
  105. if (pthread_cond_destroy(cond) != BHT_OK)
  106. return BHT_ERROR;
  107. return BHT_OK;
  108. }