aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2016-12-22 20:52:45 -0800
committerGarrett D'Amore <garrett@damore.org>2016-12-22 20:52:45 -0800
commitee45cbf4498a3c1d1868469bdb0c767d66c278e4 (patch)
treeb9116256f12a54c90f92bf5cf215f3d4c8152126 /src/platform/posix
parent718de1828cc5b5256511c5b723360d499ae21c8f (diff)
downloadnng-ee45cbf4498a3c1d1868469bdb0c767d66c278e4.tar.gz
nng-ee45cbf4498a3c1d1868469bdb0c767d66c278e4.tar.bz2
nng-ee45cbf4498a3c1d1868469bdb0c767d66c278e4.zip
Endpoint dialer implemented.
Diffstat (limited to 'src/platform/posix')
-rw-r--r--src/platform/posix/posix_config.h70
-rw-r--r--src/platform/posix/posix_impl.h1
-rw-r--r--src/platform/posix/posix_thread.c27
3 files changed, 63 insertions, 35 deletions
diff --git a/src/platform/posix/posix_config.h b/src/platform/posix/posix_config.h
index dd2167ab..1510d739 100644
--- a/src/platform/posix/posix_config.h
+++ b/src/platform/posix/posix_config.h
@@ -1,38 +1,40 @@
-/*
- * Copyright 2016 Garrett D'Amore <garrett@damore.org>
- *
- * This software is supplied under the terms of the MIT License, a
- * copy of which should be located in the distribution where this
- * file was obtained (LICENSE.txt). A copy of the license may also be
- * found online at https://opensource.org/licenses/MIT.
- */
+//
+// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
-/*
- * The following adjustments to the platform may be defined. These can
- * be defined in either platform/config.h or loaded in via external
- * defines using cmake.
- *
- * #define NNG_USE_GETTIMEOFDAY
- * This macro is defined if you lack a working clock_gettime,
- * nanosleep, or pthread_condattr_setclock. In this case the
- * library uses the system clock for relative sleeps, timers, etc.
- * This can be dangerous if the system clock is changed, so only
- * use this if you have no other choice. If it appears that
- * the system lacks clock_gettime, then it will choose this automatically.
- * This value may be ignored on platforms that don't use POSIX clocks.
- *
- * #define NNG_USE_CLOCKID
- * This macro may be defined to a different clock id (see
- * clock_gettime()). By default we use CLOCK_MONOTONIC if it exists,
- * or CLOCK_REALTIME otherwise. This is ignored if NNG_USE_GETTIMEOFDAY
- * is defined. Platforms that don't use POSIX clocks will probably
- * ignore any setting here.
- *
- * #define NNG_HAVE_BACKTRACE
- * If your system has a working backtrace(), and backtrace_symbols(),
- * along with <execinfo.h>, you can define this to get richer backtrace
- * information for debugging.
- */
+// The following adjustments to the platform may be defined. These can
+// be defined in either platform/config.h or loaded in via external
+// defines using cmake.
+//
+// #define NNG_USE_GETTIMEOFDAY
+// This macro is defined if you lack a working clock_gettime,
+// nanosleep, or pthread_condattr_setclock. In this case the
+// library uses the system clock for relative sleeps, timers, etc.
+// This can be dangerous if the system clock is changed, so only
+// use this if you have no other choice. If it appears that
+// the system lacks clock_gettime, then it will choose this automatically.
+// This value may be ignored on platforms that don't use POSIX clocks.
+//
+// #define NNG_USE_CLOCKID
+// This macro may be defined to a different clock id (see
+// clock_gettime()). By default we use CLOCK_MONOTONIC if it exists,
+// or CLOCK_REALTIME otherwise. This is ignored if NNG_USE_GETTIMEOFDAY
+// is defined. Platforms that don't use POSIX clocks will probably
+// ignore any setting here.
+//
+// #define NNG_HAVE_ARC4RANDOM
+// This indicates that the platform has the superior arc4random function
+// for getting entropy.
+//
+// #define NNG_HAVE_BACKTRACE
+// If your system has a working backtrace(), and backtrace_symbols(),
+// along with <execinfo.h>, you can define this to get richer backtrace
+// information for debugging.
#include <time.h>
diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h
index 0fa15b43..a07b5c92 100644
--- a/src/platform/posix/posix_impl.h
+++ b/src/platform/posix/posix_impl.h
@@ -21,6 +21,7 @@
#define PLATFORM_POSIX_ALLOC
#define PLATFORM_POSIX_DEBUG
#define PLATFORM_POSIX_CLOCK
+#define PLATFORM_POSIX_RANDOM
#define PLATFORM_POSIX_SYNCH
#define PLATFORM_POSIX_THREAD
diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c
index 80f3b96f..24f97b81 100644
--- a/src/platform/posix/posix_thread.c
+++ b/src/platform/posix/posix_thread.c
@@ -16,6 +16,7 @@
#include <pthread.h>
#include <time.h>
#include <string.h>
+#include <stdlib.h>
struct nni_thread {
pthread_t tid;
@@ -26,6 +27,17 @@ struct nni_thread {
static pthread_mutex_t nni_plat_lock = PTHREAD_MUTEX_INITIALIZER;
static int nni_plat_inited = 0;
static int nni_plat_forked = 0;
+static int nni_plat_next = 0;
+
+uint32_t
+nni_plat_nextid(void)
+{
+ uint32_t id;
+ pthread_mutex_lock(&nni_plat_lock);
+ id = nni_plat_next++;
+ pthread_mutex_unlock(&nni_plat_lock);
+ return (id);
+}
static void *
nni_thrfunc(void *arg)
@@ -118,6 +130,20 @@ nni_plat_init(int (*helper)(void))
return (NNG_ENOMEM);
}
+ // Generate a starting ID (used for Pipe IDs)
+#ifdef NNG_HAVE_ARC4RANDOM
+ nni_plat_next = arc4random();
+#else
+ while (nni_plat_next == 0) {
+ uint16_t xsub[3];
+ nni_time now = nni_clock();
+
+ xsub[0] = (uint16_t)now;
+ xsub[1] = (uint16_t)(now >> 16);
+ xsub[2] = (uint16_t)(now >> 24);
+ nni_plat_next = nrand48(xsub);
+ }
+#endif
if (pthread_atfork(NULL, NULL, nni_atfork_child) != 0) {
pthread_mutex_unlock(&nni_plat_lock);
@@ -144,5 +170,4 @@ nni_plat_fini(void)
pthread_mutex_unlock(&nni_plat_lock);
}
-
#endif