LCOV - code coverage report
Current view: top level - stdcasa/thread - Barrier.h (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 7 0.0 %
Date: 2024-10-09 13:55:54 Functions: 0 3 0.0 %

          Line data    Source code
       1             : // -*- C++ -*-
       2             : //# ThreadCoordinator.h: Definition of the ThreadCoordinator class
       3             : //# Copyright (C) 2015
       4             : //# Associated Universities, Inc. Washington DC, USA.
       5             : //#
       6             : //# This library is free software; you can redistribute it and/or modify it
       7             : //# under the terms of the GNU Library General Public License as published by
       8             : //# the Free Software Foundation; either version 2 of the License, or (at your
       9             : //# option) any later version.
      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
      13             : //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
      14             : //# License for more details.
      15             : //#
      16             : //# You should have received a copy of the GNU Library General Public License
      17             : //# along with this library; if not, write to the Free Software Foundation,
      18             : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
      19             : //#
      20             : //# Correspondence concerning AIPS++ should be addressed as follows:
      21             : //#        Internet email: casa-feedback@nrao.edu.
      22             : //#        Postal address: AIPS++ Project Office
      23             : //#                        National Radio Astronomy Observatory
      24             : //#                        520 Edgemont Road
      25             : //#                        Charlottesville, VA 22903-2475 USA
      26             : //#
      27             : //# $Id$
      28             : #ifndef STDCASA_THREAD_BARRIER_H_
      29             : #define STDCASA_THREAD_BARRIER_H_
      30             : #include <mutex>
      31             : #include <condition_variable>
      32             : 
      33             : namespace casa {
      34             :     class Barrier {
      35             :     private:
      36             :         std::mutex mutex_;
      37             :         std::condition_variable cvar_;
      38             :         std::size_t count_;
      39             :     public:
      40           0 :         explicit Barrier(std::size_t count) : count_{count} { }
      41           0 :         void wait( ) {
      42           0 :             std::unique_lock<std::mutex> lock(mutex_);
      43           0 :             if (--count_ == 0) {
      44           0 :                 cvar_.notify_all();
      45             :             } else {
      46           0 :                 cvar_.wait(lock, [this] { return count_ == 0; });
      47             :             }
      48           0 :         }
      49             :     };
      50             : }
      51             : 
      52             : #if 0
      53             : //
      54             : // could switch to this (from Jim) which allows for sequential applications of the barrier...
      55             : // after we switch to C++11 threads (from boost threads)...
      56             : //
      57             : class Barrier {
      58             : public:
      59             :     explicit Barrier(std::size_t iCount) : 
      60             :         mThreshold(iCount), 
      61             :         mCount(iCount), 
      62             :         mGeneration(0) {
      63             :     }
      64             : 
      65             :     void Wait() {
      66             :         auto lGen = mGeneration;
      67             :         std::unique_lock<std::mutex> lLock{mMutex};
      68             :         if (!--mCount) {
      69             :             mGeneration++;
      70             :             mCount = mThreshold;
      71             :             mCond.notify_all();
      72             :         } else {
      73             :             mCond.wait(lLock, [this, lGen] { return lGen != mGeneration; });
      74             :         }
      75             :     }
      76             : 
      77             : private:
      78             :     std::mutex mMutex;
      79             :     std::condition_variable mCond;
      80             :     std::size_t mThreshold;
      81             :     std::size_t mCount;
      82             :     std::size_t mGeneration;
      83             : };
      84             : #endif
      85             : 
      86             : #endif

Generated by: LCOV version 1.16