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 "apr_arch_threadproc.h"
  17. #include "apr_portable.h"
  18. APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new, apr_pool_t *pool)
  19. {
  20. (*new) = (apr_threadattr_t *)apr_palloc(pool,
  21. sizeof(apr_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(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr, apr_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(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
  39. {
  40. if (attr->detached == 1){
  41. return APR_DETACH;
  42. }
  43. return APR_NOTDETACH;
  44. }
  45. APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
  46. apr_size_t stacksize)
  47. {
  48. return APR_ENOTIMPL;
  49. }
  50. APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
  51. apr_size_t size)
  52. {
  53. return APR_ENOTIMPL;
  54. }
  55. static void *dummy_worker(void *opaque)
  56. {
  57. apr_thread_t *thd = (apr_thread_t*)opaque;
  58. return thd->func(thd, thd->data);
  59. }
  60. APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr,
  61. apr_thread_start_t func, void *data,
  62. apr_pool_t *pool)
  63. {
  64. int32 temp;
  65. apr_status_t stat;
  66. (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_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 = apr_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(apr_os_thread_t) apr_os_thread_current(void)
  96. {
  97. return find_thread(NULL);
  98. }
  99. int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
  100. {
  101. return tid1 == tid2;
  102. }
  103. APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
  104. {
  105. apr_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(apr_status_t) apr_thread_join(apr_status_t *retval, apr_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(apr_status_t) apr_thread_detach(apr_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 apr_thread_yield()
  140. {
  141. }
  142. APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
  143. {
  144. return apr_pool_userdata_get(data, key, thread->pool);
  145. }
  146. APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
  147. apr_status_t (*cleanup) (void *),
  148. apr_thread_t *thread)
  149. {
  150. return apr_pool_userdata_set(data, key, cleanup, thread->pool);
  151. }
  152. APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
  153. {
  154. *thethd = &thd->td;
  155. return APR_SUCCESS;
  156. }
  157. APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, apr_os_thread_t *thethd,
  158. apr_pool_t *pool)
  159. {
  160. if (pool == NULL) {
  161. return APR_ENOPOOL;
  162. }
  163. if ((*thd) == NULL) {
  164. (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
  165. (*thd)->pool = pool;
  166. }
  167. (*thd)->td = *thethd;
  168. return APR_SUCCESS;
  169. }
  170. static apr_status_t thread_once_cleanup(void *vcontrol)
  171. {
  172. apr_thread_once_t *control = (apr_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(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
  180. apr_pool_t *p)
  181. {
  182. int rc;
  183. *control = (apr_thread_once_t *)apr_pcalloc(p, sizeof(apr_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. apr_pool_cleanup_register(p, control, thread_once_cleanup, apr_pool_cleanup_null);
  189. return APR_SUCCESS;
  190. }
  191. APR_DECLARE(apr_status_t) apr_thread_once(apr_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)