--- rpl/src/interruptions.c 2010/05/22 21:45:50 1.17 +++ rpl/src/interruptions.c 2011/04/11 12:10:11 1.47 @@ -1,7 +1,7 @@ /* ================================================================================ - RPL/2 (R) version 4.0.15 - Copyright (C) 1989-2010 Dr. BERTRAND Joël + RPL/2 (R) version 4.1.0.prerelease.0 + 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" /* @@ -125,7 +125,6 @@ insertion_thread(struct_processus *s_eta } (*l_nouvel_objet).suivant = liste_threads; - liste_threads = l_nouvel_objet; # ifndef SEMAPHORES_NOMMES @@ -184,6 +183,10 @@ insertion_thread_surveillance(struct_pro } } + 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; @@ -375,7 +378,8 @@ retrait_thread_surveillance(struct_proce (*l_element_precedent).suivant = (*l_element_courant).suivant; } - 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); @@ -397,7 +401,8 @@ 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); @@ -412,11 +417,13 @@ 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); @@ -448,6 +455,7 @@ retrait_thread_surveillance(struct_proce pthread_sigmask(SIG_SETMASK, &oldset, NULL); sigpending(&set); + return; } @@ -669,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); @@ -1291,15 +1357,8 @@ 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); @@ -1314,7 +1373,16 @@ 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); @@ -1322,11 +1390,23 @@ liberation_threads(struct_processus *s_e } 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); @@ -1524,12 +1604,12 @@ verrouillage_gestionnaire_signaux() // ce que ce soit possible. # ifndef SEMAPHORES_NOMMES - while(sem_trywait(&semaphore_liste_threads) == -1) + while(sem_wait(&semaphore_liste_threads) == -1) # else - while(sem_trywait(semaphore_liste_threads) == -1) + while(sem_wait(semaphore_liste_threads) == -1) # endif { - if ((errno != EINTR) && (errno != EAGAIN)) + if (errno != EINTR) { pthread_sigmask(SIG_SETMASK, &oldset, NULL); @@ -1545,8 +1625,6 @@ verrouillage_gestionnaire_signaux() BUG(1, uprintf("Lock error !\n")); return; } - - sched_yield(); } } @@ -1657,8 +1735,10 @@ deverrouillage_gestionnaire_signaux() } void -interruption1(int signal, siginfo_t *siginfo, void *context) +interruption1(SIGHANDLER_ARGS) { + pid_t pid; + pthread_t thread; struct_processus *s_etat_processus; @@ -1667,17 +1747,40 @@ interruption1(int signal, siginfo_t *sig verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + if ((signal == SIGINT) || (signal == SIGTERM)) + { + // Si l'interruption provient du clavier, il n'y a pas eu d'appel + // à queue_in(). + + pid = getpid(); + } + else + { + pid = origine_signal(signal); + } +# else + if (siginfo != NULL) + { + pid = (*siginfo).si_pid; + } + else + { + pid = getpid(); + } +# endif + switch(signal) { case SIGALRM : { - if ((*siginfo).si_pid == getpid()) + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); - return; + return; } if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) @@ -1709,6 +1812,7 @@ interruption1(int signal, siginfo_t *sig } case SIGINT : + case SIGTERM : { /* * Une vieille spécification POSIX permet au pointeur siginfo @@ -1716,11 +1820,14 @@ interruption1(int signal, siginfo_t *sig * Solaris suit en particulier cette spécification. */ +# ifndef _BROKEN_SIGINFO if (siginfo == NULL) { kill(getpid(), signal); } - else if ((*siginfo).si_pid == getpid()) + else +# endif + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -1731,8 +1838,17 @@ interruption1(int signal, siginfo_t *sig if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGINT (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); + if (signal == SIGINT) + { + printf("[%d] SIGINT (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + } + else + { + printf("[%d] SIGTERM (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + } + fflush(stdout); } @@ -1754,16 +1870,19 @@ interruption1(int signal, siginfo_t *sig return; } - if (strncmp(getenv("LANG"), "fr", 2) == 0) - { - printf("+++Interruption\n"); - } - else + if (signal == SIGINT) { - printf("+++Interrupt\n"); - } + if (strncmp(getenv("LANG"), "fr", 2) == 0) + { + printf("+++Interruption\n"); + } + else + { + printf("+++Interrupt\n"); + } - fflush(stdout); + fflush(stdout); + } (*s_etat_processus).var_volatile_requete_arret = -1; (*s_etat_processus).var_volatile_alarme = -1; @@ -1795,13 +1914,30 @@ interruption1(int signal, siginfo_t *sig } void -interruption2(int signal, siginfo_t *siginfo, void *context) +interruption2(SIGHANDLER_ARGS) { + pid_t pid; + pthread_t thread; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + if (siginfo != NULL) + { + pid = (*siginfo).si_pid; + } + else + { + pid = getpid(); + } +# endif + +# ifndef _BROKEN_SIGINFO if (siginfo == NULL) { /* @@ -1819,7 +1955,9 @@ interruption2(int signal, siginfo_t *sig return; } } - else if ((*siginfo).si_pid == getpid()) + else +# endif + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -1867,14 +2005,22 @@ interruption2(int signal, siginfo_t *sig } void -interruption3(int signal, siginfo_t *siginfo, void *context) +interruption3(SIGHANDLER_ARGS) { + pid_t pid; + struct_processus *s_etat_processus; static int compteur = 0; verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); @@ -1927,12 +2073,20 @@ interruption3(int signal, siginfo_t *sig } void -interruption4(int signal, siginfo_t *siginfo, void *context) +interruption4(SIGHANDLER_ARGS) { + pid_t pid; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); @@ -1955,14 +2109,23 @@ interruption4(int signal, siginfo_t *sig } void -interruption5(int signal, siginfo_t *siginfo, void *context) +interruption5(SIGHANDLER_ARGS) { + pid_t pid; + pthread_t thread; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); - if ((*siginfo).si_pid == getpid()) +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -2018,12 +2181,20 @@ interruption5(int signal, siginfo_t *sig } void -interruption6(int signal, siginfo_t *siginfo, void *context) +interruption6(SIGHANDLER_ARGS) { + pid_t pid; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); @@ -2042,12 +2213,20 @@ interruption6(int signal, siginfo_t *sig } void -interruption7(int signal, siginfo_t *siginfo, void *context) +interruption7(SIGHANDLER_ARGS) { + pid_t pid; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); @@ -2069,14 +2248,23 @@ interruption7(int signal, siginfo_t *sig } void -interruption8(int signal, siginfo_t *siginfo, void *context) +interruption8(SIGHANDLER_ARGS) { + pid_t pid; + pthread_t thread; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); - if ((*siginfo).si_pid == getpid()) +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) @@ -2112,12 +2300,20 @@ interruption8(int signal, siginfo_t *sig } void -interruption9(int signal, siginfo_t *siginfo, void *context) +interruption9(SIGHANDLER_ARGS) { + pid_t pid; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); @@ -2131,22 +2327,40 @@ interruption9(int signal, siginfo_t *sig fflush(stdout); } +# ifdef _BROKEN_SIGINFO + if (queue_in(getpid(), signal) != 0) + { + return; + } + + deverrouillage_gestionnaire_signaux(); + interruption11(signal); +# else deverrouillage_gestionnaire_signaux(); interruption11(signal, siginfo, context); +# endif return; } void -interruption10(int signal, siginfo_t *siginfo, void *context) +interruption10(SIGHANDLER_ARGS) { file *fichier; + pid_t pid; + struct_processus *s_etat_processus; unsigned char nom[8 + 64 + 1]; verrouillage_gestionnaire_signaux(); +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); @@ -2178,14 +2392,23 @@ interruption10(int signal, siginfo_t *si } void -interruption11(int signal, siginfo_t *siginfo, void *context) +interruption11(SIGHANDLER_ARGS) { + pid_t pid; + pthread_t thread; + struct_processus *s_etat_processus; verrouillage_gestionnaire_signaux(); - if ((*siginfo).si_pid == getpid()) +# ifdef _BROKEN_SIGINFO + pid = origine_signal(signal); +# else + pid = (*siginfo).si_pid; +# endif + + if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL)