mturk 2004/09/22 23:17:59
Modified: threadproc/win32 Tag: APR_0_9_BRANCH thread.c
Log:
Backport from HEAD.
Makes the threads to behave like on posix. If the thread is created without
APR_DETACH expect that the thread_join will be called, so don't close the
handle in advance, if the thread has already finished.
Reviewed by: wrowe
Revision Changes Path
No revision
No revision
1.54.2.2 +32 -18 apr/threadproc/win32/thread.c
Index: thread.c
===================================================================
RCS file: /home/cvs/apr/threadproc/win32/thread.c,v
retrieving revision 1.54.2.1
retrieving revision 1.54.2.2
diff -u -r1.54.2.1 -r1.54.2.2
--- thread.c 13 Feb 2004 09:33:55 -0000 1.54.2.1
+++ thread.c 23 Sep 2004 06:17:59 -0000 1.54.2.2
@@ -38,6 +38,8 @@
}
(*new)->pool = pool;
+ (*new)->detach = 0;
+
return APR_SUCCESS;
}
@@ -69,7 +71,8 @@
{
apr_status_t stat;
unsigned temp;
-
+ HANDLE handle;
+
(*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
if ((*new) == NULL) {
@@ -79,7 +82,8 @@
(*new)->pool = pool;
(*new)->data = data;
(*new)->func = func;
-
+ (*new)->td = NULL;
+
stat = apr_pool_create(&(*new)->pool, pool);
if (stat != APR_SUCCESS) {
return stat;
@@ -89,22 +93,23 @@
* same size as the calling thread.
*/
#ifndef _WIN32_WCE
- if (((*new)->td = (HANDLE)_beginthreadex(NULL, 0,
+ if ((handle = (HANDLE)_beginthreadex(NULL, 0,
(unsigned int (APR_THREAD_FUNC *)(void
*))dummy_worker,
(*new), 0, &temp)) == 0) {
return APR_FROM_OS_ERROR(_doserrno);
}
#else
- if (((*new)->td = CreateThread(NULL, 0,
+ if ((handle = CreateThread(NULL, 0,
(unsigned int (APR_THREAD_FUNC *)(void
*))dummy_worker,
(*new), 0, &temp)) == 0) {
return apr_get_os_error();
}
#endif
if (attr && attr->detach) {
- CloseHandle((*new)->td);
- (*new)->td = NULL;
+ CloseHandle(handle);
}
+ else
+ (*new)->td = handle;
return APR_SUCCESS;
}
@@ -114,10 +119,8 @@
{
thd->exitval = retval;
apr_pool_destroy(thd->pool);
+ thd->pool = NULL;
#ifndef _WIN32_WCE
- if (thd->td) {
- CloseHandle(thd->td);
- }
_endthreadex(0);
#else
ExitThread(0);
@@ -128,15 +131,26 @@
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
apr_thread_t *thd)
{
- apr_status_t rv;
-
- rv = WaitForSingleObject(thd->td, INFINITE);
- if ( rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
- *retval = thd->exitval;
- return APR_SUCCESS;
- }
- /* Wait failed */
- return apr_get_os_error();;
+ apr_status_t rv = APR_SUCCESS;
+
+ if (!thd->td) {
+ /* Can not join on detached threads */
+ return APR_DETACH;
+ }
+ rv = WaitForSingleObject(thd->td, INFINITE);
+ if ( rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
+ /* If the thread_exit has been called */
+ if (!thd->pool)
+ *retval = thd->exitval;
+ else
+ rv = APR_INCOMPLETE;
+ }
+ else
+ rv = apr_get_os_error();
+ CloseHandle(thd->td);
+ thd->td = NULL;
+
+ return rv;
}
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)