aboutsummaryrefslogtreecommitdiff
path: root/src/core/reap.h
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-12-19 10:21:54 -0800
committerGarrett D'Amore <garrett@damore.org>2020-12-19 12:50:05 -0800
commitd12e169c1e733b255d146847ed57037b74681285 (patch)
treee4a59142a6cf097dfdda8620635f173f53db9e7a /src/core/reap.h
parent2033988343bce413763d3e9664e3e8372da48591 (diff)
downloadnng-d12e169c1e733b255d146847ed57037b74681285.tar.gz
nng-d12e169c1e733b255d146847ed57037b74681285.tar.bz2
nng-d12e169c1e733b255d146847ed57037b74681285.zip
fixes #1372 nni_reap could be smaller
Diffstat (limited to 'src/core/reap.h')
-rw-r--r--src/core/reap.h42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/core/reap.h b/src/core/reap.h
index 40c27a5d..5f631885 100644
--- a/src/core/reap.h
+++ b/src/core/reap.h
@@ -1,5 +1,5 @@
//
-// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2020 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
@@ -14,19 +14,35 @@
#include "core/defs.h"
#include "core/list.h"
-// nni_reap_item is defined here so that it can be inlined into
-// structures. Callers must access its members directly.
-typedef struct nni_reap_item {
- nni_list_node r_link;
- void * r_ptr;
- nni_cb r_func;
-} nni_reap_item;
+// nni_reap_node is to be inserted inline into structures
+// for subsystems that wish to support deferred reaping.
+// It should be zeroed at object initialization, but apart
+// from that it must not be touched directly except by the
+// reap subsystem.
+typedef struct nni_reap_node nni_reap_node;
+struct nni_reap_node {
+ nni_reap_node *rn_next;
+};
+
+// nni_reap_list is for subsystems to define their own reap lists.
+// This allows for the reap linkage to be restricted to a single
+// pointer. Subsystems should initialize rl_offset and rl_func,
+// and leave the rest zeroed. The intention is that this is a global
+// static member for each subsystem.
+typedef struct nni_reap_list nni_reap_list;
+struct nni_reap_list {
+ nni_reap_list *rl_next; // linkage in global reap list
+ nni_reap_node *rl_nodes; // list of nodes to reap
+ size_t rl_offset; // offset of reap_node within member.
+ nni_cb rl_func; // function called to reap the item
+ bool rl_inited; // initialized means it is linked in the list
+};
// nni_reap performs an asynchronous reap of an item. This allows functions
// it calls to acquire locks or resources without worrying about deadlocks
// (such as from a completion callback.) The called function should avoid
// blocking for too long if possible, since only one reap thread is present
-// in the system. The intended usage is for an nni_reap_item to be a member
+// in the system. The intended usage is for an nni_reap_node to be a member
// of the structure to be reaped, and and then this function is called to
// finalize it.
//
@@ -35,10 +51,14 @@ typedef struct nni_reap_item {
// is busy. These will be queued at the end of the reap list. This will
// allow a dependency to defer reaping until its dependents have first been
// reaped. HOWEVER, it is important that the item in question actually be
-// part of a fully reapable graph; otherwise this can lead to an infinite
+// part of a fully reap-able graph; otherwise this can lead to an infinite
// loop in the reap thread.
-extern void nni_reap(nni_reap_item *, nni_cb, void *);
+
+extern void nni_reap(nni_reap_list *, void *);
+
+// nni_reap_drain waits for the reap queue to be drained.
extern void nni_reap_drain(void);
+
extern int nni_reap_sys_init(void);
extern void nni_reap_sys_fini(void);