@cryptotaxi247 / netdata-1 / commits / 00f79017f

functions: prevent a busy wait loop (#16086)

prevent a busy wait loop

Costa Tsaousis committed Oct 2, 2023 at 20:11 UTC 00f79017f9d2c45289eaa6939b418d5a9084e906
1 file changed +11 -8
libnetdata/functions_evloop/functions_evloop.c
+11 -8
@@ -42,34 +42,37 @@ struct functions_evloop_globals {
42 static void *rrd_functions_worker_globals_worker_main(void *arg) {
43 struct functions_evloop_globals *wg = arg;
44
45 + bool last_acquired = true;
46 while (true) {
47 pthread_mutex_lock(&wg->worker_mutex);
48
48 - while (dictionary_entries(wg->worker_queue) == 0) {
49 + if(dictionary_entries(wg->worker_queue) == 0 || !last_acquired)
50 pthread_cond_wait(&wg->worker_cond_var, &wg->worker_mutex);
50 - }
51
52 const DICTIONARY_ITEM *acquired = NULL;
53 struct functions_evloop_worker_job *j;
54 dfe_start_write(wg->worker_queue, j) {
55 - if(j->running || j->cancelled)
56 - continue;
55 + if(j->running || j->cancelled)
56 + continue;
57
58 - acquired = dictionary_acquired_item_dup(wg->worker_queue, j_dfe.item);
59 - j->running = true;
60 - break;
61 - }
58 + acquired = dictionary_acquired_item_dup(wg->worker_queue, j_dfe.item);
59 + j->running = true;
60 + break;
61 + }
62 dfe_done(j);
63
64 pthread_mutex_unlock(&wg->worker_mutex);
65
66 if(acquired) {
67 + last_acquired = true;
68 j = dictionary_acquired_item_value(acquired);
69 j->cb(j->transaction, j->cmd, j->timeout, &j->cancelled);
70 dictionary_del(wg->worker_queue, j->transaction);
71 dictionary_acquired_item_release(wg->worker_queue, acquired);
72 dictionary_garbage_collect(wg->worker_queue);
73 }
74 + else
75 + last_acquired = false;
76 }
77 return NULL;
78 }