Path: blob/master/Utilities/cmzstd/lib/common/threading.c
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/**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 Wrapper26*/272829/* === Dependencies === */30#include <process.h>31#include <errno.h>323334/* === Implementation === */3536typedef struct {37void* (*start_routine)(void*);38void* arg;39int initialized;40ZSTD_pthread_cond_t initialized_cond;41ZSTD_pthread_mutex_t initialized_mutex;42} ZSTD_thread_params_t;4344static unsigned __stdcall worker(void *arg)45{46void* (*start_routine)(void*);47void* thread_arg;4849/* Initialized thread_arg and start_routine and signal main thread that we don't need it50* to wait any longer.51*/52{53ZSTD_thread_params_t* thread_param = (ZSTD_thread_params_t*)arg;54thread_arg = thread_param->arg;55start_routine = thread_param->start_routine;5657/* Signal main thread that we are running and do not depend on its memory anymore */58ZSTD_pthread_mutex_lock(&thread_param->initialized_mutex);59thread_param->initialized = 1;60ZSTD_pthread_cond_signal(&thread_param->initialized_cond);61ZSTD_pthread_mutex_unlock(&thread_param->initialized_mutex);62}6364start_routine(thread_arg);6566return 0;67}6869int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,70void* (*start_routine) (void*), void* arg)71{72ZSTD_thread_params_t thread_param;73(void)unused;7475thread_param.start_routine = start_routine;76thread_param.arg = arg;77thread_param.initialized = 0;78*thread = NULL;7980/* Setup thread initialization synchronization */81if(ZSTD_pthread_cond_init(&thread_param.initialized_cond, NULL)) {82/* Should never happen on Windows */83return -1;84}85if(ZSTD_pthread_mutex_init(&thread_param.initialized_mutex, NULL)) {86/* Should never happen on Windows */87ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);88return -1;89}9091/* Spawn thread */92*thread = (HANDLE)_beginthreadex(NULL, 0, worker, &thread_param, 0, NULL);93if (!thread) {94ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex);95ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);96return errno;97}9899/* Wait for thread to be initialized */100ZSTD_pthread_mutex_lock(&thread_param.initialized_mutex);101while(!thread_param.initialized) {102ZSTD_pthread_cond_wait(&thread_param.initialized_cond, &thread_param.initialized_mutex);103}104ZSTD_pthread_mutex_unlock(&thread_param.initialized_mutex);105ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex);106ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);107108return 0;109}110111int ZSTD_pthread_join(ZSTD_pthread_t thread)112{113DWORD result;114115if (!thread) return 0;116117result = WaitForSingleObject(thread, INFINITE);118CloseHandle(thread);119120switch (result) {121case WAIT_OBJECT_0:122return 0;123case WAIT_ABANDONED:124return EINVAL;125default:126return GetLastError();127}128}129130#endif /* ZSTD_MULTITHREAD */131132#if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)133134#define ZSTD_DEPS_NEED_MALLOC135#include "zstd_deps.h"136137int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)138{139*mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));140if (!*mutex)141return 1;142return pthread_mutex_init(*mutex, attr);143}144145int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)146{147if (!*mutex)148return 0;149{150int const ret = pthread_mutex_destroy(*mutex);151ZSTD_free(*mutex);152return ret;153}154}155156int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)157{158*cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));159if (!*cond)160return 1;161return pthread_cond_init(*cond, attr);162}163164int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)165{166if (!*cond)167return 0;168{169int const ret = pthread_cond_destroy(*cond);170ZSTD_free(*cond);171return ret;172}173}174175#endif176177178