/* ================================================================================ RPL/2 (R) version 4.0.14 Copyright (C) 1989-2010 Dr. BERTRAND Joël This file is part of RPL/2. RPL/2 is free software; you can redistribute it and/or modify it under the terms of the CeCILL V2 License as published by the french CEA, CNRS and INRIA. RPL/2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL V2 License for more details. You should have received a copy of the CeCILL License along with RPL/2. If not, write to info@cecill.info. ================================================================================ */ #ifdef SEMAPHORES_NOMMES #include "rpl.conv.h" /* ================================================================================ Fonctions d'émulation de sémaphores anonymes ================================================================================ Entrées : -------------------------------------------------------------------------------- Sorties : -------------------------------------------------------------------------------- Effets de bord : néant ================================================================================ */ sem_t * sem_init2(unsigned int valeur, enum t_semaphore semaphore) { snprintf(noms_semaphores[semaphore], LONGUEUR_NOM_SEMAPHORE, "/RPLSEM-%d-%llu-%d", (int) getpid(), (unsigned long long) pthread_self(), (int) semaphore); return(sem_open(noms_semaphores[semaphore], O_CREAT, (S_IRUSR | S_IWUSR), valeur)); } int sem_destroy2(sem_t *semaphore_p, enum t_semaphore semaphore) { sem_close(semaphore_p); return(sem_unlink(noms_semaphores[semaphore])); } #undef sem_post #undef sem_wait #undef sem_trywait int sem_getvalue2(sem_t *semaphore, int *valeur) { int i; logical1 drapeau_fin; pthread_mutex_lock(&mutex_sem); (*valeur) = 0; drapeau_fin = d_faux; do { if (sem_trywait(semaphore) == -1) { if (errno == EAGAIN) { // Le sémaphore avait une valeur nulle drapeau_fin = d_vrai; } else { // Autre erreur pthread_mutex_unlock(&mutex_sem); return(-1); } } else { (*valeur)++; } } while(drapeau_fin == d_faux); for(i = 0; i < (*valeur); i++) { if (sem_post(semaphore) != 0) { pthread_mutex_unlock(&mutex_sem); return(-1); } } pthread_mutex_unlock(&mutex_sem); return(0); } #endif // vim: ts=4