2003-01-04 22:01:32 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef IARCHMULTITHREAD_H
|
|
|
|
#define IARCHMULTITHREAD_H
|
|
|
|
|
|
|
|
#include "IInterface.h"
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
/*!
|
|
|
|
\class CArchCondImpl
|
|
|
|
\brief Internal condition variable data.
|
|
|
|
An architecture dependent type holding the necessary data for a
|
|
|
|
condition variable.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
class CArchCondImpl;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\var CArchCond
|
|
|
|
\brief Opaque condition variable type.
|
|
|
|
An opaque type representing a condition variable.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef CArchCondImpl* CArchCond;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\class CArchMutexImpl
|
|
|
|
\brief Internal mutex data.
|
|
|
|
An architecture dependent type holding the necessary data for a mutex.
|
|
|
|
*/
|
|
|
|
class CArchMutexImpl;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\var CArchMutex
|
|
|
|
\brief Opaque mutex type.
|
|
|
|
An opaque type representing a mutex.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef CArchMutexImpl* CArchMutex;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\class CArchThreadImpl
|
|
|
|
\brief Internal thread data.
|
|
|
|
An architecture dependent type holding the necessary data for a thread.
|
|
|
|
*/
|
|
|
|
class CArchThreadImpl;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\var CArchThread
|
|
|
|
\brief Opaque thread type.
|
|
|
|
An opaque type representing a thread.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef CArchThreadImpl* CArchThread;
|
|
|
|
|
|
|
|
//! Interface for architecture dependent multithreading
|
|
|
|
/*!
|
|
|
|
This interface defines the multithreading operations required by
|
|
|
|
synergy. Each architecture must implement this interface.
|
|
|
|
*/
|
|
|
|
class IArchMultithread : public IInterface {
|
|
|
|
public:
|
2003-03-12 22:34:07 +00:00
|
|
|
//! Result of waitForEvent()
|
|
|
|
enum EWaitResult {
|
|
|
|
kEvent, //!< An event is pending
|
|
|
|
kExit, //!< Thread exited
|
|
|
|
kTimeout //!< Wait timed out
|
|
|
|
};
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Type of thread entry point
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef void* (*ThreadFunc)(void*);
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Type of thread identifier
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef unsigned int ThreadID;
|
|
|
|
|
|
|
|
//! @name manipulators
|
|
|
|
//@{
|
|
|
|
|
|
|
|
//
|
|
|
|
// condition variable methods
|
|
|
|
//
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Create a condition variable
|
|
|
|
/*!
|
|
|
|
The condition variable is an opaque data type.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchCond newCondVar() = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Destroy a condition variable
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void closeCondVar(CArchCond) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Signal a condition variable
|
|
|
|
/*!
|
|
|
|
Signalling a condition variable releases one waiting thread.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void signalCondVar(CArchCond) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Broadcast a condition variable
|
|
|
|
/*!
|
|
|
|
Broadcasting a condition variable releases all waiting threads.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void broadcastCondVar(CArchCond) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Wait on a condition variable
|
|
|
|
/*!
|
2003-03-12 22:34:07 +00:00
|
|
|
Wait on a conditation variable for up to \c timeout seconds.
|
2003-01-05 21:48:54 +00:00
|
|
|
If \c timeout is < 0 then there is no timeout. The mutex must
|
|
|
|
be locked when this method is called. The mutex is unlocked
|
2003-03-12 22:34:07 +00:00
|
|
|
during the wait and locked again before returning. Returns
|
|
|
|
true if the condition variable was signalled and false on
|
|
|
|
timeout.
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
(Cancellation point)
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual bool waitCondVar(CArchCond, CArchMutex, double timeout) = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// mutex methods
|
|
|
|
//
|
|
|
|
|
2003-09-02 22:05:47 +00:00
|
|
|
//! Create a recursive mutex
|
2003-01-05 21:48:54 +00:00
|
|
|
/*!
|
2003-09-02 22:05:47 +00:00
|
|
|
Creates a recursive mutex. A thread may lock a recursive mutex
|
|
|
|
when it already holds a lock on that mutex. The mutex is an
|
|
|
|
opaque data type.
|
2003-01-05 21:48:54 +00:00
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchMutex newMutex() = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Destroy a mutex
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void closeMutex(CArchMutex) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Lock a mutex
|
|
|
|
/*!
|
|
|
|
(Cancellation point)
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void lockMutex(CArchMutex) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Unlock a mutex
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void unlockMutex(CArchMutex) = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// thread methods
|
|
|
|
//
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Start a new thread
|
|
|
|
/*!
|
|
|
|
Creates and starts a new thread, using \c func as the entry point
|
|
|
|
and passing it \c userData. The thread is an opaque data type.
|
|
|
|
*/
|
|
|
|
virtual CArchThread newThread(ThreadFunc func, void* userData) = 0;
|
|
|
|
|
|
|
|
//! Get a reference to the calling thread
|
|
|
|
/*!
|
|
|
|
Returns a thread representing the current (i.e. calling) thread.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchThread newCurrentThread() = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Copy a thread object
|
|
|
|
/*!
|
|
|
|
Returns a reference to to thread referred to by \c thread.
|
|
|
|
*/
|
|
|
|
virtual CArchThread copyThread(CArchThread thread) = 0;
|
|
|
|
|
|
|
|
//! Release a thread reference
|
|
|
|
/*!
|
|
|
|
Deletes the given thread object. This does not destroy the thread
|
|
|
|
the object referred to, even if there are no remaining references.
|
|
|
|
Use cancelThread() and waitThread() to stop a thread and wait for
|
|
|
|
it to exit.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void closeThread(CArchThread) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Force a thread to exit
|
|
|
|
/*!
|
|
|
|
Causes \c thread to exit when it next calls a cancellation point.
|
|
|
|
A thread avoids cancellation as long as it nevers calls a
|
|
|
|
cancellation point. Once it begins the cancellation process it
|
|
|
|
must always let cancellation go to completion but may take as
|
|
|
|
long as necessary to clean up.
|
|
|
|
*/
|
|
|
|
virtual void cancelThread(CArchThread thread) = 0;
|
|
|
|
|
|
|
|
//! Change thread priority
|
|
|
|
/*!
|
|
|
|
Changes the priority of \c thread by \c n. If \c n is positive
|
|
|
|
the thread has a lower priority and if negative a higher priority.
|
|
|
|
Some architectures may not support either or both directions.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void setPriorityOfThread(CArchThread, int n) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Cancellation point
|
|
|
|
/*!
|
|
|
|
This method does nothing but is a cancellation point. Clients
|
|
|
|
can make their own functions cancellation points by calling this
|
|
|
|
method at appropriate times.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void testCancelThread() = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Wait for a thread to exit
|
|
|
|
/*!
|
|
|
|
Waits for up to \c timeout seconds for \c thread to exit (normally
|
|
|
|
or by cancellation). Waits forever if \c timeout < 0. Returns
|
|
|
|
true if the thread exited, false otherwise. Waiting on the current
|
|
|
|
thread returns immediately with false.
|
|
|
|
|
|
|
|
(Cancellation point)
|
|
|
|
*/
|
|
|
|
virtual bool wait(CArchThread thread, double timeout) = 0;
|
|
|
|
|
|
|
|
//! Wait for a user event
|
|
|
|
/*!
|
2003-03-12 22:34:07 +00:00
|
|
|
Waits for up to \c timeout seconds for a pending user event or
|
|
|
|
\c thread to exit (normally or by cancellation). Waits forever
|
|
|
|
if \c timeout < 0. Returns kEvent if an event occurred, kExit
|
|
|
|
if \c thread exited, or kTimeout if the timeout expired. If
|
|
|
|
\c thread is NULL then it doesn't wait for any thread to exit
|
|
|
|
and it will not return kExit.
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
This method is not required by all platforms.
|
|
|
|
|
|
|
|
(Cancellation point)
|
|
|
|
*/
|
2003-03-12 22:34:07 +00:00
|
|
|
virtual EWaitResult waitForEvent(CArchThread thread, double timeout) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Compare threads
|
|
|
|
/*!
|
|
|
|
Returns true iff two thread objects refer to the same thread.
|
|
|
|
Note that comparing thread objects directly is meaningless.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual bool isSameThread(CArchThread, CArchThread) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Test if thread exited
|
|
|
|
/*!
|
|
|
|
Returns true iff \c thread has exited.
|
|
|
|
*/
|
|
|
|
virtual bool isExitedThread(CArchThread thread) = 0;
|
|
|
|
|
|
|
|
//! Returns the exit code of a thread
|
|
|
|
/*!
|
|
|
|
Waits indefinitely for \c thread to exit (if it hasn't yet) then
|
|
|
|
returns the thread's exit code.
|
|
|
|
|
|
|
|
(Cancellation point)
|
|
|
|
*/
|
|
|
|
virtual void* getResultOfThread(CArchThread thread) = 0;
|
|
|
|
|
|
|
|
//! Returns an ID for a thread
|
|
|
|
/*!
|
|
|
|
Returns some ID number for \c thread. This is for logging purposes.
|
|
|
|
All thread objects referring to the same thread return the same ID.
|
|
|
|
However, clients should us isSameThread() to compare thread objects
|
|
|
|
instead of comparing IDs.
|
|
|
|
*/
|
|
|
|
virtual ThreadID getIDOfThread(CArchThread thread) = 0;
|
2003-01-04 22:01:32 +00:00
|
|
|
|
|
|
|
//@}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|