Path: blob/main/sys/contrib/zstd/lib/common/threading.c
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/**14* This file will hold wrapper for systems, which do not support pthreads15*/1617#include "threading.h"1819/* create fake symbol to avoid empty translation unit warning */20int g_ZSTD_threading_useless_symbol;2122#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*/282930/* === Dependencies === */31#include <process.h>32#include <errno.h>333435/* === Implementation === */3637static unsigned __stdcall worker(void *arg)38{39ZSTD_pthread_t* const thread = (ZSTD_pthread_t*) arg;40thread->arg = thread->start_routine(thread->arg);41return 0;42}4344int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,45void* (*start_routine) (void*), void* arg)46{47(void)unused;48thread->arg = arg;49thread->start_routine = start_routine;50thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL);5152if (!thread->handle)53return errno;54else55return 0;56}5758int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)59{60DWORD result;6162if (!thread.handle) return 0;6364result = WaitForSingleObject(thread.handle, INFINITE);65switch (result) {66case WAIT_OBJECT_0:67if (value_ptr) *value_ptr = thread.arg;68return 0;69case WAIT_ABANDONED:70return EINVAL;71default:72return GetLastError();73}74}7576#endif /* ZSTD_MULTITHREAD */7778#if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)7980#define ZSTD_DEPS_NEED_MALLOC81#include "zstd_deps.h"8283int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)84{85*mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));86if (!*mutex)87return 1;88return pthread_mutex_init(*mutex, attr);89}9091int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)92{93if (!*mutex)94return 0;95{96int const ret = pthread_mutex_destroy(*mutex);97ZSTD_free(*mutex);98return ret;99}100}101102int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)103{104*cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));105if (!*cond)106return 1;107return pthread_cond_init(*cond, attr);108}109110int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)111{112if (!*cond)113return 0;114{115int const ret = pthread_cond_destroy(*cond);116ZSTD_free(*cond);117return ret;118}119}120121#endif122123124