aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/platform.h33
-rw-r--r--src/core/transport.c62
-rw-r--r--src/core/transport.h22
3 files changed, 117 insertions, 0 deletions
diff --git a/src/core/platform.h b/src/core/platform.h
index ea8f1378..e2d81139 100644
--- a/src/core/platform.h
+++ b/src/core/platform.h
@@ -95,6 +95,7 @@ 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);
+
int nni_cond_create(nni_cond_t *, nni_mutex_t);
void nni_cond_destroy(nni_cond_t);
@@ -127,6 +128,20 @@ void nni_cond_wait(nni_cond_t);
*/
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 *);
+
+/*
+ * 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);
+
/*
* nn_clock returns a number of microseconds since some arbitrary time
* in the past. The values returned by nni_clock may be used with
@@ -139,4 +154,22 @@ uint64_t nni_clock(void);
*/
void nni_usleep(uint64_t);
+/*
+ * nni_init is called to allow the platform the chance to
+ * do any necessary initialization. This routine MUST be idempotent,
+ * and threadsafe, and will be called before any other API calls, and
+ * 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.
+ */
+int nni_platform_init(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_fini(void);
+
#endif /* CORE_PLATFORM_H */
diff --git a/src/core/transport.c b/src/core/transport.c
new file mode 100644
index 00000000..67f2ca2f
--- /dev/null
+++ b/src/core/transport.c
@@ -0,0 +1,62 @@
+/*
+ * 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"
+
+/*
+ * For now the list of transports is hard-wired. Adding new transports
+ * to the system dynamically is something that might be considered later.
+ */
+extern struct nni_transport_ops nni_inproc_tran_ops;
+
+static struct nni_transport_ops *transports[] = {
+ &nni_inproc_tran_ops,
+ NULL
+};
+
+/*
+ * nni_transport_init initializes the entire transport subsystem, including
+ * each individual transport.
+ */
+void
+nni_transport_init(void)
+{
+ int i;
+ struct nni_transport_ops *ops;
+
+ for (i = 0; (ops = transports[i]) != NULL; i++) {
+ ops->tran_init();
+ }
+}
+
+void
+nni_transport_fork(int prefork)
+{
+ int i;
+ struct nni_transport_ops *ops;
+
+ for (i = 0; (ops = transports[i]) != NULL; i++) {
+ if (ops->tran_fork != NULL) {
+ ops->tran_fork(prefork);
+ }
+ }
+}
diff --git a/src/core/transport.h b/src/core/transport.h
index 567cc0c5..65f1976e 100644
--- a/src/core/transport.h
+++ b/src/core/transport.h
@@ -43,6 +43,28 @@ struct nni_transport_ops {
* tran_pipe_ops links our pipe operations.
*/
const struct nni_pipe_ops *tran_pipe_ops;
+
+ /*
+ * tran_init, if not NULL, is called once during library
+ * initialization.
+ */
+ int (*tran_init)(void);
+
+ /*
+ * tran_fini, if not NULL, is called during library deinitialization.
+ * It should release any global resources.
+ */
+ void (*tran_fini)(void);
+
+ /*
+ * tran_fork, if not NULL, is called just before fork
+ * (e.g. during pthread_atfork() for the prefork phase),
+ * and again just after returning in the parent. There is
+ * nothing called for the child. If the transport does not
+ * create any threads of its own, this can be NULL. (The
+ * intended use is to prevent O_CLOEXEC races.)
+ */
+ void (*tran_fork)(int prefork);
};
struct nni_endpt_ops {