1 /* thr_nt.c - wrapper around NT threads */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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" /* Get the thread interface */
22 #define LDAP_THREAD_IMPLEMENTATION
23 #include "ldap_thr_debug.h" /* May rename the symbols defined below */
25 typedef struct ldap_int_thread_s {
30 #ifndef NT_MAX_THREADS
31 #define NT_MAX_THREADS 1024
34 static ldap_int_thread_s tids[NT_MAX_THREADS];
38 /* mingw compiler very sensitive about getting prototypes right */
39 typedef unsigned __stdcall thrfunc_t(void *);
42 ldap_int_thread_initialize( void )
48 ldap_int_thread_destroy( void )
54 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
56 void *(*start_routine)( void *),
63 thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
67 *thread = (ldap_pvt_thread_t) tid;
68 tids[ntids].tid = tid;
69 tids[ntids].thd = thd;
77 ldap_pvt_thread_exit( void *retval )
83 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
88 for (i=0; i<ntids; i++) {
89 if ( tids[i].tid == thread )
92 if ( i > ntids ) return -1;
94 status = WaitForSingleObject( tids[i].thd, INFINITE );
95 for (; i<ntids; i++) {
99 return status == WAIT_FAILED ? -1 : 0;
103 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
109 ldap_pvt_thread_yield( void )
116 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
118 *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
123 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
130 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
137 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
138 ldap_pvt_thread_mutex_t *mutex )
140 SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
141 WaitForSingleObject( *mutex, INFINITE );
146 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
148 while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
154 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
156 *mutex = CreateMutex( NULL, 0, NULL );
161 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
163 CloseHandle( *mutex );
168 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
171 status = WaitForSingleObject( *mutex, INFINITE );
172 return status == WAIT_FAILED ? -1 : 0;
176 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
178 ReleaseMutex( *mutex );
183 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
186 status = WaitForSingleObject( *mp, 0 );
187 return status == WAIT_FAILED || status == WAIT_TIMEOUT
192 ldap_pvt_thread_self( void )
194 return GetCurrentThreadId();