Skip to content

Commit

Permalink
Move the rest of the functionality from platform.h into core.
Browse files Browse the repository at this point in the history
This also deprecates supplemental/util/platform.h.
  • Loading branch information
gdamore committed Apr 23, 2024
1 parent ff00eac commit 0181798
Show file tree
Hide file tree
Showing 34 changed files with 352 additions and 421 deletions.
3 changes: 1 addition & 2 deletions docs/man/nng_cv_alloc.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_cv_alloc(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_cv_alloc - allocate condition variable
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
typedef struct nng_cv nng_cv;
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_cv_free.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_cv_free(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_cv_free - free condition variable
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_cv_free(nng_cv *cv);
----
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_cv_until.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_cv_until(3supp)
//
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_cv_until - wait for condition or timeout
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
int nng_cv_until(nng_cv *cv, nng_time when);
----
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_cv_wait.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_cv_wait(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_cv_wait - wait for condition
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_cv_wait(nng_cv *cv);
----
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_cv_wake.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_cv_wake(3supp)
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_cv_wake - wake all waiters
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_cv_wake(nng_cv *cv);
----
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_cv_wake1.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_cv_wake1(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_cv_wake1 - wake one waiter
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_cv_wake1(nng_cv *cv);
----
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_mtx_alloc.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_mtx_alloc(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_mtx_alloc - allocate mutex
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
typedef struct nng_mtx nng_mtx;
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_mtx_free.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_mtx_free(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_mtx_free - free mutex
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_mtx_free(nng_mtx *mtx);
----
Expand Down
12 changes: 1 addition & 11 deletions docs/man/nng_mtx_lock.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_mtx_lock(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_mtx_lock - lock mutex
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_mtx_lock(nng_mtx *mtx);
----
Expand All @@ -39,15 +38,6 @@ same mutex may result in deadlock or aborting the current program.
It is a programming error for the owner of a mutex to attempt to
reacquire it.

****
_NNG_ offers neither a non-blocking variant that can fail,
nor recursive mutexes.
This is by design, as _NNG_ itself does not use such things,
and most often the need for them is the result of poor design.
If such capabilities are needed, they may be synthesized fairly
easily from mutexes and condition variables.
****

== RETURN VALUES

None.
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_mtx_unlock.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_mtx_unlock(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_mtx_unlock - unlock mutex
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_mtx_unlock(nng_mtx *mtx);
----
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_thread_create.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_thread_create(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_thread_create - create thread
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
typedef struct nng_thread nng_thread;
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_thread_destroy.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_thread_destroy(3supp)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -18,7 +18,6 @@ nng_thread_destroy - reap thread
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_thread_destroy(nng_thread *thread);
----
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_thread_set_name.3supp.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_thread_set_name(3supp)
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
//
// This document is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
Expand All @@ -17,7 +17,6 @@ nng_thread_set_name - set thread name
[source, c]
----
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_thread_set_name(nng_thread *thread, const char *name);
----
Expand Down
64 changes: 64 additions & 0 deletions include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,70 @@ NNG_DECL uint32_t nng_random(void);
// to an array of file descriptors (or HANDLES or similar).
NNG_DECL int nng_socket_pair(int[2]);

// Multithreading and synchronization functions.

// nng_thread is a handle to a "thread", which may be a real system
// thread, or a coroutine on some platforms.
typedef struct nng_thread nng_thread;

// Create and start a thread. Note that on some platforms, this might
// actually be a coroutine, with limitations about what system APIs
// you can call. Therefore, these threads should only be used with the
// I/O APIs provided by nng. The thread runs until completion.
NNG_DECL int nng_thread_create(nng_thread **, void (*)(void *), void *);

// Set the thread name. Support for this is platform specific and varies.
// It is intended to provide information for use when debugging applications,
// and not for programmatic use beyond that.
NNG_DECL void nng_thread_set_name(nng_thread *, const char *);

// Destroy a thread (waiting for it to complete.) When this function
// returns all resources for the thread are cleaned up.
NNG_DECL void nng_thread_destroy(nng_thread *);

// nng_mtx represents a mutex, which is a simple, non-reentrant, boolean lock.
typedef struct nng_mtx nng_mtx;

// nng_mtx_alloc allocates a mutex structure.
NNG_DECL int nng_mtx_alloc(nng_mtx **);

// nng_mtx_free frees the mutex. It must not be locked.
NNG_DECL void nng_mtx_free(nng_mtx *);

// nng_mtx_lock locks the mutex; if it is already locked it will block
// until it can be locked. If the caller already holds the lock, the
// results are undefined (a panic may occur).
NNG_DECL void nng_mtx_lock(nng_mtx *);

// nng_mtx_unlock unlocks a previously locked mutex. It is an error to
// call this on a mutex which is not owned by caller.
NNG_DECL void nng_mtx_unlock(nng_mtx *);

// nng_cv is a condition variable. It is always allocated with an
// associated mutex, which must be held when waiting for it, or
// when signaling it.
typedef struct nng_cv nng_cv;

NNG_DECL int nng_cv_alloc(nng_cv **, nng_mtx *);

// nng_cv_free frees the condition variable.
NNG_DECL void nng_cv_free(nng_cv *);

// nng_cv_wait waits until the condition variable is "signaled".
NNG_DECL void nng_cv_wait(nng_cv *);

// nng_cv_until waits until either the condition is signaled, or
// the timeout expires. It returns NNG_ETIMEDOUT in that case.
NNG_DECL int nng_cv_until(nng_cv *, nng_time);

// nng_cv_wake wakes all threads waiting on the condition.
NNG_DECL void nng_cv_wake(nng_cv *);

// nng_cv_wake1 wakes only one thread waiting on the condition. This may
// reduce the thundering herd problem, but care must be taken to ensure
// that no waiter starves forever.
NNG_DECL void nng_cv_wake1(nng_cv *);

#ifdef __cplusplus
}
#endif
Expand Down
86 changes: 3 additions & 83 deletions include/nng/supplemental/util/platform.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
Expand All @@ -11,88 +10,9 @@
#ifndef NNG_SUPPLEMENTAL_UTIL_PLATFORM_H
#define NNG_SUPPLEMENTAL_UTIL_PLATFORM_H

// The declarations in this file are provided to assist with application
// portability. Conceptually these APIs are based on work we have already
// done for NNG internals, and we find that they are useful in building
// portable applications.

// If it is more natural to use native system APIs like pthreads or C11
// APIs or Windows APIs, then by all means please feel free to simply
// ignore this.

#include <stddef.h>
#include <stdint.h>

// The declarations in this file have been relocated to <nng/nng.h>.
// This file itself is deprecated, and applications should stop including
// it directly.
#include <nng/nng.h>

#ifdef __cplusplus
extern "C" {
#endif

// nng_thread is a handle to a "thread", which may be a real system
// thread, or a coroutine on some platforms.
typedef struct nng_thread nng_thread;

// Create and start a thread. Note that on some platforms, this might
// actually be a coroutine, with limitations about what system APIs
// you can call. Therefore, these threads should only be used with the
// I/O APIs provided by nng. The thread runs until completion.
NNG_DECL int nng_thread_create(nng_thread **, void (*)(void *), void *);

// Set the thread name. Support for this is platform specific and varies.
// It is intended to provide information for use when debugging applications,
// and not for programmatic use beyond that.
NNG_DECL void nng_thread_set_name(nng_thread *, const char *);

// Destroy a thread (waiting for it to complete.) When this function
// returns all resources for the thread are cleaned up.
NNG_DECL void nng_thread_destroy(nng_thread *);

// nng_mtx represents a mutex, which is a simple, non-reentrant, boolean lock.
typedef struct nng_mtx nng_mtx;

// nng_mtx_alloc allocates a mutex structure.
NNG_DECL int nng_mtx_alloc(nng_mtx **);

// nng_mtx_free frees the mutex. It must not be locked.
NNG_DECL void nng_mtx_free(nng_mtx *);

// nng_mtx_lock locks the mutex; if it is already locked it will block
// until it can be locked. If the caller already holds the lock, the
// results are undefined (a panic may occur).
NNG_DECL void nng_mtx_lock(nng_mtx *);

// nng_mtx_unlock unlocks a previously locked mutex. It is an error to
// call this on a mutex which is not owned by caller.
NNG_DECL void nng_mtx_unlock(nng_mtx *);

// nng_cv is a condition variable. It is always allocated with an
// associated mutex, which must be held when waiting for it, or
// when signaling it.
typedef struct nng_cv nng_cv;

NNG_DECL int nng_cv_alloc(nng_cv **, nng_mtx *);

// nng_cv_free frees the condition variable.
NNG_DECL void nng_cv_free(nng_cv *);

// nng_cv_wait waits until the condition variable is "signaled".
NNG_DECL void nng_cv_wait(nng_cv *);

// nng_cv_until waits until either the condition is signaled, or
// the timeout expires. It returns NNG_ETIMEDOUT in that case.
NNG_DECL int nng_cv_until(nng_cv *, nng_time);

// nng_cv_wake wakes all threads waiting on the condition.
NNG_DECL void nng_cv_wake(nng_cv *);

// nng_cv_wake1 wakes only one thread waiting on the condition. This may
// reduce the thundering herd problem, but care must be taken to ensure
// that no waiter starves forever.
NNG_DECL void nng_cv_wake1(nng_cv *);

#ifdef __cplusplus
}
#endif

#endif // NNG_SUPPLEMENTAL_UTIL_PLATFORM_H

0 comments on commit 0181798

Please sign in to comment.