aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/init.c44
-rw-r--r--src/core/init.h41
-rw-r--r--src/core/nng_impl.h1
-rw-r--r--src/core/panic.c17
-rw-r--r--src/core/panic.h9
-rw-r--r--src/core/platform.c2
-rw-r--r--src/core/platform.h73
-rw-r--r--src/core/snprintf.c6
-rw-r--r--src/core/snprintf.h1
-rw-r--r--src/core/socket.c4
10 files changed, 156 insertions, 42 deletions
diff --git a/src/core/init.c b/src/core/init.c
new file mode 100644
index 00000000..4738a585
--- /dev/null
+++ b/src/core/init.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2016 Garrett D'Amore <garrett@damore.org>
+ *
+ * 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 "core/nng_impl.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+static int
+init_helper(void)
+{
+ /* This would be where we would initialize transports, etc. */
+ return (0);
+}
+
+int
+nni_init(void)
+{
+ return (nni_plat_init(init_helper));
+}
+
+void
+nni_fini(void)
+{
+ nni_plat_fini();
+}
diff --git a/src/core/init.h b/src/core/init.h
new file mode 100644
index 00000000..3a9221e4
--- /dev/null
+++ b/src/core/init.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2016 Garrett D'Amore <garrett@damore.org>
+ *
+ * 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.
+ */
+
+#ifndef CORE_INIT_H
+#define CORE_INIT_H
+
+#include "core/nng_impl.h"
+
+/*
+ * nni_init is called each time the user enters the library. It ensures that
+ * the library is initlialized properly, and also deals with checks such as
+ * whether the process has forked since last initialization.
+ */
+extern int nni_init(void);
+
+/*
+ * nni_fini tears everything down. In the future it may be used to ensure
+ * that all resources used by the library are released back to the system.
+ */
+extern void nni_fini(void);
+
+#endif /* CORE_INIT_H */ \ No newline at end of file
diff --git a/src/core/nng_impl.h b/src/core/nng_impl.h
index 4b62cfee..4d982113 100644
--- a/src/core/nng_impl.h
+++ b/src/core/nng_impl.h
@@ -40,6 +40,7 @@
#include "core/defs.h"
#include "core/list.h"
+#include "core/init.h"
#include "core/message.h"
#include "core/msgqueue.h"
#include "core/panic.h"
diff --git a/src/core/panic.c b/src/core/panic.c
index 52dcb8c8..43c25beb 100644
--- a/src/core/panic.c
+++ b/src/core/panic.c
@@ -50,7 +50,7 @@ show_backtrace(void)
return;
}
for (i = 1; i < nframes; i++) {
- nni_debug_out(lines[i]);
+ nni_println(lines[i]);
}
}
#endif
@@ -75,10 +75,17 @@ nni_panic(const char *fmt, ...)
(void) nni_vsnprintf(buf, sizeof (buf), fbuf, va);
va_end(va);
- nni_debug_out(buf);
- nni_debug_out("This message is indicative of a BUG.");
- nni_debug_out("Report this at http://github.com/nanomsg/nanomsg");
+ nni_println(buf);
+ nni_println("This message is indicative of a BUG.");
+ nni_println("Report this at http://github.com/nanomsg/nanomsg");
show_backtrace();
- nni_abort();
+ nni_plat_abort();
+}
+
+void
+nni_println(const char *msg)
+{
+ /* TODO: support redirection of this later. */
+ nni_plat_println(msg);
}
diff --git a/src/core/panic.h b/src/core/panic.h
index 6deef964..2dc65f8c 100644
--- a/src/core/panic.h
+++ b/src/core/panic.h
@@ -34,4 +34,13 @@
*/
extern void nni_panic(const char *, ...);
+/*
+ * nni_println is used to print output to a debug console. This should only
+ * be used in the most dire of circumstances -- such as during an assertion
+ * failure that is going to cause the program to crash. After the string is
+ * emitted, a new line character is emitted, so the string should not
+ * include one.
+ */
+extern void nni_println(const char *);
+
#endif /* CORE_PANIC_H */
diff --git a/src/core/platform.c b/src/core/platform.c
index d96ebe70..dbdf3a7c 100644
--- a/src/core/platform.c
+++ b/src/core/platform.c
@@ -26,8 +26,10 @@
#include "core/nng_impl.h"
+#if 0
#if defined(PLATFORM_POSIX)
#include "platform/posix/posix_impl.h"
#else
#error "unknown platform"
#endif
+#endif
diff --git a/src/core/platform.h b/src/core/platform.h
index f7af7c30..230055c3 100644
--- a/src/core/platform.h
+++ b/src/core/platform.h
@@ -56,32 +56,23 @@
*/
/*
- * nni_abort crashes the system; it should do whatever is appropriate
+ * nni_plat_abort crashes the system; it should do whatever is appropriate
* for abnormal programs on the platform, such as calling abort().
*/
-void nni_abort(void);
+extern void nni_plat_abort(void);
/*
- * nni_vnsprintf is exactly like its POSIX counterpart.
+ * nni_plat_vnsprintf is exactly like its POSIX counterpart.
* Some platforms (Windows!) need a special version of this.
*/
-void nni_vsnprintf(char *, size_t, const char *, va_list);
+extern void nni_plat_vsnprintf(char *, size_t, const char *, va_list);
/*
- * nni_debug_output is used to emit debug messages. Typically this is used
+ * nni_plat_println is used to emit debug messages. Typically this is used
* during core debugging, or to emit panic messages. Message content will
* not contain newlines, but the output will add them.
*/
-void nni_debug_out(const char *);
-
-/*
- * nni_set_debug_output is used to redirect debug output; for example an
- * application could replace the default output routine with one that sends
- * it's output to syslog. If NULL is specified, then a default handler
- * used instead. The handler should add any newlines to the output as
- * required. The default handler writes to standard error.
- */
-void nni_set_debug_out(void (*)(const char *));
+extern void nni_plat_println(const char *);
/*
* nni_alloc allocates memory. In most cases this can just be malloc().
@@ -89,7 +80,7 @@ void nni_set_debug_out(void (*)(const char *));
* possible to use a slab allocator or somesuch. It is permissible for this
* to return NULL if memory cannot be allocated.
*/
-void *nni_alloc(size_t);
+extern void *nni_alloc(size_t);
/*
* nni_free frees memory allocated with nni_alloc. It takes a size because
@@ -99,7 +90,7 @@ void *nni_alloc(size_t);
* This routine does nothing if supplied with a NULL pointer and zero size.
* Most implementations can just call free() here.
*/
-void nni_free(void *, size_t);
+extern void nni_free(void *, size_t);
typedef struct nni_mutex *nni_mutex_t;
typedef struct nni_cond *nni_cond_t;
@@ -107,32 +98,32 @@ typedef struct nni_cond *nni_cond_t;
/*
* Mutex handling.
*/
-int nni_mutex_create(nni_mutex_t *);
-void nni_mutex_destroy(nni_mutex_t);
-void nni_mutex_enter(nni_mutex_t);
-void nni_mutex_exit(nni_mutex_t);
-int nni_mutex_tryenter(nni_mutex_t);
+extern int nni_mutex_create(nni_mutex_t *);
+extern void nni_mutex_destroy(nni_mutex_t);
+extern void nni_mutex_enter(nni_mutex_t);
+extern void nni_mutex_exit(nni_mutex_t);
+extern int nni_mutex_tryenter(nni_mutex_t);
-int nni_cond_create(nni_cond_t *, nni_mutex_t);
-void nni_cond_destroy(nni_cond_t);
+extern int nni_cond_create(nni_cond_t *, nni_mutex_t);
+extern void nni_cond_destroy(nni_cond_t);
/*
* nni_cond_broadcast wakes all waiters on the condition. This should be
* called with the lock held.
*/
-void nni_cond_broadcast(nni_cond_t);
+extern void nni_cond_broadcast(nni_cond_t);
/*
* nni_cond_signal wakes a signal waiter.
*/
-void nni_cond_signal(nni_cond_t);
+extern void nni_cond_signal(nni_cond_t);
/*
* nni_condwait waits for a wake up on the condition variable. The
* associated lock is atomically released and reacquired upon wake up.
* Callers can be spuriously woken. The associated lock must be held.
*/
-void nni_cond_wait(nni_cond_t);
+extern void nni_cond_wait(nni_cond_t);
/*
* nni_cond_timedwait waits for a wakeup on the condition variable, just
@@ -143,33 +134,33 @@ void nni_cond_wait(nni_cond_t);
* can be NNG_ETIMEDOUT. Note that it is permissible to wait for longer
* than the timeout based on the resolution of your system clock.
*/
-int nni_cond_timedwait(nni_cond_t, uint64_t);
+extern int nni_cond_timedwait(nni_cond_t, uint64_t);
typedef struct nni_thread *nni_thread_t;
/*
* nni_thread_creates a thread that runs the given function. The thread
* receives a single argument.
*/
-int nni_thread_create(nni_thread_t *, void (*fn)(void *), void *);
+extern int nni_thread_create(nni_thread_t *, void (*fn)(void *), void *);
/*
* nni_thread_reap waits for the thread to exit, and then releases any
* resources associated with the thread. After this returns, it
* is an error to reference the thread in any further way.
*/
-void nni_thread_reap(nni_thread_t);
+extern void nni_thread_reap(nni_thread_t);
/*
* nn_clock returns a number of microseconds since some arbitrary time
* in the past. The values returned by nni_clock may be used with
* nni_cond_timedwait.
*/
-uint64_t nni_clock(void);
+extern uint64_t nni_clock(void);
/*
* nni_usleep sleeps for the specified number of microseconds (at least).
*/
-void nni_usleep(uint64_t);
+extern void nni_usleep(uint64_t);
/*
* nni_platform_init is called to allow the platform the chance to
@@ -178,15 +169,27 @@ void nni_usleep(uint64_t);
* may be called at any point thereafter. It is permitted to return
* an error if some critical failure inializing the platform occurs,
* but once this succeeds, all future calls must succeed as well, unless
- * nni_fini has been called.
+ * nni_plat_fini has been called.
+ *
+ * The function argument should be called if the platform has not initialized
+ * (i.e. exactly once please), and its result passed back to the caller.
*/
-int nni_platform_init(void);
+extern int nni_plat_init(int (*)(void));
/*
* nni_platform_fini is called to clean up resources. It is intended to
* be called as the last thing executed in the library, and no other functions
* will be called until nni_platform_init is called.
*/
-void nni_platform_fini(void);
+extern void nni_plat_fini(void);
+
+/*
+ * Actual platforms we support.
+ */
+#if defined(PLATFORM_POSIX)
+#include "platform/posix/posix_impl.h"
+#else
+#error "unknown platform"
+#endif
#endif /* CORE_PLATFORM_H */
diff --git a/src/core/snprintf.c b/src/core/snprintf.c
index 574d537a..004916a2 100644
--- a/src/core/snprintf.c
+++ b/src/core/snprintf.c
@@ -34,3 +34,9 @@ nni_snprintf(char *dst, size_t sz, const char *fmt, ...)
nni_vsnprintf(dst, sz, fmt, va);
va_end(va);
}
+
+void
+nni_vsnprintf(char *dst, size_t sz, const char *fmt, va_list va)
+{
+ nni_plat_vsnprintf(dst, sz, fmt, va);
+}
diff --git a/src/core/snprintf.h b/src/core/snprintf.h
index b98b6ee7..12d7f207 100644
--- a/src/core/snprintf.h
+++ b/src/core/snprintf.h
@@ -31,5 +31,6 @@
* from the platform.
*/
extern void nni_snprintf(char *, size_t, const char *, ...);
+extern void nni_vsnprintf(char *, size_t, const char *, va_list);
#endif /* CORE_SNPRINTF_H */
diff --git a/src/core/socket.c b/src/core/socket.c
index f5904823..094f9ce0 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -110,8 +110,8 @@ nni_socket_close(nni_socket_t sock)
*/
/* XXX: close remaining pipes */
- while ((pipe = nni_list_first(&sock->nn_pipes)) != NULL) {
- nni_list_remove(&sock->nn_pipes, pipe);
+ while ((pipe = nni_list_first(&sock->s_pipes)) != NULL) {
+ nni_list_remove(&sock->s_pipes, pipe);
/* XXX: call nni_pipe_close, then nni_pipe_destroy */
}