source: trunk/synergy/lib/mt/CThread.cpp@ 3232

Last change on this file since 3232 was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 3.2 KB
Line 
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
26CThread::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
36CThread::CThread(const CThread& thread)
37{
38 m_thread = ARCH->copyThread(thread.m_thread);
39}
40
41CThread::CThread(CArchThread adoptedThread)
42{
43 m_thread = adoptedThread;
44}
45
46CThread::~CThread()
47{
48 ARCH->closeThread(m_thread);
49}
50
51CThread&
52CThread::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
64void
65CThread::exit(void* result)
66{
67 throw XThreadExit(result);
68}
69
70void
71CThread::cancel()
72{
73 ARCH->cancelThread(m_thread);
74}
75
76void
77CThread::setPriority(int n)
78{
79 ARCH->setPriorityOfThread(m_thread, n);
80}
81
82void
83CThread::unblockPollSocket()
84{
85 ARCH->unblockPollSocket(m_thread);
86}
87
88CThread
89CThread::getCurrentThread()
90{
91 return CThread(ARCH->newCurrentThread());
92}
93
94void
95CThread::testCancel()
96{
97 ARCH->testCancelThread();
98}
99
100bool
101CThread::wait(double timeout) const
102{
103 return ARCH->wait(m_thread, timeout);
104}
105
106void*
107CThread::getResult() const
108{
109 if (wait())
110 return ARCH->getResultOfThread(m_thread);
111 else
112 return NULL;
113}
114
115IArchMultithread::ThreadID
116CThread::getID() const
117{
118 return ARCH->getIDOfThread(m_thread);
119}
120
121bool
122CThread::operator==(const CThread& thread) const
123{
124 return ARCH->isSameThread(m_thread, thread.m_thread);
125}
126
127bool
128CThread::operator!=(const CThread& thread) const
129{
130 return !ARCH->isSameThread(m_thread, thread.m_thread);
131}
132
133void*
134CThread::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}
Note: See TracBrowser for help on using the repository browser.