File:  [local] / rpl / src / semaphores.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Mon May 24 10:58:37 2010 UTC (13 years, 11 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_0_16, rpl-4_0_15, HEAD
En route pour la 4.0.16 !

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.0.16
    4:   Copyright (C) 1989-2010 Dr. BERTRAND Joël
    5: 
    6:   This file is part of RPL/2.
    7: 
    8:   RPL/2 is free software; you can redistribute it and/or modify it
    9:   under the terms of the CeCILL V2 License as published by the french
   10:   CEA, CNRS and INRIA.
   11:  
   12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
   13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
   15:   for more details.
   16:  
   17:   You should have received a copy of the CeCILL License
   18:   along with RPL/2. If not, write to info@cecill.info.
   19: ================================================================================
   20: */
   21: 
   22: 
   23: #ifdef SEMAPHORES_NOMMES
   24: #include "rpl.conv.h"
   25: 
   26: 
   27: /*
   28: ================================================================================
   29:   Fonctions d'émulation de sémaphores anonymes
   30: ================================================================================
   31:   Entrées :
   32: --------------------------------------------------------------------------------
   33:   Sorties :
   34: --------------------------------------------------------------------------------
   35:   Effets de bord : néant
   36: ================================================================================
   37: */
   38: 
   39: sem_t *
   40: sem_init2(unsigned int valeur, enum t_semaphore semaphore)
   41: {
   42:     snprintf(noms_semaphores[semaphore], LONGUEUR_NOM_SEMAPHORE,
   43:             "/RPLSEM-%d-%llu-%d", (int) getpid(),
   44:             (unsigned long long) pthread_self(),
   45:             (int) semaphore);
   46:     return(sem_open(noms_semaphores[semaphore], O_CREAT,
   47:             (S_IRUSR | S_IWUSR), valeur));
   48: }
   49: 
   50: int
   51: sem_destroy2(sem_t *semaphore_p, enum t_semaphore semaphore)
   52: {
   53:     sem_close(semaphore_p);
   54:     return(sem_unlink(noms_semaphores[semaphore]));
   55: }
   56: 
   57: #undef sem_post
   58: #undef sem_wait
   59: #undef sem_trywait
   60: 
   61: int
   62: sem_getvalue2(sem_t *semaphore, int *valeur)
   63: {
   64:     int                     i;
   65: 
   66:     logical1                drapeau_fin;
   67: 
   68:     pthread_mutex_lock(&mutex_sem);
   69: 
   70:     (*valeur) = 0;
   71:     drapeau_fin = d_faux;
   72: 
   73:     do
   74:     {
   75:         if (sem_trywait(semaphore) == -1)
   76:         {
   77:             if (errno == EAGAIN)
   78:             {
   79:                 // Le sémaphore avait une valeur nulle
   80:                 drapeau_fin = d_vrai;
   81:             }
   82:             else
   83:             {
   84:                 // Autre erreur
   85:                 pthread_mutex_unlock(&mutex_sem);
   86:                 return(-1);
   87:             }
   88:         }
   89:         else
   90:         {
   91:             (*valeur)++;
   92:         }
   93:     } while(drapeau_fin == d_faux);
   94: 
   95:     for(i = 0; i < (*valeur); i++)
   96:     {
   97:         if (sem_post(semaphore) != 0)
   98:         {
   99:             pthread_mutex_unlock(&mutex_sem);
  100:             return(-1);
  101:         }
  102:     }
  103: 
  104:     pthread_mutex_unlock(&mutex_sem);
  105:     return(0);
  106: }
  107: 
  108: #endif
  109: 
  110: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>