diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/periodic.c | 48 | ||||
-rw-r--r-- | src/periodic.h | 5 |
2 files changed, 39 insertions, 14 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 | ||
29 | iDeclareType(PeriodicCommand) | 30 | iDeclareType(PeriodicCommand) |
@@ -51,30 +52,50 @@ iDefineTypeConstructionArgs(PeriodicCommand, (iAny *ctx, const char *cmd), ctx, | |||
51 | 52 | ||
52 | /*----------------------------------------------------------------------------------------------*/ | 53 | /*----------------------------------------------------------------------------------------------*/ |
53 | 54 | ||
54 | static uint32_t postCommands_Periodic_(uint32_t interval, void *param) { | 55 | //static uint32_t postCommands_Periodic_(uint32_t interval, void *param) { |
55 | iPeriodic *d = param; | 56 | static 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 | ||
64 | void init_Periodic(iPeriodic *d) { | 77 | void 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 | ||
70 | void deinit_Periodic(iPeriodic *d) { | 88 | void 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 | ||
diff --git a/src/periodic.h b/src/periodic.h index acb00aa2..c643a2fe 100644 --- a/src/periodic.h +++ b/src/periodic.h | |||
@@ -24,12 +24,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |||
24 | #include <the_Foundation/sortedarray.h> | 24 | #include <the_Foundation/sortedarray.h> |
25 | 25 | ||
26 | iDeclareType(Periodic) | 26 | iDeclareType(Periodic) |
27 | iDeclareType(Thread) | ||
27 | 28 | ||
28 | /* Animation utility. Not per frame but several times per second. */ | 29 | /* Animation utility. Not per frame but several times per second. */ |
29 | struct Impl_Periodic { | 30 | struct Impl_Periodic { |
30 | int timer; | ||
31 | iMutex * mutex; | 31 | iMutex * mutex; |
32 | iSortedArray commands; | 32 | iSortedArray commands; |
33 | iCondition haveCommands; | ||
34 | iThread * thread; | ||
35 | iAtomicInt isStopping; | ||
33 | }; | 36 | }; |
34 | 37 | ||
35 | void init_Periodic (iPeriodic *); | 38 | void init_Periodic (iPeriodic *); |