mq_notify: use semaphore instead of barrier to sync args consumption

semaphores are a much lighter primitive, and more idiomatic with
current usage in the code base.
This commit is contained in:
Rich Felker 2023-02-10 11:17:02 -05:00
parent c3cd04fa5f
commit fde6891e59

View file

@ -4,10 +4,11 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <semaphore.h>
#include "syscall.h" #include "syscall.h"
struct args { struct args {
pthread_barrier_t barrier; sem_t sem;
int sock; int sock;
const struct sigevent *sev; const struct sigevent *sev;
}; };
@ -21,7 +22,7 @@ static void *start(void *p)
void (*func)(union sigval) = args->sev->sigev_notify_function; void (*func)(union sigval) = args->sev->sigev_notify_function;
union sigval val = args->sev->sigev_value; union sigval val = args->sev->sigev_value;
pthread_barrier_wait(&args->barrier); sem_post(&args->sem);
n = recv(s, buf, sizeof(buf), MSG_NOSIGNAL|MSG_WAITALL); n = recv(s, buf, sizeof(buf), MSG_NOSIGNAL|MSG_WAITALL);
close(s); close(s);
if (n==sizeof buf && buf[sizeof buf - 1] == 1) if (n==sizeof buf && buf[sizeof buf - 1] == 1)
@ -37,6 +38,7 @@ int mq_notify(mqd_t mqd, const struct sigevent *sev)
int s; int s;
struct sigevent sev2; struct sigevent sev2;
static const char zeros[32]; static const char zeros[32];
int cs;
if (!sev || sev->sigev_notify != SIGEV_THREAD) if (!sev || sev->sigev_notify != SIGEV_THREAD)
return syscall(SYS_mq_notify, mqd, sev); return syscall(SYS_mq_notify, mqd, sev);
@ -48,7 +50,7 @@ int mq_notify(mqd_t mqd, const struct sigevent *sev)
if (sev->sigev_notify_attributes) attr = *sev->sigev_notify_attributes; if (sev->sigev_notify_attributes) attr = *sev->sigev_notify_attributes;
else pthread_attr_init(&attr); else pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_barrier_init(&args.barrier, 0, 2); sem_init(&args.sem, 0, 0);
if (pthread_create(&td, &attr, start, &args)) { if (pthread_create(&td, &attr, start, &args)) {
__syscall(SYS_close, s); __syscall(SYS_close, s);
@ -56,8 +58,10 @@ int mq_notify(mqd_t mqd, const struct sigevent *sev)
return -1; return -1;
} }
pthread_barrier_wait(&args.barrier); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
pthread_barrier_destroy(&args.barrier); sem_wait(&args.sem);
pthread_setcancelstate(cs, 0);
sem_destroy(&args.sem);
sev2.sigev_notify = SIGEV_THREAD; sev2.sigev_notify = SIGEV_THREAD;
sev2.sigev_signo = s; sev2.sigev_signo = s;