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> |
Pierre Ossman | a6b0026 | 2016-01-04 14:19:15 +0100 | [diff] [blame^] | 23 | #include <signal.h> |
Pierre Ossman | 7b63a7c | 2015-11-13 14:07:52 +0100 | [diff] [blame] | 24 | #include <unistd.h> |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | #include <rdr/Exception.h> |
| 28 | |
| 29 | #include <os/Mutex.h> |
| 30 | #include <os/Thread.h> |
| 31 | |
| 32 | using namespace os; |
| 33 | |
| 34 | Thread::Thread() : running(false), threadId(NULL) |
| 35 | { |
| 36 | mutex = new Mutex; |
| 37 | |
| 38 | #ifdef WIN32 |
| 39 | threadId = new HANDLE; |
| 40 | #else |
| 41 | threadId = new pthread_t; |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | Thread::~Thread() |
| 46 | { |
| 47 | #ifdef WIN32 |
| 48 | delete (HANDLE*)threadId; |
| 49 | #else |
| 50 | if (isRunning()) |
| 51 | pthread_cancel(*(pthread_t*)threadId); |
| 52 | delete (pthread_t*)threadId; |
| 53 | #endif |
| 54 | |
| 55 | delete mutex; |
| 56 | } |
| 57 | |
| 58 | void Thread::start() |
| 59 | { |
| 60 | AutoMutex a(mutex); |
| 61 | |
| 62 | #ifdef WIN32 |
| 63 | *(HANDLE*)threadId = CreateThread(NULL, 0, startRoutine, this, 0, NULL); |
| 64 | if (*(HANDLE*)threadId == NULL) |
| 65 | throw rdr::SystemException("Failed to create thread", GetLastError()); |
| 66 | #else |
| 67 | int ret; |
Pierre Ossman | a6b0026 | 2016-01-04 14:19:15 +0100 | [diff] [blame^] | 68 | sigset_t all, old; |
| 69 | |
| 70 | // Creating threads from libraries is a bit evil, so mitigate the |
| 71 | // issue by at least avoiding signals on these threads |
| 72 | sigfillset(&all); |
| 73 | ret = pthread_sigmask(SIG_SETMASK, &all, &old); |
| 74 | if (ret != 0) |
| 75 | throw rdr::SystemException("Failed to mask signals", ret); |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 76 | |
| 77 | ret = pthread_create((pthread_t*)threadId, NULL, startRoutine, this); |
Pierre Ossman | a6b0026 | 2016-01-04 14:19:15 +0100 | [diff] [blame^] | 78 | |
| 79 | pthread_sigmask(SIG_SETMASK, &old, NULL); |
| 80 | |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 81 | if (ret != 0) |
| 82 | throw rdr::SystemException("Failed to create thread", ret); |
| 83 | #endif |
| 84 | |
| 85 | running = true; |
| 86 | } |
| 87 | |
| 88 | void Thread::wait() |
| 89 | { |
| 90 | if (!isRunning()) |
| 91 | return; |
| 92 | |
| 93 | #ifdef WIN32 |
| 94 | DWORD ret; |
| 95 | |
| 96 | ret = WaitForSingleObject(*(HANDLE*)threadId, INFINITE); |
| 97 | if (ret != WAIT_OBJECT_0) |
| 98 | throw rdr::SystemException("Failed to join thread", GetLastError()); |
| 99 | #else |
| 100 | int ret; |
| 101 | |
| 102 | ret = pthread_join(*(pthread_t*)threadId, NULL); |
| 103 | if (ret != 0) |
| 104 | throw rdr::SystemException("Failed to join thread", ret); |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | bool Thread::isRunning() |
| 109 | { |
| 110 | AutoMutex a(mutex); |
| 111 | |
| 112 | return running; |
| 113 | } |
| 114 | |
Pierre Ossman | 7b63a7c | 2015-11-13 14:07:52 +0100 | [diff] [blame] | 115 | size_t Thread::getSystemCPUCount() |
| 116 | { |
| 117 | #ifdef WIN32 |
| 118 | SYSTEM_INFO si; |
| 119 | size_t count; |
| 120 | DWORD mask; |
| 121 | |
| 122 | GetSystemInfo(&si); |
| 123 | |
| 124 | count = 0; |
| 125 | for (mask = si.dwActiveProcessorMask;mask != 0;mask >>= 1) { |
| 126 | if (mask & 0x1) |
| 127 | count++; |
| 128 | } |
| 129 | |
| 130 | if (count > si.dwNumberOfProcessors) |
| 131 | count = si.dwNumberOfProcessors; |
| 132 | |
| 133 | return count; |
| 134 | #else |
| 135 | long ret; |
| 136 | |
| 137 | ret = sysconf(_SC_NPROCESSORS_ONLN); |
| 138 | if (ret == -1) |
| 139 | return 0; |
| 140 | |
| 141 | return ret; |
| 142 | #endif |
| 143 | } |
| 144 | |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 145 | #ifdef WIN32 |
| 146 | long unsigned __stdcall Thread::startRoutine(void* data) |
| 147 | #else |
| 148 | void* Thread::startRoutine(void* data) |
| 149 | #endif |
| 150 | { |
| 151 | Thread *self; |
| 152 | |
| 153 | self = (Thread*)data; |
| 154 | |
| 155 | try { |
| 156 | self->worker(); |
| 157 | } catch(...) { |
| 158 | } |
| 159 | |
| 160 | self->mutex->lock(); |
| 161 | self->running = false; |
| 162 | self->mutex->unlock(); |
| 163 | |
| 164 | return 0; |
| 165 | } |