Skip to content

Commit a8ec54b

Browse files
committed
session client UPDATE support for more dispatched ntf threads
1 parent 443faa0 commit a8ec54b

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/session.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,14 +858,14 @@ nc_session_free(struct nc_session *session, void (*data_free)(void *))
858858
return;
859859
}
860860

861-
/* stop notifications thread if any */
862-
if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
863-
/* let the thread know it should quit */
864-
ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2);
861+
/* stop notification threads if any */
862+
if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
863+
/* let the threads know they should quit */
864+
ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
865865

866-
/* wait for it */
866+
/* wait for them */
867867
nc_gettimespec_mono_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
868-
while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
868+
while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
869869
usleep(NC_TIMEOUT_STEP);
870870
if (nc_difftimespec_mono_cur(&ts) < 1) {
871871
ERR(session, "Waiting for notification thread exit failed (timed out).");

src/session_client.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ nc_session_ntf_thread_running(const struct nc_session *session)
17881788
return 0;
17891789
}
17901790

1791-
return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) ? 1 : 0;
1791+
return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running);
17921792
}
17931793

17941794
API void
@@ -2314,7 +2314,7 @@ nc_recv_notif_thread(void *arg)
23142314
free_data = ntarg->free_data;
23152315
free(ntarg);
23162316

2317-
while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread) == 1) {
2317+
while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
23182318
msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op);
23192319
if (msgtype == NC_MSG_NOTIF) {
23202320
notif_clb(session, envp, op, user_data);
@@ -2334,7 +2334,7 @@ nc_recv_notif_thread(void *arg)
23342334
}
23352335

23362336
VRB(session, "Notification thread exit.");
2337-
ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
2337+
ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count);
23382338
if (free_data) {
23392339
free_data(user_data);
23402340
}
@@ -2365,9 +2365,6 @@ nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb no
23652365
} else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
23662366
ERR(session, "Invalid session to receive Notifications.");
23672367
return -1;
2368-
} else if (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
2369-
ERR(session, "Separate notification thread is already running.");
2370-
return -1;
23712368
}
23722369

23732370
ntarg = malloc(sizeof *ntarg);
@@ -2379,15 +2376,18 @@ nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb no
23792376
ntarg->notif_clb = notif_clb;
23802377
ntarg->user_data = user_data;
23812378
ntarg->free_data = free_data;
2379+
ATOMIC_INC_RELAXED(session->opts.client.ntf_thread_count);
23822380

23832381
/* just so that nc_recv_notif_thread() does not immediately exit */
2384-
ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 1);
2382+
ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 1);
23852383

23862384
ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg);
23872385
if (ret) {
23882386
ERR(session, "Failed to create a new thread (%s).", strerror(errno));
23892387
free(ntarg);
2390-
ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 0);
2388+
if (ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count) == 1) {
2389+
ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
2390+
}
23912391
return -1;
23922392
}
23932393

src/session_p.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ struct nc_session {
451451
char **cpblts; /**< list of server's capabilities on client side */
452452
pthread_mutex_t msgs_lock; /**< lock for the msgs buffer */
453453
struct nc_msg_cont *msgs; /**< queue for messages received of different type than expected */
454-
ATOMIC_T ntf_thread; /**< flag whether notification thread for this session is running or not,
455-
2 means it should quit */
454+
ATOMIC_T ntf_thread_count; /**< number of running notification threads */
455+
ATOMIC_T ntf_thread_running; /**< flag whether there are notification threads for this session running or not */
456456

457457
/* client flags */
458458
/* some server modules failed to load so the data from them will be ignored - not use strict flag for parsing */

0 commit comments

Comments
 (0)