f38a0dcc91dde9c451590514d25592aca2d22cdb
[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         YaspWriteW(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     __Wait(&m_awakeScilab, &m_awakeScilabLock);
66     __UnLockSignal(&m_awakeScilabLock);
67 }
68
69 void Runner::UnlockPrompt()
70 {
71     __LockSignal(&m_awakeScilabLock);
72     __Signal(&m_awakeScilab);
73     __UnLockSignal(&m_awakeScilabLock);
74 }
75
76
77 void Runner::execAndWait(ast::Exp* _theProgram, ast::ExecVisitor *_visitor)
78 {
79     try
80     {
81         Runner *runMe = new Runner(_theProgram, _visitor);
82         __threadKey threadKey;
83         __threadId threadId;
84
85         //init locker
86         __InitLock(&m_lock);
87         //lock locker
88         __Lock(&m_lock);
89         //launch thread but is can't really start since locker is locked
90         __CreateThreadWithParams(&threadId, &threadKey, &Runner::launch, runMe);
91         runMe->setThreadId(threadId);
92         runMe->setThreadKey(threadKey);
93
94         //register thread
95         ConfigVariable::addThread(new ThreadId(threadId, threadKey));
96         //free locker to release thread
97         __UnLock(&m_lock);
98         //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 }