Skip to content

Commit 46647a1

Browse files
Roytakmichalvasko
authored andcommitted
session server UPDATE make thread running flag atomic
1 parent 26ddf10 commit 46647a1

2 files changed

Lines changed: 12 additions & 47 deletions

File tree

src/session_p.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ struct nc_server_ch_thread_arg {
693693
void *new_session_fail_cb_data; /**< new session fail cb data */
694694

695695
pthread_t tid; /**< Thread ID of the Call Home client thread. */
696-
int thread_running; /**< A boolean value that is truthy while the underlying Call Home thread is running */
696+
ATOMIC_T thread_running; /**< Non-zero while the Call Home thread is running. Atomic for lock-free checks,
697+
but also used with the condition variable under cond_lock for signalling. */
697698
pthread_mutex_t cond_lock; /**< Condition's lock used for signalling the thread to terminate */
698699
pthread_cond_t cond; /**< Condition used for signalling the thread to terminate */
699700
};

src/session_server.c

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,33 +3301,6 @@ nc_server_ch_client_get_idle_timeout(const char *client_name, uint32_t *idle_tim
33013301
return ret;
33023302
}
33033303

3304-
/**
3305-
* @brief Checks if a Call Home thread should terminate.
3306-
*
3307-
* Checks the shared boolean variable thread_running. This should be done everytime
3308-
* before entering a critical section.
3309-
*
3310-
* @param[in] data Call Home thread's data.
3311-
* @return 1 if running, 0 if the thread should terminate, -1 on error.
3312-
*/
3313-
static int
3314-
nc_server_ch_client_thread_is_running(struct nc_server_ch_thread_arg *data)
3315-
{
3316-
int rc = -1;
3317-
3318-
/* COND LOCK */
3319-
if (nc_mutex_lock(&data->cond_lock, NC_CH_COND_LOCK_TIMEOUT, __func__) != 1) {
3320-
return -1;
3321-
}
3322-
3323-
rc = data->thread_running;
3324-
3325-
/* COND UNLOCK */
3326-
nc_mutex_unlock(&data->cond_lock, __func__);
3327-
3328-
return rc;
3329-
}
3330-
33313304
/**
33323305
* @brief Wait for any event after a NC session was established on a CH client.
33333306
*
@@ -3411,11 +3384,7 @@ nc_server_ch_client_thread_session_cond_wait(struct nc_server_ch_thread_arg *dat
34113384
}
34123385

34133386
/* check if the thread should terminate */
3414-
r = nc_server_ch_client_thread_is_running(data);
3415-
if (r != 1) {
3416-
if (r == -1) {
3417-
rc = -1;
3418-
}
3387+
if (!ATOMIC_LOAD_RELAXED(data->thread_running)) {
34193388
terminate = 1;
34203389
}
34213390

@@ -3467,23 +3436,22 @@ static int
34673436
nc_server_ch_client_thread_is_running_wait(struct nc_session *session, struct nc_server_ch_thread_arg *data, uint64_t cond_wait_time)
34683437
{
34693438
struct timespec ts;
3470-
int ret = 0, thread_running;
3439+
int ret = 0;
34713440

34723441
/* COND LOCK */
34733442
if (nc_mutex_lock(&data->cond_lock, NC_CH_COND_LOCK_TIMEOUT, __func__) != 1) {
34743443
return 0;
34753444
}
34763445
/* get reconnect timeout in ms */
34773446
nc_timeouttime_get(&ts, cond_wait_time * 1000);
3478-
while (!ret && data->thread_running) {
3447+
while (!ret && ATOMIC_LOAD_RELAXED(data->thread_running)) {
34793448
ret = pthread_cond_clockwait(&data->cond, &data->cond_lock, COMPAT_CLOCK_ID, &ts);
34803449
}
34813450

3482-
thread_running = data->thread_running;
34833451
/* COND UNLOCK */
34843452
nc_mutex_unlock(&data->cond_lock, __func__);
34853453

3486-
if (!thread_running) {
3454+
if (!ATOMIC_LOAD_RELAXED(data->thread_running)) {
34873455
/* thread is terminating */
34883456
VRB(session, "Call Home thread signaled to exit, client \"%s\" probably removed.", data->client_name);
34893457
ret = 0;
@@ -3513,7 +3481,7 @@ nc_server_ch_client_with_endpt_get(struct nc_server_ch_thread_arg *data, const c
35133481
{
35143482
struct nc_ch_client *client;
35153483

3516-
while (nc_server_ch_client_thread_is_running(data)) {
3484+
while (ATOMIC_LOAD_RELAXED(data->thread_running)) {
35173485
/* get the client */
35183486
client = nc_server_ch_client_get(name);
35193487
if (!client) {
@@ -3561,11 +3529,7 @@ nc_ch_client_thread(void *arg)
35613529
uint32_t reconnect_in;
35623530

35633531
/* mark the thread as running */
3564-
if (nc_mutex_lock(&data->cond_lock, NC_CH_COND_LOCK_TIMEOUT, __func__) != 1) {
3565-
goto cleanup;
3566-
}
3567-
data->thread_running = 1;
3568-
nc_mutex_unlock(&data->cond_lock, __func__);
3532+
ATOMIC_STORE_RELAXED(data->thread_running, 1);
35693533

35703534
/* CONFIG READ LOCK */
35713535
if (nc_rwlock_lock(&server_opts.config_lock, NC_RWLOCK_READ, NC_CONFIG_LOCK_TIMEOUT, __func__) != 1) {
@@ -3583,7 +3547,7 @@ nc_ch_client_thread(void *arg)
35833547
cur_endpt = &client->ch_endpts[0];
35843548
cur_endpt_name = strdup(cur_endpt->name);
35853549

3586-
while (nc_server_ch_client_thread_is_running(data)) {
3550+
while (ATOMIC_LOAD_RELAXED(data->thread_running)) {
35873551
if (!cur_attempts) {
35883552
VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" connecting...", data->client_name, cur_endpt_name);
35893553
}
@@ -3594,7 +3558,7 @@ nc_ch_client_thread(void *arg)
35943558
/* CONFIG READ UNLOCK - session established */
35953559
nc_rwlock_unlock(&server_opts.config_lock, __func__);
35963560

3597-
if (!nc_server_ch_client_thread_is_running(data)) {
3561+
if (!ATOMIC_LOAD_RELAXED(data->thread_running)) {
35983562
/* thread should stop running */
35993563
goto cleanup;
36003564
}
@@ -3607,7 +3571,7 @@ nc_ch_client_thread(void *arg)
36073571
session = NULL;
36083572

36093573
VRB(NULL, "Call Home client \"%s\" session terminated.", data->client_name);
3610-
if (!nc_server_ch_client_thread_is_running(data)) {
3574+
if (!ATOMIC_LOAD_RELAXED(data->thread_running)) {
36113575
/* thread should stop running */
36123576
goto cleanup;
36133577
}
@@ -3792,7 +3756,7 @@ nc_session_server_ch_client_dispatch_stop(struct nc_ch_client *ch_client)
37923756
}
37933757

37943758
/* notify the thread to stop */
3795-
thread_arg->thread_running = 0;
3759+
ATOMIC_STORE_RELAXED(thread_arg->thread_running, 0);
37963760
tid = thread_arg->tid;
37973761
pthread_cond_signal(&thread_arg->cond);
37983762

0 commit comments

Comments
 (0)