LispBM
eval_cps.h
Go to the documentation of this file.
1 /*
2  Copyright 2018, 2020, 2021, 2022 Joel Svensson svenssonjoel@yahoo.se
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #ifndef EVAL_CPS_H_
19 #define EVAL_CPS_H_
20 
21 #include "lbm_types.h"
22 #include "stack.h"
23 #include "lbm_channel.h"
24 #include "lbm_flat_value.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define EVAL_CPS_STATE_INIT 0
31 #define EVAL_CPS_STATE_PAUSED 1
32 #define EVAL_CPS_STATE_RUNNING 2
33 #define EVAL_CPS_STATE_STEP 3
34 #define EVAL_CPS_STATE_KILL 4
35 
36 #define EVAL_CPS_DEFAULT_MAILBOX_SIZE 10
37 
38 #define EVAL_CPS_CONTEXT_FLAG_NOTHING (uint32_t)0x0
39 #define EVAL_CPS_CONTEXT_FLAG_TRAP (uint32_t)0x1
40 #define EVAL_CPS_CONTEXT_FLAG_CONST (uint32_t)0x2
41 #define EVAL_CPS_CONTEXT_FLAG_CONST_SYMBOL_STRINGS (uint32_t)0x4
42 #define EVAL_CPS_CONTEXT_FLAG_INCREMENTAL_READ (uint32_t)0x8
43 
47 #define LBM_THREAD_STATE_READY (uint32_t)0
48 #define LBM_THREAD_STATE_BLOCKED (uint32_t)1
49 #define LBM_THREAD_STATE_TIMEOUT (uint32_t)2
50 #define LBM_THREAD_STATE_SLEEPING (uint32_t)3
51 #define LBM_THREAD_STATE_GC_BIT (uint32_t)(1 << 31)
52 
53 typedef struct eval_context_s{
54  lbm_value program;
55  lbm_value curr_exp;
56  lbm_value curr_env;
57  lbm_value *mailbox; /* Message passing mailbox */
58  uint32_t mailbox_size;
59  uint32_t num_mail; /* Number of messages in mailbox */
60  uint32_t flags;
61  lbm_value r;
62  char *error_reason;
63  bool app_cont;
64  lbm_stack_t K;
65  lbm_uint timestamp;
66  lbm_uint sleep_us;
67  uint32_t state;
68  char *name;
69  lbm_cid id;
70  lbm_cid parent;
71  /* while reading */
72  lbm_int row0;
73  lbm_int row1;
74  /* List structure */
75  struct eval_context_s *prev;
76  struct eval_context_s *next;
78 
79 typedef enum {
80  LBM_EVENT_FOR_HANDLER = 0,
81  LBM_EVENT_UNBLOCK_CTX,
82 } lbm_event_type_t;
83 
84 typedef struct {
85  lbm_event_type_t type;
86  lbm_uint parameter;
87  lbm_uint buf_ptr;
88  lbm_uint buf_len;
89 } lbm_event_t;
90 
93 
94 extern const fundamental_fun fundamental_table[];
95 
102 typedef void (*ctx_fun)(eval_context_t *, void*, void*);
103 
104 /* Common interface */
110 
111 /* Concurrent interface */
116 int lbm_eval_init(void);
121 void lbm_set_eval_step_quota(uint32_t quota);
126 bool lbm_eval_init_events(unsigned int num_events);
134 void lbm_set_event_handler_pid(lbm_cid pid);
155 
163 int lbm_remove_done_ctx(lbm_cid cid, lbm_value *v);
172 bool lbm_wait_ctx(lbm_cid cid, lbm_uint timeout_ms);
186 lbm_cid lbm_eval_program_ext(lbm_value lisp, unsigned int stack_size);
187 
191 void lbm_run_eval(void);
192 
197 void lbm_pause_eval(void);
202 void lbm_pause_eval_with_gc(uint32_t num_free);
206 void lbm_continue_eval(void);
210 void lbm_kill_eval(void);
215 uint32_t lbm_get_eval_state(void);
226 void lbm_set_error_reason(char *error_str);
237 void lbm_set_critical_error_callback(void (*fptr)(void));
246 lbm_cid lbm_create_ctx(lbm_value program, lbm_value env, lbm_uint stack_size, char *name);
263 bool lbm_unblock_ctx(lbm_cid cid, lbm_flat_value_t *fv);
271 bool lbm_unblock_ctx_unboxed(lbm_cid cid, lbm_value unboxed);
278 void lbm_running_iterator(ctx_fun f, void*, void*);
285 void lbm_blocked_iterator(ctx_fun f, void*, void*);
293 void lbm_set_verbose(bool verbose);
298 void lbm_set_usleep_callback(void (*fptr)(uint32_t));
303 void lbm_set_timestamp_us_callback(uint32_t (*fptr)(void));
315 void lbm_set_printf_callback(int (*prnt)(const char*, ...));
319 void lbm_set_dynamic_load_callback(bool (*fptr)(const char *, const char **));
324 lbm_cid lbm_get_current_cid(void);
325 
331 
337 bool lbm_mailbox_change_size(eval_context_t *ctx, lbm_uint new_size);
338 
339 bool create_string_channel(char *str, lbm_value *res);
340 
341 bool lift_char_channel(lbm_char_channel_t *ch, lbm_value *res);
342 
343 lbm_flash_status request_flash_storage_cell(lbm_value val, lbm_value *res);
344  //bool lift_array_flash(lbm_value flash_cell, char *data, lbm_uint num_elt);
345 
359 int lbm_perform_gc(void);
360 
361 #ifdef __cplusplus
362 }
363 #endif
364 #endif
bool lbm_wait_ctx(lbm_cid cid, lbm_uint timeout_ms)
lbm_cid lbm_eval_program(lbm_value lisp)
void lbm_kill_eval(void)
void lbm_set_timestamp_us_callback(uint32_t(*fptr)(void))
void lbm_run_eval(void)
bool lbm_event(lbm_flat_value_t *fv)
lbm_value(* fundamental_fun)(lbm_value *, lbm_uint, eval_context_t *)
Definition: eval_cps.h:92
lbm_cid lbm_get_event_handler_pid(void)
int lbm_perform_gc(void)
void lbm_continue_eval(void)
void lbm_set_error_reason(char *error_str)
void lbm_critical_error(void)
void lbm_set_dynamic_load_callback(bool(*fptr)(const char *, const char **))
bool lbm_eval_init_events(unsigned int num_events)
bool lbm_unblock_ctx_unboxed(lbm_cid cid, lbm_value unboxed)
void lbm_blocked_iterator(ctx_fun f, void *, void *)
void lbm_set_critical_error_callback(void(*fptr)(void))
void lbm_undo_block_ctx_from_extension(void)
void lbm_set_ctx_done_callback(void(*fptr)(eval_context_t *))
void lbm_set_event_handler_pid(lbm_cid pid)
bool lbm_event_handler_exists(void)
lbm_value lbm_find_receiver_and_send(lbm_cid cid, lbm_value msg)
bool lbm_mailbox_change_size(eval_context_t *ctx, lbm_uint new_size)
bool lbm_unblock_ctx(lbm_cid cid, lbm_flat_value_t *fv)
lbm_cid lbm_create_ctx(lbm_value program, lbm_value env, lbm_uint stack_size, char *name)
int lbm_eval_init(void)
void lbm_set_usleep_callback(void(*fptr)(uint32_t))
bool lbm_event_unboxed(lbm_value unboxed)
void lbm_set_printf_callback(int(*prnt)(const char *,...))
lbm_value eval_cps_get_env(void)
void lbm_pause_eval_with_gc(uint32_t num_free)
int lbm_remove_done_ctx(lbm_cid cid, lbm_value *v)
void(* ctx_fun)(eval_context_t *, void *, void *)
Definition: eval_cps.h:102
void lbm_set_verbose(bool verbose)
eval_context_t * lbm_get_current_context(void)
void lbm_toggle_verbose(void)
void lbm_set_eval_step_quota(uint32_t quota)
lbm_cid lbm_eval_program_ext(lbm_value lisp, unsigned int stack_size)
uint32_t lbm_get_eval_state(void)
void lbm_block_ctx_from_extension(void)
void lbm_pause_eval(void)
lbm_cid lbm_get_current_cid(void)
void lbm_block_ctx_from_extension_timeout(float s)
void lbm_running_iterator(ctx_fun f, void *, void *)
void lbm_set_error_suspect(lbm_value suspect)
uint32_t lbm_value
Definition: lbm_types.h:43
Definition: eval_cps.h:53
Definition: lbm_channel.h:68
Definition: eval_cps.h:84
Definition: lbm_flat_value.h:25
Definition: stack.h:33