🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Limit execution time?

Started by
8 comments, last by Rain Dog 19 years, 1 month ago
Hi, I was wondering if there was a good way to limit execution times on scripts? I have an game I'm starting in on where I'll be calling many scripts, some of which might take a long time to return. I don't mind if it takes 10,000 calls to Execute() allow any given script to finish, but a script CAN'T take up more than X ms (or whatever) of CPU time in one shot as I need the system as a whole to stay responsive. I've currently hacked up a version of Execute() that I call ExecuteSteps (int steps) that that calls ExecuteNext () "steps" times. Is there a better way to do this? - Andrew
Advertisement
Threads?
Quote: Original post by Jan-Lieuwe
Threads?


That will let me run multiple scripts concurrently, but one of the threads could take up 100% of my CPU, which would be really bad. Or possibly run forever and keep sucking up resources, although I guess you could stop that by stopping the thread.

I'm planning on allowing outsiders to write scripts, which is why this is an issue for me.

- Andrew
Use threads and make a simple scheduler for them.
Quote: Original post by Rain Dog
Use threads and make a simple scheduler for them.


Are you talking about threads on the host OS or within Angelscript? Can you point me at some information please? A quick google came up with nothing that looked relevant for either.

Any solution needs to be portable across platforms.

Thanks.

- Andrew
Posix threads or PTHREADS are cross platform i believe.

But OS threads.

Look on codeproject for a simple thread pool class
Quote: Original post by Rain Dog
Posix threads or PTHREADS are cross platform i believe.

But OS threads.

Look on codeproject for a simple thread pool class


Yes, pthreads are cross-platform, I'm arealdy using them.

Maybe I'm being dense here, but I don't see how thread pooling solves my problem. If I have one script in there in an infite busy loop I've still got a problem as it will take up a large chunk of my total CPU power, which is exactly what I want to avoid.

What I need to be able to do is say that any script can only take up X% of the CPU, or something approximating that, so no script can dominate things and hold everything else up. I don't care so much if the script never finishes, as long as it doesn't take up too many resources. My current solution does that by just letting each script only execute so many instructions per "ExecuteSteps" call, but I just think it's a little ugly and a hack.

Am I missing something here?

- Andrew
The easiest way (in my opinion) is to register the line callback, and have it call the context's Suspend() method when the context is no longer allowed to continue.

void LineCallback(asIScriptContext *ctx, void *param){   // Determine if the time is up   ...   if( time_is_up )   {      ctx->Suspend();   }}ctx->SetLineCallback(asFUNCTION(LineCallback), 0, asCALL_CDECL);


AngelScript doesn't have official support for multithreading (though I have done some work to prepare it). If can do without it I recommend it, or at least call all script contexts from only one thread.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Quote: Original post by WitchLord
The easiest way (in my opinion) is to register the line callback, and have it call the context's Suspend() method when the context is no longer allowed to continue.


Great! I think this is what I was looking for.

Thanks.

- Andrew
By thread pooling, you could also have thread management IE:

if( thread.executiontime > MaxexecutionTime)
{
thread.Suspend();
}


This topic is closed to new replies.

Advertisement