rc_mutex_object.cpp

Go to the documentation of this file.
00001 // vim:set et sts=4 ts=4 tw=75 sw=4 ai ci cin cino=g0,t0:
00002 /*
00003  * Copyright (C) 2007, Technical Computer Science Group,
00004  *                     University of Bonn
00005  *
00006  * This file is part of the ReChannel library.
00007  *
00008  * The ReChannel library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License as
00010  * published by the Free Software Foundation; either version 2 of the
00011  * License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be
00014  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this library; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022  *
00023  * Authors: Andreas Raabe and Armin Felke. Implementation by Armin Felke.
00024  *          {raabe, felke}@cs.uni-bonn.de
00025  */
00037 #include "rc_mutex_object.h"
00038 
00039 namespace ReChannel {
00040 
00041 rc_mutex_object::rc_mutex_object()
00042     : m_lock_count(0)
00043 { }
00044 
00045 bool rc_mutex_object::trylock()
00046 {
00047     const sc_process_handle hproc = sc_get_current_process_handle();
00048     // is mutex unlocked?
00049     if (m_lock_count == 0 || this->is_lock_owner(hproc)) {
00050         // hproc now holds the lock
00051         m_lock_owner = hproc;
00052         m_lock_count++;
00053         return true;
00054     } else {
00055         return false;
00056     }
00057 }
00058 
00059 void rc_mutex_object::lock()
00060 {
00061     const sc_process_handle hproc = sc_get_current_process_handle();
00062     // if lock is already hold by hproc, skip the lock procedure
00063     while(!this->is_lock_owner(hproc)) {
00064         // is mutex unlocked?
00065         if (m_lock_count == 0) {
00066             // hproc now holds the lock
00067             m_lock_owner = hproc;
00068             break;
00069         }
00070         // wait for a lock release
00071         ::sc_core::wait(m_lock_release_event);
00072     }
00073     // lock is acquired
00074     m_lock_count++;
00075 }
00076 
00077 bool rc_mutex_object::lock(sc_time timeout)
00078 {
00079     const sc_time stoptime = sc_time_stamp() + timeout;
00080     const sc_process_handle hproc = sc_get_current_process_handle();
00081     // if lock is already hold by hproc, skip the lock procedure
00082     while(this->is_lock_owner(hproc)) {
00083         // is mutex unlocked?
00084         if (m_lock_count == 0) {
00085             // hproc now holds the lock
00086             m_lock_owner = hproc;
00087             break;
00088         }
00089         const sc_time curtime = sc_time_stamp();
00090         if (curtime >= stoptime) {
00091             return false;
00092         }
00093         // wait for a lock release or the remaining time
00094         ::sc_core::wait(stoptime - curtime, m_lock_release_event);
00095     }
00096     // lock is acquired
00097     m_lock_count++;
00098     return true;
00099 }
00100 
00101 bool rc_mutex_object::unlock()
00102 {
00103     if (m_lock_count == 0) {
00104         return false;
00105     }
00106     const sc_process_handle hproc = sc_get_current_process_handle();
00107     // unlock if at least one lock is hold by hproc
00108     if (this->is_lock_owner(hproc)) {
00109         m_lock_count--;
00110         if (m_lock_count == 0) {
00111             // release the lock
00112             m_lock_owner = sc_process_handle();
00113             m_lock_release_event.notify();
00114         }
00115         return true;
00116     } else {
00117         return false;
00118     }
00119 }
00120 
00121 bool rc_mutex_object::has_lock() const
00122 {
00123     const sc_process_handle hproc = sc_get_current_process_handle();
00124     return (m_lock_count > 0 && this->is_lock_owner(hproc));
00125 }
00126 
00127 }
00128 
00129 //
00130 // $Id: rc_mutex_object.cpp,v 1.7 2007/12/06 11:42:43 felke Exp $
00131 // $Source: /var/cvs/projekte/ReChannel-v2/src/ReChannel/util/rc_mutex_object.cpp,v $
00132 //

Generated on Tue Jan 1 23:13:38 2008 for ReChannel by  doxygen 1.5.3