2
0

thread.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_arch_threadproc.h"
  17. #include "fspr_portable.h"
  18. APR_DECLARE(fspr_status_t) fspr_threadattr_create(fspr_threadattr_t **new, fspr_pool_t *pool)
  19. {
  20. (*new) = (fspr_threadattr_t *)fspr_palloc(pool,
  21. sizeof(fspr_threadattr_t));
  22. if ((*new) == NULL) {
  23. return APR_ENOMEM;
  24. }
  25. (*new)->pool = pool;
  26. (*new)->attr = (int32)B_NORMAL_PRIORITY;
  27. return APR_SUCCESS;
  28. }
  29. APR_DECLARE(fspr_status_t) fspr_threadattr_detach_set(fspr_threadattr_t *attr, fspr_int32_t on)
  30. {
  31. if (on == 1){
  32. attr->detached = 1;
  33. } else {
  34. attr->detached = 0;
  35. }
  36. return APR_SUCCESS;
  37. }
  38. APR_DECLARE(fspr_status_t) fspr_threadattr_detach_get(fspr_threadattr_t *attr)
  39. {
  40. if (attr->detached == 1){
  41. return APR_DETACH;
  42. }
  43. return APR_NOTDETACH;
  44. }
  45. APR_DECLARE(fspr_status_t) fspr_threadattr_stacksize_set(fspr_threadattr_t *attr,
  46. fspr_size_t stacksize)
  47. {
  48. return APR_ENOTIMPL;
  49. }
  50. APR_DECLARE(fspr_status_t) fspr_threadattr_guardsize_set(fspr_threadattr_t *attr,
  51. fspr_size_t size)
  52. {
  53. return APR_ENOTIMPL;
  54. }
  55. static void *dummy_worker(void *opaque)
  56. {
  57. fspr_thread_t *thd = (fspr_thread_t*)opaque;
  58. return thd->func(thd, thd->data);
  59. }
  60. APR_DECLARE(fspr_status_t) fspr_thread_create(fspr_thread_t **new, fspr_threadattr_t *attr,
  61. fspr_thread_start_t func, void *data,
  62. fspr_pool_t *pool)
  63. {
  64. int32 temp;
  65. fspr_status_t stat;
  66. (*new) = (fspr_thread_t *)fspr_palloc(pool, sizeof(fspr_thread_t));
  67. if ((*new) == NULL) {
  68. return APR_ENOMEM;
  69. }
  70. (*new)->pool = pool;
  71. (*new)->data = data;
  72. (*new)->func = func;
  73. (*new)->exitval = -1;
  74. /* First we create the new thread...*/
  75. if (attr)
  76. temp = attr->attr;
  77. else
  78. temp = B_NORMAL_PRIORITY;
  79. stat = fspr_pool_create(&(*new)->pool, pool);
  80. if (stat != APR_SUCCESS) {
  81. return stat;
  82. }
  83. (*new)->td = spawn_thread((thread_func)dummy_worker,
  84. "apr thread",
  85. temp,
  86. (*new));
  87. /* Now we try to run it...*/
  88. if (resume_thread((*new)->td) == B_NO_ERROR) {
  89. return APR_SUCCESS;
  90. }
  91. else {
  92. return errno;
  93. }
  94. }
  95. APR_DECLARE(fspr_os_thread_t) fspr_os_thread_current(void)
  96. {
  97. return find_thread(NULL);
  98. }
  99. int fspr_os_thread_equal(fspr_os_thread_t tid1, fspr_os_thread_t tid2)
  100. {
  101. return tid1 == tid2;
  102. }
  103. APR_DECLARE(fspr_status_t) fspr_thread_exit(fspr_thread_t *thd, fspr_status_t retval)
  104. {
  105. fspr_pool_destroy(thd->pool);
  106. thd->exitval = retval;
  107. exit_thread ((status_t)(retval));
  108. /* This will never be reached... */
  109. return APR_SUCCESS;
  110. }
  111. APR_DECLARE(fspr_status_t) fspr_thread_join(fspr_status_t *retval, fspr_thread_t *thd)
  112. {
  113. status_t rv = 0, ret;
  114. ret = wait_for_thread(thd->td, &rv);
  115. if (ret == B_NO_ERROR) {
  116. *retval = rv;
  117. return APR_SUCCESS;
  118. }
  119. else {
  120. /* if we've missed the thread's death, did we set an exit value prior
  121. * to it's demise? If we did return that.
  122. */
  123. if (thd->exitval != -1) {
  124. *retval = thd->exitval;
  125. return APR_SUCCESS;
  126. } else
  127. return ret;
  128. }
  129. }
  130. APR_DECLARE(fspr_status_t) fspr_thread_detach(fspr_thread_t *thd)
  131. {
  132. if (suspend_thread(thd->td) == B_NO_ERROR){
  133. return APR_SUCCESS;
  134. }
  135. else {
  136. return errno;
  137. }
  138. }
  139. void fspr_thread_yield()
  140. {
  141. }
  142. APR_DECLARE(fspr_status_t) fspr_thread_data_get(void **data, const char *key, fspr_thread_t *thread)
  143. {
  144. return fspr_pool_userdata_get(data, key, thread->pool);
  145. }
  146. APR_DECLARE(fspr_status_t) fspr_thread_data_set(void *data, const char *key,
  147. fspr_status_t (*cleanup) (void *),
  148. fspr_thread_t *thread)
  149. {
  150. return fspr_pool_userdata_set(data, key, cleanup, thread->pool);
  151. }
  152. APR_DECLARE(fspr_status_t) fspr_os_thread_get(fspr_os_thread_t **thethd, fspr_thread_t *thd)
  153. {
  154. *thethd = &thd->td;
  155. return APR_SUCCESS;
  156. }
  157. APR_DECLARE(fspr_status_t) fspr_os_thread_put(fspr_thread_t **thd, fspr_os_thread_t *thethd,
  158. fspr_pool_t *pool)
  159. {
  160. if (pool == NULL) {
  161. return APR_ENOPOOL;
  162. }
  163. if ((*thd) == NULL) {
  164. (*thd) = (fspr_thread_t *)fspr_pcalloc(pool, sizeof(fspr_thread_t));
  165. (*thd)->pool = pool;
  166. }
  167. (*thd)->td = *thethd;
  168. return APR_SUCCESS;
  169. }
  170. static fspr_status_t thread_once_cleanup(void *vcontrol)
  171. {
  172. fspr_thread_once_t *control = (fspr_thread_once_t *)vcontrol;
  173. if (control->sem) {
  174. release_sem(control->sem);
  175. delete_sem(control->sem);
  176. }
  177. return APR_SUCCESS;
  178. }
  179. APR_DECLARE(fspr_status_t) fspr_thread_once_init(fspr_thread_once_t **control,
  180. fspr_pool_t *p)
  181. {
  182. int rc;
  183. *control = (fspr_thread_once_t *)fspr_pcalloc(p, sizeof(fspr_thread_once_t));
  184. (*control)->hit = 0; /* we haven't done it yet... */
  185. rc = ((*control)->sem = create_sem(1, "thread_once"));
  186. if (rc < 0)
  187. return rc;
  188. fspr_pool_cleanup_register(p, control, thread_once_cleanup, fspr_pool_cleanup_null);
  189. return APR_SUCCESS;
  190. }
  191. APR_DECLARE(fspr_status_t) fspr_thread_once(fspr_thread_once_t *control,
  192. void (*func)(void))
  193. {
  194. if (!control->hit) {
  195. if (acquire_sem(control->sem) == B_OK) {
  196. control->hit = 1;
  197. func();
  198. }
  199. }
  200. return APR_SUCCESS;
  201. }
  202. APR_POOL_IMPLEMENT_ACCESSOR(thread)