xref: /freebsd/sys/contrib/zstd/lib/common/threading.h (revision 9cbefe25d46756f342c7dd3d174d2d1103808f21)
10c16b537SWarner Losh /**
20c16b537SWarner Losh  * Copyright (c) 2016 Tino Reichardt
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  *
90c16b537SWarner Losh  * You can contact the author at:
100c16b537SWarner Losh  * - zstdmt source repository: https://github.com/mcmilk/zstdmt
110c16b537SWarner Losh  */
120c16b537SWarner Losh 
130c16b537SWarner Losh #ifndef THREADING_H_938743
140c16b537SWarner Losh #define THREADING_H_938743
150c16b537SWarner Losh 
16*9cbefe25SConrad Meyer #include "debug.h"
17*9cbefe25SConrad Meyer 
180c16b537SWarner Losh #if defined (__cplusplus)
190c16b537SWarner Losh extern "C" {
200c16b537SWarner Losh #endif
210c16b537SWarner Losh 
220c16b537SWarner Losh #if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
230c16b537SWarner Losh 
240c16b537SWarner Losh /**
250c16b537SWarner Losh  * Windows minimalist Pthread Wrapper, based on :
260c16b537SWarner Losh  * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
270c16b537SWarner Losh  */
280c16b537SWarner Losh #ifdef WINVER
290c16b537SWarner Losh #  undef WINVER
300c16b537SWarner Losh #endif
310c16b537SWarner Losh #define WINVER       0x0600
320c16b537SWarner Losh 
330c16b537SWarner Losh #ifdef _WIN32_WINNT
340c16b537SWarner Losh #  undef _WIN32_WINNT
350c16b537SWarner Losh #endif
360c16b537SWarner Losh #define _WIN32_WINNT 0x0600
370c16b537SWarner Losh 
380c16b537SWarner Losh #ifndef WIN32_LEAN_AND_MEAN
390c16b537SWarner Losh #  define WIN32_LEAN_AND_MEAN
400c16b537SWarner Losh #endif
410c16b537SWarner Losh 
420c16b537SWarner Losh #undef ERROR   /* reported already defined on VS 2015 (Rich Geldreich) */
430c16b537SWarner Losh #include <windows.h>
440c16b537SWarner Losh #undef ERROR
450c16b537SWarner Losh #define ERROR(name) ZSTD_ERROR(name)
460c16b537SWarner Losh 
470c16b537SWarner Losh 
480c16b537SWarner Losh /* mutex */
490c16b537SWarner Losh #define ZSTD_pthread_mutex_t           CRITICAL_SECTION
5019fcbaf1SConrad Meyer #define ZSTD_pthread_mutex_init(a, b)  ((void)(b), InitializeCriticalSection((a)), 0)
510c16b537SWarner Losh #define ZSTD_pthread_mutex_destroy(a)  DeleteCriticalSection((a))
520c16b537SWarner Losh #define ZSTD_pthread_mutex_lock(a)     EnterCriticalSection((a))
530c16b537SWarner Losh #define ZSTD_pthread_mutex_unlock(a)   LeaveCriticalSection((a))
540c16b537SWarner Losh 
550c16b537SWarner Losh /* condition variable */
560c16b537SWarner Losh #define ZSTD_pthread_cond_t             CONDITION_VARIABLE
5719fcbaf1SConrad Meyer #define ZSTD_pthread_cond_init(a, b)    ((void)(b), InitializeConditionVariable((a)), 0)
5819fcbaf1SConrad Meyer #define ZSTD_pthread_cond_destroy(a)    ((void)(a))
590c16b537SWarner Losh #define ZSTD_pthread_cond_wait(a, b)    SleepConditionVariableCS((a), (b), INFINITE)
600c16b537SWarner Losh #define ZSTD_pthread_cond_signal(a)     WakeConditionVariable((a))
610c16b537SWarner Losh #define ZSTD_pthread_cond_broadcast(a)  WakeAllConditionVariable((a))
620c16b537SWarner Losh 
630c16b537SWarner Losh /* ZSTD_pthread_create() and ZSTD_pthread_join() */
640c16b537SWarner Losh typedef struct {
650c16b537SWarner Losh     HANDLE handle;
660c16b537SWarner Losh     void* (*start_routine)(void*);
670c16b537SWarner Losh     void* arg;
680c16b537SWarner Losh } ZSTD_pthread_t;
690c16b537SWarner Losh 
700c16b537SWarner Losh int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
710c16b537SWarner Losh                    void* (*start_routine) (void*), void* arg);
720c16b537SWarner Losh 
730c16b537SWarner Losh int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
740c16b537SWarner Losh 
750c16b537SWarner Losh /**
760c16b537SWarner Losh  * add here more wrappers as required
770c16b537SWarner Losh  */
780c16b537SWarner Losh 
790c16b537SWarner Losh 
800c16b537SWarner Losh #elif defined(ZSTD_MULTITHREAD)    /* posix assumed ; need a better detection method */
810c16b537SWarner Losh /* ===   POSIX Systems   === */
820c16b537SWarner Losh #  include <pthread.h>
830c16b537SWarner Losh 
84*9cbefe25SConrad Meyer #if DEBUGLEVEL < 1
85*9cbefe25SConrad Meyer 
860c16b537SWarner Losh #define ZSTD_pthread_mutex_t            pthread_mutex_t
870c16b537SWarner Losh #define ZSTD_pthread_mutex_init(a, b)   pthread_mutex_init((a), (b))
880c16b537SWarner Losh #define ZSTD_pthread_mutex_destroy(a)   pthread_mutex_destroy((a))
890c16b537SWarner Losh #define ZSTD_pthread_mutex_lock(a)      pthread_mutex_lock((a))
900c16b537SWarner Losh #define ZSTD_pthread_mutex_unlock(a)    pthread_mutex_unlock((a))
910c16b537SWarner Losh 
920c16b537SWarner Losh #define ZSTD_pthread_cond_t             pthread_cond_t
930c16b537SWarner Losh #define ZSTD_pthread_cond_init(a, b)    pthread_cond_init((a), (b))
940c16b537SWarner Losh #define ZSTD_pthread_cond_destroy(a)    pthread_cond_destroy((a))
950c16b537SWarner Losh #define ZSTD_pthread_cond_wait(a, b)    pthread_cond_wait((a), (b))
960c16b537SWarner Losh #define ZSTD_pthread_cond_signal(a)     pthread_cond_signal((a))
970c16b537SWarner Losh #define ZSTD_pthread_cond_broadcast(a)  pthread_cond_broadcast((a))
980c16b537SWarner Losh 
990c16b537SWarner Losh #define ZSTD_pthread_t                  pthread_t
1000c16b537SWarner Losh #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
1010c16b537SWarner Losh #define ZSTD_pthread_join(a, b)         pthread_join((a),(b))
1020c16b537SWarner Losh 
103*9cbefe25SConrad Meyer #else /* DEBUGLEVEL >= 1 */
104*9cbefe25SConrad Meyer 
105*9cbefe25SConrad Meyer /* Debug implementation of threading.
106*9cbefe25SConrad Meyer  * In this implementation we use pointers for mutexes and condition variables.
107*9cbefe25SConrad Meyer  * This way, if we forget to init/destroy them the program will crash or ASAN
108*9cbefe25SConrad Meyer  * will report leaks.
109*9cbefe25SConrad Meyer  */
110*9cbefe25SConrad Meyer 
111*9cbefe25SConrad Meyer #define ZSTD_pthread_mutex_t            pthread_mutex_t*
112*9cbefe25SConrad Meyer int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);
113*9cbefe25SConrad Meyer int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);
114*9cbefe25SConrad Meyer #define ZSTD_pthread_mutex_lock(a)      pthread_mutex_lock(*(a))
115*9cbefe25SConrad Meyer #define ZSTD_pthread_mutex_unlock(a)    pthread_mutex_unlock(*(a))
116*9cbefe25SConrad Meyer 
117*9cbefe25SConrad Meyer #define ZSTD_pthread_cond_t             pthread_cond_t*
118*9cbefe25SConrad Meyer int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);
119*9cbefe25SConrad Meyer int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
120*9cbefe25SConrad Meyer #define ZSTD_pthread_cond_wait(a, b)    pthread_cond_wait(*(a), *(b))
121*9cbefe25SConrad Meyer #define ZSTD_pthread_cond_signal(a)     pthread_cond_signal(*(a))
122*9cbefe25SConrad Meyer #define ZSTD_pthread_cond_broadcast(a)  pthread_cond_broadcast(*(a))
123*9cbefe25SConrad Meyer 
124*9cbefe25SConrad Meyer #define ZSTD_pthread_t                  pthread_t
125*9cbefe25SConrad Meyer #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
126*9cbefe25SConrad Meyer #define ZSTD_pthread_join(a, b)         pthread_join((a),(b))
127*9cbefe25SConrad Meyer 
128*9cbefe25SConrad Meyer #endif
129*9cbefe25SConrad Meyer 
1300c16b537SWarner Losh #else  /* ZSTD_MULTITHREAD not defined */
1310c16b537SWarner Losh /* No multithreading support */
1320c16b537SWarner Losh 
1330c16b537SWarner Losh typedef int ZSTD_pthread_mutex_t;
13419fcbaf1SConrad Meyer #define ZSTD_pthread_mutex_init(a, b)   ((void)(a), (void)(b), 0)
13519fcbaf1SConrad Meyer #define ZSTD_pthread_mutex_destroy(a)   ((void)(a))
13619fcbaf1SConrad Meyer #define ZSTD_pthread_mutex_lock(a)      ((void)(a))
13719fcbaf1SConrad Meyer #define ZSTD_pthread_mutex_unlock(a)    ((void)(a))
1380c16b537SWarner Losh 
1390c16b537SWarner Losh typedef int ZSTD_pthread_cond_t;
14019fcbaf1SConrad Meyer #define ZSTD_pthread_cond_init(a, b)    ((void)(a), (void)(b), 0)
14119fcbaf1SConrad Meyer #define ZSTD_pthread_cond_destroy(a)    ((void)(a))
14219fcbaf1SConrad Meyer #define ZSTD_pthread_cond_wait(a, b)    ((void)(a), (void)(b))
14319fcbaf1SConrad Meyer #define ZSTD_pthread_cond_signal(a)     ((void)(a))
14419fcbaf1SConrad Meyer #define ZSTD_pthread_cond_broadcast(a)  ((void)(a))
1450c16b537SWarner Losh 
1460c16b537SWarner Losh /* do not use ZSTD_pthread_t */
1470c16b537SWarner Losh 
1480c16b537SWarner Losh #endif /* ZSTD_MULTITHREAD */
1490c16b537SWarner Losh 
1500c16b537SWarner Losh #if defined (__cplusplus)
1510c16b537SWarner Losh }
1520c16b537SWarner Losh #endif
1530c16b537SWarner Losh 
1540c16b537SWarner Losh #endif /* THREADING_H_938743 */
155