Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 1 | /* Copyright 2015 Pierre Ossman for Cendio AB |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #ifdef WIN32 |
| 20 | #include <windows.h> |
| 21 | #else |
| 22 | #include <pthread.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <rdr/Exception.h> |
| 26 | |
| 27 | #include <os/Mutex.h> |
| 28 | #include <os/Thread.h> |
| 29 | |
| 30 | using namespace os; |
| 31 | |
| 32 | Thread::Thread() : running(false), threadId(NULL) |
| 33 | { |
| 34 | mutex = new Mutex; |
| 35 | |
| 36 | #ifdef WIN32 |
| 37 | threadId = new HANDLE; |
| 38 | #else |
| 39 | threadId = new pthread_t; |
| 40 | #endif |
| 41 | } |
| 42 | |
| 43 | Thread::~Thread() |
| 44 | { |
| 45 | #ifdef WIN32 |
| 46 | delete (HANDLE*)threadId; |
| 47 | #else |
| 48 | if (isRunning()) |
| 49 | pthread_cancel(*(pthread_t*)threadId); |
| 50 | delete (pthread_t*)threadId; |
| 51 | #endif |
| 52 | |
| 53 | delete mutex; |
| 54 | } |
| 55 | |
| 56 | void Thread::start() |
| 57 | { |
| 58 | AutoMutex a(mutex); |
| 59 | |
| 60 | #ifdef WIN32 |
| 61 | *(HANDLE*)threadId = CreateThread(NULL, 0, startRoutine, this, 0, NULL); |
| 62 | if (*(HANDLE*)threadId == NULL) |
| 63 | throw rdr::SystemException("Failed to create thread", GetLastError()); |
| 64 | #else |
| 65 | int ret; |
| 66 | |
| 67 | ret = pthread_create((pthread_t*)threadId, NULL, startRoutine, this); |
| 68 | if (ret != 0) |
| 69 | throw rdr::SystemException("Failed to create thread", ret); |
| 70 | #endif |
| 71 | |
| 72 | running = true; |
| 73 | } |
| 74 | |
| 75 | void Thread::wait() |
| 76 | { |
| 77 | if (!isRunning()) |
| 78 | return; |
| 79 | |
| 80 | #ifdef WIN32 |
| 81 | DWORD ret; |
| 82 | |
| 83 | ret = WaitForSingleObject(*(HANDLE*)threadId, INFINITE); |
| 84 | if (ret != WAIT_OBJECT_0) |
| 85 | throw rdr::SystemException("Failed to join thread", GetLastError()); |
| 86 | #else |
| 87 | int ret; |
| 88 | |
| 89 | ret = pthread_join(*(pthread_t*)threadId, NULL); |
| 90 | if (ret != 0) |
| 91 | throw rdr::SystemException("Failed to join thread", ret); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | bool Thread::isRunning() |
| 96 | { |
| 97 | AutoMutex a(mutex); |
| 98 | |
| 99 | return running; |
| 100 | } |
| 101 | |
| 102 | #ifdef WIN32 |
| 103 | long unsigned __stdcall Thread::startRoutine(void* data) |
| 104 | #else |
| 105 | void* Thread::startRoutine(void* data) |
| 106 | #endif |
| 107 | { |
| 108 | Thread *self; |
| 109 | |
| 110 | self = (Thread*)data; |
| 111 | |
| 112 | try { |
| 113 | self->worker(); |
| 114 | } catch(...) { |
| 115 | } |
| 116 | |
| 117 | self->mutex->lock(); |
| 118 | self->running = false; |
| 119 | self->mutex->unlock(); |
| 120 | |
| 121 | return 0; |
| 122 | } |