version 1.33, 2011/09/20 07:16:40
|
version 1.67, 2015/01/05 15:32:25
|
Line 1
|
Line 1
|
/* |
/* |
================================================================================ |
================================================================================ |
RPL/2 (R) version 4.1.3 |
RPL/2 (R) version 4.1.20 |
Copyright (C) 1989-2011 Dr. BERTRAND Joël |
Copyright (C) 1989-2015 Dr. BERTRAND Joël |
|
|
This file is part of RPL/2. |
This file is part of RPL/2. |
|
|
Line 22
|
Line 22
|
|
|
#include "rpl-conv.h" |
#include "rpl-conv.h" |
|
|
#ifdef SEMAPHORES_NOMMES |
|
|
|
// Les fonctions suivantes ne sont utilisées que dans le cas d'un |
// Les fonctions suivantes ne sont utilisées que dans le cas d'un |
// système POSIX qui ne possède pas de sémaphores anonymes. MacOS X |
// système POSIX qui ne possède pas de sémaphores anonymes. MacOS X |
// est dans ce cas. |
// est dans ce cas. |
|
|
static unsigned char * |
static unsigned char * |
nom_segment_semaphore(pid_t pid) |
nom_segment_semaphore(pid_t pid, int ordre) |
{ |
{ |
unsigned char *fichier; |
unsigned char *fichier; |
|
|
Line 38 nom_segment_semaphore(pid_t pid)
|
Line 37 nom_segment_semaphore(pid_t pid)
|
return(NULL); |
return(NULL); |
} |
} |
|
|
sprintf(fichier, "/RPL-SIGSEMAPHORE-%d", (int) pid); |
sprintf(fichier, "/RPL-%llu-%d", (unsigned long long) pid, ordre); |
|
return(fichier); |
|
} |
|
|
|
|
|
static unsigned char * |
|
nom_segment_semaphore_thread(pid_t pid, pthread_t tid, int ordre) |
|
{ |
|
unsigned char *fichier; |
|
|
|
if ((fichier = malloc((1 + 256 + 1) * sizeof(unsigned char))) == NULL) |
|
{ |
|
return(NULL); |
|
} |
|
|
|
sprintf(fichier, "/RPL-%llu-%llu-%d", (unsigned long long) pid, |
|
(unsigned long long) tid, ordre); |
return(fichier); |
return(fichier); |
} |
} |
|
|
Line 56 nom_segment_semaphore(pid_t pid)
|
Line 71 nom_segment_semaphore(pid_t pid)
|
*/ |
*/ |
|
|
sem_t * |
sem_t * |
sem_init2(unsigned int valeur, pid_t pid) |
sem_init2(unsigned int valeur, pid_t pid, int ordre) |
{ |
{ |
sem_t *semaphore; |
sem_t *semaphore; |
|
|
unsigned char *chemin; |
unsigned char *chemin; |
unsigned int i; |
|
|
|
if ((chemin = nom_segment_semaphore(pid)) == NULL) |
if ((chemin = nom_segment_semaphore(pid, ordre)) == NULL) |
{ |
{ |
return(SEM_FAILED); |
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); |
free(chemin); |
|
|
for(i = 0; i < valeur; i++) |
return(semaphore); |
|
} |
|
|
|
|
|
sem_t * |
|
sem_init3(unsigned int valeur, pid_t pid, pthread_t tid, int ordre) |
|
{ |
|
sem_t *semaphore; |
|
|
|
unsigned char *chemin; |
|
|
|
if ((chemin = nom_segment_semaphore_thread(pid, tid, ordre)) == NULL) |
{ |
{ |
if (sem_post(semaphore) != 0) |
return(SEM_FAILED); |
{ |
|
sem_close(semaphore); |
|
return(SEM_FAILED); |
|
} |
|
} |
} |
|
|
|
semaphore = sem_open(chemin, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR, |
|
valeur); |
|
free(chemin); |
|
|
return(semaphore); |
return(semaphore); |
} |
} |
|
|
|
|
sem_t * |
sem_t * |
sem_open2(pid_t pid) |
sem_open2(pid_t pid, int ordre) |
{ |
{ |
unsigned char *chemin; |
unsigned char *chemin; |
|
|
sem_t *semaphore; |
sem_t *semaphore; |
|
|
if ((chemin = nom_segment_semaphore(pid)) == NULL) |
if ((chemin = nom_segment_semaphore(pid, ordre)) == NULL) |
{ |
{ |
return(1); |
return(SEM_FAILED); |
} |
} |
|
|
semaphore = sem_open(chemin, O_RDWR); |
semaphore = sem_open(chemin, O_RDWR); |
Line 104 sem_open2(pid_t pid)
|
Line 130 sem_open2(pid_t pid)
|
|
|
|
|
int |
int |
sem_destroy2(sem_t *semaphore, pid_t pid) |
sem_destroy2(sem_t *semaphore, pid_t pid, int ordre) |
{ |
{ |
int erreur; |
int erreur; |
|
|
Line 112 sem_destroy2(sem_t *semaphore, pid_t pid
|
Line 138 sem_destroy2(sem_t *semaphore, pid_t pid
|
|
|
sem_close(semaphore); |
sem_close(semaphore); |
|
|
if ((chemin = nom_segment_semaphore(pid)) == NULL) |
if ((chemin = nom_segment_semaphore(pid, ordre)) == NULL) |
|
{ |
|
return(1); |
|
} |
|
|
|
erreur = sem_unlink(chemin); |
|
free(chemin); |
|
|
|
return(erreur); |
|
} |
|
|
|
|
|
int |
|
sem_destroy3(sem_t *semaphore, pid_t pid, pthread_t tid, int ordre) |
|
{ |
|
int erreur; |
|
|
|
unsigned char *chemin; |
|
|
|
sem_close(semaphore); |
|
|
|
if ((chemin = nom_segment_semaphore_thread(pid, tid, ordre)) == NULL) |
{ |
{ |
return(1); |
return(1); |
} |
} |
Line 123 sem_destroy2(sem_t *semaphore, pid_t pid
|
Line 170 sem_destroy2(sem_t *semaphore, pid_t pid
|
return(erreur); |
return(erreur); |
} |
} |
|
|
#undef sem_post |
|
#undef sem_wait |
|
#undef sem_trywait |
|
|
|
int |
int |
sem_getvalue2(sem_t *semaphore, int *valeur) |
sem_getvalue2(sem_t *semaphore, int *valeur) |
Line 192 sem_getvalue2(sem_t *semaphore, int *val
|
Line 236 sem_getvalue2(sem_t *semaphore, int *val
|
return(0); |
return(0); |
} |
} |
|
|
#endif |
|
|
#undef sem_post |
|
#undef sem_wait |
|
#undef sem_trywait |
|
|
|
|
#ifdef IPCS_SYSV |
#ifdef IPCS_SYSV |
|
|
Line 236 sem_init_SysV(sem_t *semaphore, int shar
|
Line 284 sem_init_SysV(sem_t *semaphore, int shar
|
S_IRUSR | S_IWUSR); |
S_IRUSR | S_IWUSR); |
(*semaphore).path = NULL; |
(*semaphore).path = NULL; |
(*semaphore).pid = getpid(); |
(*semaphore).pid = getpid(); |
|
(*semaphore).tid = pthread_self(); |
(*semaphore).alloue = 0; |
(*semaphore).alloue = 0; |
} |
} |
else |
else |
{ |
{ |
// Sémaphore partagé entre plusieurs processus |
// Sémaphore partagé entre plusieurs processus |
if (((*semaphore).path = malloc((strlen(racine_segment) |
if (((*semaphore).path = malloc((strlen(racine_segment) |
+ 1 + 32 + 1) * sizeof(unsigned char))) == NULL) |
+ 2 + 256 + 1) * sizeof(unsigned char))) == NULL) |
{ |
{ |
return(-1); |
return(-1); |
} |
} |
|
|
sprintf((*semaphore).path, "%s/RPL-SIGSEMAPHORE-%d", racine_segment, |
sprintf((*semaphore).path, "%s/RPL-SEMAPHORE-%d-%llX-%llX", |
(int) getpid()); |
racine_segment, (int) getpid(), |
|
(long long unsigned) pthread_self(), |
|
(long long unsigned) semaphore); |
|
|
if ((desc = open((*semaphore).path, O_RDWR | O_CREAT | O_EXCL, |
if ((desc = open((*semaphore).path, O_RDWR | O_CREAT | O_EXCL, |
S_IRUSR | S_IWUSR)) == -1) |
S_IRUSR | S_IWUSR)) == -1) |
Line 258 sem_init_SysV(sem_t *semaphore, int shar
|
Line 309 sem_init_SysV(sem_t *semaphore, int shar
|
} |
} |
|
|
(*semaphore).pid = getpid(); |
(*semaphore).pid = getpid(); |
|
(*semaphore).tid = pthread_self(); |
clef = ftok((*semaphore).path, 1); |
clef = ftok((*semaphore).path, 1); |
close(desc); |
close(desc); |
|
|
Line 278 sem_init_SysV(sem_t *semaphore, int shar
|
Line 330 sem_init_SysV(sem_t *semaphore, int shar
|
return(-1); |
return(-1); |
} |
} |
|
|
argument.val = valeur; |
argument.val = (int) valeur; |
ios = semctl((*semaphore).sem, 0, SETVAL, argument); |
ios = semctl((*semaphore).sem, 0, SETVAL, argument); |
|
|
return(ios); |
return(ios); |
# else // OS/2 |
# else // OS/2 |
|
sem_t *psem; |
|
|
sem_t *psem; |
psem = semaphore; |
|
|
psem = semaphore; |
|
|
|
if (shared != 0) |
|
{ |
|
errno = ENOSYS; |
|
return(-1); |
|
} |
|
|
|
if (((*psem).cnt = malloc(sizeof(ULONG))) == NULL) |
|
{ |
|
free(psem); |
|
errno = ENOMEM; |
|
return(-1); |
|
} |
|
|
|
if (((*psem).nopened = malloc(sizeof(ULONG))) == NULL) |
if (((*psem).cnt = malloc(sizeof(ULONG))) == NULL) |
{ |
{ |
free((*psem).cnt); |
free(psem); |
free(psem); |
errno = ENOMEM; |
errno = ENOMEM; |
return(-1); |
return(-1); |
} |
} |
|
|
|
if (DosCreateMutexSem(NULL, &((*psem).hmtx), 0, 0) != 0) |
if (((*psem).nopened = malloc(sizeof(ULONG))) == NULL) |
{ |
{ |
free((*psem).cnt); |
free((*psem).cnt); |
free((*psem).nopened); |
free(psem); |
free(psem); |
errno = ENOMEM; |
return(-1); |
return(-1); |
} |
} |
|
|
if (DosCreateEventSem(NULL, &((*psem).hev), 0, (valeur != 0) ? 1 : 0) != 0) |
if (DosCreateMutexSem(NULL, &((*psem).hmtx), 0, 0) != 0) |
{ |
{ |
DosCloseMutexSem((*psem).hmtx); |
free((*psem).cnt); |
free((*psem).cnt); |
free((*psem).nopened); |
free((*psem).nopened); |
free(psem); |
free(psem); |
return(-1); |
return(-1); |
} |
} |
|
|
|
(*(*psem).cnt) = valeur; |
if (DosCreateEventSem(NULL, &((*psem).hev), 0, (valeur != 0) ? 1 : 0) |
(*(*psem).nopened) = 1; |
!= 0) |
(*psem).shared = shared; |
{ |
(*psem).allocated = 0; |
DosCloseMutexSem((*psem).hmtx); |
|
free((*psem).cnt); |
|
free((*psem).nopened); |
|
free(psem); |
|
return(-1); |
|
} |
|
|
return(0); |
(*(*psem).cnt) = valeur; |
|
(*(*psem).nopened) = 1; |
|
(*psem).shared = shared; |
|
(*psem).allocated = 0; |
|
|
|
return(0); |
# endif |
# endif |
} |
} |
|
|
Line 359 sem_destroy_SysV(sem_t *semaphore)
|
Line 404 sem_destroy_SysV(sem_t *semaphore)
|
|
|
return(0); |
return(0); |
# else // OS/2 |
# else // OS/2 |
|
sem_t *psem; |
|
|
sem_t *psem; |
psem = semaphore; |
|
|
psem = semaphore; |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
|
{ |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
return(EINVAL); |
{ |
} |
return(EINVAL); |
|
} |
|
|
|
if (DosCloseMutexSem((*psem).hmtx) != 0) |
if (DosCloseMutexSem((*psem).hmtx) != 0) |
{ |
{ |
return(EINVAL); |
return(EINVAL); |
} |
} |
|
|
while(DosCloseEventSem((*psem).hev) == ERROR_SEM_BUSY) |
while(DosCloseEventSem((*psem).hev) == ERROR_SEM_BUSY) |
{ |
{ |
DosPostEventSem((*psem).hev); |
DosPostEventSem((*psem).hev); |
} |
} |
|
|
(*(*psem).nopened)--; |
(*(*psem).nopened)--; |
|
|
if ((*psem).shared == 0) |
if ((*psem).shared == 0) |
{ |
|
free((*psem).cnt); |
|
free((*psem).nopened); |
|
} |
|
else |
|
{ |
|
if ((*(*psem).nopened) == 0) |
|
{ |
{ |
DosFreeMem((*psem).cnt); |
free((*psem).cnt); |
|
free((*psem).nopened); |
|
} |
|
else |
|
{ |
|
if ((*(*psem).nopened) == 0) |
|
{ |
|
DosFreeMem((*psem).cnt); |
|
} |
} |
} |
} |
|
|
|
if ((*psem).allocated != 0) |
|
{ |
|
free(psem); |
|
} |
|
|
|
return(0); |
if ((*psem).allocated != 0) |
|
{ |
|
free(psem); |
|
} |
|
|
|
return(0); |
# endif |
# endif |
} |
} |
|
|
Line 410 sem_wait_SysV(sem_t *semaphore)
|
Line 453 sem_wait_SysV(sem_t *semaphore)
|
# ifndef OS2 // IPCS_SYSV |
# ifndef OS2 // IPCS_SYSV |
struct sembuf commande; |
struct sembuf commande; |
|
|
|
// semop() ne renvoie pas EINTR sur un signal ! |
|
|
commande.sem_num = 0; |
commande.sem_num = 0; |
commande.sem_op = -1; |
commande.sem_op = -1; |
commande.sem_flg = 0; |
commande.sem_flg = 0; |
|
|
while(semop((*semaphore).sem, &commande, 1) == -1) |
if (semop((*semaphore).sem, &commande, 1) == -1) |
{ |
{ |
if (errno != EINTR) |
if (errno != EAGAIN) |
{ |
{ |
errno = EINVAL; |
errno = EINVAL; |
return(-1); |
return(-1); |
} |
} |
|
else |
|
{ |
|
return(-1); |
|
} |
} |
} |
|
|
return(0); |
return(0); |
# else // OS/2 |
# else // OS/2 |
|
sem_t *psem; |
|
|
sem_t *psem; |
ULONG cnt; |
|
|
ULONG cnt; |
|
|
|
psem = semaphore; |
|
|
|
if (DosWaitEventSem((*psem).hev, SEM_INDEFINITE_WAIT) != 0) |
psem = semaphore; |
{ |
|
errno = EINVAL; |
|
return(-1); |
|
} |
|
|
|
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
if (DosWaitEventSem((*psem).hev, SEM_INDEFINITE_WAIT) != 0) |
{ |
{ |
errno = EINVAL; |
errno = EINVAL; |
return(-1); |
return(-1); |
} |
} |
|
|
if ((*(*psem).cnt) > 0) |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
{ |
{ |
(*(*psem).cnt)--; |
errno = EINVAL; |
} |
return(-1); |
|
} |
|
|
if ((*(*psem).cnt) == 0) |
if ((*(*psem).cnt) > 0) |
{ |
{ |
DosResetEventSem((*psem).hev, &cnt); |
(*(*psem).cnt)--; |
} |
} |
|
|
DosReleaseMutexSem((*psem).hmtx); |
if ((*(*psem).cnt) == 0) |
return(0); |
{ |
|
DosResetEventSem((*psem).hev, &cnt); |
|
} |
|
|
|
DosReleaseMutexSem((*psem).hmtx); |
|
return(0); |
# endif |
# endif |
} |
} |
|
|
Line 470 sem_trywait_SysV(sem_t *semaphore)
|
Line 517 sem_trywait_SysV(sem_t *semaphore)
|
commande.sem_op = -1; |
commande.sem_op = -1; |
commande.sem_flg = IPC_NOWAIT; |
commande.sem_flg = IPC_NOWAIT; |
|
|
while(semop((*semaphore).sem, &commande, 1) == -1) |
if (semop((*semaphore).sem, &commande, 1) == -1) |
{ |
{ |
if (errno != EINTR) |
return(-1); |
{ |
|
errno = EINVAL; |
|
return(-1); |
|
} |
|
} |
} |
|
|
return(0); |
return(0); |
# else // OS/2 |
# else // OS/2 |
|
int ios; |
|
|
int ios; |
sem_t *psem; |
|
|
sem_t *psem; |
ULONG cnt; |
|
|
ULONG cnt; |
psem = semaphore; |
|
|
psem = semaphore; |
if ((ios = DosWaitEventSem((*psem).hev, SEM_IMMEDIATE_RETURN)) != 0) |
|
{ |
|
errno = (ios == ERROR_TIMEOUT) ? EAGAIN : EINVAL; |
|
return(-1); |
|
} |
|
|
if ((ios = DosWaitEventSem((*psem).hev, SEM_IMMEDIATE_RETURN)) != 0) |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
{ |
{ |
errno = (ios == ERROR_TIMEOUT) ? EAGAIN : EINVAL; |
errno = EINVAL; |
return(-1); |
return(-1); |
} |
} |
|
|
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
if ((*(*psem).cnt) > 0) |
{ |
{ |
errno = EINVAL; |
(*(*psem).cnt)--; |
return(-1); |
} |
} |
|
|
|
if ((*(*psem).cnt) > 0) |
if ((*(*psem).cnt) == 0) |
{ |
{ |
(*(*psem).cnt)--; |
DosResetEventSem((*psem).hev, &cnt); |
} |
} |
|
|
|
DosReleaseMutexSem((*psem).hmtx); |
|
return(0); |
|
# endif |
|
} |
|
|
|
#ifndef timespeccmp |
|
# define timespeccmp(tsp, usp, cmp) \ |
|
(((tsp)->tv_sec == (usp)->tv_sec) ? \ |
|
((tsp)->tv_nsec cmp (usp)->tv_nsec) : \ |
|
((tsp)->tv_sec cmp (usp)->tv_sec)) |
|
# define timespecadd(tsp, usp, vsp) \ |
|
do { \ |
|
(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \ |
|
(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \ |
|
if ((vsp)->tv_nsec >= 1000000000L) { \ |
|
(vsp)->tv_sec++; \ |
|
(vsp)->tv_nsec -= 1000000000L; \ |
|
} \ |
|
} while(0) |
|
# define timespecsub(tsp, usp, vsp) \ |
|
do { \ |
|
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ |
|
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ |
|
if ((vsp)->tv_nsec < 0) { \ |
|
(vsp)->tv_sec--; \ |
|
(vsp)->tv_nsec += 1000000000L; \ |
|
} \ |
|
} while(0) |
|
#endif |
|
|
|
int |
|
sem_timedwait_SysV(sem_t *sem, struct timespec *ts) |
|
{ |
|
struct timespec onems = { 0, 1000000 }; |
|
struct timespec total = { 0, 0 }; |
|
struct timespec unslept; |
|
struct timespec elapsed; |
|
struct timespec tmp; |
|
|
if ((*(*psem).cnt) == 0) |
while(timespeccmp(ts, &total, >)) |
{ |
{ |
DosResetEventSem((*psem).hev, &cnt); |
if (sem_trywait_SysV(sem) == 0) |
} |
{ |
|
return(0); |
|
} |
|
|
DosReleaseMutexSem((*psem).hmtx); |
if (errno != EAGAIN) |
return(0); |
{ |
|
return(-1); |
|
} |
|
|
# endif |
nanosleep(&onems, &unslept); |
|
|
|
timespecsub(&onems, &unslept, &elapsed); |
|
timespecadd(&total, &elapsed, &tmp); |
|
total.tv_sec = tmp.tv_sec; |
|
total.tv_nsec = tmp.tv_nsec; |
|
} |
|
|
|
errno = ETIMEDOUT; |
|
return(-1); |
} |
} |
|
|
int |
int |
Line 539 sem_post_SysV(sem_t *semaphore)
|
Line 638 sem_post_SysV(sem_t *semaphore)
|
|
|
return(0); |
return(0); |
# else // OS/2 |
# else // OS/2 |
|
sem_t *psem; |
|
|
sem_t *psem; |
psem = semaphore; |
|
|
psem = semaphore; |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
|
{ |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
errno = EINVAL; |
{ |
return(-1); |
errno = EINVAL; |
} |
return(-1); |
|
} |
|
|
|
(*(*psem).cnt)++; |
|
DosPostEventSem((*psem).hev); |
|
DosReleaseMutexSem((*psem).hmtx); |
|
|
|
return(0); |
(*(*psem).cnt)++; |
|
DosPostEventSem((*psem).hev); |
|
DosReleaseMutexSem((*psem).hmtx); |
|
|
|
return(0); |
# endif |
# endif |
} |
} |
|
|
Line 572 sem_getvalue_SysV(sem_t *semaphore, int
|
Line 669 sem_getvalue_SysV(sem_t *semaphore, int
|
|
|
return(0); |
return(0); |
# else |
# else |
|
sem_t *psem; |
|
|
sem_t *psem; |
psem = semaphore; |
|
|
psem = semaphore; |
|
|
|
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
|
{ |
|
errno = EINVAL; |
|
return(-1); |
|
} |
|
|
|
(*valeur) = (*(*psem).cnt); |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
DosReleaseMutexSem((*psem).hmtx); |
{ |
|
errno = EINVAL; |
|
return(-1); |
|
} |
|
|
return(0); |
(*valeur) = (*(*psem).cnt); |
|
DosReleaseMutexSem((*psem).hmtx); |
|
|
|
return(0); |
# endif |
# endif |
} |
} |
|
|
Line 638 sem_t
|
Line 733 sem_t
|
} |
} |
|
|
(*semaphore).alloue = -1; |
(*semaphore).alloue = -1; |
|
(*semaphore).pid = getpid(); |
|
(*semaphore).tid = pthread_self(); |
# else |
# else |
|
if ((nom_segment = malloc((strlen(racine_memoire_OS2) + strlen(nom) + 1) |
if ((nom_segment = malloc((strlen(racine_memoire_OS2) + strlen(nom) + 1) |
* sizeof(unsigned char))) == NULL) |
* sizeof(unsigned char))) == NULL) |
|
{ |
|
return(SEM_FAILED); |
|
} |
|
|
|
sprintf(nom_segment, "%s%s", racine_memoire_OS2, nom); |
|
ptr = nom_segment; |
|
|
|
while((*ptr) != d_code_fin_chaine) |
|
{ |
|
if ((*ptr) == '/') |
|
{ |
{ |
(*ptr) = '\\'; |
return(SEM_FAILED); |
} |
} |
|
|
ptr++; |
sprintf(nom_segment, "%s%s", racine_memoire_OS2, nom); |
} |
ptr = nom_segment; |
|
|
if ((nom_absolu = malloc((strlen(racine_semaphores_OS2) + strlen(nom) |
while((*ptr) != d_code_fin_chaine) |
+ 2) * sizeof(unsigned char))) == NULL) |
{ |
{ |
if ((*ptr) == '/') |
return(SEM_FAILED); |
{ |
} |
(*ptr) = '\\'; |
|
} |
|
|
sprintf(nom_absolu, "%s%s", racine_semaphores_OS2, nom); |
ptr++; |
ptr = nom_absolu; |
} |
|
|
while((*ptr) != d_code_fin_chaine) |
if ((nom_absolu = malloc((strlen(racine_semaphores_OS2) + strlen(nom) |
{ |
+ 2) * sizeof(unsigned char))) == NULL) |
if ((*ptr) == '/') |
|
{ |
{ |
(*ptr) = '\\'; |
return(SEM_FAILED); |
} |
} |
|
|
ptr++; |
sprintf(nom_absolu, "%s%s", racine_semaphores_OS2, nom); |
} |
ptr = nom_absolu; |
|
|
(*(ptr + 1)) = d_code_fin_chaine; |
while((*ptr) != d_code_fin_chaine) |
|
{ |
|
if ((*ptr) == '/') |
|
{ |
|
(*ptr) = '\\'; |
|
} |
|
|
if ((psem = malloc(sizeof(sem_t))) == NULL) |
ptr++; |
{ |
} |
return(SEM_FAILED); |
|
} |
|
|
|
(*psem).allocated = 1; |
(*(ptr + 1)) = d_code_fin_chaine; |
|
|
|
if ((psem = malloc(sizeof(sem_t))) == NULL) |
|
{ |
|
return(SEM_FAILED); |
|
} |
|
|
|
(*psem).allocated = 1; |
# endif |
# endif |
|
|
if ((oflag & O_CREAT) == 0) |
if ((oflag & O_CREAT) == 0) |
Line 713 sem_t
|
Line 808 sem_t
|
return(SEM_FAILED); |
return(SEM_FAILED); |
} |
} |
# else // OS/2 |
# else // OS/2 |
if ((psem = malloc(sizeof(sem_t))) == NULL) |
if ((psem = malloc(sizeof(sem_t))) == NULL) |
{ |
{ |
free(nom_absolu); |
free(nom_absolu); |
free(nom_segment); |
free(nom_segment); |
return(SEM_FAILED); |
return(SEM_FAILED); |
} |
} |
|
|
(*ptr) = 'M'; |
|
|
|
if (DosOpenMutexSem(nom_absolu, &((*psem).hmtx)) != 0) |
(*ptr) = 'M'; |
{ |
|
free(psem); |
|
free(nom_absolu); |
|
free(nom_segment); |
|
|
|
return(SEM_FAILED); |
if (DosOpenMutexSem(nom_absolu, &((*psem).hmtx)) != 0) |
} |
{ |
|
free(psem); |
|
free(nom_absolu); |
|
free(nom_segment); |
|
|
(*ptr) = 'S'; |
return(SEM_FAILED); |
|
} |
|
|
if (DosOpenEventSem(nom_absolu, &((*psem).hev)) != 0) |
(*ptr) = 'S'; |
{ |
|
DosCloseMutexSem((*psem).hmtx); |
|
|
|
free(psem); |
if (DosOpenEventSem(nom_absolu, &((*psem).hev)) != 0) |
free(nom_absolu); |
{ |
free(nom_segment); |
DosCloseMutexSem((*psem).hmtx); |
|
|
return(SEM_FAILED); |
free(psem); |
} |
free(nom_absolu); |
|
free(nom_segment); |
|
|
if (DosGetNamedSharedMem(&base, nom_segment, PAG_WRITE | PAG_READ) != 0) |
return(SEM_FAILED); |
{ |
} |
DosCloseMutexSem((*psem).hmtx); |
|
|
|
free(nom_absolu); |
if (DosGetNamedSharedMem(&base, nom_segment, PAG_WRITE | PAG_READ) |
free(nom_segment); |
!= 0) |
free(psem); |
{ |
|
DosCloseMutexSem((*psem).hmtx); |
|
|
return(SEM_FAILED); |
free(nom_absolu); |
} |
free(nom_segment); |
|
free(psem); |
|
|
free(nom_segment); |
return(SEM_FAILED); |
|
} |
|
|
(*psem).cnt = (ULONG *) base; |
free(nom_segment); |
(*psem).nopened = ((ULONG *) base) + 1; |
|
(*psem).shared = 1; |
|
|
|
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
(*psem).cnt = (ULONG *) base; |
{ |
(*psem).nopened = ((ULONG *) base) + 1; |
DosCloseMutexSem((*psem).hmtx); |
(*psem).shared = 1; |
|
|
free(nom_absolu); |
if (DosRequestMutexSem((*psem).hmtx, SEM_INDEFINITE_WAIT) != 0) |
free(nom_segment); |
{ |
free(psem); |
DosCloseMutexSem((*psem).hmtx); |
|
|
return(SEM_FAILED); |
free(nom_absolu); |
} |
free(nom_segment); |
|
free(psem); |
|
|
(*((*psem).nopened))++; |
return(SEM_FAILED); |
|
} |
|
|
DosReleaseMutexSem((*psem).hmtx); |
(*((*psem).nopened))++; |
|
|
semaphore = psem; |
DosReleaseMutexSem((*psem).hmtx); |
|
|
|
semaphore = psem; |
# endif |
# endif |
} |
} |
else |
else |
Line 828 sem_t
|
Line 923 sem_t
|
return(SEM_FAILED); |
return(SEM_FAILED); |
} |
} |
|
|
argument.val = valeur; |
argument.val = (int) valeur; |
semctl((*semaphore).sem, 0, SETVAL, argument); |
semctl((*semaphore).sem, 0, SETVAL, argument); |
# else // OS/2 |
# else // OS/2 |
|
if ((psem = malloc(sizeof(sem_t))) == NULL) |
|
{ |
|
free(nom_absolu); |
|
free(nom_segment); |
|
|
if ((psem = malloc(sizeof(sem_t))) == NULL) |
return(SEM_FAILED); |
{ |
} |
free(nom_absolu); |
|
free(nom_segment); |
|
|
|
return(SEM_FAILED); |
|
} |
|
|
|
(*ptr) = 'M'; |
|
|
|
if (DosCreateMutexSem(nom_absolu, &((*psem).hmtx), 0, 0) != 0) |
(*ptr) = 'M'; |
{ |
|
free(psem); |
|
free(nom_absolu); |
|
free(nom_segment); |
|
|
|
return(SEM_FAILED); |
if (DosCreateMutexSem(nom_absolu, &((*psem).hmtx), 0, 0) != 0) |
} |
{ |
|
free(psem); |
|
free(nom_absolu); |
|
free(nom_segment); |
|
|
(*ptr) = 'S'; |
return(SEM_FAILED); |
|
} |
|
|
if (DosCreateEventSem(nom_absolu, &((*psem).hev), 0, |
(*ptr) = 'S'; |
(valeur != 0) ? 1 : 0) != 0) |
|
{ |
|
DosCloseMutexSem((*psem).hmtx); |
|
|
|
free(nom_absolu); |
if (DosCreateEventSem(nom_absolu, &((*psem).hev), 0, |
free(nom_segment); |
(valeur != 0) ? 1 : 0) != 0) |
free(psem); |
{ |
|
DosCloseMutexSem((*psem).hmtx); |
|
|
return(SEM_FAILED); |
free(nom_absolu); |
} |
free(nom_segment); |
|
free(psem); |
|
|
if (DosAllocSharedMem(&base, nom_segment, 2 * sizeof(ULONG), |
return(SEM_FAILED); |
PAG_WRITE | PAG_READ | PAG_COMMIT) != 0) |
} |
{ |
|
DosCloseMutexSem((*psem).hmtx); |
|
|
|
free(nom_absolu); |
if (DosAllocSharedMem(&base, nom_segment, 2 * sizeof(ULONG), |
free(nom_segment); |
PAG_WRITE | PAG_READ | PAG_COMMIT) != 0) |
free(psem); |
{ |
|
DosCloseMutexSem((*psem).hmtx); |
|
|
return(SEM_FAILED); |
free(nom_absolu); |
} |
free(nom_segment); |
|
free(psem); |
|
|
free(nom_segment); |
return(SEM_FAILED); |
|
} |
|
|
(*psem).cnt = (ULONG *) base; |
free(nom_segment); |
(*psem).nopened = ((ULONG *) base) + 1; |
|
(*(*psem).cnt) = valeur; |
|
(*(*psem).nopened) = 1; |
|
(*psem).shared = 1; |
|
semaphore = psem; |
|
|
|
|
(*psem).cnt = (ULONG *) base; |
|
(*psem).nopened = ((ULONG *) base) + 1; |
|
(*(*psem).cnt) = valeur; |
|
(*(*psem).nopened) = 1; |
|
(*psem).shared = 1; |
|
semaphore = psem; |
# endif |
# endif |
} |
} |
|
|
Line 909 sem_close_SysV(sem_t *semaphore)
|
Line 1002 sem_close_SysV(sem_t *semaphore)
|
|
|
return(0); |
return(0); |
# else |
# else |
|
sem_t *psem; |
|
|
sem_t *psem; |
psem = semaphore; |
|
|
psem = semaphore; |
|
|
|
if (DosCloseMutexSem((*psem).hmtx) != 0) |
if (DosCloseMutexSem((*psem).hmtx) != 0) |
{ |
{ |
return(EINVAL); |
return(EINVAL); |
} |
} |
|
|
while(DosCloseEventSem((*psem).hev) == ERROR_SEM_BUSY) |
while(DosCloseEventSem((*psem).hev) == ERROR_SEM_BUSY) |
{ |
{ |
DosPostEventSem((*psem).hev); |
DosPostEventSem((*psem).hev); |
} |
} |
|
|
(*(*psem).nopened)--; |
(*(*psem).nopened)--; |
|
|
if ((*psem).shared == 0) |
if ((*psem).shared == 0) |
{ |
|
free((*psem).cnt); |
|
free((*psem).nopened); |
|
} |
|
else |
|
{ |
|
if ((*(*psem).nopened) == 0) |
|
{ |
{ |
DosFreeMem((*psem).cnt); |
free((*psem).cnt); |
|
free((*psem).nopened); |
|
} |
|
else |
|
{ |
|
if ((*(*psem).nopened) == 0) |
|
{ |
|
DosFreeMem((*psem).cnt); |
|
} |
} |
} |
} |
|
|
|
if ((*psem).allocated != 0) |
|
{ |
|
free(psem); |
|
} |
|
|
|
return(0); |
if ((*psem).allocated != 0) |
|
{ |
|
free(psem); |
|
} |
|
|
|
return(0); |
# endif |
# endif |
} |
} |
|
|
int |
int |
sem_unlink_SysV(const char *nom) |
sem_unlink_SysV(char *nom) |
{ |
{ |
// Détruit un sémaphore nommé créé par sem_open_SysV() |
// Détruit un sémaphore nommé créé par sem_open_SysV() |
# ifndef OS2 // IPCS_SYSV |
# ifndef OS2 // IPCS_SYSV |
unsigned char *nom_absolu; |
semctl(semget(ftok(nom, 1), 0, 0), 0, IPC_RMID); |
|
|
if ((nom_absolu = malloc((strlen(racine_segment) + strlen(nom) |
|
+ 2) * sizeof(unsigned char))) == NULL) |
|
{ |
|
return(ENOMEM); |
|
} |
|
|
|
sprintf(nom_absolu, "%s/%s", racine_segment, nom); |
if (unlink(nom) == -1) |
semctl(semget(ftok(nom_absolu, 1), 0, 0), 0, IPC_RMID); |
|
|
|
if (unlink(nom_absolu) == -1) |
|
{ |
{ |
free(nom_absolu); |
|
return(EACCES); |
return(EACCES); |
} |
} |
|
|
free(nom_absolu); |
free(nom); |
return(0); |
return(0); |
# else |
# else |
|
return(0); |
return(0); |
|
|
|
# endif |
# endif |
} |
} |
|
|