]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_nt.c
Fix prev commit
[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-2004 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 #include "ldap_pvt_thread.h"
22
23 typedef struct ldap_int_thread_s {
24         long tid;
25         HANDLE thd;
26 } ldap_int_thread_s;
27
28 #ifndef NT_MAX_THREADS
29 #define NT_MAX_THREADS  1024
30 #endif
31
32 static ldap_int_thread_s tids[NT_MAX_THREADS];
33 static int ntids;
34
35
36 /* mingw compiler very sensitive about getting prototypes right */
37 typedef unsigned __stdcall thrfunc_t(void *);
38
39 int
40 ldap_int_thread_initialize( void )
41 {
42         return 0;
43 }
44
45 int
46 ldap_int_thread_destroy( void )
47 {
48         return 0;
49 }
50
51 int 
52 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
53         int detach,
54         void *(*start_routine)( void *),
55         void *arg)
56 {
57         unsigned tid;
58         HANDLE thd;
59         int rc = -1;
60
61         thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
62                                       arg, 0, &tid);
63
64         if ( thd ) {
65                 *thread = (ldap_pvt_thread_t) tid;
66                 tids[ntids].tid = tid;
67                 tids[ntids].thd = thd;
68                 ntids++;
69                 rc = 0;
70         }
71         return rc;
72 }
73         
74 void 
75 ldap_pvt_thread_exit( void *retval )
76 {
77         _endthread( );
78 }
79
80 int 
81 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
82 {
83         DWORD status;
84         int i;
85
86         for (i=0; i<ntids; i++) {
87                 if ( tids[i].tid == thread )
88                         break;
89         }
90         if ( i > ntids ) return -1;
91
92         status = WaitForSingleObject( tids[i].thd, INFINITE );
93         for (; i<ntids; i++) {
94                 tids[i] = tids[i+1];
95         }
96         ntids--;
97         return status == WAIT_FAILED ? -1 : 0;
98 }
99
100 int 
101 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
102 {
103         return 0;
104 }
105
106 int 
107 ldap_pvt_thread_yield( void )
108 {
109         Sleep( 0 );
110         return 0;
111 }
112
113 int 
114 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
115 {
116         *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
117         return( 0 );
118 }
119
120 int
121 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
122 {
123         CloseHandle( *cv );
124         return( 0 );
125 }
126
127 int 
128 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
129 {
130         SetEvent( *cond );
131         return( 0 );
132 }
133
134 int 
135 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
136         ldap_pvt_thread_mutex_t *mutex )
137 {
138         SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
139         WaitForSingleObject( *mutex, INFINITE );
140         return( 0 );
141 }
142
143 int
144 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
145 {
146         while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
147                 SetEvent( *cond );
148         return( 0 );
149 }
150
151 int 
152 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
153 {
154         *mutex = CreateMutex( NULL, 0, NULL );
155         return ( 0 );
156 }
157
158 int 
159 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
160 {
161         CloseHandle( *mutex );
162         return ( 0 );   
163 }
164
165 int 
166 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
167 {
168         DWORD status;
169         status = WaitForSingleObject( *mutex, INFINITE );
170         return status == WAIT_FAILED ? -1 : 0;
171 }
172
173 int 
174 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
175 {
176         ReleaseMutex( *mutex );
177         return ( 0 );
178 }
179
180 int
181 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
182 {
183         DWORD status;
184         status = WaitForSingleObject( *mp, 0 );
185         return status == WAIT_FAILED || status == WAIT_TIMEOUT
186                 ? -1 : 0;
187 }
188
189 ldap_pvt_thread_t
190 ldap_pvt_thread_self( void )
191 {
192         return GetCurrentThreadId();
193 }
194
195 #endif