1 /* thr_nt.c - wrapper around NT threads */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
19 #if defined( HAVE_NT_THREADS )
21 #include "ldap_pvt_thread.h"
23 typedef struct ldap_int_thread_s {
28 #ifndef NT_MAX_THREADS
29 #define NT_MAX_THREADS 1024
32 static ldap_int_thread_s tids[NT_MAX_THREADS];
36 /* mingw compiler very sensitive about getting prototypes right */
37 typedef unsigned __stdcall thrfunc_t(void *);
40 ldap_int_thread_initialize( void )
46 ldap_int_thread_destroy( void )
52 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
54 void *(*start_routine)( void *),
61 thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
65 *thread = (ldap_pvt_thread_t) tid;
66 tids[ntids].tid = tid;
67 tids[ntids].thd = thd;
75 ldap_pvt_thread_exit( void *retval )
81 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
86 for (i=0; i<ntids; i++) {
87 if ( tids[i].tid == thread )
90 if ( i > ntids ) return -1;
92 status = WaitForSingleObject( tids[i].thd, INFINITE );
93 for (; i<ntids; i++) {
97 return status == WAIT_FAILED ? -1 : 0;
101 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
107 ldap_pvt_thread_yield( void )
114 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
116 *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
121 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
128 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
135 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
136 ldap_pvt_thread_mutex_t *mutex )
138 SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
139 WaitForSingleObject( *mutex, INFINITE );
144 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
146 while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
152 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
154 *mutex = CreateMutex( NULL, 0, NULL );
159 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
161 CloseHandle( *mutex );
166 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
169 status = WaitForSingleObject( *mutex, INFINITE );
170 return status == WAIT_FAILED ? -1 : 0;
174 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
176 ReleaseMutex( *mutex );
181 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
184 status = WaitForSingleObject( *mp, 0 );
185 return status == WAIT_FAILED || status == WAIT_TIMEOUT
190 ldap_pvt_thread_self( void )
192 return GetCurrentThreadId();