| 1 | /* | 
|---|
| 2 | * synergy -- mouse and keyboard sharing utility | 
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman | 
|---|
| 4 | * | 
|---|
| 5 | * This package is free software; you can redistribute it and/or | 
|---|
| 6 | * modify it under the terms of the GNU General Public License | 
|---|
| 7 | * found in the file COPYING that should have accompanied this file. | 
|---|
| 8 | * | 
|---|
| 9 | * This package is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #include "CThread.h" | 
|---|
| 16 | #include "XMT.h" | 
|---|
| 17 | #include "XThread.h" | 
|---|
| 18 | #include "CLog.h" | 
|---|
| 19 | #include "IJob.h" | 
|---|
| 20 | #include "CArch.h" | 
|---|
| 21 |  | 
|---|
| 22 | // | 
|---|
| 23 | // CThread | 
|---|
| 24 | // | 
|---|
| 25 |  | 
|---|
| 26 | CThread::CThread(IJob* job) | 
|---|
| 27 | { | 
|---|
| 28 | m_thread = ARCH->newThread(&CThread::threadFunc, job); | 
|---|
| 29 | if (m_thread == NULL) { | 
|---|
| 30 | // couldn't create thread | 
|---|
| 31 | delete job; | 
|---|
| 32 | throw XMTThreadUnavailable(); | 
|---|
| 33 | } | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | CThread::CThread(const CThread& thread) | 
|---|
| 37 | { | 
|---|
| 38 | m_thread = ARCH->copyThread(thread.m_thread); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | CThread::CThread(CArchThread adoptedThread) | 
|---|
| 42 | { | 
|---|
| 43 | m_thread = adoptedThread; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | CThread::~CThread() | 
|---|
| 47 | { | 
|---|
| 48 | ARCH->closeThread(m_thread); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | CThread& | 
|---|
| 52 | CThread::operator=(const CThread& thread) | 
|---|
| 53 | { | 
|---|
| 54 | // copy given thread and release ours | 
|---|
| 55 | CArchThread copy = ARCH->copyThread(thread.m_thread); | 
|---|
| 56 | ARCH->closeThread(m_thread); | 
|---|
| 57 |  | 
|---|
| 58 | // cut over | 
|---|
| 59 | m_thread = copy; | 
|---|
| 60 |  | 
|---|
| 61 | return *this; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void | 
|---|
| 65 | CThread::exit(void* result) | 
|---|
| 66 | { | 
|---|
| 67 | throw XThreadExit(result); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void | 
|---|
| 71 | CThread::cancel() | 
|---|
| 72 | { | 
|---|
| 73 | ARCH->cancelThread(m_thread); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void | 
|---|
| 77 | CThread::setPriority(int n) | 
|---|
| 78 | { | 
|---|
| 79 | ARCH->setPriorityOfThread(m_thread, n); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | void | 
|---|
| 83 | CThread::unblockPollSocket() | 
|---|
| 84 | { | 
|---|
| 85 | ARCH->unblockPollSocket(m_thread); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | CThread | 
|---|
| 89 | CThread::getCurrentThread() | 
|---|
| 90 | { | 
|---|
| 91 | return CThread(ARCH->newCurrentThread()); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void | 
|---|
| 95 | CThread::testCancel() | 
|---|
| 96 | { | 
|---|
| 97 | ARCH->testCancelThread(); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | bool | 
|---|
| 101 | CThread::wait(double timeout) const | 
|---|
| 102 | { | 
|---|
| 103 | return ARCH->wait(m_thread, timeout); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void* | 
|---|
| 107 | CThread::getResult() const | 
|---|
| 108 | { | 
|---|
| 109 | if (wait()) | 
|---|
| 110 | return ARCH->getResultOfThread(m_thread); | 
|---|
| 111 | else | 
|---|
| 112 | return NULL; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | IArchMultithread::ThreadID | 
|---|
| 116 | CThread::getID() const | 
|---|
| 117 | { | 
|---|
| 118 | return ARCH->getIDOfThread(m_thread); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | bool | 
|---|
| 122 | CThread::operator==(const CThread& thread) const | 
|---|
| 123 | { | 
|---|
| 124 | return ARCH->isSameThread(m_thread, thread.m_thread); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | bool | 
|---|
| 128 | CThread::operator!=(const CThread& thread) const | 
|---|
| 129 | { | 
|---|
| 130 | return !ARCH->isSameThread(m_thread, thread.m_thread); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void* | 
|---|
| 134 | CThread::threadFunc(void* vjob) | 
|---|
| 135 | { | 
|---|
| 136 | // get this thread's id for logging | 
|---|
| 137 | IArchMultithread::ThreadID id; | 
|---|
| 138 | { | 
|---|
| 139 | CArchThread thread = ARCH->newCurrentThread(); | 
|---|
| 140 | id = ARCH->getIDOfThread(thread); | 
|---|
| 141 | ARCH->closeThread(thread); | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | // get job | 
|---|
| 145 | IJob* job = reinterpret_cast<IJob*>(vjob); | 
|---|
| 146 |  | 
|---|
| 147 | // run job | 
|---|
| 148 | void* result = NULL; | 
|---|
| 149 | try { | 
|---|
| 150 | // go | 
|---|
| 151 | LOG((CLOG_DEBUG1 "thread 0x%08x entry", id)); | 
|---|
| 152 | job->run(); | 
|---|
| 153 | LOG((CLOG_DEBUG1 "thread 0x%08x exit", id)); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | catch (XThreadCancel&) { | 
|---|
| 157 | // client called cancel() | 
|---|
| 158 | LOG((CLOG_DEBUG1 "caught cancel on thread 0x%08x", id)); | 
|---|
| 159 | delete job; | 
|---|
| 160 | throw; | 
|---|
| 161 | } | 
|---|
| 162 | catch (XThreadExit& e) { | 
|---|
| 163 | // client called exit() | 
|---|
| 164 | result = e.m_result; | 
|---|
| 165 | LOG((CLOG_DEBUG1 "caught exit on thread 0x%08x, result %p", id, result)); | 
|---|
| 166 | } | 
|---|
| 167 | catch (XBase& e) { | 
|---|
| 168 | LOG((CLOG_ERR "exception on thread 0x%08x: %s", id, e.what())); | 
|---|
| 169 | delete job; | 
|---|
| 170 | throw; | 
|---|
| 171 | } | 
|---|
| 172 | catch (...) { | 
|---|
| 173 | LOG((CLOG_ERR "exception on thread 0x%08x: <unknown>", id)); | 
|---|
| 174 | delete job; | 
|---|
| 175 | throw; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | // done with job | 
|---|
| 179 | delete job; | 
|---|
| 180 |  | 
|---|
| 181 | // return exit result | 
|---|
| 182 | return result; | 
|---|
| 183 | } | 
|---|