aesm_thread.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2011-2016 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. pthread_mutex_lock(&p->mutex);
  90. p->ae_ret = err;
  91. if(p->status == AESM_THREAD_RUNNING){
  92. p->status = AESM_THREAD_PENDING;
  93. pthread_cond_signal(&p->timeout_cond);
  94. pthread_mutex_unlock(&p->mutex);
  95. AESM_DBG_TRACE("thread %p change to status AESM_THREAD_PEDNING",param);
  96. }else if(p->status == AESM_THREAD_FREED){
  97. pthread_mutex_unlock(&p->mutex);
  98. pthread_detach(p->hthread);
  99. aesm_dealloc_resource(p);
  100. AESM_DBG_TRACE("resource of thread %p has been dealloced",param);
  101. }else{
  102. p->status = AESM_THREAD_INVALID;//It should never be reached
  103. pthread_mutex_unlock(&p->mutex);
  104. AESM_DBG_TRACE("thread %p status invalid",param);
  105. assert(0);
  106. }
  107. return reinterpret_cast<void *>(static_cast<ptrdiff_t>(err));
  108. }
  109. ae_error_t aesm_create_thread(aesm_thread_function_t function_entry, aesm_thread_arg_type_t arg, aesm_thread_t* h)
  110. {
  111. ae_error_t err = AE_SUCCESS;
  112. int r=0;
  113. int mutex_inited = 0;
  114. int copy_cond_inited = 0;
  115. int timeout_cond_inited = 0;
  116. assert(h!=NULL);
  117. AESM_DBG_TRACE("start to create a thread");
  118. struct _aesm_thread_t *p=(struct _aesm_thread_t *)malloc(sizeof(struct _aesm_thread_t));
  119. if(p==NULL){
  120. AESM_DBG_ERROR("fail to malloc");
  121. err = AE_OUT_OF_MEMORY_ERROR;
  122. goto ret_point;
  123. }
  124. memset(p, 0, sizeof(struct _aesm_thread_t));
  125. p->arg = arg;
  126. p->fun_entry = function_entry;
  127. if(pthread_mutex_init(&p->mutex, NULL)!=0){
  128. err = AE_FAILURE;
  129. AESM_DBG_ERROR("fail to init mutex");
  130. goto ret_point;
  131. }
  132. mutex_inited=1;
  133. if(pthread_cond_init(&p->copy_cond, NULL)!=0){
  134. err = AE_FAILURE;
  135. AESM_DBG_ERROR("fail to init copy cond");
  136. goto ret_point;
  137. }
  138. copy_cond_inited=1;
  139. if(pthread_cond_init(&p->timeout_cond, NULL)!=0){
  140. err = AE_FAILURE;
  141. AESM_DBG_ERROR("fail to init timeout cond");
  142. goto ret_point;
  143. }
  144. timeout_cond_inited=1;
  145. p->status = AESM_THREAD_INIT;
  146. r=pthread_create(&p->hthread, NULL, aesm_thread_proc, p);
  147. if(r!=0){
  148. AESM_DBG_ERROR("fail to create thread");
  149. err = OAL_THREAD_ERROR;
  150. goto ret_point;
  151. }
  152. *h = p;
  153. AESM_DBG_TRACE("thread %p created successfully",p);
  154. ret_point:
  155. if(err!=AE_SUCCESS&&p!=NULL){
  156. if(mutex_inited!=0){
  157. (void)pthread_mutex_destroy(&p->mutex);
  158. }
  159. if(copy_cond_inited!=0){
  160. (void)pthread_cond_destroy(&p->copy_cond);
  161. }
  162. if(timeout_cond_inited!=0){
  163. (void)pthread_cond_destroy(&p->timeout_cond);
  164. }
  165. free(p);
  166. }
  167. return err;
  168. }
  169. ae_error_t aesm_join_thread(aesm_thread_t h, ae_error_t *thread_ret)
  170. {
  171. void *ret_value;
  172. AESM_DBG_TRACE("start to join thread %p",h);
  173. if(0!=pthread_join(h->hthread, &ret_value)){
  174. AESM_DBG_ERROR("fail to join thread %p",h);
  175. return OAL_THREAD_ERROR;
  176. }
  177. if(pthread_mutex_lock(&h->mutex)!=0){
  178. AESM_DBG_ERROR("fail to lock thread %p",h);
  179. return AE_FAILURE;
  180. }
  181. if(h->status != AESM_THREAD_PENDING){
  182. AESM_DBG_ERROR("thread %p status error %d in join",h,h->status);
  183. h->status = AESM_THREAD_INVALID;//invalid
  184. pthread_mutex_unlock(&h->mutex);
  185. assert(0);
  186. return OAL_THREAD_ERROR;
  187. }
  188. h->status = AESM_THREAD_DETACHED;//no call to pthread_detach after pthread_join
  189. pthread_mutex_unlock(&h->mutex);
  190. *thread_ret = static_cast<ae_error_t>(reinterpret_cast<ptrdiff_t>(ret_value));
  191. AESM_DBG_TRACE("thread %p join successfully with return value %d",h,*thread_ret);
  192. return AE_SUCCESS;
  193. }
  194. ae_error_t aesm_free_thread(aesm_thread_t h)
  195. {
  196. AESM_DBG_TRACE("start to free thread %p",h);
  197. if(pthread_mutex_lock(&h->mutex)!=0){
  198. AESM_DBG_ERROR("fail to lock thread %p",h);
  199. return AE_FAILURE;
  200. }
  201. if(h->status == AESM_THREAD_INIT){//wait for parameters copy to be finished in thread_proc
  202. AESM_DBG_TRACE("wait for parameter copy in thread %p",h);
  203. (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
  204. //The h->status should have been updated if the waiting of copy_cond is finished
  205. }
  206. if(h->status == AESM_THREAD_RUNNING){
  207. h->status = AESM_THREAD_FREED;
  208. }else if(h->status == AESM_THREAD_PENDING){
  209. h->status = AESM_THREAD_DETACHED;
  210. pthread_detach(h->hthread);
  211. AESM_DBG_TRACE("thread %p detached",h);
  212. }
  213. if(h->status == AESM_THREAD_DETACHED){
  214. pthread_mutex_unlock(&h->mutex);
  215. aesm_dealloc_resource(h);
  216. AESM_DBG_TRACE("thread %p resource dealloced",h);
  217. return AE_SUCCESS;
  218. }else if(h->status != AESM_THREAD_FREED){
  219. pthread_mutex_unlock(&h->mutex);
  220. AESM_DBG_ERROR("thread %p status error %d",h,h->status);
  221. aesm_dealloc_resource(h);//dealloc anyway
  222. assert(0);
  223. return OAL_THREAD_ERROR;
  224. }else{
  225. pthread_mutex_unlock(&h->mutex);
  226. AESM_DBG_TRACE("thread %p marked to be free",h);
  227. return AE_SUCCESS;
  228. }
  229. }
  230. ae_error_t aesm_wait_thread(aesm_thread_t h, ae_error_t *thread_ret, unsigned long milisecond)
  231. {
  232. AESM_DBG_TRACE("start to wait thread %p for %d ms",h,milisecond);
  233. pthread_mutex_lock(&h->mutex);
  234. if(h->status==AESM_THREAD_PENDING||h->status==AESM_THREAD_DETACHED){//if the thread has been finished
  235. pthread_mutex_unlock(&h->mutex);
  236. AESM_DBG_TRACE("thread %p is pending",h);
  237. return AE_SUCCESS;
  238. }else if(h->status!=AESM_THREAD_INIT&&h->status!=AESM_THREAD_RUNNING){
  239. pthread_mutex_unlock(&h->mutex);
  240. AESM_DBG_ERROR("invalid thread status %d for thread %p",h->status,h);
  241. return OAL_THREAD_ERROR;
  242. }
  243. struct timespec abstime;
  244. clock_gettime(CLOCK_REALTIME, &abstime);
  245. abstime.tv_sec += milisecond/1000 + (abstime.tv_nsec/1000000+milisecond%1000)/1000;
  246. abstime.tv_nsec = (abstime.tv_nsec/1000000+milisecond%1000)%1000 *1000000 + abstime.tv_nsec%1000000;
  247. int err_no;
  248. err_no = pthread_cond_timedwait(&h->timeout_cond , &h->mutex , &abstime);
  249. if(err_no == ETIMEDOUT)
  250. {
  251. pthread_mutex_unlock(&h->mutex);
  252. AESM_DBG_TRACE("thread %p waiting timeout",h);
  253. return OAL_THREAD_TIMEOUT_ERROR;
  254. }
  255. else if(err_no == 0)
  256. {
  257. *thread_ret = h->ae_ret;
  258. pthread_mutex_unlock(&h->mutex);
  259. AESM_DBG_TRACE("thread %p is detached with return value %d",h,*thread_ret);
  260. return AE_SUCCESS;
  261. }
  262. else
  263. {
  264. pthread_mutex_unlock(&h->mutex);
  265. AESM_DBG_ERROR("thread wait error in thread %p",h);
  266. return OAL_THREAD_ERROR;
  267. }
  268. }