Composants | |
| struct | PhilSemaphore |
| Descripteur du sémaphore. Plus de détails... | |
Définitions des types | |
| typedef PhilSemaphore | PhilSemaphore |
| Descripteur du sémaphore. | |
Fonctions | |
| int | PhilSemaphoreInit (PhilSemaphore *s, int i) |
| Initialise le descripteur de sémaphore. | |
| int | PhilSemaphoreDestroy (PhilSemaphore *s) |
| Détruit le sémaphore. | |
| int | PhilSemaphoreP (PhilSemaphore *s) |
| Prend un jeton. | |
| int | PhilSemaphoreV (PhilSemaphore *s) |
| Depose un jeton. | |
|
|
Un sémaphore est décrit par un compteur et une file d'attente. La valeur initiale du compteur doit être >= 0
Implantation Le compteur représente le nombre de jetons si le compteur est >=0 et le nombre de threads en attente s'il est <0. |
|
|
00031 {
00032 /* le semaphore existe et la valeur initiale est >=0 */
00033 if (s !=(PhilSemaphore *) 0){
00034 if(PhilMutexDestroy(&(s->lock)))return 1 ;
00035 if(PhilCondDestroy(&(s->queue)))return 1 ;
00036 return 0;
00037 }
00038 return 1;
00039 }
|
|
||||||||||||
|
00012 {
00013 /* le semaphore existe et la valeur initiale est >=0 */
00014 if ((s !=(PhilSemaphore *) 0)&&(i>=0)){
00015 s->counter = i;
00016 if(PhilMutexInit(&(s->lock)))return 1 ;
00017 if(PhilCondInit(&(s->queue)))return 1 ;
00018 return 0;
00019 }
00020 return 1;
00021 }
|
|
|
Implantation
00056 {
00057 if(!s)return 1;
00058 if(PhilMutexLock(&(s->lock)))return 1;/* début d'exclusion mutuelle */
00059 s->counter--;
00060 while(s->counter < 0){
00061 /* pas de jeton : on suspend le thread actif */
00062 if(PhilCondWait(&(s->queue),&(s->lock))){
00063 PhilMutexUnlock(&(s->lock));
00064 return 1 ;
00065 }
00066 }
00067 if(PhilMutexUnlock(&(s->lock)))return 1;/* fin d'exclusion mutuelle */
00068 return 0;
00069 }
|
|
|
Implantation
00084 {
00085 if(!s)return 1;
00086 if(PhilMutexLock(&(s->lock)))return 1;/* début d'exclusion mutuelle */
00087 s->counter++;
00088 if(s->counter <=0){
00089 /* il y a des demandeurs en attente */
00090 if(PhilCondSignal(&(s->queue)))return 1;
00091 }
00092 if(PhilMutexUnlock(&(s->lock)))return 1;/* fin d'exclusion mutuelle */
00093 return 0;
00094 }
|
1.2.17