-
-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #1408 Reference count as a first class type
This starts by using this for the nni_pipe, but we will use it for the other primary objects as well. This should simplify the tear down and hopefully eliminate some races. It does mean that pipe destruction goes through an additional context switch, for now at least. This shouldn't be on the hot data path anyway.
- Loading branch information
Showing
6 changed files
with
89 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,8 @@ nng_sources( | |
protocol.h | ||
reap.c | ||
reap.h | ||
refcnt.c | ||
refcnt.h | ||
sockaddr.c | ||
socket.c | ||
socket.h | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Staysail Systems, Inc. <[email protected]> | ||
// | ||
// 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. | ||
// | ||
|
||
#include <core/refcnt.h> | ||
|
||
void | ||
nni_refcnt_init( | ||
nni_refcnt *rc, unsigned value, void *data, void (*fini)(void *)) | ||
{ | ||
nni_atomic_init(&rc->rc_cnt); | ||
nni_atomic_set(&rc->rc_cnt, value); | ||
rc->rc_data = data; | ||
rc->rc_fini = fini; | ||
} | ||
|
||
void | ||
nni_refcnt_hold(nni_refcnt *rc) | ||
{ | ||
nni_atomic_inc(&rc->rc_cnt); | ||
} | ||
|
||
void | ||
nni_refcnt_rele(nni_refcnt *rc) | ||
{ | ||
if (nni_atomic_dec_nv(&rc->rc_cnt) == 0) { | ||
rc->rc_fini(rc->rc_data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 Staysail Systems, Inc. <[email protected]> | ||
// | ||
// 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. | ||
// | ||
|
||
#ifndef CORE_REFCNT_H | ||
#define CORE_REFCNT_H | ||
|
||
#include <nng/nng.h> | ||
|
||
#include <core/nng_impl.h> | ||
#include <core/platform.h> | ||
|
||
typedef struct { | ||
nni_atomic_int rc_cnt; | ||
void (*rc_fini)(void *); | ||
void *rc_data; | ||
} nni_refcnt; | ||
|
||
extern void nni_refcnt_init( | ||
nni_refcnt *rc, unsigned value, void *v, void (*fini)(void *)); | ||
extern void nni_refcnt_hold(nni_refcnt *rc); | ||
extern void nni_refcnt_rele(nni_refcnt *rc); | ||
|
||
#endif // CORE_REFCNT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters