Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016 Tino Reichardt |
| 3 | * All rights reserved. |
| 4 | * |
Yann Collet | e9dc204 | 2017-08-31 11:24:54 -0700 | [diff] [blame^] | 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 8 | * |
| 9 | * You can contact the author at: |
| 10 | * - zstdmt source repository: https://github.com/mcmilk/zstdmt |
| 11 | */ |
| 12 | |
| 13 | /** |
Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 14 | * This file will hold wrapper for systems, which do not support pthreads |
Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
ds77 | 08e6a88 | 2017-02-14 00:14:24 +0100 | [diff] [blame] | 17 | /* When ZSTD_MULTITHREAD is not defined, this file would become an empty translation unit. |
Yann Collet | e9dc204 | 2017-08-31 11:24:54 -0700 | [diff] [blame^] | 18 | * Include some ISO C header code to prevent this and portably avoid related warnings. |
| 19 | * (Visual C++: C4206 / GCC: -Wpedantic / Clang: -Wempty-translation-unit) |
| 20 | */ |
ds77 | 08e6a88 | 2017-02-14 00:14:24 +0100 | [diff] [blame] | 21 | #include <stddef.h> |
cyan4973 | 5fba09f | 2017-01-20 12:23:30 -0800 | [diff] [blame] | 22 | |
| 23 | |
Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 24 | #if defined(ZSTD_MULTITHREAD) && defined(_WIN32) |
Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Windows minimalist Pthread Wrapper, based on : |
| 28 | * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | /* === Dependencies === */ |
| 33 | #include <process.h> |
| 34 | #include <errno.h> |
| 35 | #include "threading.h" |
| 36 | |
| 37 | |
| 38 | /* === Implementation === */ |
| 39 | |
| 40 | static unsigned __stdcall worker(void *arg) |
| 41 | { |
| 42 | pthread_t* const thread = (pthread_t*) arg; |
| 43 | thread->arg = thread->start_routine(thread->arg); |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | int pthread_create(pthread_t* thread, const void* unused, |
| 48 | void* (*start_routine) (void*), void* arg) |
| 49 | { |
| 50 | (void)unused; |
| 51 | thread->arg = arg; |
| 52 | thread->start_routine = start_routine; |
| 53 | thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL); |
| 54 | |
| 55 | if (!thread->handle) |
| 56 | return errno; |
| 57 | else |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int _pthread_join(pthread_t * thread, void **value_ptr) |
| 62 | { |
| 63 | DWORD result; |
| 64 | |
| 65 | if (!thread->handle) return 0; |
| 66 | |
| 67 | result = WaitForSingleObject(thread->handle, INFINITE); |
| 68 | switch (result) { |
| 69 | case WAIT_OBJECT_0: |
| 70 | if (value_ptr) *value_ptr = thread->arg; |
| 71 | return 0; |
| 72 | case WAIT_ABANDONED: |
| 73 | return EINVAL; |
| 74 | default: |
| 75 | return GetLastError(); |
| 76 | } |
| 77 | } |
| 78 | |
Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 79 | #endif /* ZSTD_MULTITHREAD */ |