Line data Source code
1 :
2 : /* Copyright (c) 2010-2017, Stefan Eilemann <eile@equalizergraphics.com>
3 : * Daniel Nachbaur <danielnachbaur@gmail.com>
4 : *
5 : * This file is part of Collage <https://github.com/Eyescale/Collage>
6 : *
7 : * This library is free software; you can redistribute it and/or modify it under
8 : * the terms of the GNU Lesser General Public License version 2.1 as published
9 : * by the Free Software Foundation.
10 : *
11 : * This library is distributed in the hope that it will be useful, but WITHOUT
12 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 : * details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public License
17 : * along with this library; if not, write to the Free Software Foundation, Inc.,
18 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : */
20 :
21 : #ifndef CO_MASTERCM_H
22 : #define CO_MASTERCM_H
23 :
24 : #include "dataIStreamQueue.h" // member
25 : #include "objectCM.h" // base class
26 : #include <co/types.h>
27 :
28 : #include <lunchbox/mtQueue.h> // member
29 : #include <lunchbox/pool.h> // member
30 : #include <lunchbox/thread.h> // thread-safety check
31 :
32 : namespace co
33 : {
34 : /**
35 : * @internal
36 : * The base class for versioned master change managers.
37 : */
38 : class VersionedMasterCM : public ObjectCM
39 : {
40 : protected:
41 : typedef lunchbox::ScopedWrite Mutex;
42 :
43 : public:
44 : explicit VersionedMasterCM(Object* object);
45 : virtual ~VersionedMasterCM();
46 :
47 13 : void init() override {}
48 : /** @name Versioning */
49 : //@{
50 : uint128_t sync(const uint128_t& version) override;
51 :
52 0 : uint128_t getHeadVersion() const override
53 : {
54 0 : Mutex mutex(_slaves);
55 0 : return _version;
56 : }
57 197 : uint128_t getVersion() const override
58 : {
59 394 : Mutex mutex(_slaves);
60 394 : return _version;
61 : }
62 : //@}
63 :
64 143 : bool isMaster() const override { return true; }
65 0 : uint32_t getMasterInstanceID() const override
66 : {
67 0 : LBDONTCALL;
68 0 : return CO_INSTANCE_INVALID;
69 : }
70 :
71 : bool addSlave(const MasterCMCommand& command) override;
72 : void removeSlave(NodePtr node, const uint32_t instanceID) override;
73 : void removeSlaves(NodePtr node) override;
74 13 : const Nodes getSlaveNodes() const override
75 : {
76 26 : Mutex mutex(_slaves);
77 26 : return *_slaves;
78 : }
79 :
80 : protected:
81 : /** The list of subscribed slave nodes. */
82 : lunchbox::Lockable<Nodes> _slaves;
83 :
84 : /** The current version. */
85 : uint128_t _version;
86 :
87 : /** Maximum master version allowed to commit. */
88 : lunchbox::Monitor<uint64_t> _maxVersion;
89 :
90 : private:
91 302 : struct SlaveData
92 : {
93 69 : SlaveData()
94 138 : : maxVersion(std::numeric_limits<uint64_t>::max())
95 138 : , instanceID(LB_UNDEFINED_UINT32)
96 : {
97 69 : }
98 35 : bool operator==(const SlaveData& rhs) const
99 : {
100 35 : return node == rhs.node && instanceID == rhs.instanceID;
101 : }
102 :
103 : NodePtr node;
104 : uint64_t maxVersion;
105 : uint32_t instanceID;
106 : };
107 : typedef std::vector<SlaveData> SlaveDatas;
108 : typedef SlaveDatas::const_iterator SlaveDatasCIter;
109 : typedef SlaveDatas::iterator SlaveDatasIter;
110 :
111 : /** Additional slave data. */
112 : SlaveDatas _slaveData;
113 :
114 : /** Slave commit queue. */
115 : DataIStreamQueue _slaveCommits;
116 :
117 : uint128_t _apply(ObjectDataIStream* is);
118 : void _updateMaxVersion();
119 :
120 : /* The command handlers. */
121 : bool _cmdSlaveDelta(ICommand& command);
122 : bool _cmdMaxVersion(ICommand& command);
123 0 : bool _cmdDiscard(ICommand&) { return true; }
124 26 : LB_TS_VAR(_cmdThread);
125 26 : LB_TS_VAR(_rcvThread);
126 : };
127 : }
128 :
129 : #endif // CO_MASTERCM_H
|