| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 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 | |
| Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 21 | #if defined(ZSTD_MULTITHREAD) && defined(_WIN32) |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Windows minimalist Pthread Wrapper, based on : |
| 25 | * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html |
| 26 | */ |
| Nick Terrell | 4204e03 | 2016-12-31 19:10:29 -0500 | [diff] [blame] | 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 |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 36 | |
| 37 | #ifndef WIN32_LEAN_AND_MEAN |
| 38 | # define WIN32_LEAN_AND_MEAN |
| 39 | #endif |
| Nick Terrell | 4204e03 | 2016-12-31 19:10:29 -0500 | [diff] [blame] | 40 | |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 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)) |
| Nick Terrell | 4204e03 | 2016-12-31 19:10:29 -0500 | [diff] [blame] | 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)) |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 57 | |
| 58 | /* pthread_create() and pthread_join() */ |
| 59 | typedef struct { |
| 60 | HANDLE handle; |
| 61 | void* (*start_routine)(void*); |
| Nick Terrell | 4204e03 | 2016-12-31 19:10:29 -0500 | [diff] [blame] | 62 | void* arg; |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 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 | |
| Anders Oleson | 517577b | 2017-02-20 12:08:59 -0800 | [diff] [blame] | 76 | #elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */ |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 77 | /* === POSIX Systems === */ |
| 78 | # include <pthread.h> |
| 79 | |
| Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 80 | #else /* ZSTD_MULTITHREAD not defined */ |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 81 | /* No multithreading support */ |
| 82 | |
| Yann Collet | b71363b | 2017-07-19 01:05:40 -0700 | [diff] [blame^] | 83 | #define pthread_mutex_t int /* #define rather than typedef, because sometimes pthread support is implicit, resulting in duplicated symbols */ |
| 84 | #define pthread_mutex_init(a,b) ((void)a, 0) |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 85 | #define pthread_mutex_destroy(a) |
| 86 | #define pthread_mutex_lock(a) |
| 87 | #define pthread_mutex_unlock(a) |
| 88 | |
| Yann Collet | f1cb551 | 2017-01-02 01:11:55 +0100 | [diff] [blame] | 89 | #define pthread_cond_t int |
| Yann Collet | b71363b | 2017-07-19 01:05:40 -0700 | [diff] [blame^] | 90 | #define pthread_cond_init(a,b) ((void)a, 0) |
| Nick Terrell | 4204e03 | 2016-12-31 19:10:29 -0500 | [diff] [blame] | 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 | |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 96 | /* do not use pthread_t */ |
| 97 | |
| Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 98 | #endif /* ZSTD_MULTITHREAD */ |
| Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 99 | |
| 100 | #if defined (__cplusplus) |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | #endif /* THREADING_H_938743 */ |