aesm_thread.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (C) 2011-2018 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 "oal/aesm_thread.h"
  32. #include <stdlib.h>
  33. #include <assert.h>
  34. #include <pthread.h>
  35. #include <errno.h>
  36. #include "oal/internal_log.h"
  37. #include "se_thread.h"//using se_mutex
  38. const uint32_t AESM_THREAD_INFINITE = 0xffffffff; // special flag to indicate that thread to be blocked
  39. //thread status:
  40. // At the beginning, status is initialized to AESM_THREAD_INIT
  41. // After the started thread has copied all input data, the status will be updated to AESM_THREAD_RUNNING (So that aesm_free_thread could be called and mainthread could quit)
  42. // If the thread has finished before function like aesm_free_thread is called, the thread will find the status is still AESM_TRHEAD_RUNNING,
  43. // it will update the status to AESM_THREAD_PENDING and leave the resource free to following call of aesm_free_thread
  44. // If the thread is finished after function aesm_free_thread is called, the status should be AESM_THREAD_FREED, so that it should free the resource.
  45. // If aesm_join_thread is called, it will wait the thread to be finished (so that it should find the status to be AESM_THREAD_PENDING when pthread_join returned
  46. // and it will update the status to AESM_THREAD_DETACHED
  47. // If aesm_free_thread is called before the thread has been finished, so the function find the status is AESM_THREAD_RUNNING,
  48. // it will update the status to AESM_THREAD_FREED and leaving the memory free the aesm_thread_proc
  49. // But when aesm_free_thread is called and it found the thread has been finished so that the status is AESM_THREAD_PENDING or AESM_THREAD_DETACHED
  50. // the function should free all the resource.
  51. enum _aesm_thread_status_t{AESM_THREAD_INIT, AESM_THREAD_INVALID, AESM_THREAD_RUNNING, AESM_THREAD_PENDING, AESM_THREAD_DETACHED, AESM_THREAD_FREED};
  52. struct _aesm_thread_t{
  53. pthread_mutex_t mutex;
  54. pthread_cond_t copy_cond;
  55. pthread_cond_t timeout_cond;
  56. aesm_thread_arg_type_t arg;
  57. aesm_thread_function_t fun_entry;
  58. ae_error_t ae_ret;
  59. pthread_t hthread;
  60. volatile _aesm_thread_status_t status;
  61. };
  62. static void aesm_dealloc_resource(aesm_thread_t h)
  63. {
  64. h->status = AESM_THREAD_INVALID;
  65. pthread_cond_destroy(&h->copy_cond);
  66. pthread_cond_destroy(&h->timeout_cond);
  67. (void)pthread_mutex_destroy(&h->mutex);
  68. free(h);
  69. }
  70. void* aesm_thread_proc(void* param)
  71. {
  72. struct _aesm_thread_t *p=(struct _aesm_thread_t *)param;
  73. aesm_thread_function_t fun_entry = NULL;
  74. aesm_thread_arg_type_t arg = 0;
  75. AESM_DBG_TRACE("start running thread %p...",param);
  76. if(pthread_mutex_lock(&p->mutex)!=0){
  77. AESM_DBG_ERROR("fail to lock the thread mutex of thread %p",param);
  78. return reinterpret_cast<void *>(static_cast<ptrdiff_t>(AE_FAILURE));
  79. }
  80. fun_entry = p->fun_entry;
  81. arg = p->arg;
  82. p->status = AESM_THREAD_RUNNING;
  83. p->ae_ret = AE_FAILURE;
  84. pthread_cond_signal(&p->copy_cond);//notify mainthread that input parameter data has been copied to thread and we could release it(if aesm_thread_free has been called)
  85. pthread_mutex_unlock(&p->mutex);
  86. AESM_DBG_TRACE("thread parameters of thread %p copied",param);
  87. ae_error_t err = fun_entry(arg);
  88. AESM_DBG_TRACE("returned from user defined thread code for thread %p",param);
  89. if(pthread_mutex_lock(&p->mutex)!=0){
  90. p->status = AESM_THREAD_INVALID;
  91. AESM_DBG_ERROR("fail to lock the thread mutex of thread %p",param);
  92. return reinterpret_cast<void *>(static_cast<ptrdiff_t>(AE_FAILURE));
  93. }
  94. p->ae_ret = err;
  95. if(p->status == AESM_THREAD_RUNNING){
  96. p->status = AESM_THREAD_PENDING;
  97. pthread_cond_signal(&p->timeout_cond);
  98. pthread_mutex_unlock(&p->mutex);
  99. AESM_DBG_TRACE("thread %p change to status AESM_THREAD_PEDNING",param);
  100. }else if(p->status == AESM_THREAD_FREED){
  101. pthread_mutex_unlock(&p->mutex);
  102. pthread_detach(p->hthread);
  103. aesm_dealloc_resource(p);
  104. AESM_DBG_TRACE("resource of thread %p has been dealloced",param);
  105. }else{
  106. p->status = AESM_THREAD_INVALID;//It should never be reached
  107. pthread_mutex_unlock(&p->mutex);
  108. AESM_DBG_TRACE("thread %p status invalid",param);
  109. assert(0);
  110. }
  111. return reinterpret_cast<void *>(static_cast<ptrdiff_t>(err));
  112. }
  113. ae_error_t aesm_create_thread(aesm_thread_function_t function_entry, aesm_thread_arg_type_t arg, aesm_thread_t* h)
  114. {
  115. ae_error_t err = AE_SUCCESS;
  116. int r=0;
  117. int mutex_inited = 0;
  118. int copy_cond_inited = 0;
  119. int timeout_cond_inited = 0;
  120. assert(h!=NULL);
  121. AESM_DBG_TRACE("start to create a thread");
  122. struct _aesm_thread_t *p=(struct _aesm_thread_t *)malloc(sizeof(struct _aesm_thread_t));
  123. if(p==NULL){
  124. AESM_DBG_ERROR("fail to malloc");
  125. err = AE_OUT_OF_MEMORY_ERROR;
  126. goto ret_point;
  127. }
  128. memset(p, 0, sizeof(struct _aesm_thread_t));
  129. p->arg = arg;
  130. p->fun_entry = function_entry;
  131. if(pthread_mutex_init(&p->mutex, NULL)!=0){
  132. err = AE_FAILURE;
  133. AESM_DBG_ERROR("fail to init mutex");
  134. goto ret_point;
  135. }
  136. mutex_inited=1;
  137. if(pthread_cond_init(&p->copy_cond, NULL)!=0){
  138. err = AE_FAILURE;
  139. AESM_DBG_ERROR("fail to init copy cond");
  140. goto ret_point;
  141. }
  142. copy_cond_inited=1;
  143. if(pthread_cond_init(&p->timeout_cond, NULL)!=0){
  144. err = AE_FAILURE;
  145. AESM_DBG_ERROR("fail to init timeout cond");
  146. goto ret_point;
  147. }
  148. timeout_cond_inited=1;
  149. p->status = AESM_THREAD_INIT;
  150. r=pthread_create(&p->hthread, NULL, aesm_thread_proc, p);
  151. if(r!=0){
  152. AESM_DBG_ERROR("fail to create thread");
  153. err = OAL_THREAD_ERROR;
  154. goto ret_point;
  155. }
  156. *h = p;
  157. AESM_DBG_TRACE("thread %p created successfully",p);
  158. ret_point:
  159. if(err!=AE_SUCCESS&&p!=NULL){
  160. if(mutex_inited!=0){
  161. (void)pthread_mutex_destroy(&p->mutex);
  162. }
  163. if(copy_cond_inited!=0){
  164. (void)pthread_cond_destroy(&p->copy_cond);
  165. }
  166. if(timeout_cond_inited!=0){
  167. (void)pthread_cond_destroy(&p->timeout_cond);
  168. }
  169. free(p);
  170. }
  171. return err;
  172. }
  173. ae_error_t aesm_join_thread(aesm_thread_t h, ae_error_t *thread_ret)
  174. {
  175. void *ret_value;
  176. AESM_DBG_TRACE("start to join thread %p",h);
  177. if (h == NULL)
  178. {
  179. AESM_DBG_ERROR("Thread handle is NULL.");
  180. return OAL_THREAD_ERROR;
  181. }
  182. if(0!=pthread_join(h->hthread, &ret_value)){
  183. AESM_DBG_ERROR("fail to join thread %p",h);
  184. return OAL_THREAD_ERROR;
  185. }
  186. if(pthread_mutex_lock(&h->mutex)!=0){
  187. AESM_DBG_ERROR("fail to lock thread %p",h);
  188. return AE_FAILURE;
  189. }
  190. if(h->status != AESM_THREAD_PENDING){
  191. AESM_DBG_ERROR("thread %p status error %d in join",h,h->status);
  192. h->status = AESM_THREAD_INVALID;//invalid
  193. pthread_mutex_unlock(&h->mutex);
  194. assert(0);
  195. return OAL_THREAD_ERROR;
  196. }
  197. h->status = AESM_THREAD_DETACHED;//no call to pthread_detach after pthread_join
  198. pthread_mutex_unlock(&h->mutex);
  199. *thread_ret = static_cast<ae_error_t>(reinterpret_cast<ptrdiff_t>(ret_value));
  200. AESM_DBG_TRACE("thread %p join successfully with return value %d",h,*thread_ret);
  201. return AE_SUCCESS;
  202. }
  203. ae_error_t aesm_free_thread(aesm_thread_t h)
  204. {
  205. if (h == NULL){
  206. return AE_SUCCESS;
  207. }
  208. AESM_DBG_TRACE("start to free thread %p",h);
  209. if(pthread_mutex_lock(&h->mutex)!=0){
  210. AESM_DBG_ERROR("fail to lock thread %p",h);
  211. return AE_FAILURE;
  212. }
  213. if(h->status == AESM_THREAD_INIT){//wait for parameters copy to be finished in thread_proc
  214. AESM_DBG_TRACE("wait for parameter copy in thread %p",h);
  215. (void)pthread_cond_wait(&h->copy_cond, &h->mutex);//ignore the waiting error, we will continue to free the thread info even if waiting failed anyway
  216. //The h->status should have been updated if the waiting of copy_cond is finished
  217. }
  218. if(h->status == AESM_THREAD_RUNNING){
  219. h->status = AESM_THREAD_FREED;
  220. }else if(h->status == AESM_THREAD_PENDING){
  221. h->status = AESM_THREAD_DETACHED;
  222. pthread_detach(h->hthread);
  223. AESM_DBG_TRACE("thread %p detached",h);
  224. }
  225. if(h->status == AESM_THREAD_DETACHED){
  226. pthread_mutex_unlock(&h->mutex);
  227. aesm_dealloc_resource(h);
  228. AESM_DBG_TRACE("thread %p resource dealloced",h);
  229. return AE_SUCCESS;
  230. }else if(h->status != AESM_THREAD_FREED){
  231. pthread_mutex_unlock(&h->mutex);
  232. AESM_DBG_ERROR("thread %p status error %d",h,h->status);
  233. aesm_dealloc_resource(h);//dealloc anyway
  234. assert(0);
  235. return OAL_THREAD_ERROR;
  236. }else{
  237. pthread_mutex_unlock(&h->mutex);
  238. AESM_DBG_TRACE("thread %p marked to be free",h);
  239. return AE_SUCCESS;
  240. }
  241. }
  242. ae_error_t aesm_wait_thread(aesm_thread_t h, ae_error_t *thread_ret, unsigned long milisecond)
  243. {
  244. AESM_DBG_TRACE("start to wait thread %p for %d ms",h,milisecond);
  245. if (h == NULL)
  246. {
  247. AESM_DBG_ERROR("Thread handle is NULL.");
  248. return OAL_THREAD_ERROR;
  249. }
  250. if(pthread_mutex_lock(&h->mutex)!=0){
  251. AESM_DBG_TRACE("Fail to hold lock of thread %p",h);
  252. return OAL_THREAD_ERROR;
  253. }
  254. if(h->status==AESM_THREAD_PENDING||h->status==AESM_THREAD_DETACHED){//if the thread has been finished
  255. pthread_mutex_unlock(&h->mutex);
  256. AESM_DBG_TRACE("thread %p is pending",h);
  257. return AE_SUCCESS;
  258. }else if(h->status!=AESM_THREAD_INIT&&h->status!=AESM_THREAD_RUNNING){
  259. pthread_mutex_unlock(&h->mutex);
  260. AESM_DBG_ERROR("invalid thread status %d for thread %p",h->status,h);
  261. return OAL_THREAD_ERROR;
  262. }
  263. struct timespec abstime;
  264. clock_gettime(CLOCK_REALTIME, &abstime);
  265. abstime.tv_sec += milisecond/1000 + (abstime.tv_nsec/1000000+milisecond%1000)/1000;
  266. abstime.tv_nsec = (abstime.tv_nsec/1000000+milisecond%1000)%1000 *1000000 + abstime.tv_nsec%1000000;
  267. int err_no;
  268. err_no = pthread_cond_timedwait(&h->timeout_cond , &h->mutex , &abstime);
  269. if(err_no == ETIMEDOUT)
  270. {
  271. pthread_mutex_unlock(&h->mutex);
  272. AESM_DBG_TRACE("thread %p waiting timeout",h);
  273. return OAL_THREAD_TIMEOUT_ERROR;
  274. }
  275. else if(err_no == 0)
  276. {
  277. *thread_ret = h->ae_ret;
  278. pthread_mutex_unlock(&h->mutex);
  279. AESM_DBG_TRACE("thread %p is detached with return value %d",h,*thread_ret);
  280. return AE_SUCCESS;
  281. }
  282. else
  283. {
  284. pthread_mutex_unlock(&h->mutex);
  285. AESM_DBG_ERROR("thread wait error in thread %p",h);
  286. return OAL_THREAD_ERROR;
  287. }
  288. }