--- rpl/src/semaphores.c 2011/09/20 07:16:40 1.33 +++ rpl/src/semaphores.c 2011/09/20 14:36:30 1.34 @@ -38,7 +38,23 @@ nom_segment_semaphore(pid_t pid) return(NULL); } - sprintf(fichier, "/RPL-SIGSEMAPHORE-%d", (int) pid); + sprintf(fichier, "/RPL-SS-%d", (int) pid); + return(fichier); +} + + +static unsigned char * +nom_segment_semaphore_thread(pid_t pid, pthread_t tid) +{ + unsigned char *fichier; + + if ((fichier = malloc((1 + 256 + 1) * sizeof(unsigned char))) == NULL) + { + return(NULL); + } + + sprintf(fichier, "/RPL-SS-%d-%llx", (int) pid, + (unsigned long long) tid); return(fichier); } @@ -68,18 +84,31 @@ sem_init2(unsigned int valeur, pid_t pid return(SEM_FAILED); } - semaphore = sem_open(chemin, O_CREAT, (S_IRUSR | S_IWUSR), valeur); + semaphore = sem_open(chemin, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, + valeur); free(chemin); - for(i = 0; i < valeur; i++) + return(semaphore); +} + + +sem_t * +sem_init3(unsigned int valeur, pid_t pid, pthread_t tid) +{ + sem_t *semaphore; + + unsigned char *chemin; + unsigned int i; + + if ((chemin = nom_segment_semaphore_thread(pid, tid)) == NULL) { - if (sem_post(semaphore) != 0) - { - sem_close(semaphore); - return(SEM_FAILED); - } + return(SEM_FAILED); } + semaphore = sem_open(chemin, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR, + valeur); + free(chemin); + return(semaphore); } @@ -93,7 +122,7 @@ sem_open2(pid_t pid) if ((chemin = nom_segment_semaphore(pid)) == NULL) { - return(1); + return(SEM_FAILED); } semaphore = sem_open(chemin, O_RDWR); @@ -123,6 +152,28 @@ sem_destroy2(sem_t *semaphore, pid_t pid return(erreur); } + +int +sem_destroy3(sem_t *semaphore, pid_t pid, pthread_t tid) +{ + int erreur; + + unsigned char *chemin; + + sem_close(semaphore); + + if ((chemin = nom_segment_semaphore_thread(pid, tid)) == NULL) + { + return(1); + } + + erreur = sem_unlink(chemin); + free(chemin); + + return(erreur); +} + + #undef sem_post #undef sem_wait #undef sem_trywait @@ -236,18 +287,20 @@ sem_init_SysV(sem_t *semaphore, int shar S_IRUSR | S_IWUSR); (*semaphore).path = NULL; (*semaphore).pid = getpid(); + (*semaphore).tid = pthread_self(); (*semaphore).alloue = 0; } else { // Sémaphore partagé entre plusieurs processus if (((*semaphore).path = malloc((strlen(racine_segment) - + 1 + 32 + 1) * sizeof(unsigned char))) == NULL) + + 2 + 256 + 1) * sizeof(unsigned char))) == NULL) { return(-1); } - sprintf((*semaphore).path, "%s/RPL-SIGSEMAPHORE-%d", racine_segment, + sprintf((*semaphore).path, "%s/RPL-SIGSEMAPHORE-%d-%llu", + racine_segment, pthread_self(), (int) getpid()); if ((desc = open((*semaphore).path, O_RDWR | O_CREAT | O_EXCL, @@ -258,6 +311,7 @@ sem_init_SysV(sem_t *semaphore, int shar } (*semaphore).pid = getpid(); + (*semaphore).tid = pthread_slef(); clef = ftok((*semaphore).path, 1); close(desc); @@ -638,6 +692,8 @@ sem_t } (*semaphore).alloue = -1; + (*semaphore).pid = getpid(); + (*semaphore).tid = pthread_self(); # else if ((nom_segment = malloc((strlen(racine_memoire_OS2) + strlen(nom) + 1) @@ -954,15 +1010,6 @@ sem_unlink_SysV(const char *nom) { // Détruit un sémaphore nommé créé par sem_open_SysV() # ifndef OS2 // IPCS_SYSV - unsigned char *nom_absolu; - - if ((nom_absolu = malloc((strlen(racine_segment) + strlen(nom) - + 2) * sizeof(unsigned char))) == NULL) - { - return(ENOMEM); - } - - sprintf(nom_absolu, "%s/%s", racine_segment, nom); semctl(semget(ftok(nom_absolu, 1), 0, 0), 0, IPC_RMID); if (unlink(nom_absolu) == -1)