|
1 |
|
2 /** |
|
3 * Copyright (c) 2016 Tino Reichardt |
|
4 * All rights reserved. |
|
5 * |
|
6 * This source code is licensed under the BSD-style license found in the |
|
7 * LICENSE file in the root directory of this source tree. An additional grant |
|
8 * of patent rights can be found in the PATENTS file in the same directory. |
|
9 * |
|
10 * You can contact the author at: |
|
11 * - zstdmt source repository: https://github.com/mcmilk/zstdmt |
|
12 */ |
|
13 |
|
14 #ifndef THREADING_H_938743 |
|
15 #define THREADING_H_938743 |
|
16 |
|
17 #if defined (__cplusplus) |
|
18 extern "C" { |
|
19 #endif |
|
20 |
|
21 #if defined(ZSTD_MULTITHREAD) && defined(_WIN32) |
|
22 |
|
23 /** |
|
24 * Windows minimalist Pthread Wrapper, based on : |
|
25 * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html |
|
26 */ |
|
27 #ifdef WINVER |
|
28 # undef WINVER |
|
29 #endif |
|
30 #define WINVER 0x0600 |
|
31 |
|
32 #ifdef _WIN32_WINNT |
|
33 # undef _WIN32_WINNT |
|
34 #endif |
|
35 #define _WIN32_WINNT 0x0600 |
|
36 |
|
37 #ifndef WIN32_LEAN_AND_MEAN |
|
38 # define WIN32_LEAN_AND_MEAN |
|
39 #endif |
|
40 |
|
41 #include <windows.h> |
|
42 |
|
43 /* mutex */ |
|
44 #define pthread_mutex_t CRITICAL_SECTION |
|
45 #define pthread_mutex_init(a,b) InitializeCriticalSection((a)) |
|
46 #define pthread_mutex_destroy(a) DeleteCriticalSection((a)) |
|
47 #define pthread_mutex_lock(a) EnterCriticalSection((a)) |
|
48 #define pthread_mutex_unlock(a) LeaveCriticalSection((a)) |
|
49 |
|
50 /* condition variable */ |
|
51 #define pthread_cond_t CONDITION_VARIABLE |
|
52 #define pthread_cond_init(a, b) InitializeConditionVariable((a)) |
|
53 #define pthread_cond_destroy(a) /* No delete */ |
|
54 #define pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE) |
|
55 #define pthread_cond_signal(a) WakeConditionVariable((a)) |
|
56 #define pthread_cond_broadcast(a) WakeAllConditionVariable((a)) |
|
57 |
|
58 /* pthread_create() and pthread_join() */ |
|
59 typedef struct { |
|
60 HANDLE handle; |
|
61 void* (*start_routine)(void*); |
|
62 void* arg; |
|
63 } pthread_t; |
|
64 |
|
65 int pthread_create(pthread_t* thread, const void* unused, |
|
66 void* (*start_routine) (void*), void* arg); |
|
67 |
|
68 #define pthread_join(a, b) _pthread_join(&(a), (b)) |
|
69 int _pthread_join(pthread_t* thread, void** value_ptr); |
|
70 |
|
71 /** |
|
72 * add here more wrappers as required |
|
73 */ |
|
74 |
|
75 |
|
76 #elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection mathod */ |
|
77 /* === POSIX Systems === */ |
|
78 # include <pthread.h> |
|
79 |
|
80 #else /* ZSTD_MULTITHREAD not defined */ |
|
81 /* No multithreading support */ |
|
82 |
|
83 #define pthread_mutex_t int /* #define rather than typedef, as sometimes pthread support is implicit, resulting in duplicated symbols */ |
|
84 #define pthread_mutex_init(a,b) |
|
85 #define pthread_mutex_destroy(a) |
|
86 #define pthread_mutex_lock(a) |
|
87 #define pthread_mutex_unlock(a) |
|
88 |
|
89 #define pthread_cond_t int |
|
90 #define pthread_cond_init(a,b) |
|
91 #define pthread_cond_destroy(a) |
|
92 #define pthread_cond_wait(a,b) |
|
93 #define pthread_cond_signal(a) |
|
94 #define pthread_cond_broadcast(a) |
|
95 |
|
96 /* do not use pthread_t */ |
|
97 |
|
98 #endif /* ZSTD_MULTITHREAD */ |
|
99 |
|
100 #if defined (__cplusplus) |
|
101 } |
|
102 #endif |
|
103 |
|
104 #endif /* THREADING_H_938743 */ |