Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 44bf3d5

Browse files
committed
Add missing pthread_barrier_t.
Supply a simple implementation of the missing pthread_barrier_t type and functions, for macOS. Discussion: https://postgr.es/m/20200227180100.zyvjwzcpiokfsqm2%40alap3.anarazel.de
1 parent 547f04e commit 44bf3d5

File tree

7 files changed

+196
-0
lines changed

7 files changed

+196
-0
lines changed

configure

+69
Original file line numberDiff line numberDiff line change
@@ -11635,6 +11635,62 @@ if test "$ac_res" != no; then :
1163511635

1163611636
fi
1163711637

11638+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing pthread_barrier_wait" >&5
11639+
$as_echo_n "checking for library containing pthread_barrier_wait... " >&6; }
11640+
if ${ac_cv_search_pthread_barrier_wait+:} false; then :
11641+
$as_echo_n "(cached) " >&6
11642+
else
11643+
ac_func_search_save_LIBS=$LIBS
11644+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
11645+
/* end confdefs.h. */
11646+
11647+
/* Override any GCC internal prototype to avoid an error.
11648+
Use char because int might match the return type of a GCC
11649+
builtin and then its argument prototype would still apply. */
11650+
#ifdef __cplusplus
11651+
extern "C"
11652+
#endif
11653+
char pthread_barrier_wait ();
11654+
int
11655+
main ()
11656+
{
11657+
return pthread_barrier_wait ();
11658+
;
11659+
return 0;
11660+
}
11661+
_ACEOF
11662+
for ac_lib in '' pthread; do
11663+
if test -z "$ac_lib"; then
11664+
ac_res="none required"
11665+
else
11666+
ac_res=-l$ac_lib
11667+
LIBS="-l$ac_lib $ac_func_search_save_LIBS"
11668+
fi
11669+
if ac_fn_c_try_link "$LINENO"; then :
11670+
ac_cv_search_pthread_barrier_wait=$ac_res
11671+
fi
11672+
rm -f core conftest.err conftest.$ac_objext \
11673+
conftest$ac_exeext
11674+
if ${ac_cv_search_pthread_barrier_wait+:} false; then :
11675+
break
11676+
fi
11677+
done
11678+
if ${ac_cv_search_pthread_barrier_wait+:} false; then :
11679+
11680+
else
11681+
ac_cv_search_pthread_barrier_wait=no
11682+
fi
11683+
rm conftest.$ac_ext
11684+
LIBS=$ac_func_search_save_LIBS
11685+
fi
11686+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_pthread_barrier_wait" >&5
11687+
$as_echo "$ac_cv_search_pthread_barrier_wait" >&6; }
11688+
ac_res=$ac_cv_search_pthread_barrier_wait
11689+
if test "$ac_res" != no; then :
11690+
test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
11691+
11692+
fi
11693+
1163811694
# Solaris:
1163911695
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing fdatasync" >&5
1164011696
$as_echo_n "checking for library containing fdatasync... " >&6; }
@@ -15883,6 +15939,19 @@ esac
1588315939

1588415940
fi
1588515941

15942+
ac_fn_c_check_func "$LINENO" "pthread_barrier_wait" "ac_cv_func_pthread_barrier_wait"
15943+
if test "x$ac_cv_func_pthread_barrier_wait" = xyes; then :
15944+
$as_echo "#define HAVE_PTHREAD_BARRIER_WAIT 1" >>confdefs.h
15945+
15946+
else
15947+
case " $LIBOBJS " in
15948+
*" pthread_barrier_wait.$ac_objext "* ) ;;
15949+
*) LIBOBJS="$LIBOBJS pthread_barrier_wait.$ac_objext"
15950+
;;
15951+
esac
15952+
15953+
fi
15954+
1588615955
ac_fn_c_check_func "$LINENO" "pwrite" "ac_cv_func_pwrite"
1588715956
if test "x$ac_cv_func_pwrite" = xyes; then :
1588815957
$as_echo "#define HAVE_PWRITE 1" >>confdefs.h

configure.ac

+2
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ AC_SEARCH_LIBS(getopt_long, [getopt gnugetopt])
11431143
AC_SEARCH_LIBS(shm_open, rt)
11441144
AC_SEARCH_LIBS(shm_unlink, rt)
11451145
AC_SEARCH_LIBS(clock_gettime, [rt posix4])
1146+
AC_SEARCH_LIBS(pthread_barrier_wait, pthread)
11461147
# Solaris:
11471148
AC_SEARCH_LIBS(fdatasync, [rt posix4])
11481149
# Required for thread_test.c on Solaris
@@ -1743,6 +1744,7 @@ AC_REPLACE_FUNCS(m4_normalize([
17431744
mkdtemp
17441745
pread
17451746
preadv
1747+
pthread_barrier_wait
17461748
pwrite
17471749
pwritev
17481750
random

src/include/pg_config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@
424424
/* Define if you have POSIX threads libraries and header files. */
425425
#undef HAVE_PTHREAD
426426

427+
/* Define to 1 if you have the `pthread_barrier_wait' function. */
428+
#undef HAVE_PTHREAD_BARRIER_WAIT
429+
427430
/* Define to 1 if you have the `pthread_is_threaded_np' function. */
428431
#undef HAVE_PTHREAD_IS_THREADED_NP
429432

src/include/port/pg_pthread.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* Declarations for missing POSIX thread components.
4+
*
5+
* Currently this supplies an implementation of pthread_barrier_t for the
6+
* benefit of macOS, which lacks it. These declarations are not in port.h,
7+
* because that'd require <pthread.h> to be included by every translation
8+
* unit.
9+
*
10+
*-------------------------------------------------------------------------
11+
*/
12+
13+
#ifndef PG_PTHREAD_H
14+
#define PG_PTHREAD_H
15+
16+
#include <pthread.h>
17+
18+
#ifndef HAVE_PTHREAD_BARRIER_WAIT
19+
20+
#ifndef PTHREAD_BARRIER_SERIAL_THREAD
21+
#define PTHREAD_BARRIER_SERIAL_THREAD (-1)
22+
#endif
23+
24+
typedef struct pg_pthread_barrier
25+
{
26+
bool sense; /* we only need a one bit phase */
27+
int count; /* number of threads expected */
28+
int arrived; /* number of threads that have arrived */
29+
pthread_mutex_t mutex;
30+
pthread_cond_t cond;
31+
} pthread_barrier_t;
32+
33+
extern int pthread_barrier_init(pthread_barrier_t *barrier,
34+
const void *attr,
35+
int count);
36+
extern int pthread_barrier_wait(pthread_barrier_t *barrier);
37+
extern int pthread_barrier_destroy(pthread_barrier_t *barrier);
38+
39+
#endif
40+
41+
#endif

src/port/pthread_barrier_wait.c

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* pthread_barrier_wait.c
4+
* Implementation of pthread_barrier_t support for platforms lacking it.
5+
*
6+
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7+
*
8+
* IDENTIFICATION
9+
* src/port/pthread_barrier_wait.c
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
14+
#include "c.h"
15+
16+
#include "port/pg_pthread.h"
17+
18+
int
19+
pthread_barrier_init(pthread_barrier_t *barrier, const void *attr, int count)
20+
{
21+
barrier->sense = false;
22+
barrier->count = count;
23+
barrier->arrived = 0;
24+
if (pthread_cond_init(&barrier->cond, NULL) < 0)
25+
return -1;
26+
if (pthread_mutex_init(&barrier->mutex, NULL) < 0)
27+
{
28+
int save_errno = errno;
29+
30+
pthread_cond_destroy(&barrier->cond);
31+
errno = save_errno;
32+
33+
return -1;
34+
}
35+
36+
return 0;
37+
}
38+
39+
int
40+
pthread_barrier_wait(pthread_barrier_t *barrier)
41+
{
42+
bool initial_sense;
43+
44+
pthread_mutex_lock(&barrier->mutex);
45+
46+
/* We have arrived at the barrier. */
47+
barrier->arrived++;
48+
Assert(barrier->arrived <= barrier->count);
49+
50+
/* If we were the last to arrive, release the others and return. */
51+
if (barrier->arrived == barrier->count)
52+
{
53+
barrier->arrived = 0;
54+
barrier->sense = !barrier->sense;
55+
pthread_mutex_unlock(&barrier->mutex);
56+
pthread_cond_broadcast(&barrier->cond);
57+
58+
return PTHREAD_BARRIER_SERIAL_THREAD;
59+
}
60+
61+
/* Wait for someone else to flip the sense. */
62+
initial_sense = barrier->sense;
63+
do
64+
{
65+
pthread_cond_wait(&barrier->cond, &barrier->mutex);
66+
} while (barrier->sense == initial_sense);
67+
68+
pthread_mutex_unlock(&barrier->mutex);
69+
70+
return 0;
71+
}
72+
73+
int
74+
pthread_barrier_destroy(pthread_barrier_t *barrier)
75+
{
76+
pthread_cond_destroy(&barrier->cond);
77+
pthread_mutex_destroy(&barrier->mutex);
78+
return 0;
79+
}

src/tools/msvc/Solution.pm

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ sub GenerateFiles
333333
HAVE_PSTAT => undef,
334334
HAVE_PS_STRINGS => undef,
335335
HAVE_PTHREAD => undef,
336+
HAVE_PTHREAD_BARRIER_WAIT => undef,
336337
HAVE_PTHREAD_IS_THREADED_NP => undef,
337338
HAVE_PTHREAD_PRIO_INHERIT => undef,
338339
HAVE_PWRITE => undef,

src/tools/pgindent/typedefs.list

+1
Original file line numberDiff line numberDiff line change
@@ -3295,6 +3295,7 @@ proclist_mutable_iter
32953295
proclist_node
32963296
promptStatus_t
32973297
pthread_attr_t
3298+
pthread_barrier_t
32983299
pthread_key_t
32993300
pthread_mutex_t
33003301
pthread_once_t

0 commit comments

Comments
 (0)