LCOV - code coverage report
Current view: top level - lunchbox - timedLock.cpp (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 29 37 78.4 %
Date: 2014-08-05 Functions: 9 10 90.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2005-2012, Stefan Eilemann <eile@equalizergraphics.com> 
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or modify it under
       5             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       6             :  * by the Free Software Foundation.
       7             :  *  
       8             :  * This library is distributed in the hope that it will be useful, but WITHOUT
       9             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      10             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      11             :  * details.
      12             :  * 
      13             :  * You should have received a copy of the GNU Lesser General Public License
      14             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      15             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      16             :  */
      17             : 
      18             : #include "timedLock.h"
      19             : 
      20             : #include "condition.h"
      21             : #include "debug.h"
      22             : 
      23             : namespace lunchbox
      24             : {
      25             : namespace detail
      26             : {
      27           5 : class TimedLock
      28             : {
      29             : public:
      30           5 :     TimedLock() : locked( false ) {}
      31             :     lunchbox::Condition condition;
      32             :     bool locked;
      33             : };
      34             : }
      35             : 
      36           5 : TimedLock::TimedLock()
      37           5 :         : _impl( new detail::TimedLock )
      38           5 : {}
      39             : 
      40           5 : TimedLock::~TimedLock()
      41             : {
      42           5 :     delete _impl;
      43           5 : }
      44             : 
      45    22813139 : bool TimedLock::set( const uint32_t timeout )
      46             : {
      47    22813139 :     _impl->condition.lock();
      48             : 
      49    22908799 :     bool acquired = true;
      50    45821731 :     while( _impl->locked )
      51             :     {
      52        4135 :         if( !_impl->condition.timedWait( timeout ))
      53             :         {
      54           2 :             acquired = false;
      55           2 :             break;
      56             :         }
      57             :     }
      58             : 
      59    22908799 :     if( acquired )
      60             :     {
      61    22908797 :         LBASSERT( !_impl->locked );
      62    22908797 :         _impl->locked = true;
      63             :     }
      64             : 
      65    22908799 :     _impl->condition.unlock();
      66    22908799 :     return acquired;
      67             : }
      68             : 
      69    22908792 : void TimedLock::unset()
      70             : {
      71    22908792 :     _impl->condition.lock();
      72    22908792 :     _impl->locked = false;
      73    22908792 :     _impl->condition.signal();
      74    22908792 :     _impl->condition.unlock();
      75    22908791 : }
      76             : 
      77             : 
      78           0 : bool TimedLock::trySet()
      79             : {
      80           0 :     _impl->condition.lock();
      81             :     
      82           0 :     bool acquired = false;
      83           0 :     if( _impl->locked )
      84             :     {
      85           0 :         _impl->locked  = true;
      86           0 :         acquired = true;
      87             :     }
      88             : 
      89           0 :     _impl->condition.unlock();
      90           0 :     return acquired;
      91             : }
      92             : 
      93    22908788 : bool TimedLock::isSet()
      94             : {
      95    22908788 :     return _impl->locked;
      96             : }
      97             : 
      98          87 : }

Generated by: LCOV version 1.10