LCOV - code coverage report
Current view: top level - threads/posix - mutex-posix.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 22 29 75.9 %
Date: 2015-06-10 18:10:59 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* src/threads/posix/mutex-posix.hpp - POSIX mutual exclusion functions
       2             : 
       3             :    Copyright (C) 2008
       4             :    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
       5             :    Copyright (C) 2008 Theobroma Systems Ltd.
       6             : 
       7             :    This file is part of CACAO.
       8             : 
       9             :    This program is free software; you can redistribute it and/or
      10             :    modify it under the terms of the GNU General Public License as
      11             :    published by the Free Software Foundation; either version 2, or (at
      12             :    your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful, but
      15             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :    General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program; if not, write to the Free Software
      21             :    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
      22             :    02110-1301, USA.
      23             : 
      24             : */
      25             : 
      26             : 
      27             : #ifndef _MUTEX_POSIX_HPP
      28             : #define _MUTEX_POSIX_HPP
      29             : 
      30             : #include "config.h"
      31             : 
      32             : #include <pthread.h>
      33             : 
      34             : /**
      35             :  * POSIX implementation of a mutex.
      36             :  */
      37             : class Mutex {
      38             : private:
      39             :         // POSIX mutex structure.
      40             :         pthread_mutex_t     _mutex;
      41             :         pthread_mutexattr_t _attr;
      42             : 
      43             :         // Condition class needs to access _mutex for wait() and
      44             :         // timedwait().
      45             :         friend class Condition;
      46             :         
      47             : public:
      48             :         inline Mutex();
      49             :         inline ~Mutex();
      50             : 
      51             :         inline void lock();
      52             :         inline void unlock();
      53             : };
      54             : 
      55             : // Includes.
      56             : #include "vm/os.hpp"
      57             : 
      58             : /**
      59             :  * Initializes the given mutex object and checks for errors.
      60             :  */
      61      645068 : inline Mutex::Mutex()
      62             : {
      63      645068 :         int result = pthread_mutexattr_init(&_attr);
      64             : 
      65      645068 :         if (result != 0) {
      66           0 :                 os::abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_init failed");
      67             :         }
      68             : 
      69      645068 :         result = pthread_mutexattr_settype(&_attr, PTHREAD_MUTEX_RECURSIVE);
      70             : 
      71      645068 :         if (result != 0) {
      72           0 :                 os::abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_settype failed");
      73             :         }
      74             : 
      75      645068 :         result = pthread_mutex_init(&_mutex, &_attr);
      76             : 
      77      645068 :         if (result != 0) {
      78           0 :                 os::abort_errnum(result, "Mutex::Mutex(): pthread_mutex_init failed");
      79             :         }
      80      645068 : }
      81             : 
      82             : 
      83             : /**
      84             :  * Destroys the given mutex object and checks for errors.
      85             :  */
      86         534 : inline Mutex::~Mutex()
      87             : {
      88         534 :         int result = pthread_mutexattr_destroy(&_attr);
      89             : 
      90         534 :         if (result != 0) {
      91           0 :                 os::abort_errnum(result, "Mutex::~Mutex(): pthread_mutexattr_destroy failed");
      92             :         }
      93             : 
      94         534 :         result = pthread_mutex_destroy(&_mutex);
      95             : 
      96         534 :         if (result != 0) {
      97           0 :                 os::abort_errnum(result, "Mutex::~Mutex(): pthread_mutex_destroy failed");
      98             :         }
      99         534 : }
     100             : 
     101             : 
     102             : /**
     103             :  * Locks the given mutex object and checks for errors. If the mutex is
     104             :  * already locked by another thread, the calling thread is suspended until
     105             :  * the mutex is unlocked.
     106             :  *
     107             :  * If the mutex is already locked by the calling thread, the same applies,
     108             :  * thus effectively causing the calling thread to deadlock. (This is because
     109             :  * we use "fast" pthread mutexes without error checking.)
     110             :  */
     111    11382200 : inline void Mutex::lock()
     112             : {
     113    11382200 :         int result = pthread_mutex_lock(&_mutex);
     114             : 
     115    11384215 :         if (result != 0) {
     116           0 :                 os::abort_errnum(result, "Mutex::lock(): pthread_mutex_lock failed");
     117             :         }
     118    11384215 : }
     119             : 
     120             : 
     121             : /**
     122             :  * Unlocks the given mutex object and checks for errors. The mutex is
     123             :  * assumed to be locked and owned by the calling thread.
     124             :  */
     125    11380769 : inline void Mutex::unlock()
     126             : {
     127    11380769 :         int result = pthread_mutex_unlock(&_mutex);
     128             : 
     129    11384243 :         if (result != 0) {
     130           0 :                 os::abort_errnum(result, "Mutex::unlock: pthread_mutex_unlock failed");
     131             :         }
     132    11384243 : }
     133             : 
     134             : #endif /* _MUTEX_POSIX_HPP */
     135             : 
     136             : 
     137             : /*
     138             :  * These are local overrides for various environment variables in Emacs.
     139             :  * Please do not remove this and leave it at the end of the file, where
     140             :  * Emacs will automagically detect them.
     141             :  * ---------------------------------------------------------------------
     142             :  * Local variables:
     143             :  * mode: c++
     144             :  * indent-tabs-mode: t
     145             :  * c-basic-offset: 4
     146             :  * tab-width: 4
     147             :  * End:
     148             :  * vim:noexpandtab:sw=4:ts=4:
     149             :  */

Generated by: LCOV version 1.11