thread.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr.h"
  17. #include "fspr_portable.h"
  18. #include "fspr_strings.h"
  19. #include "fspr_arch_threadproc.h"
  20. static int thread_count = 0;
  21. fspr_status_t fspr_threadattr_create(fspr_threadattr_t **new,
  22. fspr_pool_t *pool)
  23. {
  24. (*new) = (fspr_threadattr_t *)fspr_palloc(pool,
  25. sizeof(fspr_threadattr_t));
  26. if ((*new) == NULL) {
  27. return APR_ENOMEM;
  28. }
  29. (*new)->pool = pool;
  30. (*new)->stack_size = APR_DEFAULT_STACK_SIZE;
  31. (*new)->detach = 0;
  32. (*new)->thread_name = NULL;
  33. return APR_SUCCESS;
  34. }
  35. fspr_status_t fspr_threadattr_detach_set(fspr_threadattr_t *attr,fspr_int32_t on)
  36. {
  37. attr->detach = on;
  38. return APR_SUCCESS;
  39. }
  40. fspr_status_t fspr_threadattr_detach_get(fspr_threadattr_t *attr)
  41. {
  42. if (attr->detach == 1)
  43. return APR_DETACH;
  44. return APR_NOTDETACH;
  45. }
  46. APR_DECLARE(fspr_status_t) fspr_threadattr_stacksize_set(fspr_threadattr_t *attr,
  47. fspr_size_t stacksize)
  48. {
  49. attr->stack_size = stacksize;
  50. return APR_SUCCESS;
  51. }
  52. APR_DECLARE(fspr_status_t) fspr_threadattr_guardsize_set(fspr_threadattr_t *attr,
  53. fspr_size_t size)
  54. {
  55. return APR_ENOTIMPL;
  56. }
  57. static void *dummy_worker(void *opaque)
  58. {
  59. fspr_thread_t *thd = (fspr_thread_t *)opaque;
  60. return thd->func(thd, thd->data);
  61. }
  62. fspr_status_t fspr_thread_create(fspr_thread_t **new,
  63. fspr_threadattr_t *attr,
  64. fspr_thread_start_t func,
  65. void *data,
  66. fspr_pool_t *pool)
  67. {
  68. fspr_status_t stat;
  69. long flags = NX_THR_BIND_CONTEXT;
  70. char threadName[NX_MAX_OBJECT_NAME_LEN+1];
  71. size_t stack_size = APR_DEFAULT_STACK_SIZE;
  72. if (attr && attr->thread_name) {
  73. strncpy (threadName, attr->thread_name, NX_MAX_OBJECT_NAME_LEN);
  74. }
  75. else {
  76. sprintf(threadName, "APR_thread %04ld", ++thread_count);
  77. }
  78. /* An original stack size of 0 will allow NXCreateThread() to
  79. * assign a default system stack size. An original stack
  80. * size of less than 0 will assign the APR default stack size.
  81. * anything else will be taken as is.
  82. */
  83. if (attr && (attr->stack_size >= 0)) {
  84. stack_size = attr->stack_size;
  85. }
  86. (*new) = (fspr_thread_t *)fspr_palloc(pool, sizeof(fspr_thread_t));
  87. if ((*new) == NULL) {
  88. return APR_ENOMEM;
  89. }
  90. (*new)->pool = pool;
  91. (*new)->data = data;
  92. (*new)->func = func;
  93. (*new)->thread_name = (char*)fspr_pstrdup(pool, threadName);
  94. stat = fspr_pool_create(&(*new)->pool, pool);
  95. if (stat != APR_SUCCESS) {
  96. return stat;
  97. }
  98. if (attr && attr->detach) {
  99. flags |= NX_THR_DETACHED;
  100. }
  101. (*new)->ctx = NXContextAlloc(
  102. /* void(*start_routine)(void *arg)*/(void (*)(void *)) dummy_worker,
  103. /* void *arg */ (*new),
  104. /* int priority */ NX_PRIO_MED,
  105. /* NXSize_t stackSize */ stack_size,
  106. /* long flags */ NX_CTX_NORMAL,
  107. /* int *error */ &stat);
  108. stat = NXContextSetName(
  109. /* NXContext_t ctx */ (*new)->ctx,
  110. /* const char *name */ threadName);
  111. stat = NXThreadCreate(
  112. /* NXContext_t context */ (*new)->ctx,
  113. /* long flags */ flags,
  114. /* NXThreadId_t *thread_id */ &(*new)->td);
  115. if(stat==0)
  116. return APR_SUCCESS;
  117. return(stat);// if error
  118. }
  119. fspr_os_thread_t fspr_os_thread_current()
  120. {
  121. return NXThreadGetId();
  122. }
  123. int fspr_os_thread_equal(fspr_os_thread_t tid1, fspr_os_thread_t tid2)
  124. {
  125. return (tid1 == tid2);
  126. }
  127. void fspr_thread_yield()
  128. {
  129. NXThreadYield();
  130. }
  131. fspr_status_t fspr_thread_exit(fspr_thread_t *thd,
  132. fspr_status_t retval)
  133. {
  134. thd->exitval = retval;
  135. fspr_pool_destroy(thd->pool);
  136. NXThreadExit(NULL);
  137. return APR_SUCCESS;
  138. }
  139. fspr_status_t fspr_thread_join(fspr_status_t *retval,
  140. fspr_thread_t *thd)
  141. {
  142. fspr_status_t stat;
  143. NXThreadId_t dthr;
  144. if ((stat = NXThreadJoin(thd->td, &dthr, NULL)) == 0) {
  145. *retval = thd->exitval;
  146. return APR_SUCCESS;
  147. }
  148. else {
  149. return stat;
  150. }
  151. }
  152. fspr_status_t fspr_thread_detach(fspr_thread_t *thd)
  153. {
  154. return APR_SUCCESS;
  155. }
  156. fspr_status_t fspr_thread_data_get(void **data, const char *key,
  157. fspr_thread_t *thread)
  158. {
  159. if (thread != NULL) {
  160. return fspr_pool_userdata_get(data, key, thread->pool);
  161. }
  162. else {
  163. data = NULL;
  164. return APR_ENOTHREAD;
  165. }
  166. }
  167. fspr_status_t fspr_thread_data_set(void *data, const char *key,
  168. fspr_status_t (*cleanup) (void *),
  169. fspr_thread_t *thread)
  170. {
  171. if (thread != NULL) {
  172. return fspr_pool_userdata_set(data, key, cleanup, thread->pool);
  173. }
  174. else {
  175. data = NULL;
  176. return APR_ENOTHREAD;
  177. }
  178. }
  179. APR_DECLARE(fspr_status_t) fspr_os_thread_get(fspr_os_thread_t **thethd,
  180. fspr_thread_t *thd)
  181. {
  182. if (thd == NULL) {
  183. return APR_ENOTHREAD;
  184. }
  185. *thethd = &(thd->td);
  186. return APR_SUCCESS;
  187. }
  188. APR_DECLARE(fspr_status_t) fspr_os_thread_put(fspr_thread_t **thd,
  189. fspr_os_thread_t *thethd,
  190. fspr_pool_t *pool)
  191. {
  192. if (pool == NULL) {
  193. return APR_ENOPOOL;
  194. }
  195. if ((*thd) == NULL) {
  196. (*thd) = (fspr_thread_t *)fspr_palloc(pool, sizeof(fspr_thread_t));
  197. (*thd)->pool = pool;
  198. }
  199. (*thd)->td = *thethd;
  200. return APR_SUCCESS;
  201. }
  202. APR_DECLARE(fspr_status_t) fspr_thread_once_init(fspr_thread_once_t **control,
  203. fspr_pool_t *p)
  204. {
  205. (*control) = fspr_pcalloc(p, sizeof(**control));
  206. return APR_SUCCESS;
  207. }
  208. APR_DECLARE(fspr_status_t) fspr_thread_once(fspr_thread_once_t *control,
  209. void (*func)(void))
  210. {
  211. if (!atomic_xchg(&control->value, 1)) {
  212. func();
  213. }
  214. return APR_SUCCESS;
  215. }
  216. APR_POOL_IMPLEMENT_ACCESSOR(thread)