--- rpl/src/interruptions.c 2010/03/09 10:18:49 1.6 +++ rpl/src/interruptions.c 2011/09/14 14:34:28 1.67 @@ -1,7 +1,7 @@ /* ================================================================================ - RPL/2 (R) version 4.0.13 - Copyright (C) 1989-2010 Dr. BERTRAND Joël + RPL/2 (R) version 4.1.3 + Copyright (C) 1989-2011 Dr. BERTRAND Joël This file is part of RPL/2. @@ -20,7 +20,7 @@ */ -#include "rpl.conv.h" +#include "rpl-conv.h" /* @@ -82,22 +82,9 @@ insertion_thread(struct_processus *s_eta sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); - while(sem_wait(&semaphore_liste_threads) == -1) - { - if (errno != EINTR) - { - pthread_sigmask(SIG_SETMASK, &oldset, NULL); - sigpending(&set); - - (*s_etat_processus).erreur_systeme = d_es_processus; - return; - } - } - if ((l_nouvel_objet = malloc(sizeof(struct_liste_chainee_volatile))) == NULL) { - sem_post(&semaphore_liste_threads); pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -105,11 +92,8 @@ insertion_thread(struct_processus *s_eta return; } - (*l_nouvel_objet).suivant = liste_threads; - if (((*l_nouvel_objet).donnee = malloc(sizeof(struct_thread))) == NULL) { - sem_post(&semaphore_liste_threads); pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -124,9 +108,30 @@ insertion_thread(struct_processus *s_eta (*((struct_thread *) (*l_nouvel_objet).donnee)).s_etat_processus = s_etat_processus; +# ifndef SEMAPHORES_NOMMES + while(sem_wait(&semaphore_liste_threads) == -1) +# else + while(sem_wait(semaphore_liste_threads) == -1) +# endif + { + if (errno != EINTR) + { + pthread_sigmask(SIG_SETMASK, &oldset, NULL); + sigpending(&set); + + (*s_etat_processus).erreur_systeme = d_es_processus; + return; + } + } + + (*l_nouvel_objet).suivant = liste_threads; liste_threads = l_nouvel_objet; +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) +# else + if (sem_post(semaphore_liste_threads) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -152,7 +157,21 @@ insertion_thread_surveillance(struct_pro sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); + if ((l_nouvel_objet = malloc(sizeof(struct_liste_chainee_volatile))) + == NULL) + { + pthread_sigmask(SIG_SETMASK, &oldset, NULL); + sigpending(&set); + + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_liste_threads) == -1) +# else + while(sem_wait(semaphore_liste_threads) == -1) +# endif { if (errno != EINTR) { @@ -164,23 +183,20 @@ insertion_thread_surveillance(struct_pro } } - if ((l_nouvel_objet = malloc(sizeof(struct_liste_chainee_volatile))) - == NULL) - { - sem_post(&semaphore_liste_threads); - pthread_sigmask(SIG_SETMASK, &oldset, NULL); - sigpending(&set); - - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } + pthread_mutex_lock(&((*s_argument_thread).mutex_nombre_references)); + (*s_argument_thread).nombre_references++; + pthread_mutex_unlock(&((*s_argument_thread).mutex_nombre_references)); (*l_nouvel_objet).suivant = liste_threads_surveillance; (*l_nouvel_objet).donnee = (void *) s_argument_thread; liste_threads_surveillance = l_nouvel_objet; +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) +# else + if (sem_post(semaphore_liste_threads) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -206,7 +222,11 @@ retrait_thread(struct_processus *s_etat_ sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_liste_threads) == -1) +# else + while(sem_wait(semaphore_liste_threads) == -1) +# endif { if (errno != EINTR) { @@ -236,7 +256,11 @@ retrait_thread(struct_processus *s_etat_ if (l_element_courant == NULL) { +# ifndef SEMAPHORES_NOMMES sem_post(&semaphore_liste_threads); +# else + sem_post(semaphore_liste_threads); +# endif pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -253,20 +277,25 @@ retrait_thread(struct_processus *s_etat_ (*l_element_precedent).suivant = (*l_element_courant).suivant; } - free((void *) (*l_element_courant).donnee); - free((struct_liste_chainee_volatile *) l_element_courant); - if (pthread_setspecific(semaphore_fork_processus_courant, NULL) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; +# ifndef SEMAPHORES_NOMMES sem_post(&semaphore_liste_threads); +# else + sem_post(semaphore_liste_threads); +# endif pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); return; } +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) +# else + if (sem_post(semaphore_liste_threads) != 0) +# endif { (*s_etat_processus).erreur_systeme = d_es_processus; @@ -275,6 +304,9 @@ retrait_thread(struct_processus *s_etat_ return; } + free((void *) (*l_element_courant).donnee); + free((struct_liste_chainee_volatile *) l_element_courant); + pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); return; @@ -293,7 +325,11 @@ retrait_thread_surveillance(struct_proce sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_liste_threads) == -1) +# else + while(sem_wait(semaphore_liste_threads) == -1) +# endif { if (errno != EINTR) { @@ -321,7 +357,11 @@ retrait_thread_surveillance(struct_proce if (l_element_courant == NULL) { +# ifndef SEMAPHORES_NOMMES sem_post(&semaphore_liste_threads); +# else + sem_post(semaphore_liste_threads); +# endif pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -338,10 +378,14 @@ retrait_thread_surveillance(struct_proce (*l_element_precedent).suivant = (*l_element_courant).suivant; } - free((struct_liste_chainee_volatile *) l_element_courant); - - if (pthread_mutex_lock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_lock(&((*s_argument_thread).mutex_nombre_references)) + != 0) { +# ifndef SEMAPHORES_NOMMES + sem_post(&semaphore_liste_threads); +# else + sem_post(semaphore_liste_threads); +# endif pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -357,8 +401,14 @@ retrait_thread_surveillance(struct_proce if ((*s_argument_thread).nombre_references == 0) { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { +# ifndef SEMAPHORES_NOMMES + sem_post(&semaphore_liste_threads); +# else + sem_post(semaphore_liste_threads); +# endif pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -367,12 +417,19 @@ retrait_thread_surveillance(struct_proce } pthread_mutex_destroy(&((*s_argument_thread).mutex)); + pthread_mutex_destroy(&((*s_argument_thread).mutex_nombre_references)); free(s_argument_thread); } else { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { +# ifndef SEMAPHORES_NOMMES + sem_post(&semaphore_liste_threads); +# else + sem_post(semaphore_liste_threads); +# endif pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -381,7 +438,11 @@ retrait_thread_surveillance(struct_proce } } +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) +# else + if (sem_post(semaphore_liste_threads) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); @@ -390,8 +451,11 @@ retrait_thread_surveillance(struct_proce return; } + free((struct_liste_chainee_volatile *) l_element_courant); + pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); + return; } @@ -400,7 +464,11 @@ verrouillage_threads_concurrents(struct_ { volatile struct_liste_chainee_volatile *l_element_courant; +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_liste_threads) == -1) +# else + while(sem_wait(semaphore_liste_threads) == -1) +# endif { if (errno != EINTR) { @@ -417,8 +485,13 @@ verrouillage_threads_concurrents(struct_ == getpid()) && (pthread_equal((*((struct_thread *) (*l_element_courant).donnee)).tid, pthread_self()) == 0)) { +# ifndef SEMAPHORES_NOMMES while(sem_wait(&((*(*((struct_thread *) (*l_element_courant) .donnee)).s_etat_processus).semaphore_fork)) == -1) +# else + while(sem_wait((*(*((struct_thread *) (*l_element_courant) + .donnee)).s_etat_processus).semaphore_fork) == -1) +# endif { if (errno != EINTR) { @@ -447,15 +520,29 @@ deverrouillage_threads_concurrents(struc == getpid()) && (pthread_equal((*((struct_thread *) (*l_element_courant).donnee)).tid, pthread_self()) == 0)) { +# ifndef SEMAPHORES_NOMMES if (sem_post(&((*(*((struct_thread *) (*l_element_courant).donnee)).s_etat_processus) .semaphore_fork)) != 0) +# else + if (sem_post((*(*((struct_thread *) + (*l_element_courant).donnee)).s_etat_processus) + .semaphore_fork) != 0) +# endif { +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; return; } +# else + if (sem_post(semaphore_liste_threads) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + return; + } +# endif (*s_etat_processus).erreur_systeme = d_es_processus; return; @@ -465,7 +552,11 @@ deverrouillage_threads_concurrents(struc l_element_courant = (*l_element_courant).suivant; } +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) +# else + if (sem_post(semaphore_liste_threads) != 0) +# endif { (*s_etat_processus).erreur_systeme = d_es_processus; return; @@ -498,7 +589,11 @@ liberation_threads(struct_processus *s_e sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_liste_threads) == -1) +# else + while(sem_wait(semaphore_liste_threads) == -1) +# endif { if (errno != EINTR) { @@ -539,6 +634,8 @@ liberation_threads(struct_processus *s_e close((*s_etat_processus).pipe_nombre_objets_attente); close((*s_etat_processus).pipe_nombre_interruptions_attente); + liberation(s_etat_processus, (*s_etat_processus).at_exit); + if ((*s_etat_processus).nom_fichier_impression != NULL) { free((*s_etat_processus).nom_fichier_impression); @@ -580,18 +677,76 @@ liberation_threads(struct_processus *s_e .l_base_pile_processus; while(element_courant != NULL) { - pthread_mutex_trylock(&((*(*((struct_liste_chainee *) - element_courant)).donnee).mutex)); - pthread_mutex_unlock(&((*(*((struct_liste_chainee *) - element_courant)).donnee).mutex)); - liberation(s_etat_processus, - (*((struct_liste_chainee *) element_courant)).donnee); + s_argument_thread = (struct_descripteur_thread *) + (*((struct_liste_chainee *) element_courant)).donnee; + + if (pthread_mutex_lock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + sem_post(&semaphore_liste_threads); + return; + } + + (*s_argument_thread).nombre_references--; + + BUG((*s_argument_thread).nombre_references < 0, + printf("(*s_argument_thread).nombre_references = %d\n", + (int) (*s_argument_thread).nombre_references)); + + if ((*s_argument_thread).nombre_references == 0) + { + close((*s_argument_thread).pipe_objets[0]); + close((*s_argument_thread).pipe_acquittement[1]); + close((*s_argument_thread).pipe_injections[1]); + close((*s_argument_thread).pipe_nombre_injections[1]); + close((*s_argument_thread).pipe_nombre_objets_attente[0]); + close((*s_argument_thread).pipe_interruptions[0]); + close((*s_argument_thread) + .pipe_nombre_interruptions_attente[0]); + + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + sem_post(&semaphore_liste_threads); + return; + } + + pthread_mutex_destroy(&((*s_argument_thread).mutex)); + pthread_mutex_destroy(&((*s_argument_thread) + .mutex_nombre_references)); + + if ((*s_argument_thread).processus_detache == d_faux) + { + if ((*s_argument_thread).destruction_objet == d_vrai) + { + liberation(s_etat_processus, (*s_argument_thread) + .argument); + } + } + + free(s_argument_thread); + } + else + { + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + sem_post(&semaphore_liste_threads); + return; + } + } + element_suivant = (*((struct_liste_chainee *) element_courant)) .suivant; - free((struct_liste_chainee *) element_courant); + free(element_courant); element_courant = element_suivant; } + (*s_etat_processus).l_base_pile_processus = NULL; + pthread_mutex_trylock(&((*(*s_etat_processus).indep).mutex)); pthread_mutex_unlock(&((*(*s_etat_processus).indep).mutex)); liberation(s_etat_processus, (*s_etat_processus).indep); @@ -648,25 +803,8 @@ liberation_threads(struct_processus *s_e } } - for(i = 0; i < (*s_etat_processus).nombre_variables; i++) - { - pthread_mutex_trylock(&((*(*s_etat_processus) - .s_liste_variables[i].objet).mutex)); - pthread_mutex_unlock(&((*(*s_etat_processus) - .s_liste_variables[i].objet).mutex)); - - // Les variables de niveau 0 sont des définitions qui - // ne sont pas copiées entre threads. - if ((*s_etat_processus).s_liste_variables[i].niveau > 0) - { - liberation(s_etat_processus, - (*s_etat_processus).s_liste_variables[i].objet); - } - - free((*s_etat_processus).s_liste_variables[i].nom); - } - - free((*s_etat_processus).s_liste_variables); + liberation_arbre_variables(s_etat_processus, + (*s_etat_processus).s_arbre_variables, d_faux); for(i = 0; i < (*s_etat_processus).nombre_variables_statiques; i++) { @@ -1066,6 +1204,12 @@ liberation_threads(struct_processus *s_e element_courant = element_suivant; } +/* +================================================================================ + À noter : on ne ferme pas la connexion car la conséquence immédiate est + une destruction de l'objet pour le processus père. +================================================================================ + element_courant = (*s_etat_processus).s_connecteurs_sql; while(element_courant != NULL) { @@ -1149,6 +1293,9 @@ liberation_threads(struct_processus *s_e element_courant = element_suivant; } +*/ + + (*s_etat_processus).s_connecteurs_sql = NULL; element_courant = (*s_etat_processus).s_marques; while(element_courant != NULL) @@ -1163,9 +1310,15 @@ liberation_threads(struct_processus *s_e liberation_allocateur(s_etat_processus); +# ifndef SEMAPHORES_NOMMES sem_post(&((*s_etat_processus).semaphore_fork)); sem_destroy(&((*s_etat_processus).semaphore_fork)); +# else + sem_post((*s_etat_processus).semaphore_fork); + sem_destroy2((*s_etat_processus).semaphore_fork, sem_fork); +# endif + liberation_contexte_cas(s_etat_processus); free(s_etat_processus); s_etat_processus = candidat; @@ -1188,17 +1341,11 @@ liberation_threads(struct_processus *s_e s_argument_thread = (struct_descripteur_thread *) (*l_element_courant).donnee; - close((*s_argument_thread).pipe_objets[0]); - close((*s_argument_thread).pipe_acquittement[1]); - close((*s_argument_thread).pipe_injections[1]); - close((*s_argument_thread).pipe_nombre_injections[1]); - close((*s_argument_thread).pipe_nombre_objets_attente[0]); - close((*s_argument_thread).pipe_interruptions[0]); - close((*s_argument_thread).pipe_nombre_interruptions_attente[0]); - - if (pthread_mutex_lock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_lock(&((*s_argument_thread).mutex_nombre_references)) + != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; + sem_post(&semaphore_liste_threads); return; } @@ -1210,20 +1357,43 @@ liberation_threads(struct_processus *s_e if ((*s_argument_thread).nombre_references == 0) { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + close((*s_argument_thread).pipe_objets[0]); + close((*s_argument_thread).pipe_acquittement[1]); + close((*s_argument_thread).pipe_injections[1]); + close((*s_argument_thread).pipe_nombre_injections[1]); + close((*s_argument_thread).pipe_nombre_objets_attente[0]); + close((*s_argument_thread).pipe_interruptions[0]); + close((*s_argument_thread).pipe_nombre_interruptions_attente[0]); + + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; + sem_post(&semaphore_liste_threads); return; } pthread_mutex_destroy(&((*s_argument_thread).mutex)); + pthread_mutex_destroy(&((*s_argument_thread) + .mutex_nombre_references)); + + if ((*s_argument_thread).processus_detache == d_faux) + { + if ((*s_argument_thread).destruction_objet == d_vrai) + { + liberation(s_etat_processus, (*s_argument_thread).argument); + } + } + free(s_argument_thread); } else { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; + sem_post(&semaphore_liste_threads); return; } } @@ -1235,7 +1405,11 @@ liberation_threads(struct_processus *s_e liste_threads_surveillance = NULL; +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) +# else + if (sem_post(semaphore_liste_threads) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); (*s_etat_processus).erreur_systeme = d_es_processus; @@ -1283,8 +1457,8 @@ recherche_thread(pid_t pid, pthread_t ti return(s_etat_processus); } -static logical1 -recherche_thread_principal(pid_t pid, pthread_t *thread) +static struct_processus * +recherche_thread_principal(pid_t pid) { volatile struct_liste_chainee_volatile *l_element_courant; @@ -1308,12 +1482,11 @@ recherche_thread_principal(pid_t pid, pt * Le processus n'existe plus. On ne distribue aucun signal. */ - return(d_faux); + return(NULL); } - (*thread) = (*((struct_thread *) (*l_element_courant).donnee)).tid; - - return(d_vrai); + return((*((struct_thread *) (*l_element_courant).donnee)) + .s_etat_processus); } @@ -1330,7 +1503,7 @@ recherche_thread_principal(pid_t pid, pt */ // Les routines suivantes sont uniquement appelées depuis les gestionnaires -// des signaux asynchrones. Elles de doivent pas bloquer dans le cas où +// des signaux asynchrones. Elles ne doivent pas bloquer dans le cas où // les sémaphores sont déjà bloqués par un gestionnaire de signal. static inline void @@ -1358,7 +1531,11 @@ verrouillage_gestionnaire_signaux() sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_gestionnaires_signaux_atomique) == -1) +# else + while(sem_wait(semaphore_gestionnaires_signaux_atomique) == -1) +# endif { if (errno != EINTR) { @@ -1368,21 +1545,33 @@ verrouillage_gestionnaire_signaux() } } +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_gestionnaires_signaux) == -1) +# else + if (sem_post(semaphore_gestionnaires_signaux) == -1) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); BUG(1, uprintf("Lock error !\n")); return; } +# ifndef SEMAPHORES_NOMMES if (sem_getvalue(&semaphore_gestionnaires_signaux, &semaphore) != 0) +# else + if (sem_getvalue(semaphore_gestionnaires_signaux, &semaphore) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); BUG(1, uprintf("Lock error !\n")); return; } +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_gestionnaires_signaux_atomique) != 0) +# else + if (sem_post(semaphore_gestionnaires_signaux_atomique) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); BUG(1, uprintf("Unlock error !\n")); @@ -1397,9 +1586,13 @@ verrouillage_gestionnaire_signaux() // par un thread concurrent. On essaye donc de le bloquer jusqu'à // ce que ce soit possible. - while(sem_trywait(&semaphore_liste_threads) == -1) +# ifndef SEMAPHORES_NOMMES + while(sem_wait(&semaphore_liste_threads) == -1) +# else + while(sem_wait(semaphore_liste_threads) == -1) +# endif { - if ((errno != EINTR) && (errno != EAGAIN)) + if (errno != EINTR) { pthread_sigmask(SIG_SETMASK, &oldset, NULL); @@ -1415,8 +1608,6 @@ verrouillage_gestionnaire_signaux() BUG(1, uprintf("Lock error !\n")); return; } - - sched_yield(); } } @@ -1441,7 +1632,11 @@ deverrouillage_gestionnaire_signaux() sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_gestionnaires_signaux_atomique) == -1) +# else + while(sem_wait(semaphore_gestionnaires_signaux_atomique) == -1) +# endif { if (errno != EINTR) { @@ -1451,14 +1646,22 @@ deverrouillage_gestionnaire_signaux() } } +# ifndef SEMAPHORES_NOMMES if (sem_getvalue(&semaphore_gestionnaires_signaux, &semaphore) != 0) +# else + if (sem_getvalue(semaphore_gestionnaires_signaux, &semaphore) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); BUG(1, uprintf("Unlock error !\n")); return; } +# ifndef SEMAPHORES_NOMMES while(sem_wait(&semaphore_gestionnaires_signaux) == -1) +# else + while(sem_wait(semaphore_gestionnaires_signaux) == -1) +# endif { if (errno != EINTR) { @@ -1468,7 +1671,11 @@ deverrouillage_gestionnaire_signaux() } } +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_gestionnaires_signaux_atomique) != 0) +# else + if (sem_post(semaphore_gestionnaires_signaux_atomique) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); BUG(1, uprintf("Unlock error !\n")); @@ -1491,7 +1698,11 @@ deverrouillage_gestionnaire_signaux() if (semaphore == 1) { +# ifndef SEMAPHORES_NOMMES if (sem_post(&semaphore_liste_threads) != 0) +# else + if (sem_post(semaphore_liste_threads) != 0) +# endif { pthread_sigmask(SIG_SETMASK, &oldset, NULL); @@ -1506,137 +1717,199 @@ deverrouillage_gestionnaire_signaux() return; } +#define test_signal(signal) \ + if (signal_test == SIGTEST) { signal_test = signal; return; } + +// Récupération des signaux +// - SIGINT (arrêt au clavier) +// - SIGTERM (signal d'arrêt en provenance du système) + void -interruption1(int signal, siginfo_t *siginfo, void *context) +interruption1(int signal) { - pthread_t thread; + test_signal(signal); - struct_processus *s_etat_processus; + switch(signal) + { + case SIGINT: + envoi_signal_processus(getpid(), rpl_sigint); + break; - volatile sig_atomic_t exclusion = 0; + case SIGTERM: + envoi_signal_processus(getpid(), rpl_sigterm); + break; + + case SIGALRM: + envoi_signal_processus(getpid(), rpl_sigalrm); + break; + } + + return; +} + +inline static void +signal_alrm(struct_processus *s_etat_processus, pid_t pid) +{ + struct_processus *s_thread_principal; verrouillage_gestionnaire_signaux(); - switch(signal) + if (pid == getpid()) { - case SIGALRM : + // Si pid est égal à getpid(), le signal à traiter est issu + // du même processus que celui qui va le traiter, mais d'un thread + // différent. + + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - if ((*siginfo).si_pid == getpid()) - { - if ((s_etat_processus = recherche_thread(getpid(), - pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } + printf("[%d] RPL/SIGALRM (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); + } - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) - { - printf("[%d] SIGALRM (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); - } + if ((*s_etat_processus).pid_processus_pere != getpid()) + { + // On n'est pas dans le processus père, on remonte le signal. + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigalrm); + } + else + { + // On est dans le processus père, on effectue un arrêt d'urgence. + (*s_etat_processus).var_volatile_alarme = -1; + (*s_etat_processus).var_volatile_requete_arret = -1; + } + } + else + { + // Le signal est issu d'un processus différent. On recherche le + // thread principal pour remonter le signal. - if ((*s_etat_processus).pid_processus_pere != getpid()) - { - kill((*s_etat_processus).pid_processus_pere, signal); - } - else - { - (*s_etat_processus).var_volatile_alarme = -1; - (*s_etat_processus).var_volatile_requete_arret = -1; - } - } - else - { - if (recherche_thread_principal(getpid(), &thread) == d_vrai) - { - pthread_kill(thread, signal); - } - } + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) + { + envoi_signal_contexte(s_thread_principal, rpl_sigalrm); + } + } - break; + deverrouillage_gestionnaire_signaux(); + return; +} + +inline static void +signal_term(struct_processus *s_etat_processus, pid_t pid) +{ + struct_processus *s_thread_principal; + volatile sig_atomic_t exclusion = 0; + + verrouillage_gestionnaire_signaux(); + + if (pid == getpid()) + { + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) + { + printf("[%d] RPL/SIGTERM (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); } - case SIGINT : + if ((*s_etat_processus).pid_processus_pere != getpid()) { - /* - * Une vieille spécification POSIX permet au pointeur siginfo - * d'être nul dans le cas d'un ^C envoyé depuis le clavier. - * Solaris suit en particulier cette spécification. - */ + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigterm); + } + else + { + (*s_etat_processus).var_volatile_traitement_sigint = -1; - if (siginfo == NULL) + while(exclusion == 1); + exclusion = 1; + + if ((*s_etat_processus).var_volatile_requete_arret == -1) { - kill(getpid(), signal); + deverrouillage_gestionnaire_signaux(); + exclusion = 0; + return; } - else if ((*siginfo).si_pid == getpid()) - { - if ((s_etat_processus = recherche_thread(getpid(), - pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) - { - printf("[%d] SIGINT (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); - } + (*s_etat_processus).var_volatile_requete_arret = -1; + (*s_etat_processus).var_volatile_alarme = -1; - if ((*s_etat_processus).pid_processus_pere != getpid()) - { - kill((*s_etat_processus).pid_processus_pere, signal); - } - else - { - (*s_etat_processus).var_volatile_traitement_sigint = -1; + exclusion = 0; + } + } + else + { + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) + { + envoi_signal_contexte(s_thread_principal, rpl_sigterm); + } + } - while(exclusion == 1); - exclusion = 1; + deverrouillage_gestionnaire_signaux(); + return; +} - if ((*s_etat_processus).var_volatile_requete_arret == -1) - { - deverrouillage_gestionnaire_signaux(); - exclusion = 0; - return; - } +inline static void +signal_int(struct_processus *s_etat_processus, pid_t pid) +{ + struct_processus *s_thread_principal; + volatile sig_atomic_t exclusion = 0; - if (strncmp(getenv("LANG"), "fr", 2) == 0) - { - printf("+++Interruption\n"); - } - else - { - printf("+++Interrupt\n"); - } + verrouillage_gestionnaire_signaux(); + + if (pid == getpid()) + { + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) + { + printf("[%d] RPL/SIGINT (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); + } - fflush(stdout); + if ((*s_etat_processus).pid_processus_pere != getpid()) + { + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigint); + } + else + { + (*s_etat_processus).var_volatile_traitement_sigint = -1; - (*s_etat_processus).var_volatile_requete_arret = -1; - (*s_etat_processus).var_volatile_alarme = -1; + while(exclusion == 1); + exclusion = 1; - exclusion = 0; - } + if ((*s_etat_processus).var_volatile_requete_arret == -1) + { + deverrouillage_gestionnaire_signaux(); + exclusion = 0; + return; + } + + if ((*s_etat_processus).langue == 'F') + { + printf("+++Interruption\n"); } else { - if (recherche_thread_principal(getpid(), &thread) == d_vrai) - { - pthread_kill(thread, signal); - } + printf("+++Interrupt\n"); } - break; - } + fflush(stdout); - default : + (*s_etat_processus).var_volatile_requete_arret = -1; + (*s_etat_processus).var_volatile_alarme = -1; + + exclusion = 0; + } + } + else + { + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - BUG(1, uprintf("[%d] Unknown signal %d in this context\n", - (int) getpid(), signal)); - break; + envoi_signal_contexte(s_thread_principal, rpl_sigint); } } @@ -1644,40 +1917,33 @@ interruption1(int signal, siginfo_t *sig return; } +// Récupération des signaux +// - SIGFSTP +// +// ATTENTION : +// Le signal SIGFSTP provient de la mort du processus de contrôle. +// Sous certains systèmes (Linux...), la mort du terminal de contrôle +// se traduit par l'envoi d'un SIGHUP au processus. Sur d'autres +// (SunOS), le processus reçoit un SIGFSTP avec une structure siginfo +// non initialisée (pointeur NULL) issue de TERMIO. + void -interruption2(int signal, siginfo_t *siginfo, void *context) +interruption2(int signal) { - pthread_t thread; - struct_processus *s_etat_processus; + test_signal(signal); + envoi_signal_processus(getpid(), rpl_sigtstp); + return; +} - verrouillage_gestionnaire_signaux(); +static inline void +signal_tstp(struct_processus *s_etat_processus, pid_t pid) +{ + struct_processus *s_thread_principal; - if (siginfo == NULL) - { - /* - * Le signal SIGFSTP provient de la mort du processus de contrôle. - * Sous certains systèmes (Linux...), la mort du terminal de contrôle - * se traduit par l'envoi d'un SIGHUP au processus. Sur d'autres - * (SunOS), le processus reçoit un SIGFSTP avec une structure siginfo - * non initialisée (pointeur NULL) issue de TERMIO. - */ + verrouillage_gestionnaire_signaux(); - if (recherche_thread_principal(getpid(), &thread) == d_vrai) - { - pthread_kill(thread, SIGHUP); - deverrouillage_gestionnaire_signaux(); - return; - } - } - else if ((*siginfo).si_pid == getpid()) + if (pid == getpid()) { - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) - == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } - /* * 0 => fonctionnement normal * -1 => requête @@ -1686,14 +1952,15 @@ interruption2(int signal, siginfo_t *sig if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGTSTP (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGTSTP (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } if ((*s_etat_processus).var_volatile_processus_pere == 0) { - kill((*s_etat_processus).pid_processus_pere, signal); + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigtstp); } else { @@ -1704,11 +1971,10 @@ interruption2(int signal, siginfo_t *sig { // Envoi d'un signal au thread maître du groupe. - if (recherche_thread_principal(getpid(), &thread) == d_vrai) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - pthread_kill(thread, SIGTSTP); - deverrouillage_gestionnaire_signaux(); - return; + envoi_signal_contexte(s_thread_principal, rpl_sigtstp); } } @@ -1717,12 +1983,35 @@ interruption2(int signal, siginfo_t *sig } void -interruption3(int signal, siginfo_t *siginfo, void *context) +interruption3(int signal) { - struct_processus *s_etat_processus; + // Si on passe par ici, c'est qu'il est impossible de récupérer + // l'erreur d'accès à la mémoire. On sort donc du programme quitte à + // ce qu'il reste des processus orphelins. + + unsigned char message[] = "+++System : Uncaught access violation\n" + "+++System : Aborting !\n"; + + test_signal(signal); + + if (pid_processus_pere == getpid()) + { + kill(pid_processus_pere, SIGALRM); + } - static int compteur = 0; + write(STDERR_FILENO, message, strlen(message)); + _exit(EXIT_FAILURE); +} + +#if 0 +// Utiliser libsigsegv +void INTERRUPTION3_A_FIXER() +{ + pthread_t thread; + + struct_processus *s_etat_processus; + test_signal(signal); verrouillage_gestionnaire_signaux(); if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -1749,24 +2038,79 @@ interruption3(int signal, siginfo_t *sig // Segfault dans une routine interne if (strncmp(getenv("LANG"), "fr", 2) == 0) { - printf("+++Système : Violation d'accès (dépassement de pile)\n"); + printf("+++Système : Violation d'accès\n"); } else { - printf("+++System : Access violation (stack overflow)\n"); + printf("+++System : Access violation\n"); } fflush(stdout); - compteur++; + (*s_etat_processus).compteur_violation_d_acces++; - if (compteur > 1) + if ((*s_etat_processus).compteur_violation_d_acces > 1) { + // On vient de récupérer plus d'une erreur de segmentation + // dans le même processus ou le même thread. L'erreur n'est pas + // récupérable et on sort autoritairement du programme. Il peut + // rester des processus orphelins en attente ! + + if (strncmp(getenv("LANG"), "fr", 2) == 0) + { + printf("+++Système : Violation d'accès, tentative de " + "terminaison de la tâche\n"); + printf(" (defauts multiples)\n"); + } + else + { + printf("+++System : Access violation, trying to kill task " + "(multiple defaults)\n"); + } + + fflush(stdout); + deverrouillage_gestionnaire_signaux(); exit(EXIT_FAILURE); } else { + // Première erreur de segmentation. On essaie de terminer + // proprement le thread ou le processus. Le signal ne peut être + // envoyé que depuis le même processus. + + if (recherche_thread_principal(getpid(), &thread) == d_vrai) + { + if (pthread_equal(thread, pthread_self()) != 0) + { + deverrouillage_gestionnaire_signaux(); + + if ((*s_etat_processus).pid_processus_pere != getpid()) + { + // On est dans le thread principal d'un processus. + + longjmp(contexte_processus, -1); + } + else + { + // On est dans le thread principal du processus + // père. + + longjmp(contexte_initial, -1); + } + } + else + { + // On est dans un thread fils d'un thread principal. + + deverrouillage_gestionnaire_signaux(); + longjmp(contexte_thread, -1); + } + } + + // Là, on ramasse les miettes puisque le thread n'existe plus + // dans la base (corruption de la mémoire). + deverrouillage_gestionnaire_signaux(); longjmp(contexte_initial, -1); } @@ -1775,44 +2119,36 @@ interruption3(int signal, siginfo_t *sig deverrouillage_gestionnaire_signaux(); return; } +#endif -void -interruption4(int signal, siginfo_t *siginfo, void *context) -{ - struct_processus *s_etat_processus; - - verrouillage_gestionnaire_signaux(); - - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } +// Traitement de rpl_sigstart - /* - * Démarrage d'un processus fils ou gestion de SIGCONT (SUSPEND) - */ +static inline void +signal_start(struct_processus *s_etat_processus, pid_t pid) +{ + (*s_etat_processus).demarrage_fils = d_vrai; + return; +} - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) - { - printf("[%d] SIGSTART/SIGCONT (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); - } +// Traitement de rpl_sigcont - deverrouillage_gestionnaire_signaux(); +static inline void +signal_cont(struct_processus *s_etat_processus, pid_t pid) +{ + (*s_etat_processus).redemarrage_processus = d_vrai; return; } -void -interruption5(int signal, siginfo_t *siginfo, void *context) +// Traitement de rpl_sigstop + +static inline void +signal_stop(struct_processus *s_etat_processus, pid_t pid) { - pthread_t thread; - struct_processus *s_etat_processus; + struct_processus *s_thread_principal; verrouillage_gestionnaire_signaux(); - if ((*siginfo).si_pid == getpid()) + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -1823,7 +2159,7 @@ interruption5(int signal, siginfo_t *sig if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGFSTOP (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGFSTOP (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } @@ -1846,25 +2182,31 @@ interruption5(int signal, siginfo_t *sig } else { - // Envoi d'un signal au thread maître du groupe. - - if (recherche_thread_principal(getpid(), &thread) == d_vrai) + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) + == NULL) { - pthread_kill(thread, SIGFSTOP); deverrouillage_gestionnaire_signaux(); return; } + + // Envoi d'un signal au thread maître du groupe. + + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) + { + envoi_signal_contexte(s_thread_principal, rpl_sigstop); + } } deverrouillage_gestionnaire_signaux(); return; } -void -interruption6(int signal, siginfo_t *siginfo, void *context) -{ - struct_processus *s_etat_processus; +// Traitement de rpl_siginject +static inline void +signal_inject(struct_processus *s_etat_processus, pid_t pid) +{ verrouillage_gestionnaire_signaux(); if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -1875,7 +2217,7 @@ interruption6(int signal, siginfo_t *sig if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGINJECT/SIGQUIT (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGINJECT (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } @@ -1884,42 +2226,34 @@ interruption6(int signal, siginfo_t *sig return; } +// Récupération des signaux +// - SIGPIPE + void -interruption7(int signal, siginfo_t *siginfo, void *context) +interruption5(int signal) { - struct_processus *s_etat_processus; + unsigned char message[] = "+++System : SIGPIPE\n" + "+++System : Aborting !\n"; - verrouillage_gestionnaire_signaux(); + test_signal(signal); - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } - - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) + if (pid_processus_pere == getpid()) { - printf("[%d] SIGPIPE (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); + envoi_signal_processus(pid_processus_pere, rpl_sigalrm); } - (*s_etat_processus).var_volatile_requete_arret = -1; - deverrouillage_gestionnaire_signaux(); - - BUG(1, printf("[%d] SIGPIPE\n", (int) getpid())); + write(STDERR_FILENO, message, strlen(message)); return; } -void -interruption8(int signal, siginfo_t *siginfo, void *context) +static inline void +signal_urg(struct_processus *s_etat_processus, pid_t pid) { - pthread_t thread; - struct_processus *s_etat_processus; + struct_processus *s_thread_principal; verrouillage_gestionnaire_signaux(); - if ((*siginfo).si_pid == getpid()) + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -1930,7 +2264,7 @@ interruption8(int signal, siginfo_t *sig if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGURG (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGURG (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } @@ -1942,11 +2276,10 @@ interruption8(int signal, siginfo_t *sig { // Envoi d'un signal au thread maître du groupe. - if (recherche_thread_principal(getpid(), &thread) == d_vrai) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - pthread_kill(thread, SIGURG); - deverrouillage_gestionnaire_signaux(); - return; + envoi_signal_contexte(s_thread_principal, rpl_sigurg); } } @@ -1954,10 +2287,12 @@ interruption8(int signal, siginfo_t *sig return; } -void -interruption9(int signal, siginfo_t *siginfo, void *context) +// Traitement de rpl_sigabort + +static inline void +signal_abort(struct_processus *s_etat_processus, pid_t pid) { - struct_processus *s_etat_processus; + struct_processus *s_thread_principal; verrouillage_gestionnaire_signaux(); @@ -1969,22 +2304,84 @@ interruption9(int signal, siginfo_t *sig if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGABORT/SIGPROF (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGABORT (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } - pthread_kill((*s_etat_processus).tid_processus_pere, SIGFSTOP); + if (pid == getpid()) + { + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) + == NULL) + { + deverrouillage_gestionnaire_signaux(); + return; + } + + (*s_etat_processus).arret_depuis_abort = -1; + + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) + { + printf("[%d] SIGFABORT (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); + } + + /* + * var_globale_traitement_retarde_stop : + * 0 -> traitement immédiat + * 1 -> traitement retardé (aucun signal reçu) + * -1 -> traitement retardé (un ou plusieurs signaux stop reçus) + */ + + if ((*s_etat_processus).var_volatile_traitement_retarde_stop == 0) + { + (*s_etat_processus).var_volatile_requete_arret = -1; + } + else + { + (*s_etat_processus).var_volatile_traitement_retarde_stop = -1; + } + } + else + { + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) + == NULL) + { + deverrouillage_gestionnaire_signaux(); + return; + } + + (*s_etat_processus).arret_depuis_abort = -1; + + // Envoi d'un signal au thread maître du groupe. + + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) + { + envoi_signal_contexte(s_thread_principal, rpl_sigabort); + } + } + deverrouillage_gestionnaire_signaux(); return; } +// Récupération des signaux +// - SIGHUP + void -interruption10(int signal, siginfo_t *siginfo, void *context) +interruption4(int signal) { - file *fichier; + test_signal(signal); + envoi_signal_processus(getpid(), rpl_sighup); + return; +} - struct_processus *s_etat_processus; +static inline void +signal_hup(struct_processus *s_etat_processus, pid_t pid) +{ + file *fichier; unsigned char nom[8 + 64 + 1]; @@ -2039,4 +2436,434 @@ traitement_exceptions_gsl(const char *re return; } +static inline void +envoi_interruptions(struct_processus *s_etat_processus, enum signaux_rpl signal, + pid_t pid_source) +{ + unsigned char message[] = "+++System : Spurious signa !\n"; + + switch(signal) + { + case rpl_sigint: + signal_int(s_etat_processus, pid_source); + break; + + case rpl_sigterm: + signal_term(s_etat_processus, pid_source); + break; + + case rpl_sigstart: + signal_start(s_etat_processus, pid_source); + break; + + case rpl_sigcont: + signal_cont(s_etat_processus, pid_source); + break; + + case rpl_sigstop: + signal_stop(s_etat_processus, pid_source); + break; + + case rpl_sigabort: + signal_abort(s_etat_processus, pid_source); + break; + + case rpl_sigurg: + signal_urg(s_etat_processus, pid_source); + break; + + case rpl_siginject: + signal_inject(s_etat_processus, pid_source); + break; + + case rpl_sigalrm: + signal_alrm(s_etat_processus, pid_source); + break; + + case rpl_sighup: + signal_hup(s_etat_processus, pid_source); + break; + + case rpl_sigtstp: + signal_tstp(s_etat_processus, pid_source); + break; + + default: + write(STDERR_FILENO, message, strlen(message)); + break; + } + + return; +} + +void +scrutation_interruptions(struct_processus *s_etat_processus) +{ + // Interruptions qui arrivent sur le processus depuis un + // processus externe. + + // Interruptions qui arrivent depuis le groupe courant de threads. + + if ((*s_etat_processus).pointeur_signal_lecture != + (*s_etat_processus).pointeur_signal_ecriture) + { + // Il y a un signal dans la queue du thread courant. On le traite. + } + + return; +} + +int +envoi_signal_processus(pid_t pid, enum signaux_rpl signal) +{ + // Il s'agit d'ouvrir le segment de mémoire partagée, de le projeter en + // mémoire puis d'y inscrire le signal à traiter. + + return(0); +} + +int +envoi_signal_thread(pthread_t tid, enum signaux_rpl signal) +{ + // Un signal est envoyé d'un thread à un autre thread du même processus. + + return(0); +} + +int +envoi_signal_contexte(struct_processus *s_etat_processus_a_signaler, + enum signaux_rpl signal) +{ + (*s_etat_processus_a_signaler).signaux_en_queue + [(*s_etat_processus_a_signaler).pointeur_signal_ecriture + 1] = + signal; + + // On valide l'écriture. Cela évite l'utilisation d'un mutex. + + (*s_etat_processus_a_signaler).pointeur_signal_ecriture++; + return(0); +} + + +/* +================================================================================ + Fonction renvoyant le nom du segment de mémoire partagée en fonction + du pid du processus. +================================================================================ + Entrée : Chemin absolue servant de racine, pid du processus +-------------------------------------------------------------------------------- + Sortie : NULL ou nom du segment +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ + +static unsigned char * +nom_segment(unsigned char *chemin, pid_t pid) +{ + unsigned char *fichier; + +# ifdef IPCS_SYSV // !POSIX +# ifndef OS2 // !OS2 + + if ((fichier = malloc((strlen(chemin) + 1 + 256 + 1) * + sizeof(unsigned char))) == NULL) + { + return(NULL); + } + + sprintf(fichier, "%s/RPL-SIGQUEUES-%d", chemin, (int) pid); +# else // OS2 + if ((fichier = malloc((10 + 256 + 1) * sizeof(unsigned char))) + == NULL) + { + return(NULL); + } + + sprintf(fichier, "\\SHAREMEM\\RPL-SIGQUEUES-%d", (int) pid); +# endif // OS2 +# else // POSIX + + if ((fichier = malloc((1 + 256 + 1) * + sizeof(unsigned char))) == NULL) + { + return(NULL); + } + + sprintf(fichier, "/RPL-SIGQUEUES-%d", (int) pid); +# endif + + return(fichier); +} + + +/* +================================================================================ + Fonction créant un segment de mémoire partagée destiné à contenir + la queue des signaux. +================================================================================ + Entrée : structure de description du processus +-------------------------------------------------------------------------------- + Sortie : Néant +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ + +void +creation_queue_signaux(struct_processus *s_etat_processus) +{ + int segment; + + pthread_mutexattr_t attributs_mutex; + + unsigned char *nom; + +# ifndef IPCS_SYSV // POSIX + + if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, + getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if ((segment = shm_open(nom, O_RDWR | O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR)) == -1) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (ftruncate(segment, sizeof(struct_queue_signaux)) == -1) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + s_queue_signaux = mmap(NULL, sizeof(struct_queue_signaux), + PROT_READ | PROT_WRITE, MAP_SHARED, segment, 0); + close(segment); + + if (((void *) s_queue_signaux) == ((void *) -1)) + { + if (shm_unlink(nom) == -1) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + free(nom); + + pthread_mutexattr_init(&attributs_mutex); + pthread_mutexattr_settype(&attributs_mutex, PTHREAD_MUTEX_NORMAL); + pthread_mutex_init(&((*s_queue_signaux).mutex), &attributs_mutex); + pthread_mutexattr_destroy(&attributs_mutex); + + (*s_queue_signaux).pointeur_lecture = 0; + (*s_queue_signaux).pointeur_ecriture = 0; + +# else // SystemV +# ifndef OS2 + + file *desc; + + key_t clef; + + // Création d'un segment de données associé au PID du processus courant + + chemin = (*s_etat_processus).chemin_fichiers_temporaires; + + if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, + getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if ((desc = fopen(nom, "w")) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_erreur_fichier; + return; + } + + fclose(desc); + + if ((clef = ftok(nom, 1)) == -1) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + free(nom); + + if ((segment = shmget(clef, + nombre_queues * ((2 * longueur_queue) + 4) * sizeof(int), + IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)) == -1) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + fifos = shmat(segment, NULL, 0); + + if (((void *) fifos) == ((void *) -1)) + { + if (shmctl(segment, IPC_RMID, 0) == -1) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + +# else + + if ((nom = nom_segment(NULL, getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (DosAllocSharedMem(&ptr_os2, nom, nombre_queues * + ((2 * longueur_queue) + 4) * sizeof(int), + PAG_WRITE | PAG_READ | PAG_COMMIT) != 0) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + free(nom); + fifos = ptr_os2; + +# endif +# endif + + return; +} + + +/* +================================================================================ + Fonction libérant le segment de mémoire partagée destiné à contenir + la queue des signaux. +================================================================================ + Entrée : structure de description du processus +-------------------------------------------------------------------------------- + Sortie : Néant +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ + +void +liberation_queue_signaux(struct_processus *s_etat_processus) +{ +# ifdef IPCS_SYSV // SystemV +# ifndef OS2 +# else // OS/2 +# endif +# else // POSIX + if (munmap(s_queue_signaux, sizeof(struct_queue_signaux)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } +# endif + + return; +} + + +/* +================================================================================ + Fonction détruisant le segment de mémoire partagée destiné à contenir + la queue des signaux. +================================================================================ + Entrée : structure de description du processus +-------------------------------------------------------------------------------- + Sortie : Néant +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ + +void +destruction_queue_signaux(struct_processus *s_etat_processus) +{ + unsigned char *nom; + +# ifdef IPCS_SYSV // SystemV +# ifndef OS2 + + if (shmdt(fifos) == -1) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (shmctl(segment, IPC_RMID, 0) == -1) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, + getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + unlink(nom); + free(nom); + +# else + + if (DosFreeMem(fifos) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + +# endif +# else // POSIX + + if (munmap(s_queue_signaux, sizeof(struct_queue_signaux)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if ((nom = nom_segment(NULL, getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (shm_unlink(nom) != 0) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + free(nom); + +# endif + + return; +} + // vim: ts=4