GameMonkey Script

GameMonkey Script Forums
It is currently Sun Jan 20, 2019 11:25 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: GM With Microthreads
PostPosted: Thu Apr 29, 2010 7:41 pm 
Offline

Joined: Fri Apr 02, 2010 10:36 pm
Posts: 14
Here's a version of GM I modified to work with microthreads. With this, native functions called from GM script can use all of the GM style threading operations. If you yield, block or sleep from within native code called from GM, when the GM thread that made the call next wakes, execution will resume in the next line of native code.

Included is a small test harness that I believe exercises all of the features.

The only GM files that are modified are gmMachine, gmFunctionobject and gmThread. The bulk of the work is done in gmThread::PushStackFrame.

Here's an example of what a native routine called from GM would look like:
Code:
int GM_CDECL TestNativeRoutine(gmThread* a_thread)
{
    //
    // This is a fairly contrived example, just incrementing a counter 10 times.
    //
    GM_CHECK_MICROTHREAD("TestNativeRoutine");
    GM_CHECK_NUM_PARAMS(0);

    printf("TestNativeRoutine Starting\n");
    int loopCount = 0;
    for(int i=0;i<10;i++)
    {
        loopCount++;
        MicroThread::YieldThread();
    }
    printf("TestNativeRoutine Finishing %d Yields()\n",loopCount);  //Note, local variables are preserved across Yields
    return GM_OK;
}

This code would execute over the course of 10 gmMachine executes().

The MicroThread code is a slightly modified version of the microthreads presented in Game Programming Gems 2. The microthread code in the sample will only work on the PC. There are a handful of platform specific modifications that would need to be made for other platforms (mostly saving and restoring the proper registers).


Attachments:
GameMonkeyMicroThreads.zip [205.13 KiB]
Downloaded 244 times
Top
 Profile  
Reply with quote  
 Post subject: Re: GM With Microthreads
PostPosted: Wed May 05, 2010 6:58 pm 
Offline

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
I've been following the thread on this with great interest. Thanks for this code, I'll have a look over it and play around - it sounds like an excellent addition.

Am I right in thinking that this is what Lua's C-Coroutine libraries are also doing?


Top
 Profile  
Reply with quote  
 Post subject: Re: GM With Microthreads
PostPosted: Thu May 13, 2010 11:46 pm 
Offline

Joined: Fri Apr 02, 2010 10:36 pm
Posts: 14
downgraded wrote:
Am I right in thinking that this is what Lua's C-Coroutine libraries are also doing?

http://coco.luajit.org/ looks close to the same method.


Top
 Profile  
Reply with quote  
 Post subject: Re: GM With Microthreads
PostPosted: Wed May 19, 2010 2:34 am 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
This is awesome!


Top
 Profile  
Reply with quote  
 Post subject: Re: GM With Microthreads
PostPosted: Fri May 21, 2010 5:17 am 
Offline

Joined: Tue Nov 11, 2008 9:03 am
Posts: 20
Looks excellent!
Could you post it as a patch from your base vanilla GM tree?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group