X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=win32%2Fwin32thread.h;h=6f355f09ebba7d21f72ede2548d1ad350d73bbb6;hb=c69f112c145fabe210a7e2c5c2406baeea71af2f;hp=697af3fe8039611b06e26772cc487e53236ec359;hpb=4e35701fd273ba8d0093a29660dee38a92408e9b;p=p5sagit%2Fp5-mst-13.2.git diff --git a/win32/win32thread.h b/win32/win32thread.h index 697af3f..6f355f0 100644 --- a/win32/win32thread.h +++ b/win32/win32thread.h @@ -2,22 +2,23 @@ #define _WIN32THREAD_H typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond; typedef DWORD perl_key; -typedef HANDLE perl_thread; +typedef HANDLE perl_os_thread; -/* XXX Critical Sections used instead of mutexes: lightweight, +#ifndef DONT_USE_CRITICAL_SECTION + +/* Critical Sections used instead of mutexes: lightweight, * but can't be communicated to child processes, and can't get - * HANDLE to it for use elsewhere + * HANDLE to it for use elsewhere. */ - -#ifndef DONT_USE_CRITICAL_SECTION typedef CRITICAL_SECTION perl_mutex; #define MUTEX_INIT(m) InitializeCriticalSection(m) #define MUTEX_LOCK(m) EnterCriticalSection(m) #define MUTEX_UNLOCK(m) LeaveCriticalSection(m) #define MUTEX_DESTROY(m) DeleteCriticalSection(m) + #else -typedef HANDLE perl_mutex; +typedef HANDLE perl_mutex; #define MUTEX_INIT(m) \ STMT_START { \ if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \ @@ -46,44 +47,46 @@ typedef HANDLE perl_mutex; * so there's no separate mutex protecting access to (c)->waiters */ #define COND_INIT(c) \ - STMT_START { \ - (c)->waiters = 0; \ - (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \ - if ((c)->sem == NULL) \ - croak("panic: COND_INIT (%ld)",GetLastError()); \ + STMT_START { \ + (c)->waiters = 0; \ + (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \ + if ((c)->sem == NULL) \ + croak("panic: COND_INIT (%ld)",GetLastError()); \ } STMT_END #define COND_SIGNAL(c) \ - STMT_START { \ - if (ReleaseSemaphore((c)->sem,1,NULL) == 0) \ - croak("panic: COND_SIGNAL (%ld)",GetLastError()); \ + STMT_START { \ + if ((c)->waiters > 0 && \ + ReleaseSemaphore((c)->sem,1,NULL) == 0) \ + croak("panic: COND_SIGNAL (%ld)",GetLastError()); \ } STMT_END #define COND_BROADCAST(c) \ - STMT_START { \ - if ((c)->waiters > 0 && \ - ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \ - croak("panic: COND_BROADCAST (%ld)",GetLastError());\ + STMT_START { \ + if ((c)->waiters > 0 && \ + ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \ + croak("panic: COND_BROADCAST (%ld)",GetLastError());\ } STMT_END #define COND_WAIT(c, m) \ - STMT_START { \ - (c)->waiters++; \ - MUTEX_UNLOCK(m); \ - /* Note that there's no race here, since a \ - * COND_BROADCAST() on another thread will have seen the\ - * right number of waiters (i.e. including this one) */ \ - if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\ - croak("panic: COND_WAIT (%ld)",GetLastError()); \ - MUTEX_LOCK(m); \ - (c)->waiters--; \ + STMT_START { \ + (c)->waiters++; \ + MUTEX_UNLOCK(m); \ + /* Note that there's no race here, since a \ + * COND_BROADCAST() on another thread will have seen the\ + * right number of waiters (i.e. including this one) */ \ + if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\ + croak("panic: COND_WAIT (%ld)",GetLastError()); \ + /* XXX there may be an inconsequential race here */ \ + MUTEX_LOCK(m); \ + (c)->waiters--; \ } STMT_END #define COND_DESTROY(c) \ - STMT_START { \ - (c)->waiters = 0; \ - if (CloseHandle((c)->sem) == 0) \ - croak("panic: COND_DESTROY (%ld)",GetLastError()); \ + STMT_START { \ + (c)->waiters = 0; \ + if (CloseHandle((c)->sem) == 0) \ + croak("panic: COND_DESTROY (%ld)",GetLastError()); \ } STMT_END #define DETACH(t) \ @@ -94,7 +97,7 @@ typedef HANDLE perl_mutex; } \ } STMT_END -#define THR ((struct thread *) TlsGetValue(thr_key)) + #define THREAD_CREATE(t, f) Perl_thread_create(t, f) #define THREAD_POST_CREATE(t) NOOP #define THREAD_RET_TYPE DWORD WINAPI @@ -102,25 +105,39 @@ typedef HANDLE perl_mutex; typedef THREAD_RET_TYPE thread_func_t(void *); -#define HAVE_THREAD_INTERN + START_EXTERN_C -void Perl_init_thread_intern _((struct thread *thr)); -int Perl_thread_create _((struct thread *thr, thread_func_t *fn)); + +#if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL)) +extern __declspec(thread) struct perl_thread *Perl_current_thread; +#define SET_THR(t) (Perl_current_thread = t) +#define THR Perl_current_thread +#else +#define THR Perl_getTHR() +#define SET_THR(t) Perl_setTHR(t) +#endif +struct perl_thread; + +void Perl_alloc_thread_key _((void)); +int Perl_thread_create _((struct perl_thread *thr, thread_func_t *fn)); +void Perl_set_thread_self _((struct perl_thread *thr)); +struct perl_thread *Perl_getTHR _((void)); +void Perl_setTHR _((struct perl_thread *t)); + END_EXTERN_C +#define INIT_THREADS NOOP +#define ALLOC_THREAD_KEY Perl_alloc_thread_key() +#define SET_THREAD_SELF(thr) Perl_set_thread_self(thr) + #define JOIN(t, avp) \ STMT_START { \ if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \ - || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)) \ + || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)) \ croak("panic: JOIN"); \ } STMT_END -#define SET_THR(t) \ - STMT_START { \ - if (TlsSetValue(thr_key, (void *) (t)) == 0) \ - croak("panic: TlsSetValue"); \ - } STMT_END - #define YIELD Sleep(0) #endif /* _WIN32THREAD_H */ +