session BUGFIX use atomics instead of volatile
For notification thread TID.
Refs #253
diff --git a/src/session_client.c b/src/session_client.c
index d349c75..8784ba5 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -1493,7 +1493,7 @@
/* we read notif, want a rpc-reply */
if (msgid && (msgtype == NC_MSG_NOTIF)) {
- if (!session->opts.client.ntf_tid) {
+ if (!ATOMIC_LOAD(session->opts.client.ntf_tid)) {
ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
lyxml_free(session->ctx, xml);
return NC_MSG_ERROR;
@@ -2059,7 +2059,7 @@
return 0;
}
- return session->opts.client.ntf_tid ? 1 : 0;
+ return ATOMIC_LOAD(session->opts.client.ntf_tid) ? 1 : 0;
}
API void
@@ -2214,9 +2214,9 @@
free(ntarg);
/* remember our allocated tid, we will be freeing it */
- ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
+ ntf_tid = ATOMIC_LOAD(session->opts.client.ntf_tid);
- while (session->opts.client.ntf_tid) {
+ while (ATOMIC_LOAD(session->opts.client.ntf_tid)) {
msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, ¬if);
if (msgtype == NC_MSG_NOTIF) {
notif_clb(session, notif);
@@ -2235,7 +2235,7 @@
}
VRB("Session %u: notification thread exit.", session->id);
- session->opts.client.ntf_tid = NULL;
+ ATOMIC_STORE(session->opts.client.ntf_tid, NULL);
free(ntf_tid);
return NULL;
}
@@ -2244,6 +2244,7 @@
nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
{
struct nc_ntf_thread_arg *ntarg;
+ pthread_t *tid;
int ret;
if (!session) {
@@ -2255,7 +2256,7 @@
} else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
ERR("Session %u: invalid session to receive Notifications.", session->id);
return -1;
- } else if (session->opts.client.ntf_tid) {
+ } else if (ATOMIC_LOAD(session->opts.client.ntf_tid)) {
ERR("Session %u: separate notification thread is already running.", session->id);
return -1;
}
@@ -2268,20 +2269,21 @@
ntarg->session = session;
ntarg->notif_clb = notif_clb;
- /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
- session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
- if (!session->opts.client.ntf_tid) {
+ tid = malloc(sizeof *tid);
+ if (!tid) {
ERRMEM;
free(ntarg);
return -1;
}
+ /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
+ ATOMIC_STORE(session->opts.client.ntf_tid, tid);
- ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
+ ret = pthread_create(tid, NULL, nc_recv_notif_thread, ntarg);
if (ret) {
ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
free(ntarg);
- free((pthread_t *)session->opts.client.ntf_tid);
- session->opts.client.ntf_tid = NULL;
+ free(tid);
+ ATOMIC_STORE(session->opts.client.ntf_tid, NULL);
return -1;
}