pthread portability

From: Michael McConville <mmcco(at)mykolab(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: pthread portability
Date: 2016-03-28 07:15:25
Message-ID: 20160328071525.GB2182@thinkpad.swarthmore.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

The below diff fixes one problem: you can't compare pthread_t values
directly. Only the function pthread_equal(3) is defined. Direct
comparison usually works because most implementations define pthread_t
as an integer type.

Relatedly, INVALID_THREAD is defined as (pthread_t)0. I don't think this
is a portable way of checking whether a thread is valid, and I don't
know if it's actually possible to get an "invalid" thread out of
pthread_create(3) if it succeeds (returns 0). In the cases where you're
setting a pthread_t to INVALID_THREAD, maybe using a struct that
includes a pthread_t and a 'valid' bool would be preferable.

Thanks for your time,
Michael

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 4196b0e..f2e5aed 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -3791,7 +3791,7 @@ main(int argc, char **argv)
{
int err = pthread_create(&thread->thread, NULL, threadRun, thread);

- if (err != 0 || thread->thread == INVALID_THREAD)
+ if (err != 0 || pthread_equal(thread->thread, INVALID_THREAD))
{
fprintf(stderr, "could not create thread: %s\n", strerror(err));
exit(1);
@@ -3819,7 +3819,7 @@ main(int argc, char **argv)
TState *thread = &threads[i];

#ifdef ENABLE_THREAD_SAFETY
- if (threads[i].thread == INVALID_THREAD)
+ if (pthread_equal(threads[i].thread, INVALID_THREAD))
/* actually run this thread directly in the main thread */
(void) threadRun(thread);
else

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Geoghegan 2016-03-28 07:21:54 Re: Draft release notes for next week's releases
Previous Message Oleg Bartunov 2016-03-28 07:08:47 Re: Draft release notes for next week's releases