ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Вторник
16 апреля
885671
SciFi (30.11.2018 15:30, просмотров: 24479)
Обожаю Main Loop, жить без него не могу. Захотелось прикрутить интерпретатор тикля, а там весь скрипт исполняется разом, в главный цикл выходит только в конце. Что делать? Только не RTOS! Прикрутил простейшую переключалку потока на setjmp/longjmp, https://github.com/zserge/partcl/
вставил в код интерпретатора yield() - и золотой ключик у меня в кармане. Лепота! #include "tcl.h" #include <setjmp.h> #include <stdio.h> #include <string.h> static jmp_buf main_env, task_env; void yield(void) { if (!setjmp(task_env)) { longjmp(main_env, 1); } } static void task(void) { yield(); // do something useful struct tcl tcl; static const char *s = "set x 4; puts [+ [* $x 10] 2]; puts [* $x 3];"; static char out[1024]; tcl_init(&tcl); if (tcl_eval(&tcl, s, strlen(s)) != FERROR) { sprintf(out, "%.*s\n", tcl_length(tcl.result), tcl_string(tcl.result)); } tcl_destroy(&tcl); for (;;) { yield(); } } int main() { if (!setjmp(main_env)) { // init task stack static uint64_t stack[1024]; static const uint16_t set_stack[] = { 0x4685, // mov sp, r0 0x4770 // bx lr }; ((void (*)(void*))(1 + (int)set_stack))(stack + sizeof stack); // schedule task for the first time task(); } for (;;) { // schedule task if (!setjmp(main_env)) { longjmp(task_env, 1); } } }
ส็็็็็็็็็็็็็็็็็็็็็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้