integrating micropython into a cooperative multitasking OS

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

integrating micropython into a cooperative multitasking OS

Post by jickster » Tue Oct 03, 2017 7:07 pm

My company has an embedded OS that is cooperative i.e. not pre-emptive.

Therefore, I'm going to have to limit the execution to some time slice then relinquish control back to OS.

The location where I'm going to have to check whether timeout has occurred is inside vm.c

Code: Select all

DISPATCH();
which is expanded to

Code: Select all

do { 
   code_state->ip = ip; ; 
   goto *entry_table[*ip++];  
} while (0);
The modification will be something like

Code: Select all

do { 
   code_state->ip = ip; ; 
   goto *entry_table[*ip++];
   if(thread_timer_val == 0)
   {           
	 break;
   }	   
} while (0);
Obviously any timing-dependant tasks done in the Python (.py) context will be impacted but we're not worried about that; the user will be told upfront to not depend on Python-timing.

Are there any other considerations or pointers anyone can think of?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: integrating micropython into a cooperative multitasking OS

Post by stijn » Wed Oct 04, 2017 10:41 am

Can't comment on any implications, but have you seen the MICROPY_VM_HOOK_*** macros? That might be an easier way to extend the vm instead of modifying DISPATH, it's meant for that, though I'm not sure if it's usable for you.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: integrating micropython into a cooperative multitasking OS

Post by jickster » Wed Oct 04, 2017 3:23 pm

stijn wrote:Can't comment on any implications, but have you seen the MICROPY_VM_HOOK_*** macros? That might be an easier way to extend the vm instead of modifying DISPATH, it's meant for that, though I'm not sure if it's usable for you.
Thanks I didn't see those.

The most useful one for me is

Code: Select all

MICROPY_VM_HOOK_LOOP
however, I need to find out if there is an upper limit on the number of opcodes executed before it hits this macro.

It will only execute that macro if

Code: Select all

DISPATCH_WITH_PEND_EXC_CHECK()
gets executed which is only true for 6 opcodes.

So, it seems that the frequency of executing

Code: Select all

MICROPY_VM_HOOK_LOOP
is non-deterministic.

Am I wrong?

Because I need the micropython process to yield after xyz microseconds, I think modifying

Code: Select all

DISPATCH()
is my best bet but all suggestions are appreciated.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: integrating micropython into a cooperative multitasking OS

Post by stijn » Wed Oct 04, 2017 6:53 pm

Yes you might be right, I didn't check in detail how many times the hooks get called, sorry.

Post Reply