summaryrefslogtreecommitdiff
path: root/src/periodic.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-03-20 15:48:22 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-03-20 15:48:22 +0200
commit3711a2fb7e84abf9ee9734d81e1f49f9a1f8877d (patch)
tree2038ed9a8e5cde267ad0b9b51bf69e25ee056bb5 /src/periodic.c
parente009da58e2184ed209d118acd8343ae47276eae1 (diff)
Periodic: Use a thread instead of timers
This still doesn't solve the strange refresh time issue. Perhaps it's better to find a solution that doesn't rely on background threads.
Diffstat (limited to 'src/periodic.c')
-rw-r--r--src/periodic.c48
1 files changed, 35 insertions, 13 deletions
diff --git a/src/periodic.c b/src/periodic.c
index c840e63c..c039097d 100644
--- a/src/periodic.c
+++ b/src/periodic.c
@@ -24,6 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
24#include "app.h" 24#include "app.h"
25 25
26#include <the_Foundation/string.h> 26#include <the_Foundation/string.h>
27#include <the_Foundation/thread.h>
27#include <SDL_timer.h> 28#include <SDL_timer.h>
28 29
29iDeclareType(PeriodicCommand) 30iDeclareType(PeriodicCommand)
@@ -51,30 +52,50 @@ iDefineTypeConstructionArgs(PeriodicCommand, (iAny *ctx, const char *cmd), ctx,
51 52
52/*----------------------------------------------------------------------------------------------*/ 53/*----------------------------------------------------------------------------------------------*/
53 54
54static uint32_t postCommands_Periodic_(uint32_t interval, void *param) { 55//static uint32_t postCommands_Periodic_(uint32_t interval, void *param) {
55 iPeriodic *d = param; 56static iThreadResult poster_Periodic_(iThread *thread) {
57 iPeriodic *d = userData_Thread(thread);
56 lock_Mutex(d->mutex); 58 lock_Mutex(d->mutex);
57 iConstForEach(Array, i, &d->commands.values) { 59 while (!value_Atomic(&d->isStopping)) {
58 postCommandString_App(&((const iPeriodicCommand *) i.value)->command); 60 if (isEmpty_SortedArray(&d->commands)) {
61 /* Sleep until we have something to post. */
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);
59 } 72 }
60 unlock_Mutex(d->mutex); 73 unlock_Mutex(d->mutex);
61 return interval; 74 return 0;
62} 75}
63 76
64void init_Periodic(iPeriodic *d) { 77void init_Periodic(iPeriodic *d) {
65 d->mutex = new_Mutex(); 78 d->mutex = new_Mutex();
66 init_SortedArray(&d->commands, sizeof(iPeriodicCommand), cmp_PeriodicCommand_); 79 init_SortedArray(&d->commands, sizeof(iPeriodicCommand), cmp_PeriodicCommand_);
67 d->timer = SDL_AddTimer(500, postCommands_Periodic_, d); 80// d->timer = SDL_AddTimer(500, postCommands_Periodic_, d);
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);
68} 86}
69 87
70void deinit_Periodic(iPeriodic *d) { 88void deinit_Periodic(iPeriodic *d) {
71 SDL_RemoveTimer(d->timer); 89// SDL_RemoveTimer(d->timer);
72 iGuardMutex(d->mutex, { 90 set_Atomic(&d->isStopping, iTrue);
73 iForEach(Array, i, &d->commands.values) { 91 signal_Condition(&d->haveCommands);
74 deinit_PeriodicCommand(i.value); 92 join_Thread(d->thread);
75 } 93 iRelease(d->thread);
76 deinit_SortedArray(&d->commands); 94 iForEach(Array, i, &d->commands.values) {
77 }); 95 deinit_PeriodicCommand(i.value);
96 }
97 deinit_SortedArray(&d->commands);
98 deinit_Condition(&d->haveCommands);
78 delete_Mutex(d->mutex); 99 delete_Mutex(d->mutex);
79} 100}
80 101
@@ -91,6 +112,7 @@ void add_Periodic(iPeriodic *d, iAny *context, const char *command) {
91 init_PeriodicCommand(&pc, context, command); 112 init_PeriodicCommand(&pc, context, command);
92 insert_SortedArray(&d->commands, &pc); 113 insert_SortedArray(&d->commands, &pc);
93 } 114 }
115 signal_Condition(&d->haveCommands);
94 unlock_Mutex(d->mutex); 116 unlock_Mutex(d->mutex);
95} 117}
96 118