LCOV - code coverage report
Current view: top level - threads/posix - condition-posix.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 32 84.4 %
Date: 2015-06-10 18:10:59 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /* src/threads/posix/condition-posix.hpp - POSIX condition variable
       2             : 
       3             :    Copyright (C) 2008
       4             :    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
       5             : 
       6             :    This file is part of CACAO.
       7             : 
       8             :    This program is free software; you can redistribute it and/or
       9             :    modify it under the terms of the GNU General Public License as
      10             :    published by the Free Software Foundation; either version 2, or (at
      11             :    your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful, but
      14             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :    General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program; if not, write to the Free Software
      20             :    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
      21             :    02110-1301, USA.
      22             : 
      23             : */
      24             : 
      25             : 
      26             : #ifndef _CONDITION_POSIX_HPP
      27             : #define _CONDITION_POSIX_HPP
      28             : 
      29             : #include "config.h"
      30             : 
      31             : #include <pthread.h>
      32             : #include <time.h>
      33             : #include "threads/mutex.hpp"
      34             : 
      35             : 
      36             : /**
      37             :  * POSIX condition variable.
      38             :  */
      39             : class Condition {
      40             : private:
      41             :         // POSIX condition structure.
      42             :         pthread_cond_t _cond;
      43             : 
      44             : public:
      45             :         Condition();
      46             :         ~Condition();
      47             : 
      48             :         void broadcast();
      49             :         void signal();
      50             :         void timedwait(Mutex* mutex, const struct timespec* abstime);
      51             :         void wait(Mutex* mutex);
      52             :         void wait(Mutex& mutex);
      53             : };
      54             : 
      55             : 
      56             : // Includes.
      57             : #include "vm/vm.hpp"
      58             : 
      59             : 
      60             : // Includes.
      61             : #include "vm/os.hpp"
      62             : 
      63             : 
      64             : /**
      65             :  * Initialize a POSIX condition variable.
      66             :  */
      67        3011 : inline Condition::Condition()
      68             : {
      69        3011 :         int result = pthread_cond_init(&_cond, NULL);
      70             : 
      71        3011 :         if (result != 0) {
      72           0 :                 os::abort_errnum(result, "Condition::Condition(): pthread_cond_init failed");
      73             :         }
      74        3011 : }
      75             : 
      76             : 
      77             : /**
      78             :  * Destroys a POSIX condition variable.
      79             :  */
      80         141 : inline Condition::~Condition()
      81             : {
      82             :         // Restart all threads waiting on this condition.
      83         141 :         broadcast();
      84             : 
      85         141 :         int result = pthread_cond_destroy(&_cond);
      86             : 
      87         141 :         if (result != 0) {
      88           0 :                 os::abort_errnum(result, "Condition::~Condition(): pthread_cond_destroy failed");
      89             :         }
      90         141 : }
      91             : 
      92             : 
      93             : /**
      94             :  * Restarts all the threads that are waiting on the condition
      95             :  * variable.
      96             :  */
      97         143 : inline void Condition::broadcast()
      98             : {
      99         143 :         int result = pthread_cond_broadcast(&_cond);
     100             : 
     101         143 :         if (result != 0) {
     102           0 :                 os::abort_errnum(result, "Condition::broadcast(): pthread_cond_broadcast failed");
     103             :         }
     104         143 : }
     105             : 
     106             : 
     107             : /**
     108             :  * Restarts one of the threads that are waiting on this condition
     109             :  * variable.
     110             :  */
     111       20622 : inline void Condition::signal()
     112             : {
     113       20622 :         int result = pthread_cond_signal(&_cond);
     114             : 
     115       20622 :         if (result != 0) {
     116           0 :                 os::abort_errnum(result, "Condition::signal(): pthread_cond_signal failed");
     117             :         }
     118       20622 : }
     119             : 
     120             : 
     121             : /**
     122             :  * Waits on the condition variable, as wait() does, but it also bounds
     123             :  * the duration of the wait.
     124             :  */
     125           4 : inline void Condition::timedwait(Mutex* mutex, const struct timespec* abstime)
     126             : {
     127             :         // This function can return return values which are valid.
     128           4 :         (void) pthread_cond_timedwait(&_cond, &(mutex->_mutex), abstime);
     129           4 : }
     130             : 
     131             : 
     132             : /**
     133             :  * Waits for the condition variable.
     134             :  */
     135         364 : inline void Condition::wait(Mutex* mutex)
     136             : {
     137         364 :         wait(*mutex);
     138         342 : }
     139             : 
     140             : 
     141             : /**
     142             :  * Waits for the condition variable.
     143             :  */
     144         527 : inline void Condition::wait(Mutex& mutex)
     145             : {
     146         527 :         int result = pthread_cond_wait(&_cond, &(mutex._mutex));
     147             : 
     148         342 :         if (result != 0) {
     149           0 :                 os::abort_errnum(result, "Condition::wait(): pthread_cond_wait failed");
     150             :         }
     151         342 : }
     152             : 
     153             : #endif /* _CONDITION_POSIX_HPP */
     154             : 
     155             : 
     156             : /*
     157             :  * These are local overrides for various environment variables in Emacs.
     158             :  * Please do not remove this and leave it at the end of the file, where
     159             :  * Emacs will automagically detect them.
     160             :  * ---------------------------------------------------------------------
     161             :  * Local variables:
     162             :  * mode: c++
     163             :  * indent-tabs-mode: t
     164             :  * c-basic-offset: 4
     165             :  * tab-width: 4
     166             :  * End:
     167             :  * vim:noexpandtab:sw=4:ts=4:
     168             :  */

Generated by: LCOV version 1.11