thr_nt.c 4.54 KB
Newer Older
1
/* thr_nt.c - wrapper around NT threads */
2
/* $OpenLDAP$ */
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 1998-2020 The OpenLDAP Foundation.
Bart Hartgers's avatar
   
Bart Hartgers committed
6
7
 * All rights reserved.
 *
8
9
10
11
12
13
14
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
Bart Hartgers's avatar
   
Bart Hartgers committed
15
16
 */

Bart Hartgers's avatar
 
Bart Hartgers committed
17
18
19
20
#include "portable.h"

#if defined( HAVE_NT_THREADS )

Howard Chu's avatar
Howard Chu committed
21
22
23
24
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <process.h>

25
26
27
#include "ldap_pvt_thread.h" /* Get the thread interface */
#define LDAP_THREAD_IMPLEMENTATION
#include "ldap_thr_debug.h"	 /* May rename the symbols defined below */
28

29
30
31
32
33
34
35
36
37
typedef struct ldap_int_thread_s {
	long tid;
	HANDLE thd;
} ldap_int_thread_s;

#ifndef NT_MAX_THREADS
#define NT_MAX_THREADS	1024
#endif

Howard Chu's avatar
Howard Chu committed
38
static ldap_int_thread_s tids[NT_MAX_THREADS];
39
40
41
static int ntids;


Howard Chu's avatar
Howard Chu committed
42
43
44
/* mingw compiler very sensitive about getting prototypes right */
typedef unsigned __stdcall thrfunc_t(void *);

45
int
Mark Valence's avatar
Mark Valence committed
46
ldap_int_thread_initialize( void )
47
48
{
	return 0;
49
50
51
}

int
Mark Valence's avatar
Mark Valence committed
52
ldap_int_thread_destroy( void )
53
54
{
	return 0;
55
56
}

Bart Hartgers's avatar
 
Bart Hartgers committed
57
int 
58
ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
59
60
61
	int detach,
	void *(*start_routine)( void *),
	void *arg)
Bart Hartgers's avatar
 
Bart Hartgers committed
62
{
Howard Chu's avatar
Howard Chu committed
63
	unsigned tid;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
	HANDLE thd;
65
	int rc = -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
66

67
	thd = (HANDLE) _beginthreadex(NULL, LDAP_PVT_THREAD_STACK_SIZE, (thrfunc_t *) start_routine,
Howard Chu's avatar
Howard Chu committed
68
				      arg, 0, &tid);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
69

70
71
	if ( thd ) {
		*thread = (ldap_pvt_thread_t) tid;
Howard Chu's avatar
Howard Chu committed
72
73
		tids[ntids].tid = tid;
		tids[ntids].thd = thd;
74
		ntids++;
75
76
77
		rc = 0;
	}
	return rc;
Bart Hartgers's avatar
 
Bart Hartgers committed
78
79
80
}
	
void 
81
ldap_pvt_thread_exit( void *retval )
Bart Hartgers's avatar
 
Bart Hartgers committed
82
83
84
85
86
{
	_endthread( );
}

int 
87
ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
Bart Hartgers's avatar
 
Bart Hartgers committed
88
89
{
	DWORD status;
90
91
92
	int i;

	for (i=0; i<ntids; i++) {
Howard Chu's avatar
Howard Chu committed
93
		if ( tids[i].tid == thread )
94
95
96
			break;
	}
	if ( i > ntids ) return -1;
97

Howard Chu's avatar
Howard Chu committed
98
	status = WaitForSingleObject( tids[i].thd, INFINITE );
99
	for (; i<ntids; i++) {
Howard Chu's avatar
Howard Chu committed
100
		tids[i] = tids[i+1];
101
102
	}
	ntids--;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
103
	return status == WAIT_FAILED ? -1 : 0;
Bart Hartgers's avatar
 
Bart Hartgers committed
104
105
106
}

int 
107
ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
Bart Hartgers's avatar
 
Bart Hartgers committed
108
109
110
111
112
{
	return 0;
}

int 
113
ldap_pvt_thread_yield( void )
Bart Hartgers's avatar
 
Bart Hartgers committed
114
{
115
	Sleep( 0 );
Bart Hartgers's avatar
 
Bart Hartgers committed
116
117
118
119
	return 0;
}

int 
120
ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
Bart Hartgers's avatar
 
Bart Hartgers committed
121
122
123
124
125
126
{
	*cond = CreateEvent( NULL, FALSE, FALSE, NULL );
	return( 0 );
}

int
127
ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
Bart Hartgers's avatar
 
Bart Hartgers committed
128
129
130
131
132
133
{
	CloseHandle( *cv );
	return( 0 );
}

int 
134
ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
Bart Hartgers's avatar
 
Bart Hartgers committed
135
136
137
138
139
140
{
	SetEvent( *cond );
	return( 0 );
}

int 
141
142
ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
	ldap_pvt_thread_mutex_t *mutex )
Bart Hartgers's avatar
 
Bart Hartgers committed
143
{
144
	SignalObjectAndWait( *mutex, *cond, INFINITE, FALSE );
Bart Hartgers's avatar
 
Bart Hartgers committed
145
146
147
148
149
	WaitForSingleObject( *mutex, INFINITE );
	return( 0 );
}

int
150
ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
Bart Hartgers's avatar
 
Bart Hartgers committed
151
{
152
153
	while ( WaitForSingleObject( *cond, 0 ) == WAIT_TIMEOUT )
		SetEvent( *cond );
Bart Hartgers's avatar
 
Bart Hartgers committed
154
155
156
157
	return( 0 );
}

int 
158
ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
Bart Hartgers's avatar
 
Bart Hartgers committed
159
160
161
162
163
{
	*mutex = CreateMutex( NULL, 0, NULL );
	return ( 0 );
}

164
165
166
167
168
169
170
171
172
173
174
int
ldap_pvt_thread_mutex_init_first( ldap_pvt_thread_mutex_t *mutex )
{
	if ( *mutex == NULL ) {
		HANDLE p = CreateMutex( NULL, 0, NULL );
		if ( InterlockedCompareExchangePointer((PVOID*)mutex, (PVOID)p, NULL) != NULL)
			CloseHandle( p );
	}
	return ( 0 );
}

Bart Hartgers's avatar
 
Bart Hartgers committed
175
int 
176
ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
Bart Hartgers's avatar
 
Bart Hartgers committed
177
178
179
180
181
182
{
	CloseHandle( *mutex );
	return ( 0 );	
}

int 
183
ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
Bart Hartgers's avatar
 
Bart Hartgers committed
184
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
185
186
187
	DWORD status;
	status = WaitForSingleObject( *mutex, INFINITE );
	return status == WAIT_FAILED ? -1 : 0;
Bart Hartgers's avatar
 
Bart Hartgers committed
188
189
190
}

int 
191
ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
Bart Hartgers's avatar
 
Bart Hartgers committed
192
193
194
195
196
197
{
	ReleaseMutex( *mutex );
	return ( 0 );
}

int
198
ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
Bart Hartgers's avatar
 
Bart Hartgers committed
199
200
201
{
	DWORD status;
	status = WaitForSingleObject( *mp, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
202
203
	return status == WAIT_FAILED || status == WAIT_TIMEOUT
		? -1 : 0;
Bart Hartgers's avatar
 
Bart Hartgers committed
204
205
}

206
207
208
ldap_pvt_thread_t
ldap_pvt_thread_self( void )
{
209
	return GetCurrentThreadId();
210
211
}

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
int
ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *keyp )
{
	DWORD key = TlsAlloc();
	if ( key != TLS_OUT_OF_INDEXES ) {
		*keyp = key;
		return 0;
	} else {
		return -1;
	}
}

int
ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key )
{
	/* TlsFree returns 0 on failure */
	return( TlsFree( key ) == 0 );
}

int
ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data )
{
	return ( TlsSetValue( key, data ) == 0 );
}

int
ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data )
{
	void *ptr = TlsGetValue( key );
	*data = ptr;
	return( ptr ? GetLastError() : 0 );
}

Bart Hartgers's avatar
 
Bart Hartgers committed
245
#endif