/* ================================================================================ RPL/2 (R) version 4.1.1 Copyright (C) 1989-2011 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. ================================================================================ */ #include "rpl-conv.h" /* ================================================================================ Procédure de lecture atomique ================================================================================ Entrée : -------------------------------------------------------------------------------- Sortie : -------------------------------------------------------------------------------- Effets de bord : néant ================================================================================ */ ssize_t read_atomic(struct_processus *s_etat_processus, int descripteur, void *tampon, size_t longueur_tampon) { logical1 drapeau; size_t longueur_residuelle; ssize_t longueur_lue; struct timespec attente; void *pointeur; longueur_residuelle = longueur_tampon; pointeur = tampon; attente.tv_sec = 0; attente.tv_nsec = GRANULARITE_us * 1000; while(longueur_residuelle != 0) { errno = 0; longueur_lue = 0; while(longueur_lue == 0) { do { longueur_lue = read(descripteur, pointeur, (longueur_residuelle > PIPE_BUF) ? PIPE_BUF : longueur_residuelle); if ((longueur_lue == 0) && (longueur_tampon == longueur_residuelle)) { return(longueur_tampon - longueur_residuelle); } if ((longueur_lue == -1) && (errno == EINTR)) { nanosleep(&attente, NULL); drapeau = d_vrai; } else { drapeau = d_faux; } } while(drapeau == d_vrai); if (longueur_lue != 0) { if (longueur_lue < (longueur_residuelle > PIPE_BUF) ? PIPE_BUF : longueur_residuelle) { pointeur += longueur_lue; longueur_residuelle -= longueur_lue; return(longueur_tampon - longueur_residuelle); } } } pointeur += longueur_lue; longueur_residuelle -= longueur_lue; } return(longueur_tampon - longueur_residuelle); } /* ================================================================================ Procédure d'écriture atomique ================================================================================ Entrée : -------------------------------------------------------------------------------- Sortie : -------------------------------------------------------------------------------- Effets de bord : néant ================================================================================ */ ssize_t write_atomic(struct_processus *s_etat_processus, int descripteur, void *tampon, size_t longueur_tampon) { logical1 drapeau; size_t longueur_residuelle; ssize_t longueur_ecrite; struct timespec attente; void *pointeur; longueur_residuelle = longueur_tampon; pointeur = tampon; attente.tv_sec = 0; attente.tv_nsec = GRANULARITE_us * 1000; while(longueur_residuelle != 0) { errno = 0; longueur_ecrite = 0; while(longueur_ecrite == 0) { do { longueur_ecrite = write(descripteur, pointeur, (longueur_residuelle > PIPE_BUF) ? PIPE_BUF : longueur_residuelle); if (longueur_ecrite == -1) { if (errno == EINTR) { nanosleep(&attente, NULL); drapeau = d_vrai; } else // EPIPE { return(-1); } } else { drapeau = d_faux; } } while(drapeau == d_vrai); if (longueur_ecrite == 0) { nanosleep(&attente, NULL); } } pointeur += longueur_ecrite; longueur_residuelle -= longueur_ecrite; } return(longueur_tampon); } // vim: ts=4