Skip to content

Commit

Permalink
Add pthread missing functions in Android
Browse files Browse the repository at this point in the history
  • Loading branch information
davidBar-On authored and bmah888 committed Mar 27, 2024
1 parent b5eb3a4 commit d80b914
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ libiperf_la_SOURCES = \
iperf_util.h \
iperf_time.c \
iperf_time.h \
iperf_pthread.c \
iperf_pthread.h \
dscp.c \
net.c \
net.h \
Expand Down
4 changes: 1 addition & 3 deletions src/iperf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@
#include <openssl/evp.h>
#endif // HAVE_SSL

#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif // HAVE_PTHREAD
#include "iperf_pthread.h"

/*
* Atomic types highly desired, but if not, we approximate what we need
Expand Down
40 changes: 40 additions & 0 deletions src/iperf_pthread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "iperf_config.h"

#if defined(HAVE_PTHREAD) && defined(__ANDROID__)

/* Workaround for `pthread_cancel()` in Android, using `pthread_kill()` instead,
* as Android NDK does not support `pthread_cancel()`.
*/

#include <signal.h>
#include "iperf_pthread.h"

int pthread_setcanceltype(int type, int *oldtype) { return 0; }
int pthread_setcancelstate(int state, int *oldstate) { return 0; }
int pthread_cancel(pthread_t thread_id) {
int status;
if ((status = iperf_set_thread_exit_handler()) == 0) {
status = pthread_kill(thread_id, SIGUSR1);
}
return status;
}

void iperf_thread_exit_handler(int sig)
{
pthread_exit(0);
}

int iperf_set_thread_exit_handler() {
int rc;
struct sigaction actions;

memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = iperf_thread_exit_handler;

rc = sigaction(SIGUSR1, &actions, NULL);
return rc;
}

#endif // defined(HAVE_PTHREAD) && defined(__ANDROID__)
21 changes: 21 additions & 0 deletions src/iperf_pthread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "iperf_config.h"

#if defined(HAVE_PTHREAD)

#include <pthread.h>

#if defined(__ANDROID__)

/* Adding missing `pthread` related definitions in Android.
*/

#define PTHREAD_CANCEL_ASYNCHRONOUS 0
#define PTHREAD_CANCEL_ENABLE NULL

int pthread_setcanceltype(int type, int *oldtype);
int pthread_setcancelstate(int state, int *oldstate);
int pthread_cancel(pthread_t thread_id);

#endif // defined(__ANDROID__)

#endif // defined(HAVE_PTHREAD)

0 comments on commit d80b914

Please sign in to comment.