2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
15 /*--------------------------------------------------------------------------*/
18 #include "storeCommand.h"
23 #include "scilabWrite.hxx"
24 #include "scilabexception.hxx"
25 #include "localization.hxx"
27 #include "threadmanagement.hxx"
29 /*--------------------------------------------------------------------------*/
31 * Command queue functions
32 * This function is used to store Scilab command in a queue
34 * PUBLIC : int StoreCommand( char *command)
35 * int StoreConsoleCommand(char *command)
36 * int StorePrioritaryCommand(char *command)
38 * int C2F(getmen)(char * btn_cmd,int * lb, int * entry)
40 /*--------------------------------------------------------------------------*/
43 char* m_command; /* command info one string two integers */
44 int m_isInterruptible; /* 1 if the command execution can be interrupted */
45 int m_isPrioritary; /* 1 if the command is prioritary */
46 command_origin_t m_iCommandOrigin; /* Indicate who have stored the command (ie: console, tcl) */
47 CommandRec(char* command, int isInterruptible, int isPrioritary, command_origin_t iCmdOrigin) :
48 m_command(command), m_isInterruptible(isInterruptible), m_isPrioritary(isPrioritary), m_iCommandOrigin(iCmdOrigin) {}
50 /*--------------------------------------------------------------------------*/
51 static std::list<CommandRec> commandQueue;
52 static std::list<CommandRec> commandQueuePrioritary;
53 /*--------------------------------------------------------------------------*/
54 int StoreCommandWithFlags(const char* command, int iPrioritary, int iInterruptible, command_origin_t iCmdOrigin)
56 ThreadManagement::LockStoreCommand();
59 commandQueuePrioritary.emplace_back(os_strdup(command), iPrioritary, iInterruptible, iCmdOrigin);
61 // Awake Runner to execute this prioritary command
62 ThreadManagement::SendAwakeRunnerSignal();
66 commandQueue.emplace_back(os_strdup(command), iPrioritary, iInterruptible, iCmdOrigin);
69 ThreadManagement::UnlockStoreCommand();
70 // Awake Scilab to execute a new command
71 ThreadManagement::SendCommandStoredSignal();
76 int StoreCommand(const char *command)
78 ThreadManagement::LockStoreCommand();
79 commandQueue.emplace_back(os_strdup(command),
81 /* is interruptible*/ 1,
82 /* cmd origin */ NONE);
84 ThreadManagement::UnlockStoreCommand();
85 // Awake Scilab to execute a new command
86 ThreadManagement::SendCommandStoredSignal();
91 int StoreConsoleCommand(const char *command, int iWaitFor)
93 ThreadManagement::LockStoreCommand();
94 commandQueuePrioritary.emplace_back(os_strdup(command),
96 /* is interruptible*/ 1,
97 /* cmd origin */ CONSOLE);
99 // Awake Scilab to execute a new command
100 ThreadManagement::SendCommandStoredSignal();
101 // Awake Runner to execute this prioritary command
102 ThreadManagement::SendAwakeRunnerSignal();
106 // make this wait before unlock the Store Command will prevent
107 // dead lock in case where another thread get this command
108 // and execute it before this thread is waiting for.
109 ThreadManagement::WaitForConsoleExecDoneSignal();
113 ThreadManagement::UnlockStoreCommand();
119 int StoreDebuggerCommand(const char *command)
121 ThreadManagement::LockStoreCommand();
122 commandQueuePrioritary.emplace_back(os_strdup(command),
124 /* is interruptible*/ 1,
125 /* cmd origin */ DEBUGGER);
127 // Awake Scilab to execute a new command
128 ThreadManagement::SendCommandStoredSignal();
129 // Awake Runner to execute this prioritary command
130 ThreadManagement::SendAwakeRunnerSignal();
132 // make this wait before unlock the Store Command will prevent
133 // dead lock in case where another thread get this command
134 // and execute it before this thread is waiting for.
135 ThreadManagement::WaitForDebuggerExecDoneSignal(false);
140 int StorePrioritaryCommand(const char *command)
142 ThreadManagement::LockStoreCommand();
143 commandQueuePrioritary.emplace_back(os_strdup(command),
145 /* is interruptible*/ 0,
146 /* cmd origin */ NONE);
148 // Awake Scilab to execute a new command
149 ThreadManagement::SendCommandStoredSignal();
150 // Awake Runner to execute this prioritary command
151 ThreadManagement::SendAwakeRunnerSignal();
153 ThreadManagement::UnlockStoreCommand();
158 int isEmptyCommandQueue(void)
160 return (commandQueuePrioritary.empty() && commandQueue.empty());
164 * Gets the next command to execute
165 * and remove it from the queue
167 int GetCommand(char** cmd, int* piPrioritary, int* piInterruptible, command_origin_t* piCmdOrigin)
169 int iCommandReturned = 0;
171 ThreadManagement::LockStoreCommand();
172 if (commandQueuePrioritary.empty() == false)
174 *cmd = os_strdup(commandQueuePrioritary.front().m_command);
175 *piInterruptible = commandQueuePrioritary.front().m_isInterruptible;
176 *piPrioritary = commandQueuePrioritary.front().m_isPrioritary;
177 *piCmdOrigin = commandQueuePrioritary.front().m_iCommandOrigin;
179 FREE (commandQueuePrioritary.front().m_command);
180 commandQueuePrioritary.pop_front();
182 iCommandReturned = 1;
184 else if (commandQueue.empty() == false)
186 *cmd = os_strdup(commandQueue.front().m_command);
187 *piInterruptible = commandQueue.front().m_isInterruptible;
188 *piPrioritary = commandQueue.front().m_isPrioritary;
189 *piCmdOrigin = commandQueue.front().m_iCommandOrigin;
191 FREE (commandQueue.front().m_command);
192 commandQueue.pop_front();
194 iCommandReturned = 1;
196 ThreadManagement::UnlockStoreCommand();
198 return iCommandReturned;
201 /*--------------------------------------------------------------------------*/
204 //#pragma message("WARNING : ismenu is deprecated. It will be removed _BEFORE_ Scilab 6.0.")
205 // FIXME : Do not forget to remove me.
208 /*--------------------------------------------------------------------------*/