commit 93e7138

default  ·  2024-01-08 07:10:57 +0000 UTC
parent 22cb139
Rewritten part of the job threads to be leaner and faster.
3 files changed,  +46, -41
M data.c
M snac.h
M data.c
+1, -1
1@@ -2168,7 +2168,7 @@ void enqueue_output_raw(const char *keyid, const char *seckey,
2     qmsg = xs_dict_append(qmsg, "seckey", seckey);
3 
4     /* if it's to be sent right now, bypass the disk queue and post the job */
5-    if (retries == 0 && job_fifo_ready())
6+    if (retries == 0)
7         job_post(qmsg, 0);
8     else {
9         qmsg = _enqueue_put(fn, qmsg);
+45, -39
  1@@ -31,16 +31,22 @@
  2 srv_stat s_stat = {0};
  3 srv_stat *p_stat = NULL;
  4 
  5+
  6 /** job control **/
  7 
  8 /* mutex to access the lists of jobs */
  9 static pthread_mutex_t job_mutex;
 10 
 11-/* semaphre to trigger job processing */
 12+/* semaphore to trigger job processing */
 13 static sem_t *job_sem;
 14 
 15-/* fifo of jobs */
 16-xs_list *job_fifo = NULL;
 17+typedef struct job_fifo_item {
 18+    struct job_fifo_item *next;
 19+    xs_val *job;
 20+} job_fifo_item;
 21+
 22+static job_fifo_item *job_fifo_first = NULL;
 23+static job_fifo_item *job_fifo_last  = NULL;
 24 
 25 
 26 /* nodeinfo 2.0 template */
 27@@ -418,24 +424,6 @@ void httpd_connection(FILE *f)
 28 }
 29 
 30 
 31-static jmp_buf on_break;
 32-
 33-
 34-void term_handler(int s)
 35-{
 36-    (void)s;
 37-
 38-    longjmp(on_break, 1);
 39-}
 40-
 41-
 42-int job_fifo_ready(void)
 43-/* returns true if the job fifo is ready */
 44-{
 45-    return job_fifo != NULL;
 46-}
 47-
 48-
 49 void job_post(const xs_val *job, int urgent)
 50 /* posts a job for the threads to process it */
 51 {
 52@@ -443,19 +431,25 @@ void job_post(const xs_val *job, int urgent)
 53         /* lock the mutex */
 54         pthread_mutex_lock(&job_mutex);
 55 
 56-        /* add to the fifo */
 57-        if (job_fifo != NULL) {
 58-            if (urgent)
 59-                job_fifo = xs_list_insert(job_fifo, 0, job);
 60-            else
 61-                job_fifo = xs_list_append(job_fifo, job);
 62-
 63-            p_stat->job_fifo_size++;
 64+        job_fifo_item *i = xs_realloc(NULL, sizeof(job_fifo_item));
 65+        *i = (job_fifo_item){ NULL, xs_dup(job) };
 66 
 67-            srv_debug(2, xs_fmt(
 68-                "job_fifo sizes: %d %08x", p_stat->job_fifo_size, xs_size(job_fifo)));
 69+        if (job_fifo_first == NULL)
 70+            job_fifo_first = job_fifo_last = i;
 71+        else
 72+        if (urgent) {
 73+            /* prepend */
 74+            i->next = job_fifo_first;
 75+            job_fifo_first = i;
 76+        }
 77+        else {
 78+            /* append */
 79+            job_fifo_last->next = i;
 80+            job_fifo_last = i;
 81         }
 82 
 83+        p_stat->job_fifo_size++;
 84+
 85         /* unlock the mutex */
 86         pthread_mutex_unlock(&job_mutex);
 87 
 88@@ -475,8 +469,16 @@ void job_wait(xs_val **job)
 89         pthread_mutex_lock(&job_mutex);
 90 
 91         /* dequeue */
 92-        if (job_fifo != NULL) {
 93-            job_fifo = xs_list_shift(job_fifo, job);
 94+        job_fifo_item *i = job_fifo_first;
 95+
 96+        if (i != NULL) {
 97+            job_fifo_first = i->next;
 98+
 99+            if (job_fifo_first == NULL)
100+                job_fifo_last = NULL;
101+
102+            *job = i->job;
103+            xs_free(i);
104 
105             p_stat->job_fifo_size--;
106         }
107@@ -604,6 +606,16 @@ static void *background_thread(void *arg)
108 }
109 
110 
111+static jmp_buf on_break;
112+
113+void term_handler(int s)
114+{
115+    (void)s;
116+
117+    longjmp(on_break, 1);
118+}
119+
120+
121 void httpd(void)
122 /* starts the server */
123 {
124@@ -663,8 +675,6 @@ void httpd(void)
125         return;
126     }
127 
128-    job_fifo = xs_list_new();
129-
130     /* initialize sleep control */
131     pthread_mutex_init(&sleep_mutex, NULL);
132     pthread_cond_init(&sleep_cond, NULL);
133@@ -717,10 +727,6 @@ void httpd(void)
134     for (n = 0; n < p_stat->n_threads; n++)
135         pthread_join(threads[n], NULL);
136 
137-    pthread_mutex_lock(&job_mutex);
138-    job_fifo = xs_free(job_fifo);
139-    pthread_mutex_unlock(&job_mutex);
140-
141     sem_close(job_sem);
142     sem_unlink(sem_name);
143 
M snac.h
+0, -1
1@@ -294,7 +294,6 @@ int deluser(snac *user);
2 
3 extern const char *snac_blurb;
4 
5-int job_fifo_ready(void);
6 void job_post(const xs_val *job, int urgent);
7 void job_wait(xs_val **job);
8