1// This file is part of SmallBASIC
2//
3// SmallBASIC SB-Task manager
4//
5// This program is distributed under the terms of the GPL v2.0 or later
6// Download the GNU Public License (GPL) from www.gnu.org
7//
8// Copyright(C) 2000 Nicholas Christopoulos
9
10#if !defined(__sb_tasks_h)
11#define __sb_tasks_h
12
13#include "common/bc.h"
14
15#if defined(__cplusplus)
16extern "C" {
17#endif
18
19typedef enum {
20 tsk_free, tsk_ready
21} task_status_t;
22
23typedef struct timer_s timer_s;
24struct timer_s {
25 timer_s *next; // next timer
26 long value; // time for next event
27 long interval; // interval ms
28 bcip_t ip; // handler location
29 int active; // whether IP is being invoked
30};
31
32typedef struct {
33 bcip_t length; /**< The byte-code length (program length in bytes) */
34 bcip_t ip; /**< Register IP; the instruction pointer */
35 byte *bytecode; /**< The byte-code itself */
36 bcip_t dp; /**< Register DP; READ/DATA current position */
37 bcip_t org; /**< READ/DATA beginning position */
38 stknode_t *stack; /**< The program stack */
39 uint32_t stack_alloc; /**< The stack size */
40 uint32_t sp; /**< Register SP; The stack pointer */
41 var_t *eval_stk; /**< eval's stack */
42 uint16_t eval_stk_size; /**< eval's stack size */
43 uint16_t eval_esp; /**< Register ESP; eval's stack pointer */
44
45 /*
46 * Register R; no need
47 */
48 /*
49 * Register L; no need
50 */
51 uint32_t varcount; /**< number of global-variables */
52 uint32_t labcount; /**< number of labels */
53 uint32_t libcount; /**< number of linked libraries */
54 uint32_t symcount; /**< number of linked symbols */
55 uint32_t expcount; /**< number of exported symbols */
56
57 var_t **vartable; /**< The table of variables */
58 lab_t *labtable; /**< The table of labels */
59 bc_lib_rec_t *libtable; /**< import-libraries table */
60 bc_symbol_rec_t *symtable; /**< import-symbols table */
61 unit_sym_t *exptable; /**< export-symbols table */
62 timer_s *timer; /** timer linked list */
63} task_executor;
64
65typedef struct {
66 ext_proc_node_t *extproctable; /**< external procedure table */
67 int extprocsize; /**< ext-proc table allocated size */
68 int extproccount; /**< ext-proc table count */
69
70 ext_func_node_t *extfunctable; /**< external function table */
71 int extfuncsize; /**< ext-func table allocated size */
72 int extfunccount; /**< ext-func table count */
73
74 char file_name[OS_PATHNAME_SIZE + 1];
75 char unit_name[SB_KEYWORD_SIZE + 1];
76 int unit_flag;
77
78 bc_lib_rec_table_t libtable;
79 bc_symbol_rec_table_t imptable;
80 unit_sym_table_t exptable;
81
82 int block_level; // block level (FOR-NEXT,IF-FI,etc)
83 int block_id; // unique ID for blocks (FOR-NEXT,IF-FI,etc)
84
85 bcip_t first_data_ip;
86
87 // buffers... needed for devices with limited memory
88 char *bc_name;
89 char *bc_parm;
90 char *bc_temp; // primary all-purpose buffer
91 char *bc_tmp2; // secondary all-purpose buffer
92 char *bc_proc;
93 char *bc_sec; // used by sberr.c
94
95 int proc_level;
96 // flag - uses global variable-table for the next commands
97 byte use_global_vartable;
98
99 bc_t bc_prog;
100 bc_t bc_data;
101
102 char do_close_cmd[64];
103
104 // variable table
105 comp_var_t *vartable;
106 bid_t varcount;
107 bid_t varsize;
108
109 // label table
110 comp_label_table_t labtable;
111
112 // user defined proc/func table
113 comp_udp_t *udptable;
114 bid_t udpcount;
115 bid_t udpsize;
116
117 // pass2 stack
118 comp_pass_node_table_t stack;
119} task_compiler;
120
121/**
122 * @ingroup sys
123 *
124 * @typedef task_t
125 * Task data
126 */
127typedef struct {
128 int status;
129 int tid;
130 int parent;
131
132 // common
133 int line; /**< The current source code line */
134 int error; /**< The last RTL error code (its work as flag) */
135 char errmsg[SB_ERRMSG_SIZE + 1];
136 char file[OS_PATHNAME_SIZE + 1]; /**< The program file name (task name) */
137 byte *bytecode; /**< BC's memory handle */
138 int bc_type; /**< BC type (1=executable, 2=unit) */
139 int has_sysvars; /**< true if the task has system-variables */
140
141 // compiler/executor
142 union {
143 task_compiler comp;
144 task_executor exec;
145 } sbe;
146} task_t;
147
148EXTERN task_t *ctask; /**< current task pointer */
149
150/**
151 * @ingroup sys
152 *
153 * return the number of the tasks
154 *
155 * @return the number of the tasks
156 */
157int count_tasks();
158
159/**
160 * @ingroup sys
161 *
162 * initialize tasks manager
163 */
164int init_tasks();
165
166/**
167 * @ingroup sys
168 *
169 * destroys tasks and closes task manager
170 */
171void destroy_tasks();
172
173/**
174 * @ingroup sys
175 *
176 * create an empty new task
177 *
178 * @param name is the task name
179 * @return the task-id
180 */
181int create_task(const char *name);
182
183/**
184 * @ingroup sys
185 *
186 * closes a task and activate the next
187 */
188void close_task(int tid);
189
190/**
191 * @ingroup sys
192 *
193 * set active task
194 *
195 * @param tid the task-id
196 * @return the previous task-id
197 */
198int activate_task(int tid);
199
200/**
201 * @ingroup sys
202 *
203 * return the nth task-structure
204 *
205 * @return the nth task-structure
206 */
207task_t *taskinfo(int n);
208
209/**
210 * @ingroup sys
211 *
212 * search for a task
213 *
214 * @param task_name the name of the task
215 * @return the task-id; or -1 on error
216 */
217int search_task(const char *task_name);
218
219#if defined(__cplusplus)
220}
221#endif
222#endif
223