Skip to content

Commit 07ad01a

Browse files
authored
session FEATURE keep refcount (#303)
* referencing of nc-sessions * do it atomic
1 parent 6efbb02 commit 07ad01a

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
# define ATOMIC_STORE(x, val) atomic_store(&(x), (val))
4242
# define ATOMIC_LOAD(x) atomic_load((atomic_uintptr_t *)&(x))
4343
# define ATOMIC_INC(x) atomic_fetch_add(&(x), 1)
44+
# define ATOMIC_DEC(x) atomic_fetch_sub(&(x), 1)
4445
#else
4546
# define ATOMIC_UINT32_T uint32_t
4647
# define ATOMIC_PTR void *
4748
# define ATOMIC_STORE(x, val) (x) = (val)
4849
# define ATOMIC_LOAD(x) (x)
4950
# define ATOMIC_INC(x) __sync_add_and_fetch(&(x), 1)
51+
# define ATOMIC_DEC(x) __sync_sub_and_fetch(&(x), 1)
5052
#endif
5153

5254
/*

src/session.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,39 @@ nc_session_get_id(const struct nc_session *session)
469469
return session->id;
470470
}
471471

472+
API uint32_t
473+
nc_session_get_refcnt(const struct nc_session *session)
474+
{
475+
if (!session) {
476+
ERRARG("session");
477+
return -1;
478+
}
479+
480+
return ATOMIC_LOAD(session->opts.server.ref_count);
481+
}
482+
483+
API void
484+
nc_session_reference(struct nc_session *session)
485+
{
486+
if (!session) {
487+
ERRARG("session");
488+
}
489+
490+
ATOMIC_INC(session->opts.server.ref_count);
491+
}
492+
493+
API void
494+
nc_session_dereference(struct nc_session *session)
495+
{
496+
if (!session) {
497+
ERRARG("session");
498+
}
499+
500+
if (ATOMIC_LOAD(session->opts.server.ref_count)) {
501+
ATOMIC_DEC(session->opts.server.ref_count);
502+
}
503+
}
504+
472505
API int
473506
nc_session_get_version(const struct nc_session *session)
474507
{

src/session.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ uint32_t nc_session_get_killed_by(const struct nc_session *session);
143143
*/
144144
uint32_t nc_session_get_id(const struct nc_session *session);
145145

146+
/**
147+
* @brief Get reference count.
148+
*
149+
* @param[in] session Session to get the information from.
150+
* @return Session ID.
151+
*/
152+
uint32_t nc_session_get_refcnt(const struct nc_session *session);
153+
/**
154+
* @brief Put a reference to the session in order to avoid
155+
* unwanted release of session.
156+
*
157+
* @param[in] session Session to get the information from.
158+
* @return Session ID.
159+
*/
160+
void nc_session_reference(struct nc_session *session);
161+
/**
162+
* @brief Take reference from the session
163+
*
164+
* @param[in] session Session to get the information from.
165+
* @return Session ID.
166+
*/
167+
void nc_session_dereference(struct nc_session *session);
168+
146169
/**
147170
* @brief Get session NETCONF version.
148171
*

src/session_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ struct nc_session {
456456

457457
pthread_mutex_t ch_lock; /**< Call Home thread lock */
458458
pthread_cond_t ch_cond; /**< Call Home thread condition */
459+
ATOMIC_UINT32_T ref_count; /**< Reference count: when referenced by SR's event session ncid */
459460

460461
/* server flags */
461462
#ifdef NC_ENABLED_SSH

0 commit comments

Comments
 (0)