| 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 |
/* === Build Macro === */ |
| 21 |
|
| 22 |
#ifndef POOL_MT // can be defined on command line |
| 23 |
# define POOL_MT 1 |
| 24 |
#endif |
| 25 |
|
| 26 |
|
| 27 |
/* === Implementation === */ |
| 28 |
|
| 29 |
#if POOL_MT && defined(_WIN32) |
| 30 |
|
| 31 |
/** |
| 32 |
* Define windows version before include |
| 33 |
*/ |
| 34 |
#undef WINVER |
| 35 |
#define WINVER 0x0600 |
| 36 |
|
| 37 |
#undef _WIN32_WINNT |
| 38 |
#define _WIN32_WINNT 0x0600 |
| 39 |
|
| 40 |
#ifndef WIN32_LEAN_AND_MEAN |
| 41 |
# define WIN32_LEAN_AND_MEAN |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <windows.h> |
| 45 |
#include <stdio.h> |
| 46 |
|
| 47 |
/* mutex */ |
| 48 |
#define ZSTD_pthread_mutex_t CRITICAL_SECTION |
| 49 |
#define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0) |
| 50 |
#define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a)) |
| 51 |
#define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a)) |
| 52 |
#define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a)) |
| 53 |
|
| 54 |
/* condition variable */ |
| 55 |
#define ZSTD_pthread_cond_t CONDITION_VARIABLE |
| 56 |
#define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0) |
| 57 |
#define ZSTD_pthread_cond_destroy(a) ((void)(a)) |
| 58 |
#define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE) |
| 59 |
#define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a)) |
| 60 |
#define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a)) |
| 61 |
|
| 62 |
/* ZSTD_pthread_create() and ZSTD_pthread_join() */ |
| 63 |
typedef struct { |
| 64 |
HANDLE handle; |
| 65 |
void* (*start_routine)(void*); |
| 66 |
void* arg; |
| 67 |
} ZSTD_pthread_t; |
| 68 |
|
| 69 |
int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused, |
| 70 |
void* (*start_routine) (void*), void* arg); |
| 71 |
|
| 72 |
int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr); |
| 73 |
|
| 74 |
/** |
| 75 |
* add here more wrappers as required |
| 76 |
*/ |
| 77 |
|
| 78 |
|
| 79 |
#elif POOL_MT /* posix assumed ; need a better detection method */ |
| 80 |
/* === POSIX Systems === */ |
| 81 |
# include <pthread.h> |
| 82 |
|
| 83 |
#define ZSTD_pthread_mutex_t pthread_mutex_t |
| 84 |
#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b)) |
| 85 |
#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a)) |
| 86 |
#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a)) |
| 87 |
#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a)) |
| 88 |
|
| 89 |
#define ZSTD_pthread_cond_t pthread_cond_t |
| 90 |
#define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b)) |
| 91 |
#define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a)) |
| 92 |
#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b)) |
| 93 |
#define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a)) |
| 94 |
#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a)) |
| 95 |
|
| 96 |
#define ZSTD_pthread_t pthread_t |
| 97 |
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d)) |
| 98 |
#define ZSTD_pthread_join(a, b) pthread_join((a),(b)) |
| 99 |
|
| 100 |
#else /* POOL_MT == 0 */ |
| 101 |
/* No multithreading support */ |
| 102 |
|
| 103 |
typedef int ZSTD_pthread_mutex_t; |
| 104 |
#define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0) |
| 105 |
#define ZSTD_pthread_mutex_destroy(a) ((void)(a)) |
| 106 |
#define ZSTD_pthread_mutex_lock(a) ((void)(a)) |
| 107 |
#define ZSTD_pthread_mutex_unlock(a) ((void)(a)) |
| 108 |
|
| 109 |
typedef int ZSTD_pthread_cond_t; |
| 110 |
#define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0) |
| 111 |
#define ZSTD_pthread_cond_destroy(a) ((void)(a)) |
| 112 |
#define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b)) |
| 113 |
#define ZSTD_pthread_cond_signal(a) ((void)(a)) |
| 114 |
#define ZSTD_pthread_cond_broadcast(a) ((void)(a)) |
| 115 |
|
| 116 |
/* do not use ZSTD_pthread_t */ |
| 117 |
|
| 118 |
#endif /* POOL_MT */ |
| 119 |
|
| 120 |
#if defined (__cplusplus) |
| 121 |
} |
| 122 |
#endif |
| 123 |
|
| 124 |
#endif /* THREADING_H_938743 */ |