| 1 |
#ifndef _PROTON_EXAMPLES_C_THREADS_H |
| 2 |
#define _PROTON_EXAMPLES_C_THREADS_H 1 |
| 3 |
|
| 4 |
/* |
| 5 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 6 |
* or more contributor license agreements. See the NOTICE file |
| 7 |
* distributed with this work for additional information |
| 8 |
* regarding copyright ownership. The ASF licenses this file |
| 9 |
* to you under the Apache License, Version 2.0 (the |
| 10 |
* "License"); you may not use this file except in compliance |
| 11 |
* with the License. You may obtain a copy of the License at |
| 12 |
* |
| 13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, |
| 16 |
* software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* EXAMPLE USE ONLY. Simulate the subset of POSIX threads used by examples for windows */ |
| 23 |
|
| 24 |
#ifdef _WIN32 |
| 25 |
#include <windows.h> |
| 26 |
#include <process.h> |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
HANDLE handle; |
| 30 |
void *(*func)(void *); |
| 31 |
void *arg; |
| 32 |
} pthread_t; |
| 33 |
|
| 34 |
static unsigned __stdcall pthread_run(void *thr0) { |
| 35 |
pthread_t *t = (pthread_t *) thr0; |
| 36 |
t->func(t->arg); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
static int pthread_create(pthread_t *t, void *unused, void *(*f)(void *), void *arg) { |
| 41 |
t->func = f; |
| 42 |
t->arg = arg; |
| 43 |
t->handle = (HANDLE) _beginthreadex(0, 0, &pthread_run, t, 0, 0); |
| 44 |
if (t->handle) { |
| 45 |
return 0; |
| 46 |
} |
| 47 |
return -1; |
| 48 |
} |
| 49 |
|
| 50 |
static int pthread_join(pthread_t t, void **unused) { |
| 51 |
if (t.handle) { |
| 52 |
WaitForSingleObject(t.handle, INFINITE); |
| 53 |
CloseHandle(t.handle); |
| 54 |
} |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
typedef CRITICAL_SECTION pthread_mutex_t; |
| 59 |
#define pthread_mutex_init(m, unused) InitializeCriticalSectionAndSpinCount(m, 4000) |
| 60 |
#define pthread_mutex_destroy(m) DeleteCriticalSection(m) |
| 61 |
#define pthread_mutex_lock(m) EnterCriticalSection(m) |
| 62 |
#define pthread_mutex_unlock(m) LeaveCriticalSection(m) |
| 63 |
|
| 64 |
#else |
| 65 |
|
| 66 |
#include <pthread.h> |
| 67 |
|
| 68 |
#endif |
| 69 |
|
| 70 |
#endif /* thread.h */ |