1 #include "engine/declarations.h"
2
3 #include "declarations_app.h"
4 #include "worker_roller.h"
5
6
7
worker_roller(void * ta)8 void worker_roller(void *ta) {
9 struct timeval tv;
10 struct t_ctx *ti;
11
12 ti = (struct t_ctx *) ta;
13
14 /* This thread does not like to receive pthread_cancel() */
15 ti->ar = THREAD_TERM_SELF;
16
17 pthread_cleanup_push(worker_roller_cancel, ti);
18
19 if(worker_roller_init(ti) == 0) {
20 while(ti->pr == THREAD_STATE_RUN) {
21 (void) roller_loop_begin((struct t_str *) ti->tp, &tv);
22
23 (void) universe_run();
24
25 (void) roller_loop_end(&tv);
26 }
27 }
28
29 /* Free allocated resources and terminate thread */
30 pthread_cleanup_pop(1);
31
32 (void) worker_roller_finalize(ti);
33 }
34
worker_roller_cancel(void * ta)35 static void worker_roller_cancel(void *ta) {
36 struct t_ctx *ti;
37
38 ti = (struct t_ctx *) ta;
39
40 if(ti->tp != NULL) {
41 (void) worker_roller_cancel_local(ti, (struct t_str *) ti->tp);
42
43 (void) free(ti->tp);
44
45 ti->tp = NULL;
46 }
47
48 ti->tr = THREAD_STATE_STOP;
49 }
50
worker_roller_cancel_local(__UNUSED__ struct t_ctx * ti,__UNUSED__ struct t_str * tp)51 static void worker_roller_cancel_local(__UNUSED__ struct t_ctx *ti, __UNUSED__ struct t_str *tp) {
52 (void) universe_free();
53 }
54
worker_roller_finalize(struct t_ctx * ti)55 static void worker_roller_finalize(struct t_ctx *ti) {
56 (void) thread_exit(ti, NULL);
57 }
58
worker_roller_init(struct t_ctx * ti)59 static int worker_roller_init(struct t_ctx *ti) {
60 int r;
61
62 struct t_str *c;
63
64 APP_MALLOC_RET_VALUE(c, sizeof(struct t_str), -1);
65
66 ti->tp = c;
67
68 (void) thread_title(ti, APPLICATION_EXEC "_roller");
69
70 r = worker_roller_init_local(ti, (struct t_str *) ti->tp);
71
72 ti->tr = THREAD_STATE_IDLE;
73
74 return(r);
75 }
76
worker_roller_init_local(__UNUSED__ struct t_ctx * ti,__UNUSED__ struct t_str * tp)77 static int worker_roller_init_local(__UNUSED__ struct t_ctx *ti, __UNUSED__ struct t_str *tp) {
78 return(universe_init());
79 }
80
roller_loop_begin(__UNUSED__ struct t_str * tp,struct timeval * t)81 static void roller_loop_begin(__UNUSED__ struct t_str *tp, struct timeval *t) {
82 (void) timer_get_time(t);
83 }
84
roller_loop_end(struct timeval * t)85 static void roller_loop_end(struct timeval *t) {
86 long n;
87
88 struct timeval v;
89
90 (void) timer_get_time(&v);
91
92 n = (long) (v.tv_usec - t->tv_usec);
93
94 if(n < 0) n = 1000000 + n;
95
96 n = ((1000000 / ROLLER_UPDATE_RPS) - n) * 1000;
97
98 if(n > 0) (void) timer_wait(0, n);
99 }
100