LCOV - code coverage report
Current view: top level - framework/inc/threadhelp - gate.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 23 25 92.0 %
Date: 2015-06-13 12:38:46 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_FRAMEWORK_INC_THREADHELP_GATE_HXX
      21             : #define INCLUDED_FRAMEWORK_INC_THREADHELP_GATE_HXX
      22             : 
      23             : #include <boost/noncopyable.hpp>
      24             : #include <osl/time.h>
      25             : #include <osl/mutex.hxx>
      26             : #include <osl/conditn.hxx>
      27             : 
      28             : namespace framework{
      29             : 
      30             : /*-************************************************************************************************************
      31             :     @short          implement a gate to block multiple threads at same time or unblock all
      32             :     @descr          A gate can be used as a negative-condition! You can open a "door" - wait() will not block ...
      33             :                     or you can close it - wait() blocks till open() is called again.
      34             :                     Then all currently waiting threads are running immediately - but new ones are blocked!
      35             : 
      36             :     @attention      To prevent us against wrong using, the default ctor, copy ctor and the =operator are marked private!
      37             : 
      38             :     @devstatus      ready to use
      39             : *//*-*************************************************************************************************************/
      40             : class Gate : private boost::noncopyable
      41             : {
      42             : 
      43             :     //  public methods
      44             : 
      45             :     public:
      46             : 
      47             :         /*-****************************************************************************************************
      48             :             @short      ctor
      49             :             @descr      These initialize the object right as an open gate.
      50             :         *//*-*****************************************************************************************************/
      51        6988 :         inline Gate()
      52        6988 :             :   m_bClosed   ( false )
      53             :         {
      54        6988 :             open();
      55        6988 :         }
      56             : 
      57             :         /*-****************************************************************************************************
      58             :             @short      dtor
      59             :             @descr      Is user forget it - we open the gate ...
      60             :                         blocked threads can running ... but I don't know
      61             :                         if it's right - we are destroyed yet!?
      62             :         *//*-*****************************************************************************************************/
      63        6382 :         inline ~Gate()
      64        6382 :         {
      65        6382 :             open();
      66        6382 :         }
      67             : 
      68             :         /*-****************************************************************************************************
      69             :             @short      open the gate
      70             :             @descr      A wait() call will not block then.
      71             : 
      72             :             @seealso    method close()
      73             :         *//*-*****************************************************************************************************/
      74      742637 :         void open()
      75             :         {
      76             :             // We must safe access to our internal member!
      77      742637 :             ::osl::MutexGuard aLock( m_aAccessLock );
      78             :             // Set condition -> wait don't block any longer -> gate is open
      79      742637 :             m_aPassage.set();
      80             :             // Check if operation was successful!
      81             :             // Check returns false if condition isn't set => m_bClosed will be true then => we must return false; opening failed
      82      742637 :             m_bClosed = !m_aPassage.check();
      83      742637 :         }
      84             : 
      85             :         /*-****************************************************************************************************
      86             :             @short      close the gate
      87             :             @descr      A wait() call will block then.
      88             : 
      89             :             @seealso    method open()
      90             :         *//*-*****************************************************************************************************/
      91      722279 :         void close()
      92             :         {
      93             :             // We must safe access to our internal member!
      94      722279 :             ::osl::MutexGuard aLock( m_aAccessLock );
      95             :             // Reset condition -> wait blocks now -> gate is closed
      96      722279 :             m_aPassage.reset();
      97             :             // Check if operation was successful!
      98             :             // Check returns false if condition was reseted => m_bClosed will be true then => we can return true; closing ok
      99      722279 :             m_bClosed = !m_aPassage.check();
     100      722279 :         }
     101             : 
     102             :         /*-****************************************************************************************************
     103             :             @short      must be called to pass the gate
     104             :             @descr      If gate "open"   => wait() will not block.
     105             :                         If gate "closed" => wait() will block till somewhere open it again.
     106             : 
     107             :             @seealso    method wait()
     108             :             @seealso    method open()
     109             : 
     110             :             @param      "pTimeOut", optional parameter to wait a certain time
     111             :             @return     true, if wait was successful (gate was opened)
     112             :                         false, if condition has an error or timeout was reached!
     113             : 
     114             :             @onerror    We return false.
     115             :         *//*-*****************************************************************************************************/
     116        6966 :         bool wait(const TimeValue* pTimeOut = nullptr)
     117             :         {
     118             :             // We must safe access to our internal member!
     119        6966 :             ::osl::ClearableMutexGuard aLock( m_aAccessLock );
     120             :             // If gate not closed - caller can pass it.
     121        6966 :             bool bSuccessful = true;
     122        6966 :             if( m_bClosed )
     123             :             {
     124             :                 // Then we must release used access lock -
     125             :                 // because next call will block ...
     126             :                 // and if we hold the access lock nobody else can use this object without a dadlock!
     127           0 :                 aLock.clear();
     128             :                 // Wait for opening gate ...
     129           0 :                 bSuccessful = ( m_aPassage.wait( pTimeOut ) == ::osl::Condition::result_ok );
     130             :             }
     131             : 
     132        6966 :             return bSuccessful;
     133             :         }
     134             : 
     135             :     //  private member
     136             : 
     137             :     private:
     138             : 
     139             :         ::osl::Mutex        m_aAccessLock;
     140             :         ::osl::Condition    m_aPassage;
     141             :         bool                m_bClosed;
     142             : 
     143             : };      //  class Gate
     144             : 
     145             : }       //  namespace framework
     146             : 
     147             : #endif // INCLUDED_FRAMEWORK_INC_THREADHELP_GATE_HXX
     148             : 
     149             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11