]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_nt.c
6ebb0ccce8b42a2fbbdd3ad41dd900aea854c50b
[openldap] / libraries / libldap_r / thr_nt.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, Redwood City, California, USA
4  * All rights reserved.
5  *
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.
10  */
11
12 /* thr_nt.c - wrapper around NT threads */
13
14 #include "portable.h"
15
16 #if defined( HAVE_NT_THREADS )
17
18 #include "ldap_pvt_thread.h"
19
20 /* mingw compiler very sensitive about getting prototypes right */
21 typedef unsigned __stdcall thrfunc_t(void *);
22
23 int
24 ldap_int_thread_initialize( void )
25 {
26         return 0;
27 }
28
29 int
30 ldap_int_thread_destroy( void )
31 {
32         return 0;
33 }
34
35 int 
36 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
37         int detach,
38         void *(*start_routine)( void *),
39         void *arg)
40 {
41         unsigned tid;
42         HANDLE thd;
43
44         thd = (HANDLE) _beginthreadex(NULL, 0, (thrfunc_t *) start_routine,
45                                       arg, 0, &tid);
46
47         *thread = (ldap_pvt_thread_t) thd;
48
49         return thd == NULL ? -1 : 0;
50 }
51         
52 void 
53 ldap_pvt_thread_exit( void *retval )
54 {
55         _endthread( );
56 }
57
58 int 
59 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
60 {
61         DWORD status;
62         status = WaitForSingleObject( (HANDLE) thread, INFINITE );
63         return status == WAIT_FAILED ? -1 : 0;
64 }
65
66 int 
67 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
68 {
69         return 0;
70 }
71
72 int 
73 ldap_pvt_thread_yield( void )
74 {
75         Sleep( 0 );
76         return 0;
77 }
78
79 int 
80 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
81 {
82         *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
83         return( 0 );
84 }
85
86 int
87 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
88 {
89         CloseHandle( *cv );
90         return( 0 );
91 }
92
93 int 
94 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
95 {
96         SetEvent( *cond );
97         return( 0 );
98 }
99
100 int 
101 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
102         ldap_pvt_thread_mutex_t *mutex )
103 {
104         SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
105         WaitForSingleObject( *mutex, INFINITE );
106         return( 0 );
107 }
108
109 int
110 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
111 {
112         SetEvent( *cond );
113         return( 0 );
114 }
115
116 int 
117 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
118 {
119         *mutex = CreateMutex( NULL, 0, NULL );
120         return ( 0 );
121 }
122
123 int 
124 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
125 {
126         CloseHandle( *mutex );
127         return ( 0 );   
128 }
129
130 int 
131 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
132 {
133         DWORD status;
134         status = WaitForSingleObject( *mutex, INFINITE );
135         return status == WAIT_FAILED ? -1 : 0;
136 }
137
138 int 
139 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
140 {
141         ReleaseMutex( *mutex );
142         return ( 0 );
143 }
144
145 int
146 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
147 {
148         DWORD status;
149         status = WaitForSingleObject( *mp, 0 );
150         return status == WAIT_FAILED || status == WAIT_TIMEOUT
151                 ? -1 : 0;
152 }
153
154 #endif