File:  [local] / rpl / src / operations_atomiques.c
Revision 1.13: download - view: text, annotated - select for diffs - revision graph
Fri Aug 6 15:33:03 2010 UTC (13 years, 8 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_0_18, HEAD
Cohérence

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.0.18
    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: #include "rpl-conv.h"
   24: 
   25: 
   26: /*
   27: ================================================================================
   28:   Procédure de lecture atomique
   29: ================================================================================
   30:   Entrée :
   31: --------------------------------------------------------------------------------
   32:   Sortie :
   33: --------------------------------------------------------------------------------
   34:   Effets de bord : néant
   35: ================================================================================
   36: */
   37: 
   38: #ifndef OS2
   39: extern inline
   40: #endif
   41: ssize_t
   42: read_atomic(struct_processus *s_etat_processus,
   43:         int descripteur, void *tampon, size_t longueur_tampon)
   44: {
   45:     logical1            drapeau;
   46: 
   47:     size_t              longueur_residuelle;
   48:     ssize_t             longueur_lue;
   49: 
   50:     struct timespec     attente;
   51: 
   52:     void                *pointeur;
   53: 
   54:     longueur_residuelle = longueur_tampon;
   55:     pointeur = tampon;
   56: 
   57:     attente.tv_sec = 0;
   58:     attente.tv_nsec = GRANULARITE_us * 1000;
   59: 
   60:     while(longueur_residuelle != 0)
   61:     {
   62:         errno = 0;
   63:         longueur_lue = 0;
   64: 
   65:         while(longueur_lue == 0)
   66:         {
   67:             do
   68:             {
   69:                 longueur_lue = read(descripteur, pointeur,
   70:                         (longueur_residuelle > PIPE_BUF)
   71:                         ? PIPE_BUF : longueur_residuelle);
   72: 
   73:                 if ((longueur_lue == 0) &&
   74:                         (longueur_tampon == longueur_residuelle))
   75:                 {
   76:                     return(longueur_tampon - longueur_residuelle);
   77:                 }
   78: 
   79:                 if ((longueur_lue == -1) && (errno == EINTR))
   80:                 {
   81:                     nanosleep(&attente, NULL);
   82:                     drapeau = d_vrai;
   83:                 }
   84:                 else
   85:                 {
   86:                     drapeau = d_faux;
   87:                 }
   88:             } while(drapeau == d_vrai);
   89: 
   90:             if (longueur_lue != 0)
   91:             {
   92:                 if (longueur_lue < (longueur_residuelle > PIPE_BUF)
   93:                         ? PIPE_BUF : longueur_residuelle)
   94:                 {
   95:                     pointeur += longueur_lue;
   96:                     longueur_residuelle -= longueur_lue;
   97: 
   98:                     return(longueur_tampon - longueur_residuelle);
   99:                 }
  100:             }
  101:         }
  102: 
  103:         pointeur += longueur_lue;
  104:         longueur_residuelle -= longueur_lue;
  105:     }
  106: 
  107:     return(longueur_tampon - longueur_residuelle);
  108: }
  109: 
  110: 
  111: /*
  112: ================================================================================
  113:   Procédure d'écriture atomique
  114: ================================================================================
  115:   Entrée :
  116: --------------------------------------------------------------------------------
  117:   Sortie :
  118: --------------------------------------------------------------------------------
  119:   Effets de bord : néant
  120: ================================================================================
  121: */
  122: 
  123: #ifndef OS2
  124: extern inline
  125: #endif
  126: ssize_t
  127: write_atomic(struct_processus *s_etat_processus,
  128:         int descripteur, void *tampon, size_t longueur_tampon)
  129: {
  130:     logical1            drapeau;
  131: 
  132:     size_t              longueur_residuelle;
  133:     ssize_t             longueur_ecrite;
  134: 
  135:     struct timespec     attente;
  136: 
  137:     void                *pointeur;
  138: 
  139:     longueur_residuelle = longueur_tampon;
  140:     pointeur = tampon;
  141: 
  142:     attente.tv_sec = 0;
  143:     attente.tv_nsec = GRANULARITE_us * 1000;
  144: 
  145:     while(longueur_residuelle != 0)
  146:     {
  147:         errno = 0;
  148:         longueur_ecrite = 0;
  149: 
  150:         while(longueur_ecrite == 0)
  151:         {
  152:             do
  153:             {
  154:                 longueur_ecrite = write(descripteur, pointeur,
  155:                         (longueur_residuelle > PIPE_BUF)
  156:                         ? PIPE_BUF : longueur_residuelle);
  157: 
  158:                 if (longueur_ecrite == -1)
  159:                 {
  160:                     if (errno == EINTR)
  161:                     {
  162:                         nanosleep(&attente, NULL);
  163:                         drapeau = d_vrai;
  164:                     }
  165:                     else // EPIPE
  166:                     {
  167:                         return(-1);
  168:                     }
  169:                 }
  170:                 else
  171:                 {
  172:                     drapeau = d_faux;
  173:                 }
  174:             } while(drapeau == d_vrai);
  175: 
  176:             if (longueur_ecrite == 0)
  177:             {
  178:                 nanosleep(&attente, NULL);
  179:             }
  180:         }
  181: 
  182:         pointeur += longueur_ecrite;
  183:         longueur_residuelle -= longueur_ecrite;
  184:     }
  185: 
  186:     return(longueur_tampon);
  187: }
  188: 
  189: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>