Deskflow 1.24.0.365
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 <list>
11#include <map>
12
13template <class T> class CondVar;
14class Mutex;
15class Thread;
16class ISocket;
18
20
24{
25public:
30
33
35
36
38
39 void removeSocket(ISocket *);
40
42
44
45 // maybe belongs on ISocketMultiplexer
47
49
50private:
51 // list of jobs. we use a list so we can safely iterate over it
52 // while other threads modify it.
53 using SocketJobs = std::list<ISocketMultiplexerJob *>;
54 using JobCursor = SocketJobs::iterator;
55 using SocketJobMap = std::map<ISocket *, JobCursor>;
56
57 // service sockets. the service thread will only access m_sockets
58 // and m_update while m_pollable and m_polling are true. all other
59 // threads must only modify these when m_pollable and m_polling are
60 // false. only the service thread sets m_polling.
61 [[noreturn]] void serviceThread(const void *);
62
63 // create, iterate, and destroy a cursor. a cursor is used to
64 // safely iterate through the job list while other threads modify
65 // the list. it works by inserting a dummy item in the list and
66 // moving that item through the list. the dummy item will never
67 // be removed by other edits so an iterator pointing at the item
68 // remains valid until we remove the dummy item in deleteCursor().
69 // nextCursor() finds the next non-dummy item, moves our dummy
70 // item just past it, and returns an iterator for the non-dummy
71 // item. all cursor calls lock the mutex for their duration.
72 JobCursor newCursor();
73 JobCursor nextCursor(JobCursor);
74 void deleteCursor(JobCursor);
75
76 // lock out locking the job list. this blocks if another thread
77 // has already locked out locking. once it returns, only the
78 // calling thread will be able to lock the job list after any
79 // current lock is released.
80 void lockJobListLock();
81
82 // lock the job list. this blocks if the job list is already
83 // locked. the calling thread must have called requestJobLock.
84 void lockJobList();
85
86 // unlock the job list and the lock out on locking.
87 void unlockJobList();
88
89private:
90 Mutex *m_mutex = nullptr;
91 Thread *m_thread = nullptr;
92 bool m_update = false;
93 CondVar<bool> *m_jobsReady = nullptr;
94 CondVar<bool> *m_jobListLock = nullptr;
95 CondVar<bool> *m_jobListLockLocked = nullptr;
96 Thread *m_jobListLocker = nullptr;
97 Thread *m_jobListLockLocker = nullptr;
98
99 SocketJobs m_socketJobs = {};
100 SocketJobMap m_socketJobMap = {};
101 ISocketMultiplexerJob *m_cursorMark = nullptr;
102};
Condition variable.
Definition CondVar.h:121
Socket multiplexer job.
Definition ISocketMultiplexerJob.h:18
Generic socket interface.
Definition ISocket.h:19
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