Eliminate all global state

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
luke_titley
Posts: 10
Joined: Tue Sep 20, 2016 10:17 pm

Eliminate all global state

Post by luke_titley » Tue Sep 20, 2016 10:44 pm

Hey,
I work in the Animation/VFX as as software developer. I've been looking around for a scripting language I can embed in a hobby 3d tool I'm writing. Python is the industry standard scripting language for VFX at the moment, and is embedded into most tools and pipelines, but the abundant use of global variables in CPython and the GIL make it impractical to use as a scripting language for multi threaded vfx tools of the future. I came across Micro Python recently, and it looks like a fantastic project. I noticed just now that 'Eliminate all global state' was a wish list item for 2014. Was this ever achieved?

luke_titley
Posts: 10
Joined: Tue Sep 20, 2016 10:17 pm

Re: Eliminate all global state

Post by luke_titley » Wed Sep 21, 2016 6:06 am

I had a quick look through the source code and can see that there is still use of global variables. Does micropython have a GIL like CPython ?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Eliminate all global state

Post by deshipu » Wed Sep 21, 2016 6:49 am

Since MicroPython is single-threaded, it doesn't have a need for GIL. There is some ongoing work to add threads to it, and it may be necessary to add GIL with that (but I think so far it was avoided). However, I think MicroPython is not particularly easy to embed.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Eliminate all global state

Post by pythoncoder » Wed Sep 21, 2016 7:02 am

@luke_titley Have you read this? http://forum.micropython.org/viewtopic. ... lit=thread As I understand it the _thread() module is supported on the WiPy but it's early days and there have been reports of problems.
Peter Hinch
Index to my micropython libraries.

luke_titley
Posts: 10
Joined: Tue Sep 20, 2016 10:17 pm

Re: Eliminate all global state

Post by luke_titley » Wed Sep 21, 2016 7:16 am

Hey thanks for this. I'm mainly interested in having two completely independent instances of the interpreter running in separate threads managed by my application. Hence the questions about GIL and global variables. Probably the best bet is for me to trawl the forumn more and have a dig through the codebase. Thanks for the quick responses.

Sent from my HUAWEI VNS-L22 using Tapatalk

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Eliminate all global state

Post by dhylands » Wed Sep 21, 2016 4:42 pm

Most of the global state is stored in the _mp_state_vm_t struct
https://github.com/micropython/micropyt ... tate.h#L54
and the globals are retrieved using the MP_STATE_PORT macro.
https://github.com/micropython/micropyt ... #L222-L224
https://github.com/micropython/micropyt ... ort.h#L161

luke_titley
Posts: 10
Joined: Tue Sep 20, 2016 10:17 pm

Re: Eliminate all global state

Post by luke_titley » Wed Sep 21, 2016 7:11 pm

Hi Dave.
That's awesome that you already have it all in that 'mp_state_ctx' struct. git grep tells me there's a little under 300 references to the 'MP_STATE_CTX', 'MP_STATE_VM' and 'MP_STATE_MEM'. I think it's just a case of ripping those out and passing the mp_state_ctx through to all the functions that need it. Or am I missing something ?

luke_titley
Posts: 10
Joined: Tue Sep 20, 2016 10:17 pm

Re: Eliminate all global state

Post by luke_titley » Wed Sep 21, 2016 7:19 pm

Looks like you've also got this 'mp_dynamic_compiler', which I guess would need to be moved out too. And mp_module___main__ which I guess is the '__main__' module.
Last edited by luke_titley on Wed Sep 21, 2016 8:59 pm, edited 1 time in total.

luke_titley
Posts: 10
Joined: Tue Sep 20, 2016 10:17 pm

Re: Eliminate all global state

Post by luke_titley » Wed Sep 21, 2016 7:26 pm

Looks like you do have GIL as well, but if there can be one per interpreter then it's still fine.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Eliminate all global state

Post by dhylands » Wed Sep 21, 2016 10:04 pm

luke_titley wrote:Hi Dave.
That's awesome that you already have it all in that 'mp_state_ctx' struct. git grep tells me there's a little under 300 references to the 'MP_STATE_CTX', 'MP_STATE_VM' and 'MP_STATE_MEM'. I think it's just a case of ripping those out and passing the mp_state_ctx through to all the functions that need it. Or am I missing something ?
I would probably just change the macro to get the pointer to the state context thru TLS (thread local storage) or something similar.

Post Reply