Path: blob/master/Utilities/cmzstd/lib/common/threading.h
3158 views
/**1* Copyright (c) 2016 Tino Reichardt2* All rights reserved.3*4* You can contact the author at:5* - zstdmt source repository: https://github.com/mcmilk/zstdmt6*7* This source code is licensed under both the BSD-style license (found in the8* LICENSE file in the root directory of this source tree) and the GPLv2 (found9* in the COPYING file in the root directory of this source tree).10* You may select, at your option, one of the above-listed licenses.11*/1213#ifndef THREADING_H_93874314#define THREADING_H_9387431516#include "debug.h"1718#if defined (__cplusplus)19extern "C" {20#endif2122#if defined(ZSTD_MULTITHREAD) && defined(_WIN32)2324/**25* Windows minimalist Pthread Wrapper26*/27#ifdef WINVER28# undef WINVER29#endif30#define WINVER 0x06003132#ifdef _WIN32_WINNT33# undef _WIN32_WINNT34#endif35#define _WIN32_WINNT 0x06003637#ifndef WIN32_LEAN_AND_MEAN38# define WIN32_LEAN_AND_MEAN39#endif4041#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */42#include <windows.h>43#undef ERROR44#define ERROR(name) ZSTD_ERROR(name)454647/* mutex */48#define ZSTD_pthread_mutex_t CRITICAL_SECTION49#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))5354/* condition variable */55#define ZSTD_pthread_cond_t CONDITION_VARIABLE56#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))6162/* ZSTD_pthread_create() and ZSTD_pthread_join() */63typedef HANDLE ZSTD_pthread_t;6465int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,66void* (*start_routine) (void*), void* arg);6768int ZSTD_pthread_join(ZSTD_pthread_t thread);6970/**71* add here more wrappers as required72*/737475#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */76/* === POSIX Systems === */77# include <pthread.h>7879#if DEBUGLEVEL < 18081#define ZSTD_pthread_mutex_t pthread_mutex_t82#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))83#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))84#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a))85#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a))8687#define ZSTD_pthread_cond_t pthread_cond_t88#define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b))89#define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a))90#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b))91#define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a))92#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a))9394#define ZSTD_pthread_t pthread_t95#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))96#define ZSTD_pthread_join(a) pthread_join((a),NULL)9798#else /* DEBUGLEVEL >= 1 */99100/* Debug implementation of threading.101* In this implementation we use pointers for mutexes and condition variables.102* This way, if we forget to init/destroy them the program will crash or ASAN103* will report leaks.104*/105106#define ZSTD_pthread_mutex_t pthread_mutex_t*107int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);108int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);109#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock(*(a))110#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock(*(a))111112#define ZSTD_pthread_cond_t pthread_cond_t*113int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);114int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);115#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait(*(a), *(b))116#define ZSTD_pthread_cond_signal(a) pthread_cond_signal(*(a))117#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast(*(a))118119#define ZSTD_pthread_t pthread_t120#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))121#define ZSTD_pthread_join(a) pthread_join((a),NULL)122123#endif124125#else /* ZSTD_MULTITHREAD not defined */126/* No multithreading support */127128typedef int ZSTD_pthread_mutex_t;129#define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)130#define ZSTD_pthread_mutex_destroy(a) ((void)(a))131#define ZSTD_pthread_mutex_lock(a) ((void)(a))132#define ZSTD_pthread_mutex_unlock(a) ((void)(a))133134typedef int ZSTD_pthread_cond_t;135#define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)136#define ZSTD_pthread_cond_destroy(a) ((void)(a))137#define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))138#define ZSTD_pthread_cond_signal(a) ((void)(a))139#define ZSTD_pthread_cond_broadcast(a) ((void)(a))140141/* do not use ZSTD_pthread_t */142143#endif /* ZSTD_MULTITHREAD */144145#if defined (__cplusplus)146}147#endif148149#endif /* THREADING_H_938743 */150151152