5 ms·
Is it possible to run a program in 'steps,' between which the state is paused?
by pekk 14y ago
Is it possible to run a program in 'steps,' between which the state is paused?
- StavrosK 14y agoI don't think it's possible with the stock interpreter, but I'm guessing it's pretty simple to modify the interpreter to do it. Why do you ask?
- pekk 14y agoSimple time sharing between low-priority, potentially long-running programs submitted by untrusted users.
- StavrosK 14y agoRun a process per script and let the OS handle it, probably.
- gngeal 14y agoYou don't even need to do that, you can run Lua in a multithreaded program, as long as no lua_State is accessed from multiple threads. You can spawn independent interpreter states, have them do their job and terminate. The whole Lua codebase as such is completely stateless (apart from the state that you keep for your code). The shorter the scripts actually run, the smaller overhead you get by this.
- pekk 14y agoThat isn't what I asked about and isn't really helpful
- StavrosK 14y agoDon't read it, I guess? I don't know any other way to do it, unfortunately. I'm sorry I wasted your time.
- gngeal 14y agoWhy not? Isn't that the whole purpose of coroutines in Lua?
- ufo 14y agoYou can't yield if there is a C function lower down in the stack so I don't think you can always abuse the coroutine system to implement OS threads like that.
- gngeal 14y agoAre you sure about that? I thought that yields across C functions were added in Lua 5.2. Not to mention that in a controlled environment like the runtime that Wikipedia exposes to you, you can take care of avoiding exposing APIs that would allow people to code themselves into a corner like that (if the C calls are leaves, you're OK even in 5.1).
- ufo 14y agoYou are right that they could use a carefully crafted API to work around the problems. Its also true that yields across C were added in 5.2 but you need to code the C functions in continuation-passing-style using a special API to take advantage of that. http://www.lua.org/manual/5.2/manual.html#4.7 http://www.lua.org/manual/5.2/manual.html#4.7
- gngeal 14y agoWell, two other options for you are using the coco library with stock Lua 5.1.x, or simply using LuaJIT.
- fab13n 14y agoNo, quite the contrary: coroutines only yield explicitly, due to a call to coroutine.yield(). They let you control your scheduling deterministically (although multitasking is far from being their only use)
- fab13n 14y agoThe debug lib lets you hook behaviors on line changes, entering a function and leaving it, but nothing for single bytecodes iirc. The debug lib, allowing to take control of the whole program, is disabled in most sandboxed environments including wikipedia's. However, bytecode instruction and line changes both are poor metrics to measure time, cpu or any other kind of resource usage. Measuring process time is relatively easy and much more accurate.