barrier/src/lib/net/SocketMultiplexer.h

112 lines
3.3 KiB
C
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
2016-09-07 14:24:00 +00:00
* Copyright (C) 2012-2016 Symless Ltd.
* Copyright (C) 2004 Chris Schoeneman
2012-06-10 16:50:54 +00:00
*
* 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 LICENSE that should have accompanied this file.
2012-06-10 16:50:54 +00:00
*
* 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/>.
*/
#pragma once
2012-06-10 16:50:54 +00:00
#include "arch/IArchNetwork.h"
#include "common/stdlist.h"
#include "common/stdmap.h"
2012-06-10 16:50:54 +00:00
template <class T>
2014-11-11 13:51:47 +00:00
class CondVar;
class Mutex;
class Thread;
2012-06-10 16:50:54 +00:00
class ISocket;
class ISocketMultiplexerJob;
//! Socket multiplexer
/*!
A socket multiplexer services multiple sockets simultaneously.
*/
2014-11-11 13:51:47 +00:00
class SocketMultiplexer {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
SocketMultiplexer();
~SocketMultiplexer();
2012-06-10 16:50:54 +00:00
//! @name manipulators
//@{
void addSocket(ISocket*, ISocketMultiplexerJob*);
void removeSocket(ISocket*);
//@}
//! @name accessors
//@{
// maybe belongs on ISocketMultiplexer
2014-11-11 13:51:47 +00:00
static SocketMultiplexer*
2012-06-10 16:50:54 +00:00
getInstance();
//@}
private:
// list of jobs. we use a list so we can safely iterate over it
// while other threads modify it.
2014-11-11 13:51:47 +00:00
typedef std::list<ISocketMultiplexerJob*> SocketJobs;
typedef SocketJobs::iterator JobCursor;
typedef std::map<ISocket*, JobCursor> SocketJobMap;
2012-06-10 16:50:54 +00:00
// service sockets. the service thread will only access m_sockets
// and m_update while m_pollable and m_polling are true. all other
// threads must only modify these when m_pollable and m_polling are
// false. only the service thread sets m_polling.
void serviceThread(void*);
// create, iterate, and destroy a cursor. a cursor is used to
// safely iterate through the job list while other threads modify
// the list. it works by inserting a dummy item in the list and
// moving that item through the list. the dummy item will never
// be removed by other edits so an iterator pointing at the item
// remains valid until we remove the dummy item in deleteCursor().
// nextCursor() finds the next non-dummy item, moves our dummy
// item just past it, and returns an iterator for the non-dummy
// item. all cursor calls lock the mutex for their duration.
2014-11-11 13:51:47 +00:00
JobCursor newCursor();
JobCursor nextCursor(JobCursor);
void deleteCursor(JobCursor);
2012-06-10 16:50:54 +00:00
// lock out locking the job list. this blocks if another thread
// has already locked out locking. once it returns, only the
// calling thread will be able to lock the job list after any
// current lock is released.
void lockJobListLock();
// lock the job list. this blocks if the job list is already
// locked. the calling thread must have called requestJobLock.
void lockJobList();
// unlock the job list and the lock out on locking.
void unlockJobList();
private:
2014-11-11 13:51:47 +00:00
Mutex* m_mutex;
2015-01-12 10:51:16 +00:00
Thread* m_thread;
2012-06-10 16:50:54 +00:00
bool m_update;
2014-11-11 13:51:47 +00:00
CondVar<bool>* m_jobsReady;
CondVar<bool>* m_jobListLock;
CondVar<bool>* m_jobListLockLocked;
2015-01-12 10:51:16 +00:00
Thread* m_jobListLocker;
Thread* m_jobListLockLocker;
2014-11-11 13:51:47 +00:00
SocketJobs m_socketJobs;
SocketJobMap m_socketJobMap;
2015-01-12 10:51:16 +00:00
ISocketMultiplexerJob*
m_cursorMark;
2012-06-10 16:50:54 +00:00
};