summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Guenther <guenther@cvs.openbsd.org>2018-04-27 06:47:35 +0000
committerPhilip Guenther <guenther@cvs.openbsd.org>2018-04-27 06:47:35 +0000
commitf3855ab634a70a7aeb7c733371bedf659ba73370 (patch)
tree0d5663605cf5d4628038b9f27edffcace8952b8a
parente6dfe1dd6b5ec97cc4aaa02210b9e9573e36b859 (diff)
pthread_join() must not return EINTR
Simplify sem_trywait() ok pirofti@ mpi@
-rw-r--r--lib/librthread/rthread_sem.c22
-rw-r--r--regress/lib/libpthread/pthread_join/pthread_join.c28
2 files changed, 38 insertions, 12 deletions
diff --git a/lib/librthread/rthread_sem.c b/lib/librthread/rthread_sem.c
index daabe98af3f..66b9f917abf 100644
--- a/lib/librthread/rthread_sem.c
+++ b/lib/librthread/rthread_sem.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rthread_sem.c,v 1.27 2018/04/24 16:28:42 pirofti Exp $ */
+/* $OpenBSD: rthread_sem.c,v 1.28 2018/04/27 06:47:34 guenther Exp $ */
/*
* Copyright (c) 2004,2005,2013 Ted Unangst <tedu@openbsd.org>
* All Rights Reserved.
@@ -53,7 +53,7 @@
* Internal implementation of semaphores
*/
int
-_sem_wait(sem_t sem, int tryonly, const struct timespec *abstime,
+_sem_wait(sem_t sem, int can_eintr, const struct timespec *abstime,
int *delayed_cancel)
{
void *ident = (void *)&sem->waitcount;
@@ -66,8 +66,6 @@ _sem_wait(sem_t sem, int tryonly, const struct timespec *abstime,
if (sem->value) {
sem->value--;
r = 0;
- } else if (tryonly) {
- r = EAGAIN;
} else {
sem->waitcount++;
do {
@@ -75,8 +73,8 @@ _sem_wait(sem_t sem, int tryonly, const struct timespec *abstime,
&sem->lock, delayed_cancel);
_spinlock(&sem->lock);
/* ignore interruptions other than cancelation */
- if (r == ECANCELED && (delayed_cancel == NULL ||
- *delayed_cancel == 0))
+ if ((r == ECANCELED && *delayed_cancel == 0) ||
+ (r == EINTR && !can_eintr))
r = 0;
} while (r == 0 && sem->value == 0);
sem->waitcount--;
@@ -248,7 +246,7 @@ sem_wait(sem_t *semp)
}
ENTER_DELAYED_CANCEL_POINT(tib, self);
- r = _sem_wait(sem, 0, NULL, &self->delayed_cancel);
+ r = _sem_wait(sem, 1, NULL, &self->delayed_cancel);
LEAVE_CANCEL_POINT_INNER(tib, r);
if (r) {
@@ -279,7 +277,7 @@ sem_timedwait(sem_t *semp, const struct timespec *abstime)
self = tib->tib_thread;
ENTER_DELAYED_CANCEL_POINT(tib, self);
- r = _sem_wait(sem, 0, abstime, &self->delayed_cancel);
+ r = _sem_wait(sem, 1, abstime, &self->delayed_cancel);
LEAVE_CANCEL_POINT_INNER(tib, r);
if (r) {
@@ -301,7 +299,13 @@ sem_trywait(sem_t *semp)
return (-1);
}
- r = _sem_wait(sem, 1, NULL, NULL);
+ _spinlock(&sem->lock);
+ if (sem->value) {
+ sem->value--;
+ r = 0;
+ } else
+ r = EAGAIN;
+ _spinunlock(&sem->lock);
if (r) {
errno = r;
diff --git a/regress/lib/libpthread/pthread_join/pthread_join.c b/regress/lib/libpthread/pthread_join/pthread_join.c
index b0b60ad3e9e..101290096e8 100644
--- a/regress/lib/libpthread/pthread_join/pthread_join.c
+++ b/regress/lib/libpthread/pthread_join/pthread_join.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pthread_join.c,v 1.4 2004/02/28 08:06:47 deraadt Exp $ */
+/* $OpenBSD: pthread_join.c,v 1.5 2018/04/27 06:47:34 guenther Exp $ */
/*
* Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
* proven@mit.edu All rights reserved.
@@ -41,12 +41,19 @@
* -Started coding this file.
*/
+#include <err.h>
#include <pthread.h>
-#include <unistd.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include "test.h"
+static void
+handler(int sig)
+{
+}
+
/* This thread yields so the creator has a live thread to wait on */
static void *
new_thread_1(void * new_buf)
@@ -55,7 +62,7 @@ new_thread_1(void * new_buf)
snprintf((char *)new_buf, 512, "New thread %%d stack at %p\n", &i);
pthread_yield(); /* (ensure parent can wait on live thread) */
- sleep(1);
+ sleep(2);
return(new_buf);
PANIC("return");
}
@@ -74,15 +81,30 @@ new_thread_2(void * new_buf)
int
main(int argc, char *argv[])
{
+ struct sigaction sa;
+ sigset_t mask;
char buf[256], *status;
pthread_t thread;
int debug = 1;
int i = 0;
+ sa.sa_handler = &handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction(SIGALRM, &sa, NULL))
+ err(1, "sigaction");
+ sigemptyset(&mask);
+ sigaddset(&mask, SIGALRM);
+
if (debug)
printf("Original thread stack at %p\n", &i);
+ if (sigprocmask(SIG_BLOCK, &mask, NULL))
+ err(1, "sigprocmask");
CHECKr(pthread_create(&thread, NULL, new_thread_1, (void *)buf));
+ if (sigprocmask(SIG_UNBLOCK, &mask, NULL))
+ err(1, "sigprocmask");
+ alarm(1);
CHECKr(pthread_join(thread, (void **)(&status)));
if (debug)
printf(status, ++i);