Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pthread missing functions in Android #1651

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -74,9 +74,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)
Loading