public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Kwok Yeung <kcy@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/omp/gcc-10] libgomp: Don't access gomp_sem_t as int using atomics unconditionally Date: Fri, 22 Jan 2021 19:51:25 +0000 (GMT) [thread overview] Message-ID: <20210122195125.04D9E3896C16@sourceware.org> (raw) https://gcc.gnu.org/g:2b93ffc7219aa53815ece2adb21f56dd265ec6bc commit 2b93ffc7219aa53815ece2adb21f56dd265ec6bc Author: Jakub Jelinek <jakub@redhat.com> Date: Mon Jan 18 07:18:46 2021 +0100 libgomp: Don't access gomp_sem_t as int using atomics unconditionally This patch introduces gomp_sem_getcount wrapper, which uses sem_getvalue for POSIX and atomic loads for linux futex and accel. rtems for now remains broken. 2021-01-18 Jakub Jelinek <jakub@redhat.com> * config/linux/sem.h (gomp_sem_getcount): New function. * config/posix/sem.h (gomp_sem_getcount): New function. * config/posix/sem.c (gomp_sem_getcount): New function. * config/accel/sem.h (gomp_sem_getcount): New function. * task.c (task_fulfilled_p): Use gomp_sem_getcount. (omp_fulfill_event): Likewise. (cherry picked from commit d3b41bde961713ff4af7e18011126434c497edba) Diff: --- libgomp/ChangeLog.omp | 12 ++++++++++++ libgomp/config/accel/sem.h | 9 +++++++++ libgomp/config/linux/sem.h | 9 +++++++++ libgomp/config/posix/sem.c | 20 ++++++++++++++++++++ libgomp/config/posix/sem.h | 10 ++++++++++ libgomp/task.c | 4 ++-- 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/libgomp/ChangeLog.omp b/libgomp/ChangeLog.omp index 684e09a378b..632b86989c3 100644 --- a/libgomp/ChangeLog.omp +++ b/libgomp/ChangeLog.omp @@ -1,3 +1,15 @@ +2021-01-22 Kwok Cheung Yeung <kcy@codesourcery.com> + + Backport from mainline + 2021-01-18 Jakub Jelinek <jakub@redhat.com> + + * config/linux/sem.h (gomp_sem_getcount): New function. + * config/posix/sem.h (gomp_sem_getcount): New function. + * config/posix/sem.c (gomp_sem_getcount): New function. + * config/accel/sem.h (gomp_sem_getcount): New function. + * task.c (task_fulfilled_p): Use gomp_sem_getcount. + (omp_fulfill_event): Likewise. + 2021-01-22 Kwok Cheung Yeung <kcy@codesourcery.com> Backport from mainline diff --git a/libgomp/config/accel/sem.h b/libgomp/config/accel/sem.h index af1b20b8483..3703842df54 100644 --- a/libgomp/config/accel/sem.h +++ b/libgomp/config/accel/sem.h @@ -62,4 +62,13 @@ gomp_sem_post (gomp_sem_t *sem) { (void) __atomic_add_fetch (sem, 1, MEMMODEL_RELEASE); } + +static inline int +gomp_sem_getcount (gomp_sem_t *sem) +{ + int count = __atomic_load_n (sem, MEMMODEL_RELAXED); + if (count < 0) + return -1; + return count; +} #endif /* GOMP_SEM_H */ diff --git a/libgomp/config/linux/sem.h b/libgomp/config/linux/sem.h index 2e26a917a92..91e08f0fac8 100644 --- a/libgomp/config/linux/sem.h +++ b/libgomp/config/linux/sem.h @@ -85,4 +85,13 @@ gomp_sem_post (gomp_sem_t *sem) if (__builtin_expect (count & SEM_WAIT, 0)) gomp_sem_post_slow (sem); } + +static inline int +gomp_sem_getcount (gomp_sem_t *sem) +{ + int count = __atomic_load_n (sem, MEMMODEL_RELAXED); + if ((count & SEM_WAIT) != 0) + return -1; + return count / SEM_INC; +} #endif /* GOMP_SEM_H */ diff --git a/libgomp/config/posix/sem.c b/libgomp/config/posix/sem.c index e43bcf6c9e7..a2529de8332 100644 --- a/libgomp/config/posix/sem.c +++ b/libgomp/config/posix/sem.c @@ -112,6 +112,26 @@ void gomp_sem_destroy (gomp_sem_t *sem) return; } + +int gomp_sem_getcount (gomp_sem_t *sem) +{ + int ret, count; + + ret = pthread_mutex_lock (&sem->mutex); + if (ret) + return -1; + + count = sem->value; + + ret = pthread_mutex_unlock (&sem->mutex); + if (ret) + return -1; + + if (count < 0) + return -1; + + return count; +} #else /* HAVE_BROKEN_POSIX_SEMAPHORES */ void gomp_sem_wait (gomp_sem_t *sem) diff --git a/libgomp/config/posix/sem.h b/libgomp/config/posix/sem.h index 64d4f1ec1d6..bd58d4f057b 100644 --- a/libgomp/config/posix/sem.h +++ b/libgomp/config/posix/sem.h @@ -64,6 +64,8 @@ extern void gomp_sem_post (gomp_sem_t *sem); extern void gomp_sem_destroy (gomp_sem_t *sem); +extern int gomp_sem_getcount (gomp_sem_t *sem); + #else /* HAVE_BROKEN_POSIX_SEMAPHORES */ typedef sem_t gomp_sem_t; @@ -84,5 +86,13 @@ static inline void gomp_sem_destroy (gomp_sem_t *sem) { sem_destroy (sem); } + +static inline int gomp_sem_getcount (gomp_sem_t *sem) +{ + int val; + if (sem_getvalue (sem, &val) < 0) + return -1; + return val; +} #endif /* doesn't HAVE_BROKEN_POSIX_SEMAPHORES */ #endif /* GOMP_SEM_H */ diff --git a/libgomp/task.c b/libgomp/task.c index 67bff47fa0b..9689a3edef0 100644 --- a/libgomp/task.c +++ b/libgomp/task.c @@ -330,7 +330,7 @@ gomp_task_handle_depend (struct gomp_task *task, struct gomp_task *parent, static bool task_fulfilled_p (struct gomp_task *task) { - return __atomic_load_n (&task->completion_sem, __ATOMIC_RELAXED); + return gomp_sem_getcount (&task->completion_sem) > 0; } /* Called when encountering an explicit task directive. If IF_CLAUSE is @@ -2406,7 +2406,7 @@ omp_fulfill_event (omp_event_handle_t event) struct gomp_thread *thr = gomp_thread (); struct gomp_team *team = thr ? thr->ts.team : NULL; - if (__atomic_load_n (sem, __ATOMIC_RELAXED)) + if (gomp_sem_getcount (sem) > 0) gomp_fatal ("omp_fulfill_event: %p event already fulfilled!\n", sem); gomp_debug (0, "omp_fulfill_event: %p\n", sem);
reply other threads:[~2021-01-22 19:51 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20210122195125.04D9E3896C16@sourceware.org \ --to=kcy@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).