/
main.xc
226 lines (190 loc) · 5.81 KB
/
main.xc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2011, XMOS Ltd., All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include <print.h>
#include <xs1.h>
#include "fifo.h"
enum {
GET_DATA,
SET_OFFSET
} thread1_commands;
/** The interval in timer ticks that thread1 will generate data at */
#define DATA_GEN_INTERVAL 10000
/** A simple random data generation function */
int gen_data(int &x) {
const unsigned a=1664525;
const unsigned c=1013904223;
x = a * ((unsigned) x) + c;
return x;
}
/** thread 1.
*
* This thread periodically generates data and places it in a fifo. It signals
* to thread 2 when data is ready and responds when thread 2 asks for data.
*
* It can also respond to messages/commands sent to it from thread2
* asynchronously to the notification protocol.
*
*/
void thread1(chanend c) {
int st = 1234;
int data[DATASIZE];
fifo_t fifo;
timer tmr;
int t;
int notified = 0;
int renotify;
char cmd;
int offset = 0;
init_fifo(fifo);
tmr :> t;
while (1) {
select
{
case tmr when timerafter(t) :> void:
// Periodically this thread generates some data
// This case fires from a timer when it is time to generate some
// Generate the data
for (int i=0;i<DATASIZE;i++) {
data[i] = gen_data(st) + offset;
}
// Put the data into the fifo
printstrln("Thread1: Putting data into fifo");
add_to_fifo(fifo, data);
// If we haven't notified the other thread already then send a
// token into the channel to say that data is ready
if (!notified) {
printstrln("Thread1: Notifying thread2 that data is available.");
outct(c, XS1_CT_END);
notified = 1;
}
// Set the time for the next data generation
t += DATA_GEN_INTERVAL;
break;
case inuchar_byref(c, cmd):
if (!notified)
outct(c, XS1_CT_END);
if (notified && cmd != GET_DATA)
renotify = 1;
else
renotify = 0;
outct(c, XS1_CT_END);
chkct(c, XS1_CT_END);
switch (cmd)
{
case GET_DATA:
// The other side has received our notification and responded
printstrln("Thread1: Received data request.");
// Get some data and send it back
get_from_fifo(fifo, data);
printstrln("Thread1: Sending data.");
master {
for (int i=0;i<DATASIZE;i++) {
c <: data[i];
}
}
// If we have more data to send then renotify
if (fifo_empty(fifo)) {
notified = 0;
}
else {
printstrln("Thread1: Notifying thread2 that data is available.");
outct(c, XS1_CT_END);
notified = 1;
}
break;
case SET_OFFSET:
printstrln("Thread1: Recieved offset change command.");
c :> offset;
break;
}
if (renotify) {
outct(c, XS1_CT_END); // Before entering the busy period asynchronously send a command to
// thread1
notified = 1;
}
break;
}
}
}
/** Thread 2
*
* This thread responds to data notifications provided by thread1 and pulls
* the data from thread1.
*
* Periodically it enters a busy state where it does not respond to
* notifiations. In this state the fifo in thread1 will start to back up and
* will by emptied when this thread comes out of the busy state.
*
*/
void thread2(chanend c) {
int data[DATASIZE];
char ct;
timer tmr;
unsigned t;
tmr :> t;
t += (DATA_GEN_INTERVAL*3)/2;
while (1) {
select {
case inct_byref(c, ct):
// The other thread has notified us that data is ready
printstrln("Thread2: Received notification.");
printstrln("Thread2: Sending data request.");
// Signal that we wish to consume the data
outuchar(c, GET_DATA);
outct(c, XS1_CT_END);
chkct(c, XS1_CT_END);
// Receive the data
slave {
for (int i=0;i<DATASIZE;i++) {
c :> data[i];
}
}
printstrln("Thread2: Received data.");
break;
case tmr when timerafter(t) :> void:
// The timer case is a periodic case that enters the "busy" period
printstrln("Thread2: Sending command.");
// Before entering the busy period asynchronously send a command to
// thread1
outuchar(c, SET_OFFSET);
outct(c, XS1_CT_END);
chkct(c, XS1_CT_END);
chkct(c, XS1_CT_END);
c <: 50;
printstrln("Thread2: Busy.");
// wait for DATA_GEN_INTERVAL*3 ticks
t += DATA_GEN_INTERVAL*1;
tmr when timerafter(t) :> void;
printstrln("Thread2: Sending command.");
// Send another command to
// thread1
outuchar(c, SET_OFFSET);
outct(c, XS1_CT_END);
chkct(c, XS1_CT_END);
chkct(c, XS1_CT_END);
c <: 100;
// wait for DATA_GEN_INTERVAL*3 ticks
t += DATA_GEN_INTERVAL*2;
tmr when timerafter(t) :> void;
printstrln("Thread2: Not Busy.");
// Set up the time for the next busy case
t += (DATA_GEN_INTERVAL*3)/2;
break;
}
}
}
/** The main function
*
* This function sets thread1 and thread2 off in parallel connected by a
* channel.
*/
int main() {
chan c;
par {
thread1(c);
thread2(c);
}
return 0;
}