Yann Collet | 3b9d434 | 2016-12-31 16:32:19 +0100 | [diff] [blame^] | 1 | |
| 2 | /** |
| 3 | * Copyright (c) 2016 Tino Reichardt |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This source code is licensed under the BSD-style license found in the |
| 7 | * LICENSE file in the root directory of this source tree. An additional grant |
| 8 | * of patent rights can be found in the PATENTS file in the same directory. |
| 9 | * |
| 10 | * You can contact the author at: |
| 11 | * - zstdmt source repository: https://github.com/mcmilk/zstdmt |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * This file will hold wrapper for systems, which do not support Pthreads |
| 16 | */ |
| 17 | |
| 18 | #ifdef _WIN32 |
| 19 | |
| 20 | /** |
| 21 | * Windows minimalist Pthread Wrapper, based on : |
| 22 | * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | /* === Dependencies === */ |
| 27 | #include <process.h> |
| 28 | #include <errno.h> |
| 29 | #include "threading.h" |
| 30 | |
| 31 | |
| 32 | /* === Implementation === */ |
| 33 | |
| 34 | static unsigned __stdcall worker(void *arg) |
| 35 | { |
| 36 | pthread_t* const thread = (pthread_t*) arg; |
| 37 | thread->arg = thread->start_routine(thread->arg); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | int pthread_create(pthread_t* thread, const void* unused, |
| 42 | void* (*start_routine) (void*), void* arg) |
| 43 | { |
| 44 | (void)unused; |
| 45 | thread->arg = arg; |
| 46 | thread->start_routine = start_routine; |
| 47 | thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL); |
| 48 | |
| 49 | if (!thread->handle) |
| 50 | return errno; |
| 51 | else |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int _pthread_join(pthread_t * thread, void **value_ptr) |
| 56 | { |
| 57 | DWORD result; |
| 58 | |
| 59 | if (!thread->handle) return 0; |
| 60 | |
| 61 | result = WaitForSingleObject(thread->handle, INFINITE); |
| 62 | switch (result) { |
| 63 | case WAIT_OBJECT_0: |
| 64 | if (value_ptr) *value_ptr = thread->arg; |
| 65 | return 0; |
| 66 | case WAIT_ABANDONED: |
| 67 | return EINVAL; |
| 68 | default: |
| 69 | return GetLastError(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #endif |