FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error creating several MDThreads

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
vektorboson



Joined: 14 Sep 2006
Posts: 44

PostPosted: Mon Sep 27, 2010 2:27 pm    Post subject: Error creating several MDThreads Reply with quote

After succeeding with the Scheduler I wanted to test it, and I found another problem; I've got a function called 'asyncExec' that creates a new MDThread made from either a MiniD function or a script file and pushes it onto the Scheduler-queue. It looks like this (I took the unimportant parts out):
Code:

uword asyncExec(MDThread* t, uword numParams) {
  ...

   try {
     scope c = new Compiler(t);
     auto slot = c.compileStatements(buf, "<loaded by asyncExec>");

     auto newThreadSlot = newThread(t, slot);
     auto nt = getThread(t, newThreadSlot);
     pop(t, 2);
     setPreemptable(nt, true, MD_THREAD_PREEMPT_DELAY);
     Application.instance.scheduler.pushThread(0.0, nt);
   } catch(MDException mde) {
      catchException(t);               
      pop(t);
      Application.instance.error.text = mde.toString();
   }

   ...

   return 0;
}


Now I wanted to test asyncExec by calling it 50 times by a MiniD-script that was itself called by asyncExec.
It worked for exactly 16 scripts, but the 17th call of asyncExec failed at minid/interpreter.d in the popAR-function:

Code:

void popAR(MDThread* t)
{
   t.arIndex--;
   t.currentAR.func = null;
   t.currentAR.proto = null;

   if(t.arIndex > 0)
   {
      t.currentAR = &t.actRecs[t.arIndex - 1];
      t.stackBase = t.currentAR.base;
   }
   else
   {
      t.currentAR = null;
      t.stackBase = 0;
   }
}


It failed at the following line with an out of bounds exception:
Code:
t.currentAR = &t.actRecs[t.arIndex - 1];


I'll try to get a traced exception later.

EDIT:
I've got the exception trace; funny enough, the asyncExec is Ok; the problem is probably somewhere in the wakeup-function of my Scheduler:
Code:

void wakeupThreads(float dt) {
            float diff(float f) {
                return f - dt;
            }

            _queue.map(&diff);

            while(_queue.ready(0.001)) {
                auto t = _queue.pop(); // t is a MDThread*
                auto idx = pushThread(_context, t); // _context is a MDThread* too
                pushNull(_context);

                int ret;
                try {
                    ret = rawCall(_context, idx, 1);
                } catch(MDException mde) {
                    catchException(_context);
                    pop(_context);
                    Application.instance.error.text = mde.toString();
                }

                auto s = state(t);

                if(s == MDThread.State.Suspended) {
                    if(ret == 1 && isNum(_context, 1)) {
                        auto f = getNum(_context, 1);
                        _queue.push(f, t);
                    } else {
                        _queue.push(0.02, t);
                    }
                    pop(_context, ret);
                } else if(s == MDThread.State.Dead) {
                    SaveVM(t); // this is for recycling (not used now)
                }
            }
        }


And here's the exception trace:
Code:

...
tango.core.Exception.ArrayBoundsException._ctor
onArrayBoundsError
_d_array_bounds
_D5minid11interpreter7__arrayZ
minid.interpreter.popAR
minid.interpreter.callEpilogue
minid.interpreter.callPrologue2
minid.interpreter.callPrologue
minid.interpreter.execute
minid.interpreter.resume
minid.interpreter.callPrologue
minid.interpreter.rawCall
atlas.script.Scheduler.Scheduler.wakeupThreads
...
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Mon Sep 27, 2010 6:45 pm    Post subject: Reply with quote

My best guess at this point is that the threads that you're creating are being GCed by the MiniD GC without your knowledge. I see you create the new thread in asyncExec, but then pop it off the stack.. without any references to that thread on the stack or in the globals, it'll be collected. Then, when you try to wake up the thread, it's pointing to god-knows-what and weird things happen.

Try keeping the threads in some data structure on the MiniD heap.
Back to top
View user's profile Send private message Visit poster's website
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Sat Oct 09, 2010 1:16 pm    Post subject: Reply with quote

Boop?
Back to top
View user's profile Send private message Visit poster's website
vektorboson



Joined: 14 Sep 2006
Posts: 44

PostPosted: Sat Oct 16, 2010 11:51 am    Post subject: Reply with quote

JarrettBillingsley wrote:
Boop?

Sorry for the late reply; for a few reasons I started to port my project to C++. The reasons are basically the same as in the Open Morrowind-project, so it's got nothing to do with MiniD. Thanks for your support anyway!
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Sat Oct 16, 2010 12:07 pm    Post subject: Reply with quote

http://www.youtube.com/watch?v=GFi9Gd1qSkE#t=1m14s

Crying or Very sad
Back to top
View user's profile Send private message Visit poster's website
vektorboson



Joined: 14 Sep 2006
Posts: 44

PostPosted: Tue Oct 19, 2010 11:22 am    Post subject: Reply with quote

JarrettBillingsley wrote:
http://www.youtube.com/watch?v=GFi9Gd1qSkE#t=1m14s

Crying or Very sad


I'm really sorry! I just grew tired of dealing with D toolset-specific problems, especially the linker.
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Tue Oct 19, 2010 4:53 pm    Post subject: Reply with quote

Oh, you're not alone there. Razz
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group