@@ -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
17941794API 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
0 commit comments