Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e7eab90

Browse filesBrowse files
committed
Working example of passing struct from user to a thread.
Not general, can only be 1 structure type for all threads.
1 parent 7c1fda0 commit e7eab90
Copy full SHA for e7eab90

File tree

7 files changed

+27
-13
lines changed
Filter options

7 files changed

+27
-13
lines changed

‎examples/threads/make.m

Copy file name to clipboardExpand all lines: examples/threads/make.m
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
codegen user.m thread1.m -args int32(0) thread2.m thread3.m -O disable:inline -config cfg
3737
%}
3838

39-
codegen user.m thread1.m -args int32(0) thread2.m thread3.m -config cfg
39+
codegen user.m thread1.m -args z thread2.m thread3.m -config cfg
4040
%MAKE = gmake
4141
%MAKE_FLAGS = -f $(MAKEFILE)
4242
% hw = coder.hardware('Raspberry Pi');

‎examples/threads/thread1.m

Copy file name to clipboardExpand all lines: examples/threads/thread1.m
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
function thread1(arg) %#codegen
22

3+
stllog('hello from thread1');
4+
stllog('struct.a=%f', arg.a);
5+
stllog('struct.b=%f', arg.b);
6+
37
for i=1:10
4-
stllog('hello from thread1, arg=%d, id #%d', arg, self());
8+
%stllog('hello from thread1, arg=%d, id #%d', arg, self());
9+
stllog('hello from thread1, id #%d', self());
510
sleep(1)
611
end
712
end

‎examples/threads/user.m

Copy file name to clipboardExpand all lines: examples/threads/user.m
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919

2020
% now we can launch a couple of threads, see thread1.m and thread2.m
2121
% launching returns a thread id, a small integer
22-
t1 = launch('thread1', 24) % pass a value to this thread
22+
23+
z.a = 1
24+
z.b = 2
25+
26+
coder.cstructname(z, 'arg_t')
27+
28+
t1 = launch('thread1', z) % pass a value to this thread
2329
stllog('thread id %d', t1)
2430
t2 = launch('thread2')
2531
stllog('thread id %d', t2)

‎stl/launch.m

Copy file name to clipboardExpand all lines: stl/launch.m
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
arg = 0;
66
end
77
tid = int32(0);
8-
tid = coder.ceval('stl_thread_create', cstring(name), int32(arg), int32(0)); % evaluate the C function
8+
tid = coder.ceval('stl_thread_create', cstring(name), coder.ref(arg), int32(0)); % evaluate the C function
99
end

‎stl/main.c

Copy file name to clipboardExpand all lines: stl/main.c
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include <stdlib.h>
66
#include <unistd.h>
77

8-
#include "stl.h"
8+
99
#include "user.h"
1010
#include "user_initialize.h"
1111
#include "user_terminate.h"
12+
#include "user_types.h"
13+
#include "stl.h"
1214

1315
int
1416
main(int argc, char **argv)

‎stl/stl.c

Copy file name to clipboardExpand all lines: stl/stl.c
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#endif
2525
#include <time.h>
2626

27+
#include "user_types.h"
2728
#include "stl.h"
2829

2930
// parameters
@@ -52,8 +53,8 @@ typedef struct _thread {
5253
pthread_t pthread; // the POSIX thread handle
5354
char *name;
5455
int busy;
55-
void * (*f)(int32_t); // pointer to thread function entry point
56-
int32_t arg;
56+
void * (*f)(void *); // pointer to thread function entry point
57+
void *arg;
5758
} thread;
5859

5960
typedef struct _semaphore {
@@ -77,7 +78,7 @@ typedef struct _timer {
7778
#endif
7879

7980
// local forward defines
80-
static void stl_thread_wrapper( thread *tp, int32_t arg);
81+
static void stl_thread_wrapper( thread *tp);
8182
extern int errno;
8283

8384
// local data
@@ -153,7 +154,7 @@ stl_sleep(double t)
153154

154155

155156
int
156-
stl_thread_create(char *func, int32_t arg, char *name)
157+
stl_thread_create(char *func, arg_t * arg, char *name)
157158
{
158159
pthread_attr_t attr;
159160
void * (*f)(int32_t);
@@ -185,21 +186,21 @@ stl_thread_create(char *func, int32_t arg, char *name)
185186
else
186187
tp->name = stl_stralloc(func);
187188
tp->f = f;
188-
tp->arg = arg;
189+
tp->arg = (void *)arg;
189190

190191
// set attributes
191192
pthread_attr_init(&attr);
192193

193194
// check result
194-
status = pthread_create(&(tp->pthread), &attr, (void *(*)(void *))stl_thread_wrapper, &threadlist[slot]);
195+
status = pthread_create(&(tp->pthread), &attr, (void *(*)(void *))stl_thread_wrapper, tp);
195196
if (status)
196197
stl_error("create: failed %s", strerror(status));
197198

198199
return slot;
199200
}
200201

201202
static void
202-
stl_thread_wrapper( thread *tp, int32_t arg)
203+
stl_thread_wrapper( thread *tp)
203204
{
204205
STL_DEBUG("starting posix thread <%s> (0x%X)", tp->name, (uint32_t)tp->f);
205206

‎stl/stl.h

Copy file name to clipboardExpand all lines: stl/stl.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ char *stl_stralloc(char *s);
1212
void stl_sleep(double t);
1313

1414
// threads
15-
int32_t stl_thread_create(char *func, int32_t arg, char *name);
15+
int32_t stl_thread_create(char *func, arg_t * arg, char *name);
1616
int32_t stl_thread_join(int32_t slot);
1717
void stl_thread_cancel(int32_t slot);
1818
int32_t stl_thread_self();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.