File:  [local] / rpl / src / instructions_p8.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Mon Dec 5 19:35:54 2011 UTC (12 years, 5 months ago) by bertrand
Branches: MAIN
CVS tags: HEAD
Ajout de POLL.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.1.5
    4:   Copyright (C) 1989-2011 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:   Fonction 'poll'
   29: ================================================================================
   30:   Entrées : pointeur sur une structure struct_processus
   31: --------------------------------------------------------------------------------
   32:   Sorties :
   33: --------------------------------------------------------------------------------
   34:   Effets de bord : néant
   35: ================================================================================
   36: */
   37: 
   38: void
   39: instruction_poll(struct_processus *s_etat_processus)
   40: {
   41:     int                     ios;
   42:     int                     timeout;
   43: 
   44:     logical1                drapeau;
   45: 
   46:     struct_liste_chainee    *l_element_courant;
   47: 
   48:     struct_objet            *s_objet_argument_1;
   49:     struct_objet            *s_objet_argument_2;
   50:     struct_objet            *s_objet_argument_3;
   51:     struct_objet            *s_objet_resultat;
   52: 
   53:     struct pollfd           s_poll;
   54: 
   55:     unsigned char           *registre;
   56: 
   57:     if ((*s_etat_processus).affichage_arguments == 'Y')
   58:     {
   59:         printf("\n  POLL ");
   60: 
   61:         if ((*s_etat_processus).langue == 'F')
   62:         {
   63:             printf("(attente d'un événement sur un fichier ou une socket)\n\n");
   64:         }
   65:         else
   66:         {
   67:             printf("(wait for event on file or socket)\n\n");
   68:         }
   69: 
   70:         if ((*s_etat_processus).langue == 'F')
   71:         {
   72:             printf("  Utilisation :\n\n");
   73:         }
   74:         else
   75:         {
   76:             printf("  Usage:\n\n");
   77:         }
   78: 
   79:         printf("    FILE { \"POLLIN\" \"POLLOUT\" } TIMEOUT POLL\n");
   80:         printf("    3: %s, %s\n", d_SCK, d_FCH);
   81:         printf("    2: %s\n", d_LST);
   82:         printf("    1: %s, %s\n", d_INT, d_REL);
   83:         printf("->  1: %s\n", d_INT);
   84: 
   85:         return;
   86:     }
   87:     else if ((*s_etat_processus).test_instruction == 'Y')
   88:     {
   89:         (*s_etat_processus).nombre_arguments = -1;
   90:         return;
   91:     }
   92: 
   93:     if (test_cfsf(s_etat_processus, 31) == d_vrai)
   94:     {
   95:         if (empilement_pile_last(s_etat_processus, 3) == d_erreur)
   96:         {
   97:             return;
   98:         }
   99:     }
  100: 
  101:     if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
  102:             &s_objet_argument_1) == d_erreur)
  103:     {
  104:         (*s_etat_processus).erreur_execution = d_ex_manque_argument;
  105:         return;
  106:     }
  107: 
  108:     if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
  109:             &s_objet_argument_2) == d_erreur)
  110:     {
  111:         liberation(s_objet_argument_1);
  112: 
  113:         (*s_etat_processus).erreur_execution = d_ex_manque_argument;
  114:         return;
  115:     }
  116: 
  117:     if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
  118:             &s_objet_argument_3) == d_erreur)
  119:     {
  120:         liberation(s_objet_argument_1);
  121:         liberation(s_objet_argument_2);
  122: 
  123:         (*s_etat_processus).erreur_execution = d_ex_manque_argument;
  124:         return;
  125:     }
  126: 
  127:     if (((*s_objet_argument_3).type == SCK) ||
  128:             ((*s_objet_argument_3).type == FCH))
  129:     {
  130:         if ((*s_objet_argument_3).type == SCK)
  131:         {
  132:             s_poll.fd = (*((struct_socket *)
  133:                     (*s_objet_argument_1).objet)).socket;
  134:         }
  135:         else
  136:         {
  137:             s_poll.fd = (*((struct_fichier *) 
  138:                     (*s_objet_argument_1).objet)).descripteur;
  139:         }
  140: 
  141:         if ((*s_objet_argument_2).type != LST)
  142:         {
  143:             liberation(s_etat_processus, s_objet_argument_1);
  144:             liberation(s_etat_processus, s_objet_argument_2);
  145:             liberation(s_etat_processus, s_objet_argument_3);
  146: 
  147:             (*s_etat_processus).erreur_execution = d_ex_erreur_type_argument;
  148:             return;
  149:         }
  150: 
  151:         l_element_courant = (*s_objet_argument_2).objet;
  152:         s_poll.events = 0;
  153: 
  154:         while(l_element_courant != NULL)
  155:         {
  156:             if ((*(*l_element_courant).donnee).type != CHN)
  157:             {
  158:                 liberation(s_etat_processus, s_objet_argument_1);
  159:                 liberation(s_etat_processus, s_objet_argument_2);
  160:                 liberation(s_etat_processus, s_objet_argument_3);
  161: 
  162:                 (*s_etat_processus).erreur_execution =
  163:                         d_ex_erreur_type_argument;
  164:                 return;
  165:             }
  166: 
  167:             if ((registre = conversion_majuscule((unsigned char *)
  168:                     (*(*l_element_courant).donnee).objet)) == NULL)
  169:             {
  170:                 (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
  171:                 return;
  172:             }
  173: 
  174:             if (strcmp(registre, "POLLIN") == 0)
  175:             {
  176:                 s_poll.events |= POLLIN;
  177:             }
  178:             else (strcmp(registre, "POLLOUT") == 0)
  179:             {
  180:                 s_poll.events |= POLLOUT;
  181:             }
  182:             else
  183:             {
  184:                 free(registre);
  185: 
  186:                 liberation(s_etat_processus, s_objet_argument_1);
  187:                 liberation(s_etat_processus, s_objet_argument_2);
  188:                 liberation(s_etat_processus, s_objet_argument_3);
  189: 
  190:                 (*s_etat_processus).erreur_execution =
  191:                         d_ex_erreur_parametre_fichier;
  192:                 return;
  193:             }
  194: 
  195:             free(registre);
  196:             l_element_courant = (*l_element_courant).suivant;
  197:         }
  198: 
  199:         s_poll.revents = 0;
  200: 
  201:         if ((*s_objet_argument_1).type == INT)
  202:         {
  203:             timeout = (*((integer8 *) (*s_objet_argument_1).objet)) * 1000L;
  204:         }
  205:         else if ((*s_objet_argument_1).type == REL)
  206:         {
  207:             timeout = (int) ((*((real8 *) (*s_objet_argument_1).objet))
  208:                     * 1000L);
  209:         }
  210:         else
  211:         {
  212:             liberation(s_etat_processus, s_objet_argument_1);
  213:             liberation(s_etat_processus, s_objet_argument_2);
  214:             liberation(s_etat_processus, s_objet_argument_3);
  215: 
  216:             (*s_etat_processus).erreur_execution =
  217:                     d_ex_erreur_type_argument;
  218:             return;
  219:         }
  220: 
  221:         if ((s_objet_resultat = allocation(s_etat_processus, INT)) == NULL)
  222:         {
  223:             (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
  224:             return;
  225:         }
  226: 
  227:         do
  228:         {
  229:             drapeau = d_vrai;
  230: 
  231: #           ifndef SEMAPHORES_NOMMES
  232:             if (sem_post(&((*s_etat_processus).semaphore_fork)) != 0)
  233: #           else
  234:             if (sem_post((*s_etat_processus).semaphore_fork) != 0)
  235: #           endif
  236:             {
  237:                 (*s_etat_processus).erreur_systeme = d_es_processus;
  238:                 return;
  239:             }
  240: 
  241:             if ((ios = poll(&s_poll, 1, timeout)) < 0)
  242:             {
  243:                 erreur = errno;
  244: 
  245: #               ifndef SEMAPHORES_NOMMES
  246:                 while(sem_wait(&((*s_etat_processus)
  247:                         .semaphore_fork)) != 0)
  248: #               else
  249:                 while(sem_wait((*s_etat_processus).semaphore_fork) != 0)
  250: #               endif
  251: 
  252:                 if (erreur != EINTR)
  253:                 {
  254:                     liberation(s_etat_processus, s_objet_argument_1);
  255:                     liberation(s_etat_processus, s_objet_argument_2);
  256:                     liberation(s_etat_processus, s_objet_argument_3);
  257:                     liberation(s_etat_processus, s_objet_resultat);
  258: 
  259:                     (*s_etat_processus).erreur_execution =
  260:                             d_ex_erreur_acces_fichier;
  261:                     return;
  262:                 }
  263: 
  264:                 scrutation_injection(s_etat_processus);
  265: 
  266:                 if ((*s_etat_processus).var_volatile_requete_arret != 0)
  267:                 {
  268:                     drapeau = d_vrai;
  269:                 }
  270:                 else
  271:                 {
  272:                     drapeau = d_faux;
  273:                 }
  274:             }
  275:             else
  276:             {
  277: #               ifndef SEMAPHORES_NOMMES
  278:                     while(sem_wait(&((*s_etat_processus)
  279:                             .semaphore_fork)) != 0)
  280: #               else
  281:                     while(sem_wait((*s_etat_processus).semaphore_fork) != 0)
  282: #               endif
  283:                 {
  284:                     if (errno != EINTR)
  285:                     {
  286:                         (*s_etat_processus).erreur_systeme =
  287:                                 d_es_processus;
  288:                         return;
  289:                     }
  290:                 }
  291:             }
  292:         } while(drapeau == d_faux);
  293: 
  294:         if (ios > 0)
  295:         {
  296:             // Sortie sur un événement
  297:             (*((integer8 *) (*s_objet_resultat).objet)) = -1;
  298:         }
  299:         else
  300:         {
  301:             // Sortie sur timeout
  302:             (*((integer8 *) (*s_objet_resultat).objet)) = 0;
  303:         }
  304: 
  305:         liberation(s_etat_processus, s_objet_argument_1);
  306:         liberation(s_etat_processus, s_objet_argument_2);
  307:         liberation(s_etat_processus, s_objet_argument_3);
  308: 
  309:         if (empilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
  310:                 s_objet_resultat) == d_erreur)
  311:         {
  312:             return;
  313:         }
  314:     }
  315:     else
  316:     {
  317:         liberation(s_etat_processus, s_objet_argument_1);
  318:         liberation(s_etat_processus, s_objet_argument_2);
  319:         liberation(s_etat_processus, s_objet_argument_3);
  320: 
  321:         (*s_etat_processus).erreur_execution = d_ex_erreur_type_argument;
  322:         return;
  323:     }
  324: 
  325:     return;
  326: }
  327: 
  328: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>