aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/net/SocketMultiplexer.h
blob: 989155800e202d9bbe95c5b3c52f39854a94f907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2004 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 LICENSE 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/>.
 */

#pragma once

#include "arch/IArchNetwork.h"
#include "common/stdlist.h"
#include "common/stdmap.h"
#include <memory>

template <class T>
class CondVar;
class Mutex;
class Thread;
class ISocket;
class ISocketMultiplexerJob;

//! Socket multiplexer
/*!
A socket multiplexer services multiple sockets simultaneously.
*/
class SocketMultiplexer {
public:
    SocketMultiplexer();
    ~SocketMultiplexer();

    //! @name manipulators
    //@{

    void                addSocket(ISocket*, std::unique_ptr<ISocketMultiplexerJob>&& job);

    void                removeSocket(ISocket*);

    //@}
    //! @name accessors
    //@{

    // maybe belongs on ISocketMultiplexer
    static SocketMultiplexer*
                        getInstance();

    //@}

private:
    // list of jobs.  we use a list so we can safely iterate over it
    // while other threads modify it.
    using SocketJobs = std::list<std::unique_ptr<ISocketMultiplexerJob>>;
    typedef SocketJobs::iterator JobCursor;
    typedef std::map<ISocket*, JobCursor> SocketJobMap;

    // 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.
    JobCursor            newCursor();
    JobCursor            nextCursor(JobCursor);
    void                deleteCursor(JobCursor);

    // 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:
    Mutex*                m_mutex;
    Thread*                m_thread;
    bool                m_update;
    CondVar<bool>*        m_jobsReady;
    CondVar<bool>*        m_jobListLock;
    CondVar<bool>*        m_jobListLockLocked;
    Thread*                m_jobListLocker;
    Thread*                m_jobListLockLocker;

    SocketJobs            m_socketJobs;
    SocketJobMap        m_socketJobMap;
};