1 /* thr_nt.c - wrapper around NT threads */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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 /* mingw compiler very sensitive about getting prototypes right */
24 typedef unsigned __stdcall thrfunc_t(void *);
27 ldap_int_thread_initialize( void )
33 ldap_int_thread_destroy( void )
39 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
41 void *(*start_routine)( void *),
47 thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
50 *thread = (ldap_pvt_thread_t) thd;
52 return thd == NULL ? -1 : 0;
56 ldap_pvt_thread_exit( void *retval )
62 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
65 status = WaitForSingleObject( (HANDLE) thread, INFINITE );
66 return status == WAIT_FAILED ? -1 : 0;
70 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
76 ldap_pvt_thread_yield( void )
83 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
85 *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
90 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
97 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
104 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
105 ldap_pvt_thread_mutex_t *mutex )
107 SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
108 WaitForSingleObject( *mutex, INFINITE );
113 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
115 while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
121 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
123 *mutex = CreateMutex( NULL, 0, NULL );
128 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
130 CloseHandle( *mutex );
135 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
138 status = WaitForSingleObject( *mutex, INFINITE );
139 return status == WAIT_FAILED ? -1 : 0;
143 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
145 ReleaseMutex( *mutex );
150 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
153 status = WaitForSingleObject( *mp, 0 );
154 return status == WAIT_FAILED || status == WAIT_TIMEOUT
159 ldap_pvt_thread_self( void )
161 return GetCurrentThread();