Skip to content

Instantly share code, notes, and snippets.

@kroggen
Created December 20, 2015 02:31
Show Gist options
  • Save kroggen/ff49ad06e78754068be6 to your computer and use it in GitHub Desktop.
Save kroggen/ff49ad06e78754068be6 to your computer and use it in GitHub Desktop.
nanomsg on Android-15 and previous
LOCAL_PATH := $(call my-dir)/nanomsg
include $(CLEAR_VARS)
LOCAL_SRC_FILES := src/core/epbase.c src/core/sock.c src/core/poll.c \
src/core/symbol.c src/core/ep.c src/core/pipe.c \
src/core/sockbase.c src/core/global.c src/devices/device.c \
src/transports/inproc/ins.c src/transports/inproc/inproc.c \
src/transports/inproc/cinproc.c src/transports/inproc/binproc.c \
src/transports/inproc/sinproc.c src/transports/inproc/msgqueue.c \
src/transports/utils/dns.c src/transports/utils/literal.c \
src/transports/utils/streamhdr.c src/transports/utils/backoff.c \
src/transports/utils/iface.c src/transports/utils/port.c \
src/transports/tcp/tcp.c src/transports/tcp/stcp.c \
src/transports/tcp/ctcp.c src/transports/tcp/atcp.c \
src/transports/tcp/btcp.c src/transports/ipc/aipc.c \
src/transports/ipc/bipc.c src/transports/ipc/cipc.c \
src/transports/ipc/ipc.c src/transports/ipc/sipc.c \
src/protocols/survey/xrespondent.c \
src/protocols/survey/surveyor.c src/protocols/survey/xsurveyor.c \
src/protocols/survey/respondent.c src/protocols/pair/pair.c \
src/protocols/pair/xpair.c src/protocols/utils/dist.c \
src/protocols/utils/priolist.c src/protocols/utils/fq.c \
src/protocols/utils/excl.c src/protocols/utils/lb.c \
src/protocols/bus/xbus.c src/protocols/bus/bus.c \
src/protocols/pipeline/xpull.c src/protocols/pipeline/push.c \
src/protocols/pipeline/pull.c src/protocols/pipeline/xpush.c \
src/protocols/reqrep/rep.c src/protocols/reqrep/req.c \
src/protocols/reqrep/xrep.c src/protocols/reqrep/task.c \
src/protocols/reqrep/xreq.c src/protocols/pubsub/sub.c \
src/protocols/pubsub/xpub.c src/protocols/pubsub/xsub.c \
src/protocols/pubsub/trie.c src/protocols/pubsub/pub.c \
src/aio/worker.c src/aio/fsm.c src/aio/ctx.c src/aio/usock.c \
src/aio/poller.c src/aio/pool.c src/aio/timerset.c \
src/aio/timer.c src/utils/err.c src/utils/thread.c \
src/utils/closefd.c src/utils/atomic.c src/utils/list.c \
src/utils/stopwatch.c src/utils/random.c src/utils/wire.c \
src/utils/mutex.c src/utils/msg.c src/utils/clock.c \
src/utils/queue.c src/utils/chunk.c src/utils/efd.c \
src/utils/hash.c src/utils/alloc.c src/utils/glock.c \
src/utils/sleep.c src/utils/chunkref.c src/utils/sem.c
LOCAL_CFLAGS := \
-DNN_HAVE_STDINT \
-DNN_HAVE_CLOCK_MONOTONIC \
-DNN_HAVE_PIPE \
-DNN_HAVE_SOCKETPAIR \
-DNN_HAVE_SEMAPHORE \
-DNN_HAVE_POLL \
-DNN_USE_POLL \
-DNN_USE_LITERAL_IFADDR \
-DNN_HAVE_MSG_CONTROL
LOCAL_MODULE := libnanomsg
LOCAL_MODULE_TAGS := optional
LOCAL_COPY_HEADERS_TO := nanomsg
LOCAL_COPY_HEADERS := nn.h bus.h reqrep.h pipeline.h pair.h survey.h pubsub.h
include $(BUILD_SHARED_LIBRARY)
diff --git a/src/core/global.c b/src/core/global.c
index ff5a610..b73ede5 100644
--- a/src/core/global.c
+++ b/src/core/global.c
@@ -255,8 +255,8 @@ static void nn_global_init (void)
nn_global_add_transport (nn_inproc);
nn_global_add_transport (nn_ipc);
nn_global_add_transport (nn_tcp);
- nn_global_add_transport (nn_ws);
- nn_global_add_transport (nn_tcpmux);
+// nn_global_add_transport (nn_ws);
+// nn_global_add_transport (nn_tcpmux);
/* Plug in individual socktypes. */
nn_global_add_socktype (nn_pair_socktype);
// --- Start of Android platform fix --
/* Despite the fact that our kernel headers define sigset_t explicitly
* as a 32-bit integer, the kernel system call really expects a 64-bit
* bitmap for the signal set, or more exactly an array of two-32-bit
* values (see $KERNEL/arch/$ARCH/include/asm/signal.h for details).
*
* Unfortunately, we cannot fix the sigset_t definition without breaking
* the C library ABI, so perform a little runtime translation here.
*/
typedef union {
sigset_t bionic;
uint32_t kernel[2];
} kernel_sigset_t;
int pthread_sigmask_android16(int how, const sigset_t *set, sigset_t *oset)
{
int ret, old_errno = errno;
/* We must convert *set into a kernel_sigset_t */
kernel_sigset_t in_set, *in_set_ptr;
kernel_sigset_t out_set;
in_set.kernel[0] = in_set.kernel[1] = 0;
out_set.kernel[0] = out_set.kernel[1] = 0;
/* 'in_set_ptr' is the second parameter to __rt_sigprocmask. It must be NULL
* if 'set' is NULL to ensure correct semantics (which in this case would
* be to ignore 'how' and return the current signal set into 'oset'.
*/
if (set == NULL) {
in_set_ptr = NULL;
} else {
in_set.bionic = *set;
in_set_ptr = &in_set;
}
ret = sigprocmask(how, (const sigset_t *)in_set_ptr, (const sigset_t *)&out_set);
if (ret < 0)
ret = errno;
if (oset)
*oset = out_set.bionic;
errno = old_errno;
return ret;
}
// --- End of Android platform fix --
// first try to call pthread_sigmask, in case of failure try again with the API 16 fix
int pthread_sigmask_patched(int how, const sigset_t *set, sigset_t *oset) {
int ret = pthread_sigmask(how, set, oset);
if (ret == EINVAL) {
return pthread_sigmask_android16(how, set, oset);
}
}
/*
Copyright (c) 2012-2013 Martin Sustrik All rights reserved.
Copyright (c) 2014 Achille Roussel All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include "err.h"
#ifdef __ANDROID__
#include "thread_android.inc"
#endif
#include <signal.h>
static void *nn_thread_main_routine (void *arg)
{
struct nn_thread *self;
self = (struct nn_thread*) arg;
/* Run the thread routine. */
self->routine (self->arg);
return NULL;
}
void nn_thread_init (struct nn_thread *self,
nn_thread_routine *routine, void *arg)
{
int rc;
sigset_t new_sigmask;
sigset_t old_sigmask;
/* No signals should be processed by this thread. The library doesn't
use signals and thus all the signals should be delivered to application
threads, not to worker threads. */
rc = sigfillset (&new_sigmask);
errno_assert (rc == 0);
#ifdef __ANDROID__
rc = pthread_sigmask_patched (SIG_BLOCK, &new_sigmask, &old_sigmask);
#else
rc = pthread_sigmask (SIG_BLOCK, &new_sigmask, &old_sigmask);
#endif
errnum_assert (rc == 0, rc);
self->routine = routine;
self->arg = arg;
rc = pthread_create (&self->handle, NULL, nn_thread_main_routine,
(void*) self);
errnum_assert (rc == 0, rc);
/* Restore signal set to what it was before. */
#ifdef __ANDROID__
rc = pthread_sigmask_patched (SIG_SETMASK, &old_sigmask, NULL);
#else
rc = pthread_sigmask (SIG_SETMASK, &old_sigmask, NULL);
#endif
errnum_assert (rc == 0, rc);
}
void nn_thread_term (struct nn_thread *self)
{
int rc;
rc = pthread_join (self->handle, NULL);
errnum_assert (rc == 0, rc);
}
@kroggen
Copy link
Author

kroggen commented Jan 1, 2017

Now they are in the android-nanomsg-native

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment