|
| 1 | +classdef webserver < handle %#codegen |
| 2 | + |
| 3 | + methods(Static) |
| 4 | + |
| 5 | + // command line arguments |
| 6 | + function ac = argc() |
| 7 | + coder.cinclude('stl.h'); |
| 8 | + |
| 9 | + ac = int32(0); |
| 10 | + ac = coder.ceval('stl_argc'); % evaluate the C function |
| 11 | + end |
| 12 | + |
| 13 | + function s = argv(a) |
| 14 | + coder.cinclude('stl.h'); |
| 15 | + |
| 16 | + s = ''; |
| 17 | + coder.varsize('s'); |
| 18 | + |
| 19 | + BUFSIZ = 256; |
| 20 | + buf = char(zeros(1,BUFSIZ)); % create a buffer to write into, all nulls |
| 21 | + |
| 22 | + coder.ceval('stl_argv', a, coder.wref(buf), BUFSIZ); % evaluate the C function |
| 23 | + |
| 24 | + % find the end of the string, where the first unwritten null is |
| 25 | + for i=1:BUFSIZ-1 |
| 26 | + if buf(i) == 0 |
| 27 | + % found a null, return variable length array up to here |
| 28 | + s = buf(1:i-1); |
| 29 | + return; |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + // thread |
| 35 | + function tid = launch(name, arg) |
| 36 | + coder.cinclude('stl.h'); |
| 37 | + |
| 38 | + if nargin < 2 |
| 39 | + arg = 0; |
| 40 | + end |
| 41 | + tid = int32(0); |
| 42 | + tid = coder.ceval('stl_thread_create', cstring(name), coder.ref(arg), int32(0)); % evaluate the C function |
| 43 | + end |
| 44 | + |
| 45 | + function cancel(id) |
| 46 | + coder.cinclude('stl.h'); |
| 47 | + |
| 48 | + coder.ceval('stl_thread_cancel', id ); % evaluate the C function |
| 49 | + end |
| 50 | + |
| 51 | + function join(id) |
| 52 | + coder.cinclude('stl.h'); |
| 53 | + |
| 54 | + coder.ceval('stl_thread_join', id ); % evaluate the C function |
| 55 | + end |
| 56 | + |
| 57 | + function sleep(t) |
| 58 | + coder.cinclude('stl.h'); |
| 59 | + coder.ceval('stl_sleep', t); % evaluate the C function |
| 60 | + end |
| 61 | + |
| 62 | + function id = self() |
| 63 | + coder.cinclude('stl.h'); |
| 64 | + |
| 65 | + id = int32(0); |
| 66 | + id = coder.ceval('stl_thread_self'); % evaluate the C function |
| 67 | + end |
| 68 | + |
| 69 | + // mutex |
| 70 | + function mutex(name) |
| 71 | + coder.ceval('stl_launch', [name 0]); % evaluate the C function |
| 72 | + end |
| 73 | + |
| 74 | + // semaphore |
| 75 | + function sid = semaphore(name) |
| 76 | + coder.cinclude('stl.h'); |
| 77 | + |
| 78 | + sid = int32(0); |
| 79 | + sid = coder.ceval('stl_sem_create', cstring(name)); % evaluate the C function |
| 80 | + end |
| 81 | + |
| 82 | + function sempost(id) |
| 83 | + coder.cinclude('stl.h'); |
| 84 | + |
| 85 | + coder.ceval('stl_sem_post', id); % evaluate the C function |
| 86 | + end |
| 87 | + |
| 88 | + function semwait(id, wait) |
| 89 | + coder.cinclude('stl.h'); |
| 90 | + |
| 91 | + if nargin < 2 |
| 92 | + wait = int32(0); |
| 93 | + end |
| 94 | + coder.ceval('stl_sem_wait', id, wait); % evaluate the C function |
| 95 | + end |
| 96 | + |
| 97 | + // logging |
| 98 | + function stllog(varargin) |
| 99 | + %stl.log Send formatted string to log |
| 100 | + % |
| 101 | + % stl.log(fmt, args...) has printf() like semantics and sends the formatted |
| 102 | + % string to the log where it is displayed with date, time and thread name. |
| 103 | + % |
| 104 | + % NOTES:: |
| 105 | + % - String arguments, not the format string, must be wrapped with cstring, eg. cstring('hello') |
| 106 | + coder.cinclude('stl.h'); |
| 107 | + |
| 108 | + coder.ceval('stl_log', cstring(varargin{1}), varargin{2:end} ); % evaluate the C function |
| 109 | + end |
| 110 | + |
| 111 | + end % methods(Static) |
| 112 | +end % classdef |
0 commit comments