]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_nt.c
Sync with HEAD
[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-2003 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
47         thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
48                                       arg, 0, &tid);
49
50         *thread = (ldap_pvt_thread_t) thd;
51
52         return thd == NULL ? -1 : 0;
53 }
54         
55 void 
56 ldap_pvt_thread_exit( void *retval )
57 {
58         _endthread( );
59 }
60
61 int 
62 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
63 {
64         DWORD status;
65         status = WaitForSingleObject( (HANDLE) thread, INFINITE );
66         return status == WAIT_FAILED ? -1 : 0;
67 }
68
69 int 
70 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
71 {
72         return 0;
73 }
74
75 int 
76 ldap_pvt_thread_yield( void )
77 {
78         Sleep( 0 );
79         return 0;
80 }
81
82 int 
83 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
84 {
85         *cond = CreateEvent( NULL, FALSE, FALSE, NULL );
86         return( 0 );
87 }
88
89 int
90 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
91 {
92         CloseHandle( *cv );
93         return( 0 );
94 }
95
96 int 
97 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
98 {
99         SetEvent( *cond );
100         return( 0 );
101 }
102
103 int 
104 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
105         ldap_pvt_thread_mutex_t *mutex )
106 {
107         SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
108         WaitForSingleObject( *mutex, INFINITE );
109         return( 0 );
110 }
111
112 int
113 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
114 {
115         while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
116                 SetEvent( *cond );
117         return( 0 );
118 }
119
120 int 
121 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
122 {
123         *mutex = CreateMutex( NULL, 0, NULL );
124         return ( 0 );
125 }
126
127 int 
128 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
129 {
130         CloseHandle( *mutex );
131         return ( 0 );   
132 }
133
134 int 
135 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
136 {
137         DWORD status;
138         status = WaitForSingleObject( *mutex, INFINITE );
139         return status == WAIT_FAILED ? -1 : 0;
140 }
141
142 int 
143 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
144 {
145         ReleaseMutex( *mutex );
146         return ( 0 );
147 }
148
149 int
150 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
151 {
152         DWORD status;
153         status = WaitForSingleObject( *mp, 0 );
154         return status == WAIT_FAILED || status == WAIT_TIMEOUT
155                 ? -1 : 0;
156 }
157
158 ldap_pvt_thread_t
159 ldap_pvt_thread_self( void )
160 {
161         return GetCurrentThread();
162 }
163
164 #endif