summaryrefslogtreecommitdiff
path: root/src/periodic.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-03-20 18:51:03 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-03-20 18:51:03 +0200
commit5dfb890cf841579edaeb2633025db218e86f45c2 (patch)
treea6937d904579b8d7051cf033e03e99464d298c98 /src/periodic.c
parentaa70507d43d17ca2c426acd6bf91548fe267ac19 (diff)
App: Periodic commands without timers/threads
Now the periodic commands get posted on the main thread at intervals, and the event loop cooperates by not sleeping if there are periodic commands pending. The macOS animation hangup seems to be unrelated, though — perhaps some internal SDL/Metal machinery related to app refresh stopping for a while?
Diffstat (limited to 'src/periodic.c')
-rw-r--r--src/periodic.c44
1 files changed, 14 insertions, 30 deletions
diff --git a/src/periodic.c b/src/periodic.c
index c039097d..29c26de9 100644
--- a/src/periodic.c
+++ b/src/periodic.c
@@ -52,50 +52,35 @@ iDefineTypeConstructionArgs(PeriodicCommand, (iAny *ctx, const char *cmd), ctx,
52 52
53/*----------------------------------------------------------------------------------------------*/ 53/*----------------------------------------------------------------------------------------------*/
54 54
55//static uint32_t postCommands_Periodic_(uint32_t interval, void *param) { 55static const uint32_t postingInterval_Periodic_ = 500;
56static iThreadResult poster_Periodic_(iThread *thread) { 56
57 iPeriodic *d = userData_Thread(thread); 57iBool postCommands_Periodic(iPeriodic *d) {
58 const uint32_t now = SDL_GetTicks();
59 if (now - d->lastPostTime < postingInterval_Periodic_) {
60 return iFalse;
61 }
62 d->lastPostTime = now;
63 iBool wasPosted = iFalse;
58 lock_Mutex(d->mutex); 64 lock_Mutex(d->mutex);
59 while (!value_Atomic(&d->isStopping)) { 65 iConstForEach(Array, i, &d->commands.values) {
60 if (isEmpty_SortedArray(&d->commands)) { 66 postCommandString_App(&((const iPeriodicCommand *) i.value)->command);
61 /* Sleep until we have something to post. */ 67 wasPosted = iTrue;
62 wait_Condition(&d->haveCommands, d->mutex);
63 continue;
64 }
65 iConstForEach(Array, i, &d->commands.values) {
66 postCommandString_App(&((const iPeriodicCommand *) i.value)->command);
67 }
68 /* Sleep for a while. */
69 iTime until;
70 initTimeout_Time(&until, 0.5f);
71 waitTimeout_Condition(&d->haveCommands, d->mutex, &until);
72 } 68 }
73 unlock_Mutex(d->mutex); 69 unlock_Mutex(d->mutex);
74 return 0; 70 return wasPosted;
75} 71}
76 72
77void init_Periodic(iPeriodic *d) { 73void init_Periodic(iPeriodic *d) {
78 d->mutex = new_Mutex(); 74 d->mutex = new_Mutex();
79 init_SortedArray(&d->commands, sizeof(iPeriodicCommand), cmp_PeriodicCommand_); 75 init_SortedArray(&d->commands, sizeof(iPeriodicCommand), cmp_PeriodicCommand_);
80// d->timer = SDL_AddTimer(500, postCommands_Periodic_, d); 76 d->lastPostTime = 0;
81 set_Atomic(&d->isStopping, iFalse);
82 init_Condition(&d->haveCommands);
83 d->thread = new_Thread(poster_Periodic_);
84 setUserData_Thread(d->thread, d);
85 start_Thread(d->thread);
86} 77}
87 78
88void deinit_Periodic(iPeriodic *d) { 79void deinit_Periodic(iPeriodic *d) {
89// SDL_RemoveTimer(d->timer);
90 set_Atomic(&d->isStopping, iTrue);
91 signal_Condition(&d->haveCommands);
92 join_Thread(d->thread);
93 iRelease(d->thread);
94 iForEach(Array, i, &d->commands.values) { 80 iForEach(Array, i, &d->commands.values) {
95 deinit_PeriodicCommand(i.value); 81 deinit_PeriodicCommand(i.value);
96 } 82 }
97 deinit_SortedArray(&d->commands); 83 deinit_SortedArray(&d->commands);
98 deinit_Condition(&d->haveCommands);
99 delete_Mutex(d->mutex); 84 delete_Mutex(d->mutex);
100} 85}
101 86
@@ -112,7 +97,6 @@ void add_Periodic(iPeriodic *d, iAny *context, const char *command) {
112 init_PeriodicCommand(&pc, context, command); 97 init_PeriodicCommand(&pc, context, command);
113 insert_SortedArray(&d->commands, &pc); 98 insert_SortedArray(&d->commands, &pc);
114 } 99 }
115 signal_Condition(&d->haveCommands);
116 unlock_Mutex(d->mutex); 100 unlock_Mutex(d->mutex);
117} 101}
118 102