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.h" 00038 00039 namespace ReChannel { 00040 00041 rc_mutex::rc_mutex() 00042 : rc_prim_channel(sc_gen_unique_name("mutex")) 00043 { } 00044 00045 rc_mutex::rc_mutex(const char* name_) 00046 : rc_prim_channel(name_) 00047 { } 00048 00049 void rc_mutex::rc_on_reset() 00050 { 00051 rc_prim_channel::rc_on_reset(); 00052 00053 // reset the mutex state 00054 m_lock_owner = sc_process_handle(); 00055 rc_notify(m_free); 00056 } 00057 00058 int rc_mutex::lock() 00059 { 00060 while(this->is_locked()) { 00061 rc_wait(m_free); 00062 } 00063 m_lock_owner = sc_get_current_process_handle(); 00064 return 0; 00065 } 00066 00067 int rc_mutex::trylock() 00068 { 00069 if (this->is_locked()) { 00070 return -1; 00071 } 00072 m_lock_owner = sc_get_current_process_handle(); 00073 return 0; 00074 } 00075 00076 int rc_mutex::unlock() 00077 { 00078 if (m_lock_owner != sc_get_current_process_handle()) { 00079 return -1; 00080 } 00081 m_lock_owner = sc_process_handle(); 00082 rc_notify(m_free); 00083 return 0; 00084 } 00085 00086 } // namespace ReChannel 00087 00088 // 00089 // $Id: rc_mutex.cpp,v 1.3 2007/10/09 00:22:26 felke Exp $ 00090 // $Source: /var/cvs/projekte/ReChannel-v2/src/ReChannel/components/rc_mutex.cpp,v $ 00091 // 00092