Path: blob/main/sys/contrib/zstd/lib/common/threading.h
48378 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 Wrapper, based on :26* http://www.cse.wustl.edu/~schmidt/win32-cv-1.html27*/28#ifdef WINVER29# undef WINVER30#endif31#define WINVER 0x06003233#ifdef _WIN32_WINNT34# undef _WIN32_WINNT35#endif36#define _WIN32_WINNT 0x06003738#ifndef WIN32_LEAN_AND_MEAN39# define WIN32_LEAN_AND_MEAN40#endif4142#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */43#include <windows.h>44#undef ERROR45#define ERROR(name) ZSTD_ERROR(name)464748/* mutex */49#define ZSTD_pthread_mutex_t CRITICAL_SECTION50#define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0)51#define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a))52#define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a))53#define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a))5455/* condition variable */56#define ZSTD_pthread_cond_t CONDITION_VARIABLE57#define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0)58#define ZSTD_pthread_cond_destroy(a) ((void)(a))59#define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)60#define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a))61#define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a))6263/* ZSTD_pthread_create() and ZSTD_pthread_join() */64typedef struct {65HANDLE handle;66void* (*start_routine)(void*);67void* arg;68} ZSTD_pthread_t;6970int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,71void* (*start_routine) (void*), void* arg);7273int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);7475/**76* add here more wrappers as required77*/787980#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */81/* === POSIX Systems === */82# include <pthread.h>8384#if DEBUGLEVEL < 18586#define ZSTD_pthread_mutex_t pthread_mutex_t87#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))88#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))89#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a))90#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a))9192#define ZSTD_pthread_cond_t pthread_cond_t93#define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b))94#define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a))95#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b))96#define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a))97#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a))9899#define ZSTD_pthread_t pthread_t100#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))101#define ZSTD_pthread_join(a, b) pthread_join((a),(b))102103#else /* DEBUGLEVEL >= 1 */104105/* Debug implementation of threading.106* In this implementation we use pointers for mutexes and condition variables.107* This way, if we forget to init/destroy them the program will crash or ASAN108* will report leaks.109*/110111#define ZSTD_pthread_mutex_t pthread_mutex_t*112int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);113int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);114#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock(*(a))115#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock(*(a))116117#define ZSTD_pthread_cond_t pthread_cond_t*118int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);119int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);120#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait(*(a), *(b))121#define ZSTD_pthread_cond_signal(a) pthread_cond_signal(*(a))122#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast(*(a))123124#define ZSTD_pthread_t pthread_t125#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))126#define ZSTD_pthread_join(a, b) pthread_join((a),(b))127128#endif129130#else /* ZSTD_MULTITHREAD not defined */131/* No multithreading support */132133typedef int ZSTD_pthread_mutex_t;134#define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)135#define ZSTD_pthread_mutex_destroy(a) ((void)(a))136#define ZSTD_pthread_mutex_lock(a) ((void)(a))137#define ZSTD_pthread_mutex_unlock(a) ((void)(a))138139typedef int ZSTD_pthread_cond_t;140#define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)141#define ZSTD_pthread_cond_destroy(a) ((void)(a))142#define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))143#define ZSTD_pthread_cond_signal(a) ((void)(a))144#define ZSTD_pthread_cond_broadcast(a) ((void)(a))145146/* do not use ZSTD_pthread_t */147148#endif /* ZSTD_MULTITHREAD */149150#if defined (__cplusplus)151}152#endif153154#endif /* THREADING_H_938743 */155156157