Skip to content

Commit d80b914

Browse files
davidBar-Onbmah888
authored andcommitted
Add pthread missing functions in Android
1 parent b5eb3a4 commit d80b914

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ libiperf_la_SOURCES = \
3333
iperf_util.h \
3434
iperf_time.c \
3535
iperf_time.h \
36+
iperf_pthread.c \
37+
iperf_pthread.h \
3638
dscp.c \
3739
net.c \
3840
net.h \

src/iperf.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@
6161
#include <openssl/evp.h>
6262
#endif // HAVE_SSL
6363

64-
#ifdef HAVE_PTHREAD
65-
#include <pthread.h>
66-
#endif // HAVE_PTHREAD
64+
#include "iperf_pthread.h"
6765

6866
/*
6967
* Atomic types highly desired, but if not, we approximate what we need

src/iperf_pthread.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "iperf_config.h"
2+
3+
#if defined(HAVE_PTHREAD) && defined(__ANDROID__)
4+
5+
/* Workaround for `pthread_cancel()` in Android, using `pthread_kill()` instead,
6+
* as Android NDK does not support `pthread_cancel()`.
7+
*/
8+
9+
#include <signal.h>
10+
#include "iperf_pthread.h"
11+
12+
int pthread_setcanceltype(int type, int *oldtype) { return 0; }
13+
int pthread_setcancelstate(int state, int *oldstate) { return 0; }
14+
int pthread_cancel(pthread_t thread_id) {
15+
int status;
16+
if ((status = iperf_set_thread_exit_handler()) == 0) {
17+
status = pthread_kill(thread_id, SIGUSR1);
18+
}
19+
return status;
20+
}
21+
22+
void iperf_thread_exit_handler(int sig)
23+
{
24+
pthread_exit(0);
25+
}
26+
27+
int iperf_set_thread_exit_handler() {
28+
int rc;
29+
struct sigaction actions;
30+
31+
memset(&actions, 0, sizeof(actions));
32+
sigemptyset(&actions.sa_mask);
33+
actions.sa_flags = 0;
34+
actions.sa_handler = iperf_thread_exit_handler;
35+
36+
rc = sigaction(SIGUSR1, &actions, NULL);
37+
return rc;
38+
}
39+
40+
#endif // defined(HAVE_PTHREAD) && defined(__ANDROID__)

src/iperf_pthread.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "iperf_config.h"
2+
3+
#if defined(HAVE_PTHREAD)
4+
5+
#include <pthread.h>
6+
7+
#if defined(__ANDROID__)
8+
9+
/* Adding missing `pthread` related definitions in Android.
10+
*/
11+
12+
#define PTHREAD_CANCEL_ASYNCHRONOUS 0
13+
#define PTHREAD_CANCEL_ENABLE NULL
14+
15+
int pthread_setcanceltype(int type, int *oldtype);
16+
int pthread_setcancelstate(int state, int *oldstate);
17+
int pthread_cancel(pthread_t thread_id);
18+
19+
#endif // defined(__ANDROID__)
20+
21+
#endif // defined(HAVE_PTHREAD)

0 commit comments

Comments
 (0)