2
0

thread.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_private.h"
  17. #include "win32/apr_arch_threadproc.h"
  18. #include "apr_thread_proc.h"
  19. #include "apr_general.h"
  20. #include "apr_lib.h"
  21. #include "apr_portable.h"
  22. #if APR_HAVE_PROCESS_H
  23. #include <process.h>
  24. #endif
  25. #include "apr_arch_misc.h"
  26. /* Chosen for us by apr_initialize */
  27. DWORD tls_apr_thread = 0;
  28. APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new,
  29. apr_pool_t *pool)
  30. {
  31. (*new) = (apr_threadattr_t *)apr_palloc(pool,
  32. sizeof(apr_threadattr_t));
  33. if ((*new) == NULL) {
  34. return APR_ENOMEM;
  35. }
  36. (*new)->pool = pool;
  37. (*new)->detach = 0;
  38. (*new)->stacksize = 0;
  39. return APR_SUCCESS;
  40. }
  41. APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
  42. apr_int32_t on)
  43. {
  44. attr->detach = on;
  45. return APR_SUCCESS;
  46. }
  47. APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
  48. {
  49. if (attr->detach == 1)
  50. return APR_DETACH;
  51. return APR_NOTDETACH;
  52. }
  53. APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
  54. apr_size_t stacksize)
  55. {
  56. attr->stacksize = stacksize;
  57. return APR_SUCCESS;
  58. }
  59. APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
  60. apr_size_t size)
  61. {
  62. return APR_ENOTIMPL;
  63. }
  64. static void *dummy_worker(void *opaque)
  65. {
  66. apr_thread_t *thd = (apr_thread_t *)opaque;
  67. TlsSetValue(tls_apr_thread, thd->td);
  68. return thd->func(thd, thd->data);
  69. }
  70. APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
  71. apr_threadattr_t *attr,
  72. apr_thread_start_t func,
  73. void *data, apr_pool_t *pool)
  74. {
  75. apr_status_t stat;
  76. unsigned temp;
  77. HANDLE handle;
  78. int priority = THREAD_PRIORITY_NORMAL;
  79. (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
  80. if ((*new) == NULL) {
  81. return APR_ENOMEM;
  82. }
  83. (*new)->pool = pool;
  84. (*new)->data = data;
  85. (*new)->func = func;
  86. (*new)->td = NULL;
  87. stat = apr_pool_create(&(*new)->pool, pool);
  88. if (stat != APR_SUCCESS) {
  89. return stat;
  90. }
  91. if (attr && attr->priority && attr->priority > 0) {
  92. if (attr->priority >= 99) {
  93. priority = THREAD_PRIORITY_TIME_CRITICAL;
  94. } else if (attr->priority >= 50) {
  95. priority = THREAD_PRIORITY_ABOVE_NORMAL;
  96. } else if (attr->priority >= 10) {
  97. priority = THREAD_PRIORITY_NORMAL;
  98. } else if (attr->priority >= 1) {
  99. priority = THREAD_PRIORITY_LOWEST;
  100. }
  101. }
  102. /* Use 0 for Thread Stack Size, because that will default the stack to the
  103. * same size as the calling thread.
  104. */
  105. #ifndef _WIN32_WCE
  106. if ((handle = (HANDLE)_beginthreadex(NULL,
  107. attr && attr->stacksize > 0 ? attr->stacksize : 0,
  108. (unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
  109. (*new), 0, &temp)) == 0) {
  110. return APR_FROM_OS_ERROR(_doserrno);
  111. }
  112. #else
  113. if ((handle = CreateThread(NULL,
  114. attr && attr->stacksize > 0 ? attr->stacksize : 0,
  115. (unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
  116. (*new), 0, &temp)) == 0) {
  117. return apr_get_os_error();
  118. }
  119. #endif
  120. if (priority) {
  121. SetThreadPriority(handle, priority);
  122. }
  123. if (attr && attr->detach) {
  124. CloseHandle(handle);
  125. }
  126. else
  127. (*new)->td = handle;
  128. return APR_SUCCESS;
  129. }
  130. APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
  131. apr_status_t retval)
  132. {
  133. thd->exitval = retval;
  134. apr_pool_destroy(thd->pool);
  135. thd->pool = NULL;
  136. #ifndef _WIN32_WCE
  137. _endthreadex(0);
  138. #else
  139. ExitThread(0);
  140. #endif
  141. return APR_SUCCESS;
  142. }
  143. APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
  144. apr_thread_t *thd)
  145. {
  146. apr_status_t rv = APR_SUCCESS;
  147. if (!thd->td) {
  148. /* Can not join on detached threads */
  149. return APR_DETACH;
  150. }
  151. rv = WaitForSingleObject(thd->td, INFINITE);
  152. if ( rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
  153. /* If the thread_exit has been called */
  154. if (!thd->pool)
  155. *retval = thd->exitval;
  156. else
  157. rv = APR_INCOMPLETE;
  158. }
  159. else
  160. rv = apr_get_os_error();
  161. CloseHandle(thd->td);
  162. thd->td = NULL;
  163. return rv;
  164. }
  165. APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
  166. {
  167. if (thd->td && CloseHandle(thd->td)) {
  168. thd->td = NULL;
  169. return APR_SUCCESS;
  170. }
  171. else {
  172. return apr_get_os_error();
  173. }
  174. }
  175. APR_DECLARE(void) apr_thread_yield()
  176. {
  177. /* SwitchToThread is not supported on Win9x, but since it's
  178. * primarily a noop (entering time consuming code, therefore
  179. * providing more critical threads a bit larger timeslice)
  180. * we won't worry too much if it's not available.
  181. */
  182. #ifndef _WIN32_WCE
  183. if (apr_os_level >= APR_WIN_NT) {
  184. SwitchToThread();
  185. }
  186. #endif
  187. }
  188. APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
  189. apr_thread_t *thread)
  190. {
  191. return apr_pool_userdata_get(data, key, thread->pool);
  192. }
  193. APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
  194. apr_status_t (*cleanup) (void *),
  195. apr_thread_t *thread)
  196. {
  197. return apr_pool_userdata_set(data, key, cleanup, thread->pool);
  198. }
  199. APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
  200. {
  201. HANDLE hthread = (HANDLE)TlsGetValue(tls_apr_thread);
  202. HANDLE hproc;
  203. if (hthread) {
  204. return hthread;
  205. }
  206. hproc = GetCurrentProcess();
  207. hthread = GetCurrentThread();
  208. if (!DuplicateHandle(hproc, hthread,
  209. hproc, &hthread, 0, FALSE,
  210. DUPLICATE_SAME_ACCESS)) {
  211. return NULL;
  212. }
  213. TlsSetValue(tls_apr_thread, hthread);
  214. return hthread;
  215. }
  216. APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
  217. apr_thread_t *thd)
  218. {
  219. if (thd == NULL) {
  220. return APR_ENOTHREAD;
  221. }
  222. *thethd = thd->td;
  223. return APR_SUCCESS;
  224. }
  225. APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
  226. apr_os_thread_t *thethd,
  227. apr_pool_t *pool)
  228. {
  229. if (pool == NULL) {
  230. return APR_ENOPOOL;
  231. }
  232. if ((*thd) == NULL) {
  233. (*thd) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
  234. (*thd)->pool = pool;
  235. }
  236. (*thd)->td = thethd;
  237. return APR_SUCCESS;
  238. }
  239. APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
  240. apr_pool_t *p)
  241. {
  242. (*control) = apr_pcalloc(p, sizeof(**control));
  243. return APR_SUCCESS;
  244. }
  245. APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
  246. void (*func)(void))
  247. {
  248. if (!InterlockedExchange(&control->value, 1)) {
  249. func();
  250. }
  251. return APR_SUCCESS;
  252. }
  253. APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
  254. apr_os_thread_t tid2)
  255. {
  256. /* Since the only tid's we support our are own, and
  257. * apr_os_thread_current returns the identical handle
  258. * to the one we created initially, the test is simple.
  259. */
  260. return (tid1 == tid2);
  261. }
  262. APR_POOL_IMPLEMENT_ACCESSOR(thread)