Path: blob/master/Utilities/cmzstd/lib/common/threading.h
5002 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(ZSTD_MULTITHREAD) && defined(_WIN32)1920/**21* Windows minimalist Pthread Wrapper22*/23#ifdef WINVER24# undef WINVER25#endif26#define WINVER 0x06002728#ifdef _WIN32_WINNT29# undef _WIN32_WINNT30#endif31#define _WIN32_WINNT 0x06003233#ifndef WIN32_LEAN_AND_MEAN34# define WIN32_LEAN_AND_MEAN35#endif3637#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */38#include <windows.h>39#undef ERROR40#define ERROR(name) ZSTD_ERROR(name)414243/* mutex */44#define ZSTD_pthread_mutex_t CRITICAL_SECTION45#define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0)46#define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a))47#define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a))48#define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a))4950/* condition variable */51#define ZSTD_pthread_cond_t CONDITION_VARIABLE52#define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0)53#define ZSTD_pthread_cond_destroy(a) ((void)(a))54#define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)55#define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a))56#define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a))5758/* ZSTD_pthread_create() and ZSTD_pthread_join() */59typedef HANDLE ZSTD_pthread_t;6061int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,62void* (*start_routine) (void*), void* arg);6364int ZSTD_pthread_join(ZSTD_pthread_t thread);6566/**67* add here more wrappers as required68*/6970#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */71/* === POSIX Systems === */72# include <pthread.h>7374#if DEBUGLEVEL < 17576#define ZSTD_pthread_mutex_t pthread_mutex_t77#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))78#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))79#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a))80#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a))8182#define ZSTD_pthread_cond_t pthread_cond_t83#define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b))84#define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a))85#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b))86#define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a))87#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a))8889#define ZSTD_pthread_t pthread_t90#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))91#define ZSTD_pthread_join(a) pthread_join((a),NULL)9293#else /* DEBUGLEVEL >= 1 */9495/* Debug implementation of threading.96* In this implementation we use pointers for mutexes and condition variables.97* This way, if we forget to init/destroy them the program will crash or ASAN98* will report leaks.99*/100101#define ZSTD_pthread_mutex_t pthread_mutex_t*102int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);103int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);104#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock(*(a))105#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock(*(a))106107#define ZSTD_pthread_cond_t pthread_cond_t*108int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);109int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);110#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait(*(a), *(b))111#define ZSTD_pthread_cond_signal(a) pthread_cond_signal(*(a))112#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast(*(a))113114#define ZSTD_pthread_t pthread_t115#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))116#define ZSTD_pthread_join(a) pthread_join((a),NULL)117118#endif119120#else /* ZSTD_MULTITHREAD not defined */121/* No multithreading support */122123typedef int ZSTD_pthread_mutex_t;124#define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)125#define ZSTD_pthread_mutex_destroy(a) ((void)(a))126#define ZSTD_pthread_mutex_lock(a) ((void)(a))127#define ZSTD_pthread_mutex_unlock(a) ((void)(a))128129typedef int ZSTD_pthread_cond_t;130#define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)131#define ZSTD_pthread_cond_destroy(a) ((void)(a))132#define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))133#define ZSTD_pthread_cond_signal(a) ((void)(a))134#define ZSTD_pthread_cond_broadcast(a) ((void)(a))135136/* do not use ZSTD_pthread_t */137138#endif /* ZSTD_MULTITHREAD */139140141#endif /* THREADING_H_938743 */142143144