]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_nt.c
ITS#8605 - spelling fixes
[openldap] / libraries / libldap_r / thr_nt.c
1 /* thr_nt.c - wrapper around NT threads */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2017 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #if defined( HAVE_NT_THREADS )
20
21 #define _WIN32_WINNT 0x0400
22 #include <windows.h>
23 #include <process.h>
24
25 #include "ldap_pvt_thread.h" /* Get the thread interface */
26 #define LDAP_THREAD_IMPLEMENTATION
27 #include "ldap_thr_debug.h"      /* May rename the symbols defined below */
28
29 typedef struct ldap_int_thread_s {
30         long tid;
31         HANDLE thd;
32 } ldap_int_thread_s;
33
34 #ifndef NT_MAX_THREADS
35 #define NT_MAX_THREADS  1024
36 #endif
37
38 static ldap_int_thread_s tids[NT_MAX_THREADS];
39 static int ntids;
40
41
42 /* mingw compiler very sensitive about getting prototypes right */
43 typedef unsigned __stdcall thrfunc_t(void *);
44
45 int
46 ldap_int_thread_initialize( void )
47 {
48         return 0;
49 }
50
51 int
52 ldap_int_thread_destroy( void )
53 {
54         return 0;
55 }
56
57 int 
58 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
59         int detach,
60         void *(*start_routine)( void *),
61         void *arg)
62 {
63         unsigned tid;
64         HANDLE thd;
65         int rc = -1;
66
67         thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
68                                       arg, 0, &tid);
69
70         if ( thd ) {
71                 *thread = (ldap_pvt_thread_t) tid;
72                 tids[ntids].tid = tid;
73                 tids[ntids].thd = thd;
74                 ntids++;
75                 rc = 0;
76         }
77         return rc;
78 }
79         
80 void 
81 ldap_pvt_thread_exit( void *retval )
82 {
83         _endthread( );
84 }
85
86 int 
87 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
88 {
89         DWORD status;
90         int i;
91
92         for (i=0; i<ntids; i++) {
93                 if ( tids[i].tid == thread )
94                         break;
95         }
96         if ( i > ntids ) return -1;
97
98         status = WaitForSingleObject( tids[i].thd, INFINITE );
99         for (; i<ntids; i++) {
100                 tids[i] = tids[i+1];
101         }
102         ntids--;
103         return status == WAIT_FAILED ? -1 : 0;
104 }
105
106 int 
107 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
108 {
109         return 0;
110 }
111
112 int 
113 ldap_pvt_thread_yield( void )
114 {
115         Sleep( 0 );
116         return 0;
117 }
118
119 int 
120 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
121 {
122         *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
123         return( 0 );
124 }
125
126 int
127 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
128 {
129         CloseHandle( *cv );
130         return( 0 );
131 }
132
133 int 
134 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
135 {
136         SetEvent( *cond );
137         return( 0 );
138 }
139
140 int 
141 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
142         ldap_pvt_thread_mutex_t *mutex )
143 {
144         SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
145         WaitForSingleObject( *mutex, INFINITE );
146         return( 0 );
147 }
148
149 int
150 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
151 {
152         while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
153                 SetEvent( *cond );
154         return( 0 );
155 }
156
157 int 
158 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
159 {
160         *mutex = CreateMutex( NULL, 0, NULL );
161         return ( 0 );
162 }
163
164 int 
165 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
166 {
167         CloseHandle( *mutex );
168         return ( 0 );   
169 }
170
171 int 
172 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
173 {
174         DWORD status;
175         status = WaitForSingleObject( *mutex, INFINITE );
176         return status == WAIT_FAILED ? -1 : 0;
177 }
178
179 int 
180 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
181 {
182         ReleaseMutex( *mutex );
183         return ( 0 );
184 }
185
186 int
187 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
188 {
189         DWORD status;
190         status = WaitForSingleObject( *mp, 0 );
191         return status == WAIT_FAILED || status == WAIT_TIMEOUT
192                 ? -1 : 0;
193 }
194
195 int ldap_pvt_thread_mutex_recursive_init( ldap_pvt_thread_mutex_recursive_t *mutex )
196         LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_init")));
197 int ldap_pvt_thread_mutex_recursive_destroy( ldap_pvt_thread_mutex_recursive_t *mutex )
198         LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_destroy")));
199 int ldap_pvt_thread_mutex_recursive_lock( ldap_pvt_thread_mutex_recursive_t *mutex )
200         LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_lock")));
201 int ldap_pvt_thread_mutex_recursive_trylock( ldap_pvt_thread_mutex_recursive_t *mutex )
202         LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_trylock")));
203 int ldap_pvt_thread_mutex_recursive_unlock( ldap_pvt_thread_mutex_recursive_t *mutex )
204         LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_unlock")));
205
206 ldap_pvt_thread_t
207 ldap_pvt_thread_self( void )
208 {
209         return GetCurrentThreadId();
210 }
211
212 int
213 ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *keyp )
214 {
215         DWORD key = TlsAlloc();
216         if ( key != TLS_OUT_OF_INDEXES ) {
217                 *keyp = key;
218                 return 0;
219         } else {
220                 return -1;
221         }
222 }
223
224 int
225 ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key )
226 {
227         /* TlsFree returns 0 on failure */
228         return( TlsFree( key ) == 0 );
229 }
230
231 int
232 ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data )
233 {
234         return ( TlsSetValue( key, data ) == 0 );
235 }
236
237 int
238 ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data )
239 {
240         void *ptr = TlsGetValue( key );
241         *data = ptr;
242         return( ptr ? GetLastError() : 0 );
243 }
244
245 #endif