dol: initial dol commit
[jump.git] / dol / src / dol / visitor / rtems / lib / process_wrapper_template.c
1 #include <bsp.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "rtems_process_wrapper.h"
6
7 #ifdef WORKLOAD_EXTRACT
8 #define DOL_read(port, buf, len, process) \
9 { ((RtemsProcessWrapper*)process->wptr)->end_line = __LINE__; \
10   DOL_read(port, buf, len, process); \
11   ((RtemsProcessWrapper*)process->wptr)->start_line = __LINE__; }
12
13 #define DOL_write(port, buf, len, process) \
14 { ((RtemsProcessWrapper*)process->wptr)->end_line = __LINE__; \
15   DOL_write(port, buf, len, process); \
16   ((RtemsProcessWrapper*)process->wptr)->start_line = __LINE__; }
17 #endif
18
19 #ifdef PRINTF_TO_DEBUG
20 #undef printf
21 #define printf(...) \
22     do {            \
23         char _buffer[128];                     \
24         sprintf(_buffer, __VA_ARGS__);         \
25         SHOW_DEBUG((int)_buffer);              \
26     } while (0)
27 #endif
28
29 //#include "@PROCESSNAME@.c"
30
31 //DOL-specific implementation
32 rtems_task @PROCESSNAME@_task(rtems_task_argument argument) {
33     RtemsProcessWrapper* wrapper = (RtemsProcessWrapper*)argument;
34     DOLProcess* process;
35     LocalState state;
36     int number_of_activations = 0;
37     int i;
38     rtems_task_priority old_priority;
39
40     process = (DOLProcess *)malloc(sizeof(DOLProcess));
41     state = (LocalState)malloc(sizeof(@Processname@_State));
42     process->init = @PROCESSNAME@_init;
43     process->fire = @PROCESSNAME@_fire;
44     process->local = state;
45     process->wptr = wrapper;
46
47     //initialize the index array
48     wrapper->index = (int *)malloc(4 * sizeof(int));
49     for (i = 0; i < 4; i++) {
50         wrapper->index[i] = getIndex(wrapper->name, "_", i);
51     }
52
53     printf("Start process %s.\n", wrapper->name);
54     process->init(process);
55
56     //yield process such that other processes can be initialized
57 #ifdef MPARM
58     do {
59         rtems_task_wake_after(RTEMS_YIELD_PROCESSOR);
60     } while((int)get_cycle() < 1511576);
61 #endif
62     rtems_task_set_priority(RTEMS_SELF, wrapper->priority, &old_priority);
63
64     while (!wrapper->is_detached) {
65
66         //placeholder for periodic trigger
67
68 #ifdef WORKLOAD_EXTRACT
69         wrapper->start_line = 0;
70         SHOW_DEBUG("log start_fire");
71         SHOW_DEBUG_INT((int)get_cycle());
72 #endif
73
74         process->fire(process);
75
76 #ifdef WORKLOAD_EXTRACT
77         SHOW_DEBUG("log end_fire");
78         SHOW_DEBUG_INT((int)get_cycle());
79         wrapper->end_line = 0;
80 #endif
81         number_of_activations++;
82     }
83
84 #ifdef MPARM_SCRATCHPAD_QUEUE
85     #ifdef QUEUE_BUFF_SHAPER
86     @ENDING_SHAPER@
87     #endif
88 #else // pc386
89     //below is not necessary if no dynamic switching
90     //printf("Detached @PROCESSNAME@. Cleanup... ");
91     if (wrapper->index) {
92         free(wrapper->index);
93     } else {
94         printf("Could not free memory for index of @PROCESSNAME@.\n");
95     }
96
97     if (wrapper->port_id) {
98         free(wrapper->port_id);
99     } else {
100         printf("Could not free memory for port_id of @PROCESSNAME@.\n");
101     }
102
103     if (wrapper->port_queue_id) {
104     } else {
105         printf("Could not free memory for port_queue_id of @PROCESSNAME@.\n");
106     }
107 #endif // MPARM_SCRATCHPAD_QUEUE
108     //printf("Done.\n");
109
110     printf("Detach process %s.\n", wrapper->name);
111 #ifdef MPARM
112     Cleanup();
113 #endif
114
115     rtems_task_delete(RTEMS_SELF);
116 }