Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2004-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file MFXSynchSet.h 15 : /// @author Jakob Erdmann 16 : /// @date 2020-03-29 17 : /// 18 : // missing_desc 19 : /****************************************************************************/ 20 : #pragma once 21 : #include <config.h> 22 : 23 : #ifdef HAVE_FOX 24 : #include "fxheader.h" 25 : #endif 26 : #include <list> 27 : #include <cassert> 28 : #include <algorithm> 29 : 30 : //#define DEBUG_LOCKING 31 : 32 : #ifdef DEBUG_LOCKING 33 : #include <iostream> 34 : #include "MFXWorkerThread.h" 35 : #endif 36 : 37 : template<class T, class Container = std::set<T> > 38 34183 : class MFXSynchSet { 39 : public: 40 34650 : MFXSynchSet(const bool condition = true): 41 : #ifdef HAVE_FOX 42 34650 : myMutex(true), 43 : #endif 44 34650 : myCondition(condition) 45 : {} 46 : 47 : // Attention! Removes locking behavior 48 : void unsetCondition() { 49 : myCondition = false; 50 : } 51 : 52 : // Attention! Retains the lock 53 : Container& getContainer() { 54 : #ifdef HAVE_FOX 55 194101 : if (myCondition) { 56 55938 : myMutex.lock(); 57 : } 58 : #endif 59 : #ifdef DEBUG_LOCKING 60 : if (debugflag) { 61 : std::cout << " MFXSynchSet::getContainer thread=" << MFXWorkerThread::current() << "\n"; 62 : } 63 : myOwningThread = MFXWorkerThread::current(); 64 : #endif 65 : return myItems; 66 : } 67 : 68 : void unlock() { 69 : #ifdef HAVE_FOX 70 194101 : if (myCondition) { 71 55938 : myMutex.unlock(); 72 : } 73 : #endif 74 : #ifdef DEBUG_LOCKING 75 : if (debugflag) { 76 : std::cout << " MFXSynchSet::unlock thread=" << MFXWorkerThread::current() << "\n"; 77 : } 78 : myOwningThread = 0; 79 : #endif 80 : } 81 : 82 414344 : void insert(T what) { 83 : #ifdef HAVE_FOX 84 414344 : if (myCondition) { 85 111562 : myMutex.lock(); 86 : } 87 : #endif 88 : myItems.insert(what); 89 : #ifdef HAVE_FOX 90 414344 : if (myCondition) { 91 111562 : myMutex.unlock(); 92 : } 93 : #endif 94 414344 : } 95 : 96 : bool empty() { 97 : #ifdef HAVE_FOX 98 : if (myCondition) { 99 : myMutex.lock(); 100 : } 101 : #endif 102 : const bool ret = myItems.size() == 0; 103 : #ifdef HAVE_FOX 104 : if (myCondition) { 105 : myMutex.unlock(); 106 : } 107 : #endif 108 : return ret; 109 : } 110 : 111 194101 : void clear() { 112 : #ifdef HAVE_FOX 113 194101 : if (myCondition) { 114 55938 : myMutex.lock(); 115 : } 116 : #endif 117 : myItems.clear(); 118 : #ifdef HAVE_FOX 119 194101 : if (myCondition) { 120 55938 : myMutex.unlock(); 121 : } 122 : #endif 123 194101 : } 124 : 125 257726997 : size_t size() const { 126 : #ifdef HAVE_FOX 127 257726997 : if (myCondition) { 128 13229547 : myMutex.lock(); 129 : } 130 : #endif 131 : size_t res = myItems.size(); 132 : #ifdef HAVE_FOX 133 257726997 : if (myCondition) { 134 13229547 : myMutex.unlock(); 135 : } 136 : #endif 137 257726997 : return res; 138 : } 139 : 140 : bool contains(const T& item) const { 141 : #ifdef HAVE_FOX 142 : if (myCondition) { 143 : myMutex.lock(); 144 : } 145 : #endif 146 : bool res = std::find(myItems.begin(), myItems.end(), item) != myItems.end(); 147 : #ifdef HAVE_FOX 148 : if (myCondition) { 149 : myMutex.unlock(); 150 : } 151 : #endif 152 : return res; 153 : } 154 : 155 : bool isLocked() const { 156 : #ifdef HAVE_FOX 157 : return myMutex.locked(); 158 : #else 159 : return false; 160 : #endif 161 : } 162 : 163 : private: 164 : #ifdef HAVE_FOX 165 : mutable FXMutex myMutex; 166 : #endif 167 : Container myItems; 168 : bool myCondition; 169 : 170 : #ifdef DEBUG_LOCKING 171 : mutable long long int myOwningThread = 0; 172 : public: 173 : mutable bool debugflag = false; 174 : #endif 175 : 176 : };