@@ -1493,7 +1493,7 @@ get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_el
14931493
14941494 /* we read notif, want a rpc-reply */
14951495 if (msgid && (msgtype == NC_MSG_NOTIF )) {
1496- if (!session -> opts .client .ntf_tid ) {
1496+ if (!ATOMIC_LOAD ( session -> opts .client .ntf_tid ) ) {
14971497 ERR ("Session %u: received a <notification> but session is not subscribed." , session -> id );
14981498 lyxml_free (session -> ctx , xml );
14991499 return NC_MSG_ERROR ;
@@ -2059,7 +2059,7 @@ nc_session_ntf_thread_running(const struct nc_session *session)
20592059 return 0 ;
20602060 }
20612061
2062- return session -> opts .client .ntf_tid ? 1 : 0 ;
2062+ return ATOMIC_LOAD ( session -> opts .client .ntf_tid ) ? 1 : 0 ;
20632063}
20642064
20652065API void
@@ -2214,9 +2214,9 @@ nc_recv_notif_thread(void *arg)
22142214 free (ntarg );
22152215
22162216 /* remember our allocated tid, we will be freeing it */
2217- ntf_tid = ( pthread_t * ) session -> opts .client .ntf_tid ;
2217+ ntf_tid = ATOMIC_LOAD ( session -> opts .client .ntf_tid ) ;
22182218
2219- while (session -> opts .client .ntf_tid ) {
2219+ while (ATOMIC_LOAD ( session -> opts .client .ntf_tid ) ) {
22202220 msgtype = nc_recv_notif (session , NC_CLIENT_NOTIF_THREAD_SLEEP / 1000 , & notif );
22212221 if (msgtype == NC_MSG_NOTIF ) {
22222222 notif_clb (session , notif );
@@ -2235,7 +2235,7 @@ nc_recv_notif_thread(void *arg)
22352235 }
22362236
22372237 VRB ("Session %u: notification thread exit." , session -> id );
2238- session -> opts .client .ntf_tid = NULL ;
2238+ ATOMIC_STORE ( session -> opts .client .ntf_tid , NULL ) ;
22392239 free (ntf_tid );
22402240 return NULL ;
22412241}
@@ -2244,6 +2244,7 @@ API int
22442244nc_recv_notif_dispatch (struct nc_session * session , void (* notif_clb )(struct nc_session * session , const struct nc_notif * notif ))
22452245{
22462246 struct nc_ntf_thread_arg * ntarg ;
2247+ pthread_t * tid ;
22472248 int ret ;
22482249
22492250 if (!session ) {
@@ -2255,7 +2256,7 @@ nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_s
22552256 } else if ((session -> status != NC_STATUS_RUNNING ) || (session -> side != NC_CLIENT )) {
22562257 ERR ("Session %u: invalid session to receive Notifications." , session -> id );
22572258 return -1 ;
2258- } else if (session -> opts .client .ntf_tid ) {
2259+ } else if (ATOMIC_LOAD ( session -> opts .client .ntf_tid ) ) {
22592260 ERR ("Session %u: separate notification thread is already running." , session -> id );
22602261 return -1 ;
22612262 }
@@ -2268,20 +2269,21 @@ nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_s
22682269 ntarg -> session = session ;
22692270 ntarg -> notif_clb = notif_clb ;
22702271
2271- /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
2272- session -> opts .client .ntf_tid = malloc (sizeof * session -> opts .client .ntf_tid );
2273- if (!session -> opts .client .ntf_tid ) {
2272+ tid = malloc (sizeof * tid );
2273+ if (!tid ) {
22742274 ERRMEM ;
22752275 free (ntarg );
22762276 return -1 ;
22772277 }
2278+ /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
2279+ ATOMIC_STORE (session -> opts .client .ntf_tid , tid );
22782280
2279- ret = pthread_create (( pthread_t * ) session -> opts . client . ntf_tid , NULL , nc_recv_notif_thread , ntarg );
2281+ ret = pthread_create (tid , NULL , nc_recv_notif_thread , ntarg );
22802282 if (ret ) {
22812283 ERR ("Session %u: failed to create a new thread (%s)." , strerror (errno ));
22822284 free (ntarg );
2283- free (( pthread_t * ) session -> opts . client . ntf_tid );
2284- session -> opts .client .ntf_tid = NULL ;
2285+ free (tid );
2286+ ATOMIC_STORE ( session -> opts .client .ntf_tid , NULL ) ;
22852287 return -1 ;
22862288 }
22872289
0 commit comments