File: pthread.h

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (266 lines) | stat: -rw-r--r-- 6,392 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (c) 2017 Intel Corporation. All rights reserved.
* Copyright (c) 2020 Amazon.com, Inc. or its affiliates. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses.  You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* BSD license below:
*
*     Redistribution and use in source and binary forms, with or
*     without modification, are permitted provided that the following
*     conditions are met:
*
*      - Redistributions of source code must retain the above
*        copyright notice, this list of conditions and the following
*        disclaimer.
*
*      - Redistributions in binary form must reproduce the above
*        copyright notice, this list of conditions and the following
*        disclaimer in the documentation and/or other materials
*        provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#pragma once

#include <WinSock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

#define PTHREAD_MUTEX_INITIALIZER {0}
#define PTHREAD_RWLOCK_INITIALIZER {0}

#define pthread_cond_signal WakeConditionVariable
#define pthread_cond_broadcast WakeAllConditionVariable
#define pthread_mutex_init(mutex, attr) (InitializeCriticalSection(mutex), 0)
#define pthread_mutex_destroy(mutex) (DeleteCriticalSection(mutex), 0)
#define pthread_cond_init(cond, attr) (InitializeConditionVariable(cond), 0)
#define pthread_cond_destroy(x)	/* nothing to do */

typedef CRITICAL_SECTION	pthread_mutex_t;
typedef CONDITION_VARIABLE	pthread_cond_t;
typedef HANDLE			pthread_t;

static inline int pthread_mutex_lock(pthread_mutex_t* mutex)
{
	EnterCriticalSection(mutex);
	return 0;
}

static inline int pthread_mutex_trylock(pthread_mutex_t* mutex)
{
	return !TryEnterCriticalSection(mutex);
}

static inline int pthread_mutex_unlock(pthread_mutex_t* mutex)
{
	LeaveCriticalSection(mutex);
	return 0;
}

static inline int pthread_join(pthread_t thread, void** exit_code)
{
	if (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0) {
		if (exit_code) {
			DWORD ex = 0;
			GetExitCodeThread(thread, &ex);
			*exit_code = (void*)(uint64_t)ex;
		}
		CloseHandle(thread);
		return 0;
	}

	return -1;
}

typedef struct fi_thread_arg
{
	void* (*routine)(void*);
	void* arg;
} fi_thread_arg;

static DWORD WINAPI ofi_thread_starter(void* arg)
{
	fi_thread_arg data = *(fi_thread_arg*)arg;
	free(arg);
	return (DWORD)(uint64_t)data.routine(data.arg);
}

static inline int pthread_create(pthread_t* thread, void* attr, void *(*routine)(void*), void* arg)
{
	(void) attr;
	fi_thread_arg* data = (fi_thread_arg*)malloc(sizeof(*data));
	data->routine = routine;
	data->arg = arg;
	DWORD threadid;
	*thread = CreateThread(0, 0, ofi_thread_starter, data, 0, &threadid);
	return *thread == 0;
}

static inline int pthread_equal(pthread_t t1, pthread_t t2)
{
	(void)t1;
	(void)t2;
	/*
	 * TODO: temporary solution
	 * Need to implement
	 */
	return ENOSYS;
}

static inline int pthread_cancel(pthread_t thread)
{
	(void)thread;
	/*
	 * TODO: temporary solution
	 * Need to implement
	 */
	return ENOSYS;
}

static inline pthread_t pthread_self(void)
{
	/*
	 * TODO: temporary solution
	 * Need to implement
	 */
	return (pthread_t) ENOSYS;
}

static inline int pthread_yield(void)
{
	(void) SwitchToThread();
	return 0;
}

/*
 * TODO: temporary solution
 * Need to re-implement
 */
typedef void(*pthread_cleanup_callback_t)(void *);
typedef struct pthread_cleanup_t
{
	pthread_cleanup_callback_t routine;
	void *arg;
} pthread_cleanup_t;

/* Read-Write lock implementation */

typedef struct {
    SRWLOCK	lock; /* Windows Slim Reader Writer Lock */
    bool	write_mode;
} pthread_rwlock_t;
typedef void pthread_rwlockattr_t;

static inline int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
{
	(void)attr;
	if (rwlock) {
		InitializeSRWLock(&(rwlock->lock));
		rwlock->write_mode = false;
		return 0;
	}
	return 1;
}

static inline int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
{
	/* No SRWLock cleanup function */
	(void)rwlock;
	return 0;
}

static inline int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
{
	if (rwlock) {
		AcquireSRWLockShared(&(rwlock->lock));
		return 0;
	}
	return 1;
}

static inline int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
{
	if (rwlock && TryAcquireSRWLockShared(&(rwlock->lock))) {
		return 0;
	}
	return 1;
}

static inline int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
{
	if (rwlock) {
		AcquireSRWLockExclusive(&(rwlock->lock));
		rwlock->write_mode = true;
		return 0;
	}
	return 1;
}

static inline int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
{
	if (rwlock && TryAcquireSRWLockExclusive(&(rwlock->lock))) {
		rwlock->write_mode = true;
		return 0;
	}
	return 1;
}


static inline int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{
	if (rwlock) {
		if (rwlock->write_mode) {
			rwlock->write_mode = false;
			ReleaseSRWLockExclusive(&(rwlock->lock));
		} else {
			ReleaseSRWLockShared(&(rwlock->lock));
		}
		return 0;
	}
	return 1;
}

#ifndef __cplusplus 
#define pthread_cleanup_push(_rout, _arg)				\
{									\
	pthread_cleanup_t _cleanup = {					\
		.routine = (pthread_cleanup_callback_t) (_rout),	\
		.arg = (_arg),						\
	};								\

#define pthread_cleanup_pop(_execute)					\
	if (_execute)							\
		(void) (*(_cleanup.routine))(_cleanup.arg);		\
}
#else
#define pthread_cleanup_push(_rout, _arg)				\
{									\
	pthread_cleanup_t _cleanup;					\
	_cleanup.routine = (pthread_cleanup_callback_t) _rout,		\
	_cleanup.arg = (_arg);						\
	__try								\
	{								\

#define pthread_cleanup_pop(_execute)					\
	}								\
	__finally {							\
		if( _execute || AbnormalTermination())			\
			(*(_cleanup.routine))(_cleanup.arg);		\
	}								\
}
#endif