expand implicit list in cell, like c={1:3} -> c={[1 2 3]}
[scilab.git] / scilab / modules / core / src / cpp / runner.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
4  *
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
10  *
11  */
12
13 #include "runner.hxx"
14
15 __threadLock Runner::m_lock;
16 __threadSignal Runner::m_awakeScilab;
17 __threadSignalLock Runner::m_awakeScilabLock;
18
19 void Runner::init()
20 {
21     __InitSignal(&m_awakeScilab);
22     __InitSignalLock(&m_awakeScilabLock);
23 }
24
25 void *Runner::launch(void *args)
26 {
27     //try to lock locker ( waiting parent thread register me )
28     __Lock(&m_lock);
29     //just release locker
30     __UnLock(&m_lock);
31
32     //exec !
33     Runner *me = (Runner *)args;
34     try
35     {
36         me->getProgram()->accept(*(me->getVisitor()));
37         ConfigVariable::clearLastError();
38     }
39     catch(ScilabException se)
40     {
41         YaspErrorW(se.GetErrorMessage().c_str());
42     }
43
44
45
46     //change thread status
47     ThreadId* pThread = ConfigVariable::getThread(__GetCurrentThreadKey());
48     if(pThread->getStatus() != ThreadId::Aborted)
49     {
50         pThread->setStatus(ThreadId::Done);
51         UnlockPrompt();
52     }
53
54     //unregister thread
55     ConfigVariable::deleteThread(__GetCurrentThreadKey());
56
57
58     delete me;
59     return NULL;
60 }
61
62 void Runner::LockPrompt()
63 {
64     __LockSignal(&m_awakeScilabLock);
65     //free locker to release thread
66     __UnLock(&m_lock);
67     __Wait(&m_awakeScilab, &m_awakeScilabLock);
68     __UnLockSignal(&m_awakeScilabLock);
69 }
70
71 void Runner::UnlockPrompt()
72 {
73     __LockSignal(&m_awakeScilabLock);
74     __Signal(&m_awakeScilab);
75     __UnLockSignal(&m_awakeScilabLock);
76 }
77
78
79 void Runner::execAndWait(ast::Exp* _theProgram, ast::ExecVisitor *_visitor)
80 {
81     try
82     {
83         Runner *runMe = new Runner(_theProgram, _visitor);
84         __threadKey threadKey;
85         __threadId threadId;
86
87         //init locker
88         __InitLock(&m_lock);
89         //lock locker
90         __Lock(&m_lock);
91         //launch thread but is can't really start since locker is locked
92         __CreateThreadWithParams(&threadId, &threadKey, &Runner::launch, runMe);
93         runMe->setThreadId(threadId);
94         runMe->setThreadKey(threadKey);
95
96         //register thread
97         ConfigVariable::addThread(new ThreadId(threadId, threadKey));
98         //free locker to release thread && wait and of thread execution
99         LockPrompt();
100     }
101     catch(ScilabException se)
102     {
103         throw se;
104     }
105 }
106
107 void Runner::exec(ast::Exp* _theProgram, ast::ExecVisitor *_visitor)
108 {
109     m_theProgram = _theProgram;
110     m_visitor = _visitor;
111     __CreateThreadWithParams(&m_threadId, &m_threadKey, &Runner::launch, this);
112 }