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