Skip to content

Commit 950a8d1

Browse files
committed
passing current time as parameter instead of global var.
1 parent d7cb47c commit 950a8d1

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

include/proto_activities.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ typedef uint16_t pa_pc_t;
3131
typedef int8_t pa_rc_t;
3232
typedef uint32_t pa_time_t;
3333

34-
/* Externals */
35-
36-
/* When unsing time features, use the following macro to define the current time variable */
37-
#define pa_define_current_time_var __thread pa_time_t pa_current_time_ms;
38-
extern __thread pa_time_t pa_current_time_ms;
39-
4034
/* Constants */
4135

4236
#define PA_RC_WAIT ((pa_rc_t)-1)
@@ -48,8 +42,8 @@ extern __thread pa_time_t pa_current_time_ms;
4842
#define _pa_frame_type(nm) struct _pa_frame_name(nm)
4943
#define _pa_inst_name(nm) nm##_inst
5044
#define _pa_inst_ptr(nm) &(pa_this->_pa_inst_name(nm))
51-
#define _pa_call(nm, ...) nm(_pa_inst_ptr(nm), ##__VA_ARGS__)
52-
#define _pa_call_as(nm, alias, ...) nm(_pa_inst_ptr(alias), ##__VA_ARGS__)
45+
#define _pa_call(nm, ...) nm(_pa_inst_ptr(nm), pa_current_time_ms, ##__VA_ARGS__)
46+
#define _pa_call_as(nm, alias, ...) nm(_pa_inst_ptr(alias), pa_current_time_ms, ##__VA_ARGS__)
5347
#ifndef _PA_ENABLE_CPP
5448
#define _pa_reset(inst) memset(inst, 0, sizeof(*inst));
5549
#define _pa_abort(inst) _pa_reset(inst); *inst._pa_pc = 0xffff;
@@ -118,7 +112,7 @@ namespace proto_activities {
118112
#define pa_activity_ctx_tm(nm, vars...) pa_activity_ctx(nm, pa_ctx_tm(vars))
119113

120114
#define pa_activity_def(nm, ...) \
121-
pa_rc_t nm(_pa_frame_type(nm)* pa_this, ##__VA_ARGS__) { \
115+
pa_rc_t nm(_pa_frame_type(nm)* pa_this, pa_time_t pa_current_time_ms, ##__VA_ARGS__) { \
122116
_pa_enter_invoke(_pa_frame_name(nm)); \
123117
switch (pa_this->_pa_pc) { \
124118
case 0: \
@@ -130,7 +124,7 @@ namespace proto_activities {
130124
}
131125

132126
#define pa_activity_sig(nm, ...) \
133-
_pa_extern pa_rc_t nm(_pa_frame_type(nm)* pa_this, ##__VA_ARGS__);
127+
_pa_extern pa_rc_t nm(_pa_frame_type(nm)* pa_this, pa_time_t pa_current_time_ms, ##__VA_ARGS__);
134128

135129
#define pa_activity_decl(nm, ctx, ...) \
136130
pa_activity_ctx(nm, ctx); \
@@ -576,10 +570,12 @@ using pa_val_sig = proto_activities::ValSignal<T>;
576570
/* Trigger */
577571

578572
#define pa_init(nm) _pa_reset(&_pa_inst_name(nm));
579-
#define pa_tick(nm, ...) nm(&_pa_inst_name(nm), ##__VA_ARGS__)
580-
#define pa_tick_tm(tm, nm, ...) (pa_current_time_ms = tm, nm(&_pa_inst_name(nm), ##__VA_ARGS__))
581-
#define pa_tick_local _pa_call
582-
#define pa_tick_local_as _pa_call_as
573+
#define pa_tick_tm(tm, nm, ...) nm(&_pa_inst_name(nm), tm, ##__VA_ARGS__)
574+
#ifndef ARDUINO
575+
#define pa_tick(nm, ...) pa_tick_tm(0, nm, ##__VA_ARGS__)
576+
#else
577+
#define pa_tick(nm, ...) pa_tick_tm(millis(), nm, ##__VA_ARGS__)
578+
#endif
583579

584580
/* Convenience */
585581

tests/tests.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
#include <stdio.h>
88
#include <assert.h>
99

10+
/* Defines */
11+
12+
#define set_current_time_ms(ms) current_time_ms = *local_current_time_ms = ms;
13+
1014
/* Gobals */
1115

12-
__thread pa_time_t pa_current_time_ms;
16+
pa_time_t current_time_ms = 0;
1317

1418
/* Helpers */
1519

@@ -77,7 +81,7 @@ pa_activity (TestAwait, pa_ctx(pa_co_res(4); int value; int actual; int expected
7781

7882
/* Delay Tests */
7983

80-
pa_activity (TestDelaySpec, pa_ctx(), int* value, int* expected) {
84+
pa_activity (TestDelaySpec, pa_ctx(), int* value, int* expected, pa_time_t* local_current_time_ms) {
8185
*expected = 1;
8286
pa_pause;
8387
*expected = 2;
@@ -88,25 +92,25 @@ pa_activity (TestDelaySpec, pa_ctx(), int* value, int* expected) {
8892
*expected = 5;
8993
pa_pause;
9094

91-
pa_current_time_ms = 0;
95+
set_current_time_ms(0);
9296
*expected = 6;
9397
pa_pause;
94-
pa_current_time_ms = 9;
98+
set_current_time_ms(9);
9599
pa_pause;
96-
pa_current_time_ms = 10;
100+
set_current_time_ms(10);
97101
*expected = 7;
98102
pa_pause;
99-
pa_current_time_ms = 11;
103+
set_current_time_ms(11);
100104
*expected = 9;
101105
pa_pause;
102106

103107
*expected = 10;
104-
pa_current_time_ms = -3;
108+
set_current_time_ms(-3);
105109
pa_pause;
106-
pa_current_time_ms = 0;
110+
set_current_time_ms(0);
107111
pa_pause;
108112
*expected = 11;
109-
pa_current_time_ms = 2;
113+
set_current_time_ms(2);
110114
pa_pause;
111115
} pa_end;
112116

@@ -149,7 +153,7 @@ pa_activity (TestDelayCheck, pa_ctx(), int actual, int expected) {
149153
pa_activity (TestDelay, pa_ctx(pa_co_res(4); int value; int actual; int expected;
150154
pa_use(TestDelaySpec); pa_use(TestDelayTest); pa_use(TestDelayCheck))) {
151155
pa_co(3) {
152-
pa_with_weak (TestDelaySpec, &pa_self.value, &pa_self.expected);
156+
pa_with_weak (TestDelaySpec, &pa_self.value, &pa_self.expected, &pa_current_time_ms);
153157
pa_with (TestDelayTest, pa_self.value, &pa_self.actual);
154158
pa_with_weak (TestDelayCheck, pa_self.actual, pa_self.expected);
155159
} pa_co_end;
@@ -283,7 +287,7 @@ pa_activity (TestCo, pa_ctx(pa_co_res(4); int value; int actual; int expected;
283287

284288
/* When-Abort Tests */
285289

286-
pa_activity (TestWhenAbortSpec, pa_ctx(), int* value, int* expected) {
290+
pa_activity (TestWhenAbortSpec, pa_ctx(), int* value, int* expected, pa_time_t* local_current_time_ms) {
287291

288292
/* Test that preemption happens only when conditiopn is true. */
289293
*expected = 0;
@@ -345,13 +349,13 @@ pa_activity (TestWhenAbortSpec, pa_ctx(), int* value, int* expected) {
345349
pa_pause;
346350

347351
/* Test after ms abort. */
348-
pa_current_time_ms = 0;
352+
set_current_time_ms(0);
349353
*expected = 0;
350354
pa_pause;
351-
pa_current_time_ms = 9;
355+
set_current_time_ms(9);
352356
*expected = 1;
353357
pa_pause;
354-
pa_current_time_ms = 10;
358+
set_current_time_ms(10);
355359
*expected = -2;
356360
pa_pause;
357361

@@ -430,7 +434,7 @@ pa_activity (TestWhenAbortCheck, pa_ctx(), int actual, int expected) {
430434
pa_activity (TestWhenAbort, pa_ctx(pa_co_res(4); int value; int actual; int expected;
431435
pa_use(TestWhenAbortSpec); pa_use(TestWhenAbortTest); pa_use(TestWhenAbortCheck))) {
432436
pa_co(3) {
433-
pa_with_weak (TestWhenAbortSpec, &pa_self.value, &pa_self.expected);
437+
pa_with_weak (TestWhenAbortSpec, &pa_self.value, &pa_self.expected, &pa_current_time_ms);
434438
pa_with (TestWhenAbortTest, pa_self.value, &pa_self.actual);
435439
pa_with_weak (TestWhenAbortCheck, pa_self.actual, pa_self.expected);
436440
} pa_co_end;
@@ -499,7 +503,7 @@ pa_activity (TestWhenReset, pa_ctx(pa_co_res(4); int value; int actual; int expe
499503

500504
/* Every Tests */
501505

502-
pa_activity (TestEverySpec, pa_ctx(), int* value, int* expected) {
506+
pa_activity (TestEverySpec, pa_ctx(), int* value, int* expected, pa_time_t* local_current_time_ms) {
503507

504508
/* Test that we don't enter every on false condition. */
505509
*value = 0;
@@ -534,19 +538,19 @@ pa_activity (TestEverySpec, pa_ctx(), int* value, int* expected) {
534538
pa_pause;
535539

536540
/* Test every_ms */
537-
pa_current_time_ms = 0;
541+
set_current_time_ms(0);
538542
*value = 0;
539543
*expected = 1;
540544
pa_pause;
541545

542-
pa_current_time_ms = 1;
546+
set_current_time_ms(1);
543547
pa_pause;
544548

545-
pa_current_time_ms = 5;
549+
set_current_time_ms(5);
546550
*expected = 2;
547551
pa_pause;
548552

549-
pa_current_time_ms = 9;
553+
set_current_time_ms(9);
550554
pa_pause;
551555

552556
*value = 1;
@@ -622,7 +626,7 @@ pa_activity (TestEveryCheck, pa_ctx(), int actual, int expected) {
622626
pa_activity (TestEvery, pa_ctx(pa_co_res(4); int value; int actual; int expected;
623627
pa_use(TestEverySpec); pa_use(TestEveryTest); pa_use(TestEveryCheck))) {
624628
pa_co(3) {
625-
pa_with (TestEverySpec, &pa_self.value, &pa_self.expected);
629+
pa_with (TestEverySpec, &pa_self.value, &pa_self.expected, &pa_current_time_ms);
626630
pa_with_weak (TestEveryTest, pa_self.value, &pa_self.actual);
627631
pa_with_weak (TestEveryCheck, pa_self.actual, pa_self.expected);
628632
} pa_co_end;
@@ -633,7 +637,7 @@ pa_activity (TestEvery, pa_ctx(pa_co_res(4); int value; int actual; int expected
633637
#define run_test(nm) \
634638
pa_use(nm); \
635639
pa_init(nm); \
636-
while (pa_tick(nm) == PA_RC_WAIT) {}
640+
while (pa_tick_tm(current_time_ms, nm) == PA_RC_WAIT) {}
637641

638642
int main(int argc, char* argv[]) {
639643
printf("Start\n");

tests_cpp/tests.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
#include <iostream>
88
#include <assert.h>
99

10+
// Defines
11+
12+
#define set_current_time_ms(ms) current_time_ms = local_current_time_ms = ms;
13+
14+
1015
// Gobals
1116

12-
__thread pa_time_t pa_current_time_ms;
17+
pa_time_t current_time_ms;
1318

1419
// Helpers
1520

@@ -89,7 +94,7 @@ pa_activity (TestAwait, pa_ctx(pa_co_res(4); int value; int actual; int expected
8994

9095
// Delay Tests
9196

92-
pa_activity (TestDelaySpec, pa_ctx(), int& value, int& expected) {
97+
pa_activity (TestDelaySpec, pa_ctx(), int& value, int& expected, pa_time_t& local_current_time_ms) {
9398
expected = 1;
9499
pa_pause;
95100
expected = 2;
@@ -100,25 +105,25 @@ pa_activity (TestDelaySpec, pa_ctx(), int& value, int& expected) {
100105
expected = 5;
101106
pa_pause;
102107

103-
pa_current_time_ms = 0;
108+
set_current_time_ms(0);
104109
expected = 6;
105110
pa_pause;
106-
pa_current_time_ms = 9;
111+
set_current_time_ms(9);
107112
pa_pause;
108-
pa_current_time_ms = 10;
113+
set_current_time_ms(10);
109114
expected = 7;
110115
pa_pause;
111-
pa_current_time_ms = 11;
116+
set_current_time_ms(11);
112117
expected = 9;
113118
pa_pause;
114119

115120
expected = 10;
116-
pa_current_time_ms = -3;
121+
set_current_time_ms(-3);
117122
pa_pause;
118-
pa_current_time_ms = 0;
123+
set_current_time_ms(0);
119124
pa_pause;
120125
expected = 11;
121-
pa_current_time_ms = 2;
126+
set_current_time_ms(2);
122127
pa_pause;
123128
} pa_end;
124129

@@ -161,7 +166,7 @@ pa_activity (TestDelayCheck, pa_ctx(), int actual, int expected) {
161166
pa_activity (TestDelay, pa_ctx(pa_co_res(4); int value; int actual; int expected;
162167
pa_use(TestDelaySpec); pa_use(TestDelayTest); pa_use(TestDelayCheck))) {
163168
pa_co(3) {
164-
pa_with_weak (TestDelaySpec, pa_self.value, pa_self.expected);
169+
pa_with_weak (TestDelaySpec, pa_self.value, pa_self.expected, pa_current_time_ms);
165170
pa_with (TestDelayTest, pa_self.value, pa_self.actual);
166171
pa_with_weak (TestDelayCheck, pa_self.actual, pa_self.expected);
167172
} pa_co_end;
@@ -295,7 +300,7 @@ pa_activity (TestCo, pa_ctx(pa_co_res(4); int value; int actual; int expected;
295300

296301
// When-Abort Tests
297302

298-
pa_activity (TestWhenAbortSpec, pa_ctx(), int& value, int& expected) {
303+
pa_activity (TestWhenAbortSpec, pa_ctx(), int& value, int& expected, pa_time_t& local_current_time_ms) {
299304

300305
// Test that preemption happens only when conditiopn is true.
301306
expected = 0;
@@ -357,13 +362,13 @@ pa_activity (TestWhenAbortSpec, pa_ctx(), int& value, int& expected) {
357362
pa_pause;
358363

359364
// Test after ms abort.
360-
pa_current_time_ms = 0;
365+
set_current_time_ms(0);
361366
expected = 0;
362367
pa_pause;
363-
pa_current_time_ms = 9;
368+
set_current_time_ms(9);
364369
expected = 1;
365370
pa_pause;
366-
pa_current_time_ms = 10;
371+
set_current_time_ms(10);
367372
expected = -2;
368373
pa_pause;
369374

@@ -442,7 +447,7 @@ pa_activity (TestWhenAbortCheck, pa_ctx(), int actual, int expected) {
442447
pa_activity (TestWhenAbort, pa_ctx(pa_co_res(4); int value; int actual; int expected;
443448
pa_use(TestWhenAbortSpec); pa_use(TestWhenAbortTest); pa_use(TestWhenAbortCheck))) {
444449
pa_co(3) {
445-
pa_with_weak (TestWhenAbortSpec, pa_self.value, pa_self.expected);
450+
pa_with_weak (TestWhenAbortSpec, pa_self.value, pa_self.expected, pa_current_time_ms);
446451
pa_with (TestWhenAbortTest, pa_self.value, pa_self.actual);
447452
pa_with_weak (TestWhenAbortCheck, pa_self.actual, pa_self.expected);
448453
} pa_co_end;
@@ -511,7 +516,7 @@ pa_activity (TestWhenReset, pa_ctx(pa_co_res(4); int value; int actual; int expe
511516

512517
// Every Tests
513518

514-
pa_activity (TestEverySpec, pa_ctx(), int& value, int& expected) {
519+
pa_activity (TestEverySpec, pa_ctx(), int& value, int& expected, pa_time_t& local_current_time_ms) {
515520

516521
// Test that we don't enter every on false condition.
517522
value = 0;
@@ -546,19 +551,19 @@ pa_activity (TestEverySpec, pa_ctx(), int& value, int& expected) {
546551
pa_pause;
547552

548553
// Test every_ms
549-
pa_current_time_ms = 0;
554+
set_current_time_ms(0);
550555
value = 0;
551556
expected = 1;
552557
pa_pause;
553558

554-
pa_current_time_ms = 1;
559+
set_current_time_ms(1);
555560
pa_pause;
556561

557-
pa_current_time_ms = 5;
562+
set_current_time_ms(5);
558563
expected = 2;
559564
pa_pause;
560565

561-
pa_current_time_ms = 9;
566+
set_current_time_ms(9);
562567
pa_pause;
563568

564569
value = 1;
@@ -634,7 +639,7 @@ pa_activity (TestEveryCheck, pa_ctx(), int actual, int expected) {
634639
pa_activity (TestEvery, pa_ctx(pa_co_res(4); int value; int actual; int expected;
635640
pa_use(TestEverySpec); pa_use(TestEveryTest); pa_use(TestEveryCheck))) {
636641
pa_co(3) {
637-
pa_with (TestEverySpec, pa_self.value, pa_self.expected);
642+
pa_with (TestEverySpec, pa_self.value, pa_self.expected, pa_current_time_ms);
638643
pa_with_weak (TestEveryTest, pa_self.value, pa_self.actual);
639644
pa_with_weak (TestEveryCheck, pa_self.actual, pa_self.expected);
640645
} pa_co_end;

0 commit comments

Comments
 (0)