2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 __threadLock Runner::m_lock;
16 __threadSignal Runner::m_awakeScilab;
17 __threadSignalLock Runner::m_awakeScilabLock;
23 __InitSignal(&m_awakeScilab);
24 __InitSignalLock(&m_awakeScilabLock);
27 void *Runner::launch(void *args)
29 bool bdoUnlock = false;
30 //try to lock locker ( waiting parent thread register me )
36 Runner *me = (Runner *)args;
39 me->getProgram()->accept(*(me->getVisitor()));
40 //ConfigVariable::clearLastError();
42 catch (const ast::ScilabException& se)
44 scilabErrorW(se.GetErrorMessage().c_str());
47 // reset error state when new prompt occurs
48 ConfigVariable::resetError();
50 __threadKey currentThreadKey = __GetCurrentThreadKey();
52 //change thread status
53 ThreadId* pThread = ConfigVariable::getThread(currentThreadKey);
54 if (pThread->getStatus() != ThreadId::Aborted)
56 pThread->setStatus(ThreadId::Done);
61 ConfigVariable::deleteThread(currentThreadKey);
72 void Runner::LockPrompt()
74 __LockSignal(&m_awakeScilabLock);
75 //free locker to release thread
77 __Wait(&m_awakeScilab, &m_awakeScilabLock);
78 __UnLockSignal(&m_awakeScilabLock);
81 void Runner::UnlockPrompt()
83 __LockSignal(&m_awakeScilabLock);
84 __Signal(&m_awakeScilab);
85 __UnLockSignal(&m_awakeScilabLock);
89 void Runner::execAndWait(ast::Exp* _theProgram, ast::ExecVisitor *_visitor)
93 Runner *runMe = new Runner(_theProgram, _visitor);
94 __threadKey threadKey;
101 //launch thread but is can't really start since locker is locked
102 __CreateThreadWithParams(&threadId, &threadKey, &Runner::launch, runMe);
103 runMe->setThreadId(threadId);
104 runMe->setThreadKey(threadKey);
107 ConfigVariable::addThread(new ThreadId(threadId, threadKey));
108 //free locker to release thread && wait and of thread execution
111 types::ThreadId* pExecThread = ConfigVariable::getThread(threadKey);
112 if (pExecThread == NULL)
114 //call pthread_join to clean stack allocation
115 __WaitThreadDie(threadId);
118 catch (const ast::ScilabException& se)
124 void Runner::exec(ast::Exp* _theProgram, ast::ExecVisitor *_visitor)
126 m_theProgram = _theProgram;
127 m_visitor = _visitor;
128 __CreateThreadWithParams(&m_threadId, &m_threadKey, &Runner::launch, this);