1 /* thr_nt.c - wrapper around NT threads */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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 *),
48 thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
52 *thread = (ldap_pvt_thread_t) tid;
60 ldap_pvt_thread_exit( void *retval )
66 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
70 HANDLE __stdcall OpenThread( int, int, int );
72 thd = OpenThread( SYNCHRONIZE, 0, thread );
73 status = WaitForSingleObject( thd, INFINITE );
75 return status == WAIT_FAILED ? -1 : 0;
79 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
85 ldap_pvt_thread_yield( void )
92 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
94 *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
99 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
106 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
113 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
114 ldap_pvt_thread_mutex_t *mutex )
116 SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
117 WaitForSingleObject( *mutex, INFINITE );
122 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
124 while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
130 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
132 *mutex = CreateMutex( NULL, 0, NULL );
137 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
139 CloseHandle( *mutex );
144 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
147 status = WaitForSingleObject( *mutex, INFINITE );
148 return status == WAIT_FAILED ? -1 : 0;
152 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
154 ReleaseMutex( *mutex );
159 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
162 status = WaitForSingleObject( *mp, 0 );
163 return status == WAIT_FAILED || status == WAIT_TIMEOUT
168 ldap_pvt_thread_self( void )
170 return GetCurrentThreadId();