3 * Copyright 1998-2003 The OpenLDAP Foundation, Redwood City, California, USA
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
12 /* thr_nt.c - wrapper around NT threads */
16 #if defined( HAVE_NT_THREADS )
18 #include "ldap_pvt_thread.h"
20 /* mingw compiler very sensitive about getting prototypes right */
21 typedef unsigned __stdcall thrfunc_t(void *);
24 ldap_int_thread_initialize( void )
30 ldap_int_thread_destroy( void )
36 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
38 void *(*start_routine)( void *),
44 thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
47 *thread = (ldap_pvt_thread_t) thd;
49 return thd == NULL ? -1 : 0;
53 ldap_pvt_thread_exit( void *retval )
59 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
62 status = WaitForSingleObject( (HANDLE) thread, INFINITE );
63 return status == WAIT_FAILED ? -1 : 0;
67 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
73 ldap_pvt_thread_yield( void )
80 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
82 *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
87 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
94 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
101 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
102 ldap_pvt_thread_mutex_t *mutex )
104 SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
105 WaitForSingleObject( *mutex, INFINITE );
110 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
112 while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
118 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
120 *mutex = CreateMutex( NULL, 0, NULL );
125 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
127 CloseHandle( *mutex );
132 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
135 status = WaitForSingleObject( *mutex, INFINITE );
136 return status == WAIT_FAILED ? -1 : 0;
140 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
142 ReleaseMutex( *mutex );
147 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
150 status = WaitForSingleObject( *mp, 0 );
151 return status == WAIT_FAILED || status == WAIT_TIMEOUT
156 ldap_pvt_thread_self( void )
158 return GetCurrentThread();