blob: ab09977a86dc31e5e49a616bc7fa97929622937f [file] [log] [blame]
Yann Collet3b9d4342016-12-31 16:32:19 +01001/**
2 * Copyright (c) 2016 Tino Reichardt
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
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)
17extern "C" {
18#endif
19
Yann Collet0f984d92017-01-19 14:05:07 -080020#if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
Yann Collet3b9d4342016-12-31 16:32:19 +010021
22/**
23 * Windows minimalist Pthread Wrapper, based on :
24 * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
25 */
Nick Terrell4204e032016-12-31 19:10:29 -050026#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
Yann Collet3b9d4342016-12-31 16:32:19 +010035
36#ifndef WIN32_LEAN_AND_MEAN
37# define WIN32_LEAN_AND_MEAN
38#endif
Nick Terrell4204e032016-12-31 19:10:29 -050039
Yann Collet3b9d4342016-12-31 16:32:19 +010040#include <windows.h>
41
42/* mutex */
43#define pthread_mutex_t CRITICAL_SECTION
Yann Colleta90b16e2017-07-20 15:57:55 -070044#define pthread_mutex_init(a,b) (InitializeCriticalSection((a)), 0)
Yann Collet3b9d4342016-12-31 16:32:19 +010045#define pthread_mutex_destroy(a) DeleteCriticalSection((a))
Nick Terrell4204e032016-12-31 19:10:29 -050046#define pthread_mutex_lock(a) EnterCriticalSection((a))
47#define pthread_mutex_unlock(a) LeaveCriticalSection((a))
48
49/* condition variable */
50#define pthread_cond_t CONDITION_VARIABLE
Yann Colleta90b16e2017-07-20 15:57:55 -070051#define pthread_cond_init(a, b) (InitializeConditionVariable((a)), 0)
Nick Terrell4204e032016-12-31 19:10:29 -050052#define pthread_cond_destroy(a) /* No delete */
53#define pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)
54#define pthread_cond_signal(a) WakeConditionVariable((a))
55#define pthread_cond_broadcast(a) WakeAllConditionVariable((a))
Yann Collet3b9d4342016-12-31 16:32:19 +010056
57/* pthread_create() and pthread_join() */
58typedef struct {
59 HANDLE handle;
60 void* (*start_routine)(void*);
Nick Terrell4204e032016-12-31 19:10:29 -050061 void* arg;
Yann Collet3b9d4342016-12-31 16:32:19 +010062} pthread_t;
63
64int pthread_create(pthread_t* thread, const void* unused,
65 void* (*start_routine) (void*), void* arg);
66
67#define pthread_join(a, b) _pthread_join(&(a), (b))
68int _pthread_join(pthread_t* thread, void** value_ptr);
69
70/**
71 * add here more wrappers as required
72 */
73
74
Anders Oleson517577b2017-02-20 12:08:59 -080075#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
Yann Collet3b9d4342016-12-31 16:32:19 +010076/* === POSIX Systems === */
77# include <pthread.h>
78
Yann Collet0f984d92017-01-19 14:05:07 -080079#else /* ZSTD_MULTITHREAD not defined */
Yann Collet3b9d4342016-12-31 16:32:19 +010080/* No multithreading support */
81
Yann Colletb71363b2017-07-19 01:05:40 -070082#define pthread_mutex_t int /* #define rather than typedef, because sometimes pthread support is implicit, resulting in duplicated symbols */
83#define pthread_mutex_init(a,b) ((void)a, 0)
Yann Collet3b9d4342016-12-31 16:32:19 +010084#define pthread_mutex_destroy(a)
85#define pthread_mutex_lock(a)
86#define pthread_mutex_unlock(a)
87
Yann Colletf1cb5512017-01-02 01:11:55 +010088#define pthread_cond_t int
Yann Colletb71363b2017-07-19 01:05:40 -070089#define pthread_cond_init(a,b) ((void)a, 0)
Nick Terrell4204e032016-12-31 19:10:29 -050090#define pthread_cond_destroy(a)
91#define pthread_cond_wait(a,b)
92#define pthread_cond_signal(a)
93#define pthread_cond_broadcast(a)
94
Yann Collet3b9d4342016-12-31 16:32:19 +010095/* do not use pthread_t */
96
Yann Collet0f984d92017-01-19 14:05:07 -080097#endif /* ZSTD_MULTITHREAD */
Yann Collet3b9d4342016-12-31 16:32:19 +010098
99#if defined (__cplusplus)
100}
101#endif
102
103#endif /* THREADING_H_938743 */