Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_Semaphore.cpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include "Utils.hpp"
00021 #include "C_Semaphore.hpp"
00022 #include "SemaphoreImpl.h"
00023 
00024 #include "GeneratorTrace.hpp"
00025 
00026 
00027 C_Semaphore::C_Semaphore() {
00028   GEN_DEBUG (0, "C_Semaphore::C_Semaphore() start");
00029   ALLOC_VAR (m_impl, _pc_semaphore, sizeof(_c_semaphore)); 
00030   init () ;
00031   GEN_DEBUG (0, "C_Semaphore::C_Semaphore() end");
00032 }
00033 
00034 C_Semaphore::~C_Semaphore() {
00035   GEN_DEBUG(0,"C_Semaphore::~C_Semaphore() start");
00036   destroy () ;
00037   FREE_VAR (m_impl) ;
00038   GEN_DEBUG(0,"C_Semaphore::~C_Semaphore() end");
00039 }
00040 
00041 void C_Semaphore::init () {
00042   pthread_mutexattr_t L_mxAttr;
00043   pthread_condattr_t  L_cdAttr;
00044 
00045   SemCounter = COUNTER_INIT_VALUE ;
00046 
00047   pthread_mutexattr_init(&L_mxAttr);
00048   pthread_mutexattr_settype(&L_mxAttr, PTHREAD_MUTEX_NORMAL);
00049   pthread_mutex_init (SemMutex, &L_mxAttr) ;
00050 
00051   pthread_condattr_init(&L_cdAttr);
00052   pthread_cond_init (SemCond, &L_cdAttr) ;
00053 } // init ()
00054 
00055 void C_Semaphore::destroy () {
00056   GEN_DEBUG(0, "C_Semaphore::destroy() start");
00057   SemCounter = COUNTER_INIT_VALUE ;
00058   pthread_mutex_destroy (SemMutex) ;
00059   pthread_cond_destroy (SemCond) ;
00060   GEN_DEBUG(0, "C_Semaphore::destroy() end");
00061 } // destroy ()
00062 
00063 T_CounterValue C_Semaphore::V() {
00064 
00065   T_CounterValue L_value ;
00066 
00067   pthread_mutex_lock (SemMutex) ;
00068   SemCounter ++ ;
00069   L_value = SemCounter ;
00070   pthread_mutex_unlock (SemMutex);
00071   pthread_cond_signal (SemCond) ;
00072 
00073   return (L_value);
00074   
00075 } // V()
00076 
00077 T_CounterValue C_Semaphore::P() {
00078 
00079   T_CounterValue L_value = 0 ;
00080 
00081   pthread_mutex_lock (SemMutex) ;
00082   while (SemCounter <= COUNTER_COMP_VALUE) {
00083     pthread_cond_wait (SemCond, SemMutex);
00084   }
00085   SemCounter -- ;
00086   L_value = SemCounter ;
00087   pthread_mutex_unlock (SemMutex);
00088 
00089   return (L_value) ;
00090 
00091 } // P()

Generated on Wed Mar 7 14:44:58 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002