LCOV - code coverage report
Current view: top level - src/utils/foxtools - MFXSynchQue.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 51 51 100.0 %
Date: 2024-04-27 15:34:54 Functions: 12 12 100.0 %

          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    MFXSynchQue.h
      15             : /// @author  Daniel Krajzewicz
      16             : /// @author  Michael Behrisch
      17             : /// @date    2004-03-19
      18             : ///
      19             : // missing_desc
      20             : /****************************************************************************/
      21             : #pragma once
      22             : #include <config.h>
      23             : 
      24             : #ifdef HAVE_FOX
      25             : #include "fxheader.h"
      26             : #endif
      27             : #include <list>
      28             : #include <cassert>
      29             : #include <algorithm>
      30             : 
      31             : //#define DEBUG_LOCKING
      32             : 
      33             : #ifdef DEBUG_LOCKING
      34             : #include <iostream>
      35             : #include "MFXWorkerThread.h"
      36             : #endif
      37             : 
      38             : template<class T, class Container = std::list<T> >
      39             : class MFXSynchQue {
      40             : public:
      41     1823831 :     MFXSynchQue(const bool condition = true):
      42             : #ifdef HAVE_FOX
      43     1823831 :         myMutex(true),
      44             : #endif
      45     1823831 :         myCondition(condition)
      46             :     {}
      47             : 
      48     5403332 :     T top() {
      49             :         assert(myItems.size() != 0);
      50             : #ifdef HAVE_FOX
      51     5403332 :         if (myCondition) {
      52     5403332 :             myMutex.lock();
      53             :         }
      54             : #endif
      55     5403332 :         T ret = myItems.front();
      56             : #ifdef HAVE_FOX
      57     5403332 :         if (myCondition) {
      58     5403332 :             myMutex.unlock();
      59             :         }
      60             : #endif
      61     5403332 :         return ret;
      62             :     }
      63             : 
      64     5403332 :     void pop() {
      65             : #ifdef HAVE_FOX
      66     5403332 :         if (myCondition) {
      67     5403332 :             myMutex.lock();
      68             :         }
      69             : #endif
      70     5403332 :         myItems.erase(myItems.begin());
      71             : #ifdef HAVE_FOX
      72     5403332 :         if (myCondition) {
      73     5403332 :             myMutex.unlock();
      74             :         }
      75             : #endif
      76     5403332 :     }
      77             : 
      78             :     // Attention! Removes locking behavior
      79             :     void unsetCondition() {
      80     1536887 :         myCondition = false;
      81     1536887 :     }
      82             : 
      83             :     // Attention! Retains the lock
      84             :     Container& getContainer() {
      85             : #ifdef HAVE_FOX
      86   314273365 :         if (myCondition) {
      87    16691508 :             myMutex.lock();
      88             :         }
      89             : #endif
      90             : #ifdef DEBUG_LOCKING
      91             :         if (debugflag) {
      92             :             std::cout << " MFXSynchQue::getContainer thread=" << MFXWorkerThread::current() << "\n";
      93             :         }
      94             :         myOwningThread = MFXWorkerThread::current();
      95             : #endif
      96    78099447 :         return myItems;
      97             :     }
      98             : 
      99             :     void unlock() {
     100             : #ifdef HAVE_FOX
     101   314273365 :         if (myCondition) {
     102    16691508 :             myMutex.unlock();
     103             :         }
     104             : #endif
     105             : #ifdef DEBUG_LOCKING
     106             :         if (debugflag) {
     107             :             std::cout << " MFXSynchQue::unlock       thread=" << MFXWorkerThread::current() << "\n";
     108             :         }
     109             :         myOwningThread = 0;
     110             : #endif
     111             :     }
     112             : 
     113    59088488 :     void push_back(T what) {
     114             : #ifdef HAVE_FOX
     115    59088488 :         if (myCondition) {
     116    10840907 :             myMutex.lock();
     117             :         }
     118             : #endif
     119    59088488 :         myItems.push_back(what);
     120             : #ifdef HAVE_FOX
     121    59088488 :         if (myCondition) {
     122    10840907 :             myMutex.unlock();
     123             :         }
     124             : #endif
     125    59088488 :     }
     126             : 
     127     9816934 :     bool empty() {
     128             : #ifdef HAVE_FOX
     129     9816934 :         if (myCondition) {
     130     9816934 :             myMutex.lock();
     131             :         }
     132             : #endif
     133     9816934 :         const bool ret = myItems.size() == 0;
     134             : #ifdef HAVE_FOX
     135     9816934 :         if (myCondition) {
     136     9816934 :             myMutex.unlock();
     137             :         }
     138             : #endif
     139     9816934 :         return ret;
     140             :     }
     141             : 
     142    64464280 :     void clear() {
     143             : #ifdef HAVE_FOX
     144    64464280 :         if (myCondition) {
     145     3310998 :             myMutex.lock();
     146             :         }
     147             : #endif
     148             :         myItems.clear();
     149             : #ifdef HAVE_FOX
     150    64464280 :         if (myCondition) {
     151     3310998 :             myMutex.unlock();
     152             :         }
     153             : #endif
     154    64464280 :     }
     155             : 
     156             :     size_t size() const {
     157             : #ifdef HAVE_FOX
     158             :         if (myCondition) {
     159             :             myMutex.lock();
     160             :         }
     161             : #endif
     162             :         size_t res = myItems.size();
     163             : #ifdef HAVE_FOX
     164             :         if (myCondition) {
     165             :             myMutex.unlock();
     166             :         }
     167             : #endif
     168             :         return res;
     169             :     }
     170             : 
     171       17373 :     bool contains(const T& item) const {
     172             : #ifdef HAVE_FOX
     173       17373 :         if (myCondition) {
     174        3561 :             myMutex.lock();
     175             :         }
     176             : #endif
     177       17373 :         bool res = std::find(myItems.begin(), myItems.end(), item) != myItems.end();
     178             : #ifdef HAVE_FOX
     179       17373 :         if (myCondition) {
     180        3561 :             myMutex.unlock();
     181             :         }
     182             : #endif
     183       17373 :         return res;
     184             :     }
     185             : 
     186             :     bool isLocked() const {
     187             : #ifdef HAVE_FOX
     188             :         return myMutex.locked();
     189             : #else
     190             :         return false;
     191             : #endif
     192             :     }
     193             : 
     194             : private:
     195             : #ifdef HAVE_FOX
     196             :     mutable FXMutex myMutex;
     197             : #endif
     198             :     Container myItems;
     199             :     bool myCondition;
     200             : 
     201             : #ifdef DEBUG_LOCKING
     202             :     mutable long long int myOwningThread = 0;
     203             : public:
     204             :     mutable bool debugflag = false;
     205             : #endif
     206             : 
     207             : };

Generated by: LCOV version 1.14