mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-28 06:42:28 +08:00
moving 1.4 to trunk
This commit is contained in:
84
src/lib/mt/CCondVar.cpp
Normal file
84
src/lib/mt/CCondVar.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CCondVar.h"
|
||||
#include "CStopwatch.h"
|
||||
#include "CArch.h"
|
||||
|
||||
//
|
||||
// CCondVarBase
|
||||
//
|
||||
|
||||
CCondVarBase::CCondVarBase(CMutex* mutex) :
|
||||
m_mutex(mutex)
|
||||
{
|
||||
assert(m_mutex != NULL);
|
||||
m_cond = ARCH->newCondVar();
|
||||
}
|
||||
|
||||
CCondVarBase::~CCondVarBase()
|
||||
{
|
||||
ARCH->closeCondVar(m_cond);
|
||||
}
|
||||
|
||||
void
|
||||
CCondVarBase::lock() const
|
||||
{
|
||||
m_mutex->lock();
|
||||
}
|
||||
|
||||
void
|
||||
CCondVarBase::unlock() const
|
||||
{
|
||||
m_mutex->unlock();
|
||||
}
|
||||
|
||||
void
|
||||
CCondVarBase::signal()
|
||||
{
|
||||
ARCH->signalCondVar(m_cond);
|
||||
}
|
||||
|
||||
void
|
||||
CCondVarBase::broadcast()
|
||||
{
|
||||
ARCH->broadcastCondVar(m_cond);
|
||||
}
|
||||
|
||||
bool
|
||||
CCondVarBase::wait(CStopwatch& timer, double timeout) const
|
||||
{
|
||||
// check timeout against timer
|
||||
if (timeout >= 0.0) {
|
||||
timeout -= timer.getTime();
|
||||
if (timeout < 0.0)
|
||||
return false;
|
||||
}
|
||||
return wait(timeout);
|
||||
}
|
||||
|
||||
bool
|
||||
CCondVarBase::wait(double timeout) const
|
||||
{
|
||||
return ARCH->waitCondVar(m_cond, m_mutex->m_mutex, timeout);
|
||||
}
|
||||
|
||||
CMutex*
|
||||
CCondVarBase::getMutex() const
|
||||
{
|
||||
return m_mutex;
|
||||
}
|
||||
227
src/lib/mt/CCondVar.h
Normal file
227
src/lib/mt/CCondVar.h
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CCONDVAR_H
|
||||
#define CCONDVAR_H
|
||||
|
||||
#include "CMutex.h"
|
||||
#include "BasicTypes.h"
|
||||
|
||||
class CStopwatch;
|
||||
|
||||
//! Generic condition variable
|
||||
/*!
|
||||
This class provides functionality common to all condition variables
|
||||
but doesn't provide the actual variable storage. A condition variable
|
||||
is a multiprocessing primitive that can be waited on. Every condition
|
||||
variable has an associated mutex.
|
||||
*/
|
||||
class CCondVarBase {
|
||||
public:
|
||||
/*!
|
||||
\c mutex must not be NULL. All condition variables have an
|
||||
associated mutex. The mutex needn't be unique to one condition
|
||||
variable.
|
||||
*/
|
||||
CCondVarBase(CMutex* mutex);
|
||||
~CCondVarBase();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Lock the condition variable's mutex
|
||||
/*!
|
||||
Lock the condition variable's mutex. The condition variable should
|
||||
be locked before reading or writing it. It must be locked for a
|
||||
call to wait(). Locks are not recursive; locking a locked mutex
|
||||
will deadlock the thread.
|
||||
*/
|
||||
void lock() const;
|
||||
|
||||
//! Unlock the condition variable's mutex
|
||||
void unlock() const;
|
||||
|
||||
//! Signal the condition variable
|
||||
/*!
|
||||
Wake up one waiting thread, if there are any. Which thread gets
|
||||
woken is undefined.
|
||||
*/
|
||||
void signal();
|
||||
|
||||
//! Signal the condition variable
|
||||
/*!
|
||||
Wake up all waiting threads, if any.
|
||||
*/
|
||||
void broadcast();
|
||||
|
||||
//@}
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Wait on the condition variable
|
||||
/*!
|
||||
Wait on the condition variable. If \c timeout < 0 then wait until
|
||||
signalled, otherwise up to \c timeout seconds or until signalled,
|
||||
whichever comes first. Returns true if the object was signalled
|
||||
during the wait, false otherwise.
|
||||
|
||||
The proper way to wait for a condition is:
|
||||
\code
|
||||
cv.lock();
|
||||
while (cv-expr) {
|
||||
cv.wait();
|
||||
}
|
||||
cv.unlock();
|
||||
\endcode
|
||||
where \c cv-expr involves the value of \c cv and is false when the
|
||||
condition is satisfied.
|
||||
|
||||
(cancellation point)
|
||||
*/
|
||||
bool wait(double timeout = -1.0) const;
|
||||
|
||||
//! Wait on the condition variable
|
||||
/*!
|
||||
Same as \c wait(double) but use \c timer to compare against \c timeout.
|
||||
Since clients normally wait on condition variables in a loop, clients
|
||||
can use this to avoid recalculating \c timeout on each iteration.
|
||||
Passing a stopwatch with a negative \c timeout is pointless (it will
|
||||
never time out) but permitted.
|
||||
|
||||
(cancellation point)
|
||||
*/
|
||||
bool wait(CStopwatch& timer, double timeout) const;
|
||||
|
||||
//! Get the mutex
|
||||
/*!
|
||||
Get the mutex passed to the c'tor.
|
||||
*/
|
||||
CMutex* getMutex() const;
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
// not implemented
|
||||
CCondVarBase(const CCondVarBase&);
|
||||
CCondVarBase& operator=(const CCondVarBase&);
|
||||
|
||||
private:
|
||||
CMutex* m_mutex;
|
||||
CArchCond m_cond;
|
||||
};
|
||||
|
||||
//! Condition variable
|
||||
/*!
|
||||
A condition variable with storage for type \c T.
|
||||
*/
|
||||
template <class T>
|
||||
class CCondVar : public CCondVarBase {
|
||||
public:
|
||||
//! Initialize using \c value
|
||||
CCondVar(CMutex* mutex, const T& value);
|
||||
//! Initialize using another condition variable's value
|
||||
CCondVar(const CCondVar&);
|
||||
~CCondVar();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Assigns the value of \c cv to this
|
||||
/*!
|
||||
Set the variable's value. The condition variable should be locked
|
||||
before calling this method.
|
||||
*/
|
||||
CCondVar& operator=(const CCondVar& cv);
|
||||
|
||||
//! Assigns \c value to this
|
||||
/*!
|
||||
Set the variable's value. The condition variable should be locked
|
||||
before calling this method.
|
||||
*/
|
||||
CCondVar& operator=(const T& v);
|
||||
|
||||
//@}
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Get the variable's value
|
||||
/*!
|
||||
Get the variable's value. The condition variable should be locked
|
||||
before calling this method.
|
||||
*/
|
||||
operator const volatile T&() const;
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
volatile T m_data;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
CCondVar<T>::CCondVar(
|
||||
CMutex* mutex,
|
||||
const T& data) :
|
||||
CCondVarBase(mutex),
|
||||
m_data(data)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
CCondVar<T>::CCondVar(
|
||||
const CCondVar& cv) :
|
||||
CCondVarBase(cv.getMutex()),
|
||||
m_data(cv.m_data)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
CCondVar<T>::~CCondVar()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
CCondVar<T>&
|
||||
CCondVar<T>::operator=(const CCondVar<T>& cv)
|
||||
{
|
||||
m_data = cv.m_data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
CCondVar<T>&
|
||||
CCondVar<T>::operator=(const T& data)
|
||||
{
|
||||
m_data = data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
CCondVar<T>::operator const volatile T&() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
#endif
|
||||
41
src/lib/mt/CLock.cpp
Normal file
41
src/lib/mt/CLock.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CLock.h"
|
||||
#include "CCondVar.h"
|
||||
#include "CMutex.h"
|
||||
|
||||
//
|
||||
// CLock
|
||||
//
|
||||
|
||||
CLock::CLock(const CMutex* mutex) :
|
||||
m_mutex(mutex)
|
||||
{
|
||||
m_mutex->lock();
|
||||
}
|
||||
|
||||
CLock::CLock(const CCondVarBase* cv) :
|
||||
m_mutex(cv->getMutex())
|
||||
{
|
||||
m_mutex->lock();
|
||||
}
|
||||
|
||||
CLock::~CLock()
|
||||
{
|
||||
m_mutex->unlock();
|
||||
}
|
||||
51
src/lib/mt/CLock.h
Normal file
51
src/lib/mt/CLock.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CLOCK_H
|
||||
#define CLOCK_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
class CMutex;
|
||||
class CCondVarBase;
|
||||
|
||||
//! Mutual exclusion lock utility
|
||||
/*!
|
||||
This class locks a mutex or condition variable in the c'tor and unlocks
|
||||
it in the d'tor. It's easier and safer than manually locking and
|
||||
unlocking since unlocking must usually be done no matter how a function
|
||||
exits (including by unwinding due to an exception).
|
||||
*/
|
||||
class CLock {
|
||||
public:
|
||||
//! Lock the mutex \c mutex
|
||||
CLock(const CMutex* mutex);
|
||||
//! Lock the condition variable \c cv
|
||||
CLock(const CCondVarBase* cv);
|
||||
//! Unlock the mutex or condition variable
|
||||
~CLock();
|
||||
|
||||
private:
|
||||
// not implemented
|
||||
CLock(const CLock&);
|
||||
CLock& operator=(const CLock&);
|
||||
|
||||
private:
|
||||
const CMutex* m_mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
51
src/lib/mt/CMakeLists.txt
Normal file
51
src/lib/mt/CMakeLists.txt
Normal file
@@ -0,0 +1,51 @@
|
||||
# synergy -- mouse and keyboard sharing utility
|
||||
# Copyright (C) 2009 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
#
|
||||
# This package is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# found in the file COPYING that should have accompanied this file.
|
||||
#
|
||||
# This package is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
set(inc
|
||||
CCondVar.h
|
||||
CLock.h
|
||||
CMutex.h
|
||||
CThread.h
|
||||
XMT.h
|
||||
XThread.h
|
||||
)
|
||||
|
||||
set(src
|
||||
CCondVar.cpp
|
||||
CLock.cpp
|
||||
CMutex.cpp
|
||||
CThread.cpp
|
||||
XMT.cpp
|
||||
)
|
||||
|
||||
if (WIN32)
|
||||
list(APPEND src ${inc})
|
||||
endif()
|
||||
|
||||
set(inc
|
||||
../arch
|
||||
../base
|
||||
../common
|
||||
../synergy
|
||||
)
|
||||
|
||||
if (UNIX)
|
||||
list(APPEND inc
|
||||
../../..
|
||||
)
|
||||
endif()
|
||||
|
||||
include_directories(${inc})
|
||||
add_library(mt STATIC ${src})
|
||||
56
src/lib/mt/CMutex.cpp
Normal file
56
src/lib/mt/CMutex.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CMutex.h"
|
||||
#include "CArch.h"
|
||||
|
||||
//
|
||||
// CMutex
|
||||
//
|
||||
|
||||
CMutex::CMutex()
|
||||
{
|
||||
m_mutex = ARCH->newMutex();
|
||||
}
|
||||
|
||||
CMutex::CMutex(const CMutex&)
|
||||
{
|
||||
m_mutex = ARCH->newMutex();
|
||||
}
|
||||
|
||||
CMutex::~CMutex()
|
||||
{
|
||||
ARCH->closeMutex(m_mutex);
|
||||
}
|
||||
|
||||
CMutex&
|
||||
CMutex::operator=(const CMutex&)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::lock() const
|
||||
{
|
||||
ARCH->lockMutex(m_mutex);
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::unlock() const
|
||||
{
|
||||
ARCH->unlockMutex(m_mutex);
|
||||
}
|
||||
81
src/lib/mt/CMutex.h
Normal file
81
src/lib/mt/CMutex.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CMUTEX_H
|
||||
#define CMUTEX_H
|
||||
|
||||
#include "IArchMultithread.h"
|
||||
|
||||
//! Mutual exclusion
|
||||
/*!
|
||||
A non-recursive mutual exclusion object. Only one thread at a time can
|
||||
hold a lock on a mutex. Any thread that attempts to lock a locked mutex
|
||||
will block until the mutex is unlocked. At that time, if any threads are
|
||||
blocked, exactly one waiting thread will acquire the lock and continue
|
||||
running. A thread may not lock a mutex it already owns the lock on; if
|
||||
it tries it will deadlock itself.
|
||||
*/
|
||||
class CMutex {
|
||||
public:
|
||||
CMutex();
|
||||
//! Equivalent to default c'tor
|
||||
/*!
|
||||
Copy c'tor doesn't copy anything. It just makes it possible to
|
||||
copy objects that contain a mutex.
|
||||
*/
|
||||
CMutex(const CMutex&);
|
||||
~CMutex();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Does nothing
|
||||
/*!
|
||||
This does nothing. It just makes it possible to assign objects
|
||||
that contain a mutex.
|
||||
*/
|
||||
CMutex& operator=(const CMutex&);
|
||||
|
||||
//@}
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Lock the mutex
|
||||
/*!
|
||||
Locks the mutex, which must not have been previously locked by the
|
||||
calling thread. This blocks if the mutex is already locked by another
|
||||
thread.
|
||||
|
||||
(cancellation point)
|
||||
*/
|
||||
void lock() const;
|
||||
|
||||
//! Unlock the mutex
|
||||
/*!
|
||||
Unlocks the mutex, which must have been previously locked by the
|
||||
calling thread.
|
||||
*/
|
||||
void unlock() const;
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
friend class CCondVarBase;
|
||||
CArchMutex m_mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
186
src/lib/mt/CThread.cpp
Normal file
186
src/lib/mt/CThread.cpp
Normal file
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CThread.h"
|
||||
#include "XMT.h"
|
||||
#include "XThread.h"
|
||||
#include "CLog.h"
|
||||
#include "IJob.h"
|
||||
#include "CArch.h"
|
||||
|
||||
//
|
||||
// CThread
|
||||
//
|
||||
|
||||
CThread::CThread(IJob* job)
|
||||
{
|
||||
m_thread = ARCH->newThread(&CThread::threadFunc, job);
|
||||
if (m_thread == NULL) {
|
||||
// couldn't create thread
|
||||
delete job;
|
||||
throw XMTThreadUnavailable();
|
||||
}
|
||||
}
|
||||
|
||||
CThread::CThread(const CThread& thread)
|
||||
{
|
||||
m_thread = ARCH->copyThread(thread.m_thread);
|
||||
}
|
||||
|
||||
CThread::CThread(CArchThread adoptedThread)
|
||||
{
|
||||
m_thread = adoptedThread;
|
||||
}
|
||||
|
||||
CThread::~CThread()
|
||||
{
|
||||
ARCH->closeThread(m_thread);
|
||||
}
|
||||
|
||||
CThread&
|
||||
CThread::operator=(const CThread& thread)
|
||||
{
|
||||
// copy given thread and release ours
|
||||
CArchThread copy = ARCH->copyThread(thread.m_thread);
|
||||
ARCH->closeThread(m_thread);
|
||||
|
||||
// cut over
|
||||
m_thread = copy;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void
|
||||
CThread::exit(void* result)
|
||||
{
|
||||
throw XThreadExit(result);
|
||||
}
|
||||
|
||||
void
|
||||
CThread::cancel()
|
||||
{
|
||||
ARCH->cancelThread(m_thread);
|
||||
}
|
||||
|
||||
void
|
||||
CThread::setPriority(int n)
|
||||
{
|
||||
ARCH->setPriorityOfThread(m_thread, n);
|
||||
}
|
||||
|
||||
void
|
||||
CThread::unblockPollSocket()
|
||||
{
|
||||
ARCH->unblockPollSocket(m_thread);
|
||||
}
|
||||
|
||||
CThread
|
||||
CThread::getCurrentThread()
|
||||
{
|
||||
return CThread(ARCH->newCurrentThread());
|
||||
}
|
||||
|
||||
void
|
||||
CThread::testCancel()
|
||||
{
|
||||
ARCH->testCancelThread();
|
||||
}
|
||||
|
||||
bool
|
||||
CThread::wait(double timeout) const
|
||||
{
|
||||
return ARCH->wait(m_thread, timeout);
|
||||
}
|
||||
|
||||
void*
|
||||
CThread::getResult() const
|
||||
{
|
||||
if (wait())
|
||||
return ARCH->getResultOfThread(m_thread);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
IArchMultithread::ThreadID
|
||||
CThread::getID() const
|
||||
{
|
||||
return ARCH->getIDOfThread(m_thread);
|
||||
}
|
||||
|
||||
bool
|
||||
CThread::operator==(const CThread& thread) const
|
||||
{
|
||||
return ARCH->isSameThread(m_thread, thread.m_thread);
|
||||
}
|
||||
|
||||
bool
|
||||
CThread::operator!=(const CThread& thread) const
|
||||
{
|
||||
return !ARCH->isSameThread(m_thread, thread.m_thread);
|
||||
}
|
||||
|
||||
void*
|
||||
CThread::threadFunc(void* vjob)
|
||||
{
|
||||
// get this thread's id for logging
|
||||
IArchMultithread::ThreadID id;
|
||||
{
|
||||
CArchThread thread = ARCH->newCurrentThread();
|
||||
id = ARCH->getIDOfThread(thread);
|
||||
ARCH->closeThread(thread);
|
||||
}
|
||||
|
||||
// get job
|
||||
IJob* job = reinterpret_cast<IJob*>(vjob);
|
||||
|
||||
// run job
|
||||
void* result = NULL;
|
||||
try {
|
||||
// go
|
||||
LOG((CLOG_DEBUG1 "thread 0x%08x entry", id));
|
||||
job->run();
|
||||
LOG((CLOG_DEBUG1 "thread 0x%08x exit", id));
|
||||
}
|
||||
|
||||
catch (XThreadCancel&) {
|
||||
// client called cancel()
|
||||
LOG((CLOG_DEBUG1 "caught cancel on thread 0x%08x", id));
|
||||
delete job;
|
||||
throw;
|
||||
}
|
||||
catch (XThreadExit& e) {
|
||||
// client called exit()
|
||||
result = e.m_result;
|
||||
LOG((CLOG_DEBUG1 "caught exit on thread 0x%08x, result %p", id, result));
|
||||
}
|
||||
catch (XBase& e) {
|
||||
LOG((CLOG_ERR "exception on thread 0x%08x: %s", id, e.what()));
|
||||
delete job;
|
||||
throw;
|
||||
}
|
||||
catch (...) {
|
||||
LOG((CLOG_ERR "exception on thread 0x%08x: <unknown>", id));
|
||||
delete job;
|
||||
throw;
|
||||
}
|
||||
|
||||
// done with job
|
||||
delete job;
|
||||
|
||||
// return exit result
|
||||
return result;
|
||||
}
|
||||
212
src/lib/mt/CThread.h
Normal file
212
src/lib/mt/CThread.h
Normal file
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CTHREAD_H
|
||||
#define CTHREAD_H
|
||||
|
||||
#include "IArchMultithread.h"
|
||||
|
||||
class IJob;
|
||||
|
||||
//! Thread handle
|
||||
/*!
|
||||
Creating a CThread creates a new context of execution (i.e. thread) that
|
||||
runs simulatenously with the calling thread. A CThread is only a handle
|
||||
to a thread; deleting a CThread does not cancel or destroy the thread it
|
||||
refers to and multiple CThread objects can refer to the same thread.
|
||||
|
||||
Threads can terminate themselves but cannot be forced to terminate by
|
||||
other threads. However, other threads can signal a thread to terminate
|
||||
itself by cancelling it. And a thread can wait (block) on another thread
|
||||
to terminate.
|
||||
|
||||
Most functions that can block for an arbitrary time are cancellation
|
||||
points. A cancellation point is a function that can be interrupted by
|
||||
a request to cancel the thread. Cancellation points are noted in the
|
||||
documentation.
|
||||
*/
|
||||
// note -- do not derive from this class
|
||||
class CThread {
|
||||
public:
|
||||
//! Run \c adoptedJob in a new thread
|
||||
/*!
|
||||
Create and start a new thread executing the \c adoptedJob. The
|
||||
new thread takes ownership of \c adoptedJob and will delete it.
|
||||
*/
|
||||
CThread(IJob* adoptedJob);
|
||||
|
||||
//! Duplicate a thread handle
|
||||
/*!
|
||||
Make a new thread object that refers to an existing thread.
|
||||
This does \b not start a new thread.
|
||||
*/
|
||||
CThread(const CThread&);
|
||||
|
||||
//! Release a thread handle
|
||||
/*!
|
||||
Release a thread handle. This does not terminate the thread. A thread
|
||||
will keep running until the job completes or calls exit() or allows
|
||||
itself to be cancelled.
|
||||
*/
|
||||
~CThread();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Assign thread handle
|
||||
/*!
|
||||
Assign a thread handle. This has no effect on the threads, it simply
|
||||
makes this thread object refer to another thread. It does \b not
|
||||
start a new thread.
|
||||
*/
|
||||
CThread& operator=(const CThread&);
|
||||
|
||||
//! Terminate the calling thread
|
||||
/*!
|
||||
Terminate the calling thread. This function does not return but
|
||||
the stack is unwound and automatic objects are destroyed, as if
|
||||
exit() threw an exception (which is, in fact, what it does). The
|
||||
argument is saved as the result returned by getResult(). If you
|
||||
have \c catch(...) blocks then you should add the following before
|
||||
each to avoid catching the exit:
|
||||
\code
|
||||
catch(CThreadExit&) { throw; }
|
||||
\endcode
|
||||
or add the \c RETHROW_XTHREAD macro to the \c catch(...) block.
|
||||
*/
|
||||
static void exit(void*);
|
||||
|
||||
//! Cancel thread
|
||||
/*!
|
||||
Cancel the thread. cancel() never waits for the thread to
|
||||
terminate; it just posts the cancel and returns. A thread will
|
||||
terminate when it enters a cancellation point with cancellation
|
||||
enabled. If cancellation is disabled then the cancel is
|
||||
remembered but not acted on until the first call to a
|
||||
cancellation point after cancellation is enabled.
|
||||
|
||||
A cancellation point is a function that can act on cancellation.
|
||||
A cancellation point does not return if there's a cancel pending.
|
||||
Instead, it unwinds the stack and destroys automatic objects, as
|
||||
if cancel() threw an exception (which is, in fact, what it does).
|
||||
Threads must take care to unlock and clean up any resources they
|
||||
may have, especially mutexes. They can \c catch(XThreadCancel) to
|
||||
do that then rethrow the exception or they can let it happen
|
||||
automatically by doing clean up in the d'tors of automatic
|
||||
objects (like CLock). Clients are strongly encouraged to do the latter.
|
||||
During cancellation, further cancel() calls are ignored (i.e.
|
||||
a thread cannot be interrupted by a cancel during cancellation).
|
||||
|
||||
Clients that \c catch(XThreadCancel) must always rethrow the
|
||||
exception. Clients that \c catch(...) must either rethrow the
|
||||
exception or include a \c catch(XThreadCancel) handler that
|
||||
rethrows. The \c RETHROW_XTHREAD macro may be useful for that.
|
||||
*/
|
||||
void cancel();
|
||||
|
||||
//! Change thread priority
|
||||
/*!
|
||||
Change the priority of the thread. Normal priority is 0, 1 is
|
||||
the next lower, etc. -1 is the next higher, etc. but boosting
|
||||
the priority may not be permitted and will be silenty ignored.
|
||||
*/
|
||||
void setPriority(int n);
|
||||
|
||||
//! Force pollSocket() to return
|
||||
/*!
|
||||
Forces a currently blocked pollSocket() in the thread to return
|
||||
immediately.
|
||||
*/
|
||||
void unblockPollSocket();
|
||||
|
||||
//@}
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Get current thread's handle
|
||||
/*!
|
||||
Return a CThread object representing the calling thread.
|
||||
*/
|
||||
static CThread getCurrentThread();
|
||||
|
||||
//! Test for cancellation
|
||||
/*!
|
||||
testCancel() does nothing but is a cancellation point. Call
|
||||
this to make a function itself a cancellation point. If the
|
||||
thread was cancelled and cancellation is enabled this will
|
||||
cause the thread to unwind the stack and terminate.
|
||||
|
||||
(cancellation point)
|
||||
*/
|
||||
static void testCancel();
|
||||
|
||||
//! Wait for thread to terminate
|
||||
/*!
|
||||
Waits for the thread to terminate (by exit() or cancel() or
|
||||
by returning from the thread job) for up to \c timeout seconds,
|
||||
returning true if the thread terminated and false otherwise.
|
||||
This returns immediately with false if called by a thread on
|
||||
itself and immediately with true if the thread has already
|
||||
terminated. This will wait forever if \c timeout < 0.0.
|
||||
|
||||
(cancellation point)
|
||||
*/
|
||||
bool wait(double timeout = -1.0) const;
|
||||
|
||||
//! Get the exit result
|
||||
/*!
|
||||
Returns the exit result. This does an implicit wait(). It returns
|
||||
NULL immediately if called by a thread on itself or on a thread that
|
||||
was cancelled.
|
||||
|
||||
(cancellation point)
|
||||
*/
|
||||
void* getResult() const;
|
||||
|
||||
//! Get the thread id
|
||||
/*!
|
||||
Returns an integer id for this thread. This id must not be used to
|
||||
check if two CThread objects refer to the same thread. Use
|
||||
operator==() for that.
|
||||
*/
|
||||
IArchMultithread::ThreadID
|
||||
getID() const;
|
||||
|
||||
//! Compare thread handles
|
||||
/*!
|
||||
Returns true if two CThread objects refer to the same thread.
|
||||
*/
|
||||
bool operator==(const CThread&) const;
|
||||
|
||||
//! Compare thread handles
|
||||
/*!
|
||||
Returns true if two CThread objects do not refer to the same thread.
|
||||
*/
|
||||
bool operator!=(const CThread&) const;
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
CThread(CArchThread);
|
||||
|
||||
static void* threadFunc(void*);
|
||||
|
||||
private:
|
||||
CArchThread m_thread;
|
||||
};
|
||||
|
||||
#endif
|
||||
28
src/lib/mt/XMT.cpp
Normal file
28
src/lib/mt/XMT.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "XMT.h"
|
||||
|
||||
//
|
||||
// XMTThreadUnavailable
|
||||
//
|
||||
|
||||
CString
|
||||
XMTThreadUnavailable::getWhat() const throw()
|
||||
{
|
||||
return format("XMTThreadUnavailable", "cannot create thread");
|
||||
}
|
||||
32
src/lib/mt/XMT.h
Normal file
32
src/lib/mt/XMT.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMT_H
|
||||
#define XMT_H
|
||||
|
||||
#include "XBase.h"
|
||||
|
||||
//! Generic multithreading exception
|
||||
XBASE_SUBCLASS(XMT, XBase);
|
||||
|
||||
//! Thread creation exception
|
||||
/*!
|
||||
Thrown when a thread cannot be created.
|
||||
*/
|
||||
XBASE_SUBCLASS_WHAT(XMTThreadUnavailable, XMT);
|
||||
|
||||
#endif
|
||||
39
src/lib/mt/XThread.h
Normal file
39
src/lib/mt/XThread.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XTHREAD_H
|
||||
#define XTHREAD_H
|
||||
|
||||
#include "XArch.h"
|
||||
|
||||
//! Thread exception to exit
|
||||
/*!
|
||||
Thrown by CThread::exit() to exit a thread. Clients of CThread
|
||||
must not throw this type but must rethrow it if caught (by
|
||||
XThreadExit, XThread, or ...).
|
||||
*/
|
||||
class XThreadExit : public XThread {
|
||||
public:
|
||||
//! \c result is the result of the thread
|
||||
XThreadExit(void* result) : m_result(result) { }
|
||||
~XThreadExit() { }
|
||||
|
||||
public:
|
||||
void* m_result;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user