jcs
/subtext
/amendments
/422
main: Move periodic jobs and binkp polling to one thread here
jcs made amendment 422 about 1 year ago
--- main.c Fri Mar 10 21:38:45 2023
+++ main.c Tue Mar 14 15:23:02 2023
@@ -38,10 +38,12 @@ struct db *db = NULL;
bool blanker_on = false;
unsigned long blanker_last_blank = 0;
WindowPtr blanker_win = NULL;
+struct uthread *periodic_job_thread = NULL;
bool handle_menu(long menu_id);
void handle_exit(void);
void blanker_idle(void);
+void periodic_jobs(struct uthread *uthread, void *arg);
int
main(void)
@@ -54,8 +56,7 @@ main(void)
struct focusable *found_focusable;
unsigned long zone_size, stack_size, heap_size;
short event_in, n, finder_action, finder_count;
- char key, last_daily_job[9] = { 0 }, today[9];
- struct tm *date_tm;
+ char key;
uthread_init();
@@ -119,10 +120,12 @@ main(void)
if (db->config.modem_port)
serial_init();
if (db->config.binkp_hostname[0])
- uthread_add(binkp_process, NULL);
-
+ binkp_init();
+
blanker_last_blank = Time;
+ periodic_job_thread = uthread_add(periodic_jobs, NULL);
+
while (!quitting) {
if (db->config.telnet_port)
telnet_idle();
@@ -138,14 +141,9 @@ main(void)
#endif
blanker_idle();
- if (nsessions == 0) {
- date_tm = localtime((time_t *)&Time);
- strftime(today, sizeof(today), "%Y%m%d", date_tm);
- if (strcmp(today, last_daily_job) != 0) {
- session_purge_logs(db->config.session_log_days);
- strlcpy(last_daily_job, today, sizeof(last_daily_job));
- }
- }
+ if (nsessions == 0)
+ ;//daily_job_checks();
+
continue;
}
@@ -440,4 +438,46 @@ blanker_unblank(void)
*/
logger_printf("[blanker] Blanked screen for %ld second%s",
rt, rt == 1 ? "" : "s");
-}
+}
+
+void
+periodic_jobs(struct uthread *uthread, void *arg)
+{
+ struct tm *date_tm;
+ size_t n;
+ short last_daily_job = -1;
+
+ for (;;) {
+ date_tm = localtime((time_t *)&Time);
+
+ if (nsessions != 0)
+ goto sleep;
+
+ if (last_daily_job != date_tm->tm_mday) {
+ session_prune_logs(db->config.session_log_days);
+ uthread_yield();
+
+ for (n = 0; n < db->nboards; n++) {
+ board_prune_old_posts(&db->boards[n]);
+ uthread_yield();
+ }
+
+ last_daily_job = date_tm->tm_mday;
+ }
+
+ if (binkp_ready) {
+ if (Time > binkp_next_poll) {
+ binkp_poll();
+ uthread_yield();
+ }
+
+ if (binkp_packets_in_outbox())
+ binkp_poll();
+ }
+
+sleep:
+ uthread_msleep((unsigned long)1000 * 10);
+ }
+
+ periodic_job_thread = NULL;
+}
--- subtext.h Wed Nov 9 22:04:37 2022
+++ subtext.h Tue Mar 14 15:22:58 2023
@@ -17,6 +17,7 @@
#ifndef __SUBTEXT_H__
#define __SUBTEXT_H__
+#include "uthread.h"
#include "util.h"
#define PROGRAM_NAME "Subtext"
@@ -74,6 +75,7 @@
extern struct db *db;
extern MenuHandle file_menu;
+extern struct uthread *periodic_job_thread;
void blanker_unblank(void);