00001 /* 00002 * This file is part of openMask © INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software 00003 * 00004 * The Software has been developped within the Siames Project. 00005 * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights 00006 * 00007 * The Software has been registered with the Agence pour la Protection des 00008 * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200 00009 * 00010 * This file may be distributed under the terms of the Q Public License 00011 * version 1.0 as defined by Trolltech AS of Norway and appearing in the file 00012 * LICENSE.QPL included in the packaging of this file. 00013 * 00014 * Licensees holding valid specific licenses issued by INRIA, CNRS or Université de Rennes 1 00015 * for the software may use this file in accordance with that specific license 00016 * 00017 */ 00018 #include <PsnSemaphore.h> 00019 00020 #ifdef _SPROC 00021 PsnSemaphore::PsnSemaphore(const int valeurInitiale) { 00022 if(sem_init(&semaphore,0, valeurInitiale)) { 00023 perror("PsnSemaphore : constructeur : impossible de creer le semaphore"); 00024 exit(0); 00025 } 00026 } 00027 00028 PsnSemaphore::~PsnSemaphore() { 00029 sem_destroy(&semaphore); 00030 } 00031 00032 int PsnSemaphore::V() { 00033 int ret; 00034 #ifdef _DEBUG 00035 cout <<"V "<<endl; 00036 #endif 00037 ret=sem_post(&semaphore); 00038 if(ret) { 00039 perror("PsnSemaphore : v() a échoué"); 00040 exit(0); 00041 } 00042 return(ret); 00043 } 00044 00045 int PsnSemaphore::P() { 00046 int ret; 00047 #ifdef _DEBUG 00048 cout <<"P "; 00049 #endif 00050 ret=sem_wait(&semaphore); 00051 if(ret) { 00052 perror("PsnSemaphore : P a echoue"); 00053 } 00054 #ifdef _DEBUG 00055 cout<<ret<<";"<<endl; 00056 #endif 00057 return (ret); 00058 } 00059 00060 #else 00061 PsnSemaphore::PsnSemaphore(const int valeurInitiale) { 00062 int ret; 00063 v=valeurInitiale; 00064 ret=pthread_mutex_init(&mutex,NULL); 00065 if(ret) { 00066 perror("PsnSemaphore : impossible d'initialiser le mutex"); 00067 } 00068 ret=pthread_cond_init(&cond,NULL); 00069 if(ret) { 00070 perror("PsnSemaphore : impossible d'initialiser le cond"); 00071 } 00072 } 00073 00074 PsnSemaphore::~PsnSemaphore() { 00075 pthread_mutex_destroy(&mutex); 00076 pthread_cond_destroy(&cond); 00077 } 00078 00079 int PsnSemaphore::V() { 00080 int retval,ret; 00081 ret=pthread_mutex_lock(&mutex); 00082 if(ret) { 00083 perror("PsnSemaphore : V a echoue au niveau du mutex lock"); 00084 } 00085 v++; 00086 retval=v; 00087 ret=pthread_mutex_unlock(&mutex); 00088 if(ret) { 00089 perror("PsnSemaphore : V a echoue au niveau du mutex unlock"); 00090 } 00091 ret=pthread_cond_signal(&cond); 00092 if(ret) { 00093 perror("PsnSemaphore : V a echoue au niveau du cond"); 00094 } 00095 return(retval); 00096 } 00097 00098 int PsnSemaphore::P() { 00099 int retval,ret; 00100 ret=pthread_mutex_lock(&mutex); 00101 if(ret) { 00102 perror("PsnSemaphore : P a echoue au niveau du mutex lock"); 00103 } 00104 v--; 00105 while(v<0) { 00106 ret=pthread_cond_wait(&cond,&mutex); 00107 if(ret) { 00108 perror("PsnSemaphore : P a echoue au niveau du cond"); 00109 } 00110 } 00111 retval=v; 00112 ret=pthread_mutex_unlock(&mutex); 00113 if(ret) { 00114 perror("PsnSemaphore : P a echoue au niveau du mutex unlock"); 00115 } 00116 return (retval); 00117 } 00118 #endif
| Documentation generated on Mon Nov 25 15:25:01 2002 |
Generated with doxygen 1.2.12 by Dimitri van Heesch , 1997-2001 |