blob: ee7864555951219e73203d62fb4bd62b73b03fc9 [file] [log] [blame]
Yann Collet3b9d4342016-12-31 16:32:19 +01001
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)
18extern "C" {
19#endif
20
Yann Collet0f984d92017-01-19 14:05:07 -080021#if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
Yann Collet3b9d4342016-12-31 16:32:19 +010022
23/**
24 * Windows minimalist Pthread Wrapper, based on :
25 * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
26 */
Nick Terrell4204e032016-12-31 19:10:29 -050027#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 Collet3b9d4342016-12-31 16:32:19 +010036
37#ifndef WIN32_LEAN_AND_MEAN
38# define WIN32_LEAN_AND_MEAN
39#endif
Nick Terrell4204e032016-12-31 19:10:29 -050040
Yann Collet3b9d4342016-12-31 16:32:19 +010041#include <windows.h>
42
43/* mutex */
44#define pthread_mutex_t CRITICAL_SECTION
Yann Colleta90b16e2017-07-20 15:57:55 -070045#define pthread_mutex_init(a,b) (InitializeCriticalSection((a)), 0)
Yann Collet3b9d4342016-12-31 16:32:19 +010046#define pthread_mutex_destroy(a) DeleteCriticalSection((a))
Nick Terrell4204e032016-12-31 19:10:29 -050047#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
Yann Colleta90b16e2017-07-20 15:57:55 -070052#define pthread_cond_init(a, b) (InitializeConditionVariable((a)), 0)
Nick Terrell4204e032016-12-31 19:10:29 -050053#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 Collet3b9d4342016-12-31 16:32:19 +010057
58/* pthread_create() and pthread_join() */
59typedef struct {
60 HANDLE handle;
61 void* (*start_routine)(void*);
Nick Terrell4204e032016-12-31 19:10:29 -050062 void* arg;
Yann Collet3b9d4342016-12-31 16:32:19 +010063} pthread_t;
64
65int 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))
69int _pthread_join(pthread_t* thread, void** value_ptr);
70
71/**
72 * add here more wrappers as required
73 */
74
75
Anders Oleson517577b2017-02-20 12:08:59 -080076#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
Yann Collet3b9d4342016-12-31 16:32:19 +010077/* === POSIX Systems === */
78# include <pthread.h>
79
Yann Collet0f984d92017-01-19 14:05:07 -080080#else /* ZSTD_MULTITHREAD not defined */
Yann Collet3b9d4342016-12-31 16:32:19 +010081/* No multithreading support */
82
Yann Colletb71363b2017-07-19 01:05:40 -070083#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 Collet3b9d4342016-12-31 16:32:19 +010085#define pthread_mutex_destroy(a)
86#define pthread_mutex_lock(a)
87#define pthread_mutex_unlock(a)
88
Yann Colletf1cb5512017-01-02 01:11:55 +010089#define pthread_cond_t int
Yann Colletb71363b2017-07-19 01:05:40 -070090#define pthread_cond_init(a,b) ((void)a, 0)
Nick Terrell4204e032016-12-31 19:10:29 -050091#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 Collet3b9d4342016-12-31 16:32:19 +010096/* do not use pthread_t */
97
Yann Collet0f984d92017-01-19 14:05:07 -080098#endif /* ZSTD_MULTITHREAD */
Yann Collet3b9d4342016-12-31 16:32:19 +010099
100#if defined (__cplusplus)
101}
102#endif
103
104#endif /* THREADING_H_938743 */