2 * Copyright (c) 2016 Tino Reichardt
5 * You can contact the author at:
6 * - zstdmt source repository: https://github.com/mcmilk/zstdmt
8 * This source code is licensed under both the BSD-style license (found in the
9 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
10 * in the COPYING file in the root directory of this source tree).
11 * You may select, at your option, one of the above-listed licenses.
15 * This file will hold wrapper for systems, which do not support pthreads
18 #include "threading.h"
20 /* create fake symbol to avoid empty translation unit warning */
21 int g_ZSTD_threading_useless_symbol;
23 #if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
26 * Windows minimalist Pthread Wrapper
30 /* === Dependencies === */
35 /* === Implementation === */
38 void* (*start_routine)(void*);
41 ZSTD_pthread_cond_t initialized_cond;
42 ZSTD_pthread_mutex_t initialized_mutex;
43 } ZSTD_thread_params_t;
45 static unsigned __stdcall worker(void *arg)
47 void* (*start_routine)(void*);
50 /* Initialized thread_arg and start_routine and signal main thread that we don't need it
54 ZSTD_thread_params_t* thread_param = (ZSTD_thread_params_t*)arg;
55 thread_arg = thread_param->arg;
56 start_routine = thread_param->start_routine;
58 /* Signal main thread that we are running and do not depend on its memory anymore */
59 ZSTD_pthread_mutex_lock(&thread_param->initialized_mutex);
60 thread_param->initialized = 1;
61 ZSTD_pthread_cond_signal(&thread_param->initialized_cond);
62 ZSTD_pthread_mutex_unlock(&thread_param->initialized_mutex);
65 start_routine(thread_arg);
70 int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
71 void* (*start_routine) (void*), void* arg)
73 ZSTD_thread_params_t thread_param;
76 thread_param.start_routine = start_routine;
77 thread_param.arg = arg;
78 thread_param.initialized = 0;
81 /* Setup thread initialization synchronization */
82 if(ZSTD_pthread_cond_init(&thread_param.initialized_cond, NULL)) {
83 /* Should never happen on Windows */
86 if(ZSTD_pthread_mutex_init(&thread_param.initialized_mutex, NULL)) {
87 /* Should never happen on Windows */
88 ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);
93 *thread = (HANDLE)_beginthreadex(NULL, 0, worker, &thread_param, 0, NULL);
95 ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex);
96 ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);
100 /* Wait for thread to be initialized */
101 ZSTD_pthread_mutex_lock(&thread_param.initialized_mutex);
102 while(!thread_param.initialized) {
103 ZSTD_pthread_cond_wait(&thread_param.initialized_cond, &thread_param.initialized_mutex);
105 ZSTD_pthread_mutex_unlock(&thread_param.initialized_mutex);
106 ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex);
107 ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);
112 int ZSTD_pthread_join(ZSTD_pthread_t thread)
116 if (!thread) return 0;
118 result = WaitForSingleObject(thread, INFINITE);
127 return GetLastError();
131 #endif /* ZSTD_MULTITHREAD */
133 #if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)
135 #define ZSTD_DEPS_NEED_MALLOC
136 #include "zstd_deps.h"
138 int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
140 *mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));
143 return pthread_mutex_init(*mutex, attr);
146 int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
151 int const ret = pthread_mutex_destroy(*mutex);
157 int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
159 *cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));
162 return pthread_cond_init(*cond, attr);
165 int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)
170 int const ret = pthread_cond_destroy(*cond);