00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 }
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 }
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 }
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 }