LCOV - code coverage report
Current view: top level - lunchbox - condition.cpp (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 44 58 75.9 %
Date: 2014-10-01 Functions: 10 10 100.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2010-2012, Stefan Eilemann <eile@eyescale.ch> 
       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 "condition.h"
      19             : #include "debug.h"
      20             : #include "time.h"
      21             : 
      22             : #include <cstring>
      23             : #include <errno.h>
      24             : 
      25             : #ifdef _WIN32
      26             : #  include "condition_w32.ipp"
      27             : #else
      28             : #  include <pthread.h>
      29             : #  include <sys/time.h>
      30             : #endif
      31             : 
      32             : namespace lunchbox
      33             : {
      34             : namespace detail
      35             : {
      36             : class Condition
      37             : {
      38             : public:
      39             :     pthread_mutex_t mutex;
      40             :     pthread_cond_t  cond;
      41             : };
      42             : }
      43             : 
      44        4191 : Condition::Condition()
      45        4191 :         : _impl( new detail::Condition )
      46             : {
      47             :     // mutex init
      48        4191 :     int error = pthread_mutex_init( &_impl->mutex, 0 );
      49        4191 :     if( error )
      50             :     {
      51           0 :         LBERROR << "Error creating pthread mutex: " << strerror( error )
      52           0 :                 << std::endl;
      53           0 :         return;
      54             :     }
      55             : 
      56             :     // condvar init
      57        4191 :     error = pthread_cond_init( &_impl->cond, 0 );
      58        4191 :     if( error )
      59             :     {
      60           0 :         LBERROR << "Error creating pthread condition: " << strerror( error )
      61           0 :                 << std::endl;
      62           0 :         return;
      63             :     }
      64             : }
      65             : 
      66        4191 : Condition::~Condition()
      67             : {
      68        4191 :     int error = pthread_mutex_destroy( &_impl->mutex );
      69        4191 :     if( error )
      70           0 :         LBERROR << "Error destroying pthread mutex: " << strerror( error )
      71           0 :                 << std::endl;
      72             : 
      73        4191 :     error = pthread_cond_destroy( &_impl->cond );
      74        4191 :     if( error )
      75           0 :         LBERROR << "Error destroying pthread condition: " << strerror( error )
      76           0 :                 << std::endl;
      77             : 
      78        4191 :     delete _impl;
      79        4191 : }
      80             : 
      81    19980519 : void Condition::lock()
      82             : {
      83    19980519 :     pthread_mutex_lock( &_impl->mutex );
      84    19991076 : }
      85             : 
      86     9329178 : void Condition::signal()
      87             : {
      88     9329178 :     pthread_cond_signal( &_impl->cond );
      89     9329178 : }
      90             : 
      91      817872 : void Condition::broadcast()
      92             : {
      93      817872 :     pthread_cond_broadcast( &_impl->cond );
      94      817881 : }
      95             : 
      96    19991065 : void Condition::unlock()
      97             : {
      98    19991065 :     pthread_mutex_unlock( &_impl->mutex );
      99    19987538 : }
     100             : 
     101      973857 : void Condition::wait()
     102             : {
     103      973857 :     pthread_cond_wait( &_impl->cond, &_impl->mutex );
     104      973856 : }
     105             : 
     106      439495 : bool Condition::timedWait( const uint32_t timeout )
     107             : {
     108      439495 :     if( timeout == LB_TIMEOUT_INDEFINITE )
     109             :     {
     110      439462 :         wait();
     111      439462 :         return true;
     112             :     }
     113             : 
     114             :     const uint32_t time = timeout == LB_TIMEOUT_DEFAULT ?
     115          33 :         300000 /* 5 min */ : timeout;
     116             : 
     117             : #ifdef _WIN32
     118             :     int error = pthread_cond_timedwait_w32_np( &_impl->cond, &_impl->mutex,
     119             :                                                time );
     120             : #else
     121          33 :     const timespec delta = convertToTimespec( time );
     122             :     timeval now;
     123          33 :     gettimeofday( &now, 0 );
     124             : 
     125             :     timespec then;
     126          33 :     then.tv_sec  = delta.tv_sec + now.tv_sec;
     127          33 :     then.tv_nsec = delta.tv_nsec + now.tv_usec * 1000;
     128          70 :     while( then.tv_nsec > 1000000000 )
     129             :     {
     130           4 :         ++then.tv_sec;
     131           4 :         then.tv_nsec -= 1000000000;
     132             :     }
     133             : 
     134          33 :     int error = pthread_cond_timedwait( &_impl->cond, &_impl->mutex, &then );
     135             : #endif
     136          33 :     if( error == ETIMEDOUT )
     137          33 :         return false;
     138             : 
     139           0 :     if( error )
     140           0 :         LBERROR << "pthread_cond_timedwait failed: " << strerror( error )
     141           0 :                 << std::endl;
     142           0 :     return true;
     143             : }
     144             : 
     145          90 : }

Generated by: LCOV version 1.10