-
Notifications
You must be signed in to change notification settings - Fork 33
/
smp.c
424 lines (364 loc) · 10.4 KB
/
smp.c
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <stdio.h>
#include <stdlib.h>
#include <postgres.h>
#include <utils/elog.h>
#include "smp.h"
#define IMCS_SMP_CHECK(call) if ((call) != 0) imcs_error_handler(__FILE__, __LINE__, #call)
#ifdef _WIN32
#include <windows.h>
#define IMCS_MAX_SEM_VALUE 1000000
#ifndef TLS_OUT_OF_INDEXES
#define TLS_OUT_OF_INDEXES 0xffffffff
#endif
static void imcs_error_handler(char const* file, int line, char const* msg)
{
elog(ERROR, "%s:%d %s error=%d", file, line, msg, GetLastError());
}
typedef struct imcs_win_tls_t
{
imcs_tls_t vtab;
unsigned int key;
} imcs_win_tls_t;
static void* imcs_get_tls(imcs_tls_t* t)
{
imcs_win_tls_t* tls = (imcs_win_tls_t*)t;
return TlsGetValue(tls->key);
}
static void imcs_set_tls(imcs_tls_t* t, void* value)
{
imcs_win_tls_t* tls = (imcs_win_tls_t*)t;
TlsSetValue(tls->key, value);
}
static void imcs_destroy_tls(imcs_tls_t* t)
{
imcs_win_tls_t* tls = (imcs_win_tls_t*)t;
TlsFree(tls->key);
free(tls);
}
imcs_tls_t* imcs_create_tls(void)
{
imcs_win_tls_t* tls = (imcs_win_tls_t*)malloc(sizeof(imcs_win_tls_t));
tls->vtab.get = imcs_get_tls;
tls->vtab.set = imcs_set_tls;
tls->vtab.destroy = imcs_destroy_tls;
tls->key = TlsAlloc();
if (tls->key == TLS_OUT_OF_INDEXES) {
free(tls);
return NULL;
}
return &tls->vtab;
}
typedef struct imcs_win_thread_t {
imcs_thread_t vtab;
HANDLE handle;
} imcs_win_thread_t;
static void imcs_join_thread(imcs_thread_t* t)
{
imcs_win_thread_t* thread = (imcs_win_thread_t*)t;
IMCS_SMP_CHECK(WaitForSingleObject(thread->handle, INFINITE) == WAIT_OBJECT_0);
IMCS_SMP_CHECK(CloseHandle(thread->handle));
free(thread);
}
imcs_thread_t* imcs_create_thread(imcs_thread_proc_t proc, void* arg)
{
DWORD threadid;
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) proc, arg, 0, &threadid);
if (h == NULL) {
return NULL;
} else {
imcs_win_thread_t* thread = (imcs_win_thread_t*)malloc(sizeof(imcs_win_thread_t));
thread->vtab.join = imcs_join_thread;
thread->handle = h;
return &thread->vtab;
}
}
int imcs_get_number_of_cpus(void)
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}
typedef struct
{
imcs_mutex_t vtab;
CRITICAL_SECTION cs;
} imcs_win_mutex_t;
static void imcs_lock_mutex(imcs_mutex_t* m)
{
imcs_win_mutex_t* mutex = (imcs_win_mutex_t*)m;
EnterCriticalSection(&mutex->cs);
}
static void imcs_unlock_mutex(imcs_mutex_t* m)
{
imcs_win_mutex_t* mutex = (imcs_win_mutex_t*)m;
LeaveCriticalSection(&mutex->cs);
}
static void imcs_destroy_mutex(imcs_mutex_t* m)
{
imcs_win_mutex_t* mutex = (imcs_win_mutex_t*)m;
DeleteCriticalSection(&mutex->cs);
free(mutex);
}
imcs_mutex_t* imcs_create_mutex()
{
imcs_win_mutex_t* mutex = (imcs_win_mutex_t*)malloc(sizeof(imcs_win_mutex_t));
InitializeCriticalSection(&mutex->cs);
mutex->vtab.lock = imcs_lock_mutex;
mutex->vtab.unlock = imcs_unlock_mutex;
mutex->vtab.destroy = imcs_destroy_mutex;
return &mutex->vtab;
}
typedef struct
{
imcs_semaphore_t vtab;
HANDLE handle;
} imcs_win_semaphore_t;
static imcs_bool imcs_semaphore_wait(imcs_semaphore_t* s, imcs_mutex_t* mutex, int n, unsigned timeout)
{
imcs_win_semaphore_t* sem = (imcs_win_semaphore_t*)s;
int rc;
mutex->unlock(mutex);
while (--n >= 0) {
rc = WaitForSingleObject(sem->handle, timeout);
if (rc == WAIT_TIMEOUT) {
mutex->lock(mutex);
return 0;
}
IMCS_SMP_CHECK(rc == WAIT_OBJECT_0);
}
mutex->lock(mutex);
return 1;
}
static void imcs_semaphore_signal(imcs_semaphore_t* s, int inc)
{
imcs_win_semaphore_t* sem = (imcs_win_semaphore_t*)s;
IMCS_SMP_CHECK(ReleaseSemaphore(sem->handle, inc, NULL));
}
static void imcs_semaphore_destroy(imcs_semaphore_t* s)
{
imcs_win_semaphore_t* sem = (imcs_win_semaphore_t*)s;
CloseHandle(sem->handle);
free(sem);
}
imcs_semaphore_t* imcs_create_semaphore(int value)
{
imcs_win_semaphore_t* sem;
HANDLE s = CreateSemaphore(NULL, value, IMCS_MAX_SEM_VALUE, NULL);
IMCS_SMP_CHECK(s != NULL);
sem = (imcs_win_semaphore_t*)malloc(sizeof (imcs_win_semaphore_t));
sem->vtab.wait = imcs_semaphore_wait;
sem->vtab.signal = imcs_semaphore_signal;
sem->vtab.destroy = imcs_semaphore_destroy;
sem->handle = s;
return &sem->vtab;
}
imcs_process_t imcs_get_pid(void)
{
return (imcs_process_t)(imcs_size_t)GetCurrentProcessId();
}
imcs_bool imcs_is_process_alive(imcs_process_t proc)
{
HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)(imcs_size_t)proc);
if (h == NULL) {
return 0;
}
CloseHandle(h);
return 1;
}
#else
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <pthread.h>
static void imcs_error_handler(char const* file, int line, char const* msg)
{
elog(ERROR, "%s:%d %s (%m)", file, line, msg);
}
/* Posix threads */
typedef struct imcs_posix_tls_t
{
imcs_tls_t vtab;
pthread_key_t key;
} imcs_posix_tls_t;
static void* imcs_get_tls(imcs_tls_t* t)
{
imcs_posix_tls_t* tls = (imcs_posix_tls_t*)t;
return pthread_getspecific(tls->key);
}
static void imcs_set_tls(imcs_tls_t* t, void* value)
{
imcs_posix_tls_t* tls = (imcs_posix_tls_t*)t;
IMCS_SMP_CHECK(pthread_setspecific(tls->key, value));
}
static void imcs_destroy_tls(imcs_tls_t* t)
{
imcs_posix_tls_t* tls = (imcs_posix_tls_t*)t;
pthread_key_delete(tls->key);
free(tls);
}
imcs_tls_t* imcs_create_tls(void)
{
imcs_posix_tls_t* tls = (imcs_posix_tls_t*)malloc(sizeof(imcs_posix_tls_t));
tls->vtab.get = imcs_get_tls;
tls->vtab.set = imcs_set_tls;
tls->vtab.destroy = imcs_destroy_tls;
IMCS_SMP_CHECK(pthread_key_create(&tls->key, NULL));
return &tls->vtab;
}
typedef struct imcs_posix_thread_t {
imcs_thread_t vtab;
pthread_t thread;
} imcs_posix_thread_t;
static void imcs_join_thread(imcs_thread_t* t)
{
IMCS_SMP_CHECK(pthread_join(((imcs_posix_thread_t*)t)->thread, NULL));
free(t);
}
imcs_thread_t* imcs_create_thread(imcs_thread_proc_t proc, void* arg)
{
imcs_posix_thread_t* t = (imcs_posix_thread_t*)malloc(sizeof(imcs_posix_thread_t));
IMCS_SMP_CHECK(pthread_create(&t->thread, NULL, (void*(*)(void*))proc, arg));
t->vtab.join = imcs_join_thread;
return &t->vtab;
}
#if defined(_SC_NPROCESSORS_ONLN)
int imcs_get_number_of_cpus(void)
{
return sysconf(_SC_NPROCESSORS_ONLN);
}
#elif defined(__linux__)
#include <linux/smp.h>
int imcs_get_number_of_cpus(void)
{
return smp_num_cpus;
}
#elif defined(__FreeBSD__) || defined(__bsdi__) || defined(__OpenBSD__) || defined(__NetBSD__)
#if defined(__bsdi__) || defined(__OpenBSD__)
#include <sys/param.h>
#endif
#include <sys/sysctl.h>
int imcs_get_number_of_cpus(void)
{
int mib[2],ncpus=0;
size_t len=sizeof(ncpus);
mib[0]= CTL_HW;
mib[1]= HW_NCPU;
sysctl(mib,2,&ncpus,&len,NULL,0);
return ncpus;
}
#else
int imcs_get_number_of_cpus(void)
{
return 1;
}
#endif
typedef struct
{
imcs_mutex_t vtab;
pthread_mutex_t cs;
} imcs_posix_mutex_t;
static void imcs_lock_mutex(imcs_mutex_t* m)
{
imcs_posix_mutex_t* mutex = (imcs_posix_mutex_t*)m;
IMCS_SMP_CHECK(pthread_mutex_lock(&mutex->cs));
}
static void imcs_unlock_mutex(imcs_mutex_t* m)
{
imcs_posix_mutex_t* mutex = (imcs_posix_mutex_t*)m;
IMCS_SMP_CHECK(pthread_mutex_unlock(&mutex->cs));
}
static void imcs_destroy_mutex(imcs_mutex_t* m)
{
imcs_posix_mutex_t* mutex = (imcs_posix_mutex_t*)m;
pthread_mutex_destroy(&mutex->cs);
free(mutex);
}
imcs_mutex_t* imcs_create_mutex(void)
{
imcs_posix_mutex_t* mutex = (imcs_posix_mutex_t*)malloc(sizeof(imcs_posix_mutex_t));
IMCS_SMP_CHECK(pthread_mutex_init(&mutex->cs, NULL));
mutex->vtab.lock = imcs_lock_mutex;
mutex->vtab.unlock = imcs_unlock_mutex;
mutex->vtab.destroy = imcs_destroy_mutex;
return &mutex->vtab;
}
typedef struct
{
imcs_semaphore_t vtab;
pthread_cond_t cond;
int count;
} imcs_posix_semaphore_t;
static imcs_bool imcs_semaphore_wait(imcs_semaphore_t* s, imcs_mutex_t* mutex, int n, unsigned timeout)
{
imcs_posix_semaphore_t* sem = (imcs_posix_semaphore_t*)s;
struct timespec abs_ts;
int rc;
if (timeout != IMCS_TM_INFINITE) {
#ifdef PTHREAD_GET_EXPIRATION_NP
struct timespec rel_ts;
rel_ts.tv_sec = timeout/1000;
rel_ts.tv_nsec = timeout%1000*1000000;
pthread_get_expiration_np(&rel_ts, &abs_ts);
#else
struct timeval cur_tv;
gettimeofday(&cur_tv, NULL);
abs_ts.tv_sec = cur_tv.tv_sec + timeout/1000;
abs_ts.tv_nsec = cur_tv.tv_usec*1000 + timeout%1000*1000000;
if (abs_ts.tv_nsec > 1000000000) {
abs_ts.tv_nsec -= 1000000000;
abs_ts.tv_sec += 1;
}
#endif
}
while (sem->count < n)
{
rc = timeout != IMCS_TM_INFINITE
? pthread_cond_timedwait(&sem->cond, &((imcs_posix_mutex_t*)mutex)->cs, &abs_ts)
: pthread_cond_wait(&sem->cond, &((imcs_posix_mutex_t*)mutex)->cs);
if (rc == ETIMEDOUT) {
return 0;
}
IMCS_SMP_CHECK(rc);
}
sem->count -= n;
return 1;
}
static void imcs_semaphore_signal(imcs_semaphore_t* s, int inc)
{
imcs_posix_semaphore_t* sem = (imcs_posix_semaphore_t*)s;
sem->count += inc;
if (inc > 1)
{
IMCS_SMP_CHECK(pthread_cond_broadcast(&sem->cond));
}
else if (inc == 1)
{
IMCS_SMP_CHECK(pthread_cond_signal(&sem->cond));
}
}
static void imcs_semaphore_destroy(imcs_semaphore_t* s)
{
imcs_posix_semaphore_t* sem = (imcs_posix_semaphore_t*)s;
pthread_cond_destroy(&sem->cond);
free(sem);
}
imcs_semaphore_t* imcs_create_semaphore(int value)
{
imcs_posix_semaphore_t* sem = (imcs_posix_semaphore_t*)malloc(sizeof(imcs_posix_semaphore_t));
IMCS_SMP_CHECK(pthread_cond_init(&sem->cond, NULL));
sem->vtab.wait = imcs_semaphore_wait;
sem->vtab.signal = imcs_semaphore_signal;
sem->vtab.destroy = imcs_semaphore_destroy;
sem->count = value;
return &sem->vtab;
}
imcs_process_t imcs_get_pid(void)
{
return (imcs_process_t)(size_t)getpid();
}
imcs_bool imcs_is_process_alive(imcs_process_t proc)
{
return kill((int)(size_t)proc, 0) == 0;
}
#endif