Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
SocketMultiplexer.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
4 * SPDX-FileCopyrightText: (C) 2004 Chris Schoeneman
5 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
6 */
7
8#pragma once
9
10#include "arch/IArchNetwork.h"
11
12#include <list>
13#include <map>
14
15template <class T> class CondVar;
16class Mutex;
17class Thread;
18class ISocket;
20
22
26{
27public:
32
35
37
38
40
41 void removeSocket(ISocket *);
42
44
46
47 // maybe belongs on ISocketMultiplexer
49
51
52private:
53 // list of jobs. we use a list so we can safely iterate over it
54 // while other threads modify it.
55 using SocketJobs = std::list<ISocketMultiplexerJob *>;
56 using JobCursor = SocketJobs::iterator;
57 using SocketJobMap = std::map<ISocket *, JobCursor>;
58
59 // service sockets. the service thread will only access m_sockets
60 // and m_update while m_pollable and m_polling are true. all other
61 // threads must only modify these when m_pollable and m_polling are
62 // false. only the service thread sets m_polling.
63 [[noreturn]] void serviceThread(void *);
64
65 // create, iterate, and destroy a cursor. a cursor is used to
66 // safely iterate through the job list while other threads modify
67 // the list. it works by inserting a dummy item in the list and
68 // moving that item through the list. the dummy item will never
69 // be removed by other edits so an iterator pointing at the item
70 // remains valid until we remove the dummy item in deleteCursor().
71 // nextCursor() finds the next non-dummy item, moves our dummy
72 // item just past it, and returns an iterator for the non-dummy
73 // item. all cursor calls lock the mutex for their duration.
74 JobCursor newCursor();
75 JobCursor nextCursor(JobCursor);
76 void deleteCursor(JobCursor);
77
78 // lock out locking the job list. this blocks if another thread
79 // has already locked out locking. once it returns, only the
80 // calling thread will be able to lock the job list after any
81 // current lock is released.
82 void lockJobListLock();
83
84 // lock the job list. this blocks if the job list is already
85 // locked. the calling thread must have called requestJobLock.
86 void lockJobList();
87
88 // unlock the job list and the lock out on locking.
89 void unlockJobList();
90
91private:
92 Mutex *m_mutex = nullptr;
93 Thread *m_thread = nullptr;
94 bool m_update = false;
95 CondVar<bool> *m_jobsReady = nullptr;
96 CondVar<bool> *m_jobListLock = nullptr;
97 CondVar<bool> *m_jobListLockLocked = nullptr;
98 Thread *m_jobListLocker = nullptr;
99 Thread *m_jobListLockLocker = nullptr;
100
101 SocketJobs m_socketJobs = {};
102 SocketJobMap m_socketJobMap = {};
103 ISocketMultiplexerJob *m_cursorMark = nullptr;
104};
Condition variable.
Definition CondVar.h:122
Socket multiplexer job.
Definition ISocketMultiplexerJob.h:18
Generic socket interface.
Definition ISocket.h:22
Mutual exclusion.
Definition Mutex.h:22
void addSocket(ISocket *, ISocketMultiplexerJob *)
Definition SocketMultiplexer.cpp:62
~SocketMultiplexer()
Definition SocketMultiplexer.cpp:43
SocketMultiplexer(SocketMultiplexer &&)=delete
SocketMultiplexer()
Definition SocketMultiplexer.cpp:26
SocketMultiplexer(SocketMultiplexer const &)=delete
static SocketMultiplexer * getInstance()
SocketMultiplexer & operator=(SocketMultiplexer const &)=delete
SocketMultiplexer & operator=(SocketMultiplexer &&)=delete
void removeSocket(ISocket *)
Definition SocketMultiplexer.cpp:96
Thread handle.
Definition Thread.h:33