Skip to content

Commit b97386b

Browse files
committed
New example, periodic semaphores
1 parent aea1833 commit b97386b

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

examples/timer/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# copy this into the codegen/exe/user folder on the target machine
2+
# then run it, it will build the binary in this folder
3+
vpath %c ../../../stl
4+
OBJ = thread1.o thread2.o thread3.o user.o user_initialize.o user_terminate.o main.o stl.o
5+
CFLAGS += -I . -I ../../../stl
6+
LIBS = -ldl -lpthread -lrt
7+
8+
user: $(OBJ)
9+
$(CC) -o main -fPIC $(OBJ) $(LIBS)

examples/timer/make.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
% make.m
2+
3+
cfg = coder.config('exe')
4+
cfg.TargetLang = 'C';
5+
cfg.MultiInstanceCode = true;
6+
7+
cfg.GenerateReport = true;
8+
%cfg.LaunchReport = true;
9+
10+
% build options
11+
%cfg.GenerateExampleMain = 'DoNotGenerate';
12+
cfg.GenCodeOnly = true;
13+
14+
cfg.GenerateComments = true;
15+
cfg.MATLABSourceComments = true; % lots of comments
16+
cfg.PreserveVariableNames = 'UserNames';
17+
18+
% want to include some files from this directory, but this doesnt work??
19+
cfg.CustomSource = 'main.c stl.c'
20+
cfg.CustomInclude = '../../stl';
21+
22+
cfg.BuildConfiguration = 'Faster Builds';
23+
%cfg.BuildConfiguration = 'Debug';
24+
25+
% would be nice if could set make -j to get some parallel building
26+
% happening.
27+
28+
%for Raspberry Pi
29+
% hw = coder.hardware('Raspberry Pi');
30+
% cfg.Hardware = hw;
31+
% cfg.GenCodeOnly = true;
32+
33+
cfg.PostCodeGenCommand = 'postbuild(projectName, buildInfo)';
34+
%{
35+
cfg.InlineThreshold = 0; % tone down the aggressive inlining
36+
codegen user.m thread1.m -args int32(0) thread2.m thread3.m -O disable:inline -config cfg
37+
%}
38+
39+
codegen user.m thread1.m -config cfg
40+
%MAKE = gmake
41+
%MAKE_FLAGS = -f $(MAKEFILE)
42+
% hw = coder.hardware('Raspberry Pi');
43+
% cfg.Hardware = hw;

examples/timer/thread1.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function thread1() %#codegen
2+
3+
stllog('hello from thread1');
4+
5+
for i=1:20
6+
stl.semwait(0)
7+
stl.log('wakeup');
8+
end
9+
end
10+

examples/timer/user.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function user %#codegen
2+
3+
4+
5+
% z.a = 1
6+
% z.b = 2
7+
% coder.cstructname(z, 'arg_t')
8+
9+
s1 = stl.semaphore('sem1');
10+
stllog('sem id ', s1);
11+
12+
t1 = launch('thread1', 0) % pass a value to this thread
13+
stllog('thread id %d', t1)
14+
15+
timer = stl.timer('timer1', 2.0, s1);
16+
join(t1); % wait for thread 1 to finish
17+
18+
19+
% done, exiting will tear down all the threads
20+
21+
end
22+

0 commit comments

Comments
 (0)