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_semaphore.h" 00038 00039 namespace ReChannel { 00040 00041 rc_semaphore::rc_semaphore(int value_) 00042 : rc_prim_channel(sc_gen_unique_name("semaphore")), 00043 m_value(value_), m_reset_value(value_) 00044 { 00045 if (m_value < 0) { 00046 RC_REPORT_ERROR(RC_ID_SEMAPHORE_INVALID_VALUE_, 00047 "negative initial value (in semaphore '" 00048 << this->name() << "')"); 00049 } 00050 } 00051 00052 rc_semaphore::rc_semaphore(const char* name_, int value_) 00053 : rc_prim_channel(name_), m_value(value_), m_reset_value(value_) 00054 { 00055 if (m_value < 0) { 00056 RC_REPORT_ERROR(RC_ID_SEMAPHORE_INVALID_VALUE_, 00057 "negative initial value (in semaphore '" 00058 << this->name() << "')"); 00059 } 00060 } 00061 00062 void rc_semaphore::rc_on_reset() 00063 { 00064 rc_prim_channel::rc_on_reset(); 00065 00066 // reset the semaphore value 00067 m_value = m_reset_value; 00068 00069 if (m_value > 0) { 00070 rc_notify(m_free); 00071 } 00072 } 00073 00074 int rc_semaphore::wait() 00075 { 00076 while(m_value <= 0) { 00077 rc_wait(m_free); 00078 } 00079 --m_value; 00080 return 0; 00081 } 00082 00083 int rc_semaphore::trywait() 00084 { 00085 if (m_value <= 0) { 00086 return -1; 00087 } 00088 --m_value; 00089 return 0; 00090 } 00091 00092 int rc_semaphore::post() 00093 { 00094 ++m_value; 00095 rc_notify(m_free); 00096 return 0; 00097 } 00098 00099 } // namespace ReChannel 00100 00101 // 00102 // $Id: rc_semaphore.cpp,v 1.3 2007/10/09 00:22:26 felke Exp $ 00103 // $Source: /var/cvs/projekte/ReChannel-v2/src/ReChannel/components/rc_semaphore.cpp,v $ 00104 // 00105