Skip to content

Commit 7dabd57

Browse files
committed
android: fix compilation with new NDK versions
Fixes compiling with Android NDK when using Unified Headers (default since r15). Fixes: #1417 PR-URL: #1433 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent cf38297 commit 7dabd57

6 files changed

Lines changed: 112 additions & 131 deletions

File tree

Makefile.am

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ if ANDROID
334334
include_HEADERS += include/android-ifaddrs.h \
335335
include/pthread-barrier.h
336336
libuv_la_SOURCES += src/unix/android-ifaddrs.c \
337-
src/unix/pthread-fixes.c \
338-
src/unix/pthread-barrier.c
337+
src/unix/pthread-fixes.c
339338
endif
340339

341340
if CYGWIN
@@ -361,8 +360,7 @@ libuv_la_SOURCES += src/unix/bsd-ifaddrs.c \
361360
src/unix/darwin-proctitle.c \
362361
src/unix/fsevents.c \
363362
src/unix/kqueue.c \
364-
src/unix/proctitle.c \
365-
src/unix/pthread-barrier.c
363+
src/unix/proctitle.c
366364
test_run_tests_LDFLAGS += -lutil
367365
endif
368366

@@ -454,7 +452,6 @@ libuv_la_CFLAGS += -D_UNIX03_THREADS \
454452
-qFLOAT=IEEE
455453
libuv_la_LDFLAGS += -qXPLINK
456454
libuv_la_SOURCES += src/unix/pthread-fixes.c \
457-
src/unix/pthread-barrier.c \
458455
src/unix/no-fsevents.c \
459456
src/unix/os390.c \
460457
src/unix/os390-syscalls.c \

android-configure

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
export TOOLCHAIN=$PWD/android-toolchain
44
mkdir -p $TOOLCHAIN
5+
API=${3:-24}
56
$1/build/tools/make-standalone-toolchain.sh \
67
--toolchain=arm-linux-androideabi-4.9 \
78
--arch=arm \
89
--install-dir=$TOOLCHAIN \
9-
--platform=android-21
10+
--platform=android-$API \
11+
--force
1012
export PATH=$TOOLCHAIN/bin:$PATH
1113
export AR=arm-linux-androideabi-ar
1214
export CC=arm-linux-androideabi-gcc
1315
export CXX=arm-linux-androideabi-g++
1416
export LINK=arm-linux-androideabi-g++
1517
export PLATFORM=android
18+
export CFLAGS="-D__ANDROID_API__=$API"
1619

1720
if [[ $2 == 'gyp' ]]
1821
then

include/pthread-barrier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2323
#endif
2424

2525
#define PTHREAD_BARRIER_SERIAL_THREAD 0x12345
26+
#define UV__PTHREAD_BARRIER_FALLBACK 1
2627

2728
/*
2829
* To maintain ABI compatibility with

src/unix/pthread-barrier.c

Lines changed: 0 additions & 121 deletions
This file was deleted.

src/unix/thread.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,110 @@
4141
#define NANOSEC ((uint64_t) 1e9)
4242

4343

44+
#if defined(UV__PTHREAD_BARRIER_FALLBACK)
45+
/* TODO: support barrier_attr */
46+
int pthread_barrier_init(pthread_barrier_t* barrier,
47+
const void* barrier_attr,
48+
unsigned count) {
49+
int rc;
50+
_uv_barrier* b;
51+
52+
if (barrier == NULL || count == 0)
53+
return EINVAL;
54+
55+
if (barrier_attr != NULL)
56+
return ENOTSUP;
57+
58+
b = uv__malloc(sizeof(*b));
59+
if (b == NULL)
60+
return ENOMEM;
61+
62+
b->in = 0;
63+
b->out = 0;
64+
b->threshold = count;
65+
66+
if ((rc = pthread_mutex_init(&b->mutex, NULL)) != 0)
67+
goto error2;
68+
if ((rc = pthread_cond_init(&b->cond, NULL)) != 0)
69+
goto error;
70+
71+
barrier->b = b;
72+
return 0;
73+
74+
error:
75+
pthread_mutex_destroy(&b->mutex);
76+
error2:
77+
uv__free(b);
78+
return rc;
79+
}
80+
81+
int pthread_barrier_wait(pthread_barrier_t* barrier) {
82+
int rc;
83+
_uv_barrier* b;
84+
85+
if (barrier == NULL || barrier->b == NULL)
86+
return EINVAL;
87+
88+
b = barrier->b;
89+
/* Lock the mutex*/
90+
if ((rc = pthread_mutex_lock(&b->mutex)) != 0)
91+
return rc;
92+
93+
/* Increment the count. If this is the first thread to reach the threshold,
94+
wake up waiters, unlock the mutex, then return
95+
PTHREAD_BARRIER_SERIAL_THREAD. */
96+
if (++b->in == b->threshold) {
97+
b->in = 0;
98+
b->out = b->threshold - 1;
99+
rc = pthread_cond_signal(&b->cond);
100+
assert(rc == 0);
101+
102+
pthread_mutex_unlock(&b->mutex);
103+
return PTHREAD_BARRIER_SERIAL_THREAD;
104+
}
105+
/* Otherwise, wait for other threads until in is set to 0,
106+
then return 0 to indicate this is not the first thread. */
107+
do {
108+
if ((rc = pthread_cond_wait(&b->cond, &b->mutex)) != 0)
109+
break;
110+
} while (b->in != 0);
111+
112+
/* mark thread exit */
113+
b->out--;
114+
pthread_cond_signal(&b->cond);
115+
pthread_mutex_unlock(&b->mutex);
116+
return rc;
117+
}
118+
119+
int pthread_barrier_destroy(pthread_barrier_t* barrier) {
120+
int rc;
121+
_uv_barrier* b;
122+
123+
if (barrier == NULL || barrier->b == NULL)
124+
return EINVAL;
125+
126+
b = barrier->b;
127+
128+
if ((rc = pthread_mutex_lock(&b->mutex)) != 0)
129+
return rc;
130+
131+
if (b->in > 0 || b->out > 0)
132+
rc = EBUSY;
133+
134+
pthread_mutex_unlock(&b->mutex);
135+
136+
if (rc)
137+
return rc;
138+
139+
pthread_cond_destroy(&b->cond);
140+
pthread_mutex_destroy(&b->mutex);
141+
uv__free(barrier->b);
142+
barrier->b = NULL;
143+
return 0;
144+
}
145+
#endif
146+
147+
44148
int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
45149
int err;
46150
pthread_attr_t* attr;

uv.gyp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@
217217
'sources': [
218218
'src/unix/darwin.c',
219219
'src/unix/fsevents.c',
220-
'src/unix/darwin-proctitle.c',
221-
'src/unix/pthread-barrier.c'
220+
'src/unix/darwin-proctitle.c'
222221
],
223222
'defines': [
224223
'_DARWIN_USE_64_BIT_INODE=1',
@@ -253,7 +252,6 @@
253252
'src/unix/linux-syscalls.h',
254253
'src/unix/pthread-fixes.c',
255254
'src/unix/android-ifaddrs.c',
256-
'src/unix/pthread-barrier.c',
257255
'src/unix/procfs-exepath.c',
258256
'src/unix/sysinfo-loadavg.c',
259257
'src/unix/sysinfo-memory.c',
@@ -322,7 +320,6 @@
322320
['OS=="os390"', {
323321
'sources': [
324322
'src/unix/pthread-fixes.c',
325-
'src/unix/pthread-barrier.c',
326323
'src/unix/no-fsevents.c',
327324
'src/unix/os390.c',
328325
'src/unix/os390-syscalls.c'

0 commit comments

Comments
 (0)