Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright (c) 2016 Tino Reichardt |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
| 8 | * |
| 9 | * You can contact the author at: |
| 10 | * - zstdmt source repository: https://github.com/mcmilk/zstdmt |
| 11 | */ |
| 12 | |
| 13 | #ifndef THREADING_H_938743 |
| 14 | #define THREADING_H_938743 |
| 15 | |
| 16 | #if defined (__cplusplus) |
| 17 | extern "C" { |
| 18 | #endif |
| 19 | |
| 20 | #if defined(ZSTD_MULTITHREAD) && defined(_WIN32) |
| 21 | |
| 22 | /** |
| 23 | * Windows minimalist Pthread Wrapper, based on : |
| 24 | * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html |
| 25 | */ |
| 26 | #ifdef WINVER |
| 27 | # undef WINVER |
| 28 | #endif |
| 29 | #define WINVER 0x0600 |
| 30 | |
| 31 | #ifdef _WIN32_WINNT |
| 32 | # undef _WIN32_WINNT |
| 33 | #endif |
| 34 | #define _WIN32_WINNT 0x0600 |
| 35 | |
| 36 | #ifndef WIN32_LEAN_AND_MEAN |
| 37 | # define WIN32_LEAN_AND_MEAN |
| 38 | #endif |
| 39 | |
| 40 | #undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */ |
| 41 | #include <windows.h> |
| 42 | #undef ERROR |
| 43 | #define ERROR(name) ZSTD_ERROR(name) |
| 44 | |
| 45 | |
| 46 | /* mutex */ |
| 47 | #define ZSTD_pthread_mutex_t CRITICAL_SECTION |
| 48 | #define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0) |
| 49 | #define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a)) |
| 50 | #define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a)) |
| 51 | #define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a)) |
| 52 | |
| 53 | /* condition variable */ |
| 54 | #define ZSTD_pthread_cond_t CONDITION_VARIABLE |
| 55 | #define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0) |
| 56 | #define ZSTD_pthread_cond_destroy(a) ((void)(a)) |
| 57 | #define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE) |
| 58 | #define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a)) |
| 59 | #define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a)) |
| 60 | |
| 61 | /* ZSTD_pthread_create() and ZSTD_pthread_join() */ |
| 62 | typedef struct { |
| 63 | HANDLE handle; |
| 64 | void* (*start_routine)(void*); |
| 65 | void* arg; |
| 66 | } ZSTD_pthread_t; |
| 67 | |
| 68 | int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused, |
| 69 | void* (*start_routine) (void*), void* arg); |
| 70 | |
| 71 | int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr); |
| 72 | |
| 73 | /** |
| 74 | * add here more wrappers as required |
| 75 | */ |
| 76 | |
| 77 | |
| 78 | #elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */ |
| 79 | /* === POSIX Systems === */ |
| 80 | # include <pthread.h> |
| 81 | |
| 82 | #define ZSTD_pthread_mutex_t pthread_mutex_t |
| 83 | #define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b)) |
| 84 | #define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a)) |
| 85 | #define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a)) |
| 86 | #define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a)) |
| 87 | |
| 88 | #define ZSTD_pthread_cond_t pthread_cond_t |
| 89 | #define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b)) |
| 90 | #define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a)) |
| 91 | #define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b)) |
| 92 | #define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a)) |
| 93 | #define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a)) |
| 94 | |
| 95 | #define ZSTD_pthread_t pthread_t |
| 96 | #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d)) |
| 97 | #define ZSTD_pthread_join(a, b) pthread_join((a),(b)) |
| 98 | |
| 99 | #else /* ZSTD_MULTITHREAD not defined */ |
| 100 | /* No multithreading support */ |
| 101 | |
| 102 | typedef int ZSTD_pthread_mutex_t; |
| 103 | #define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0) |
| 104 | #define ZSTD_pthread_mutex_destroy(a) ((void)(a)) |
| 105 | #define ZSTD_pthread_mutex_lock(a) ((void)(a)) |
| 106 | #define ZSTD_pthread_mutex_unlock(a) ((void)(a)) |
| 107 | |
| 108 | typedef int ZSTD_pthread_cond_t; |
| 109 | #define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0) |
| 110 | #define ZSTD_pthread_cond_destroy(a) ((void)(a)) |
| 111 | #define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b)) |
| 112 | #define ZSTD_pthread_cond_signal(a) ((void)(a)) |
| 113 | #define ZSTD_pthread_cond_broadcast(a) ((void)(a)) |
| 114 | |
| 115 | /* do not use ZSTD_pthread_t */ |
| 116 | |
| 117 | #endif /* ZSTD_MULTITHREAD */ |
| 118 | |
| 119 | #if defined (__cplusplus) |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | #endif /* THREADING_H_938743 */ |