summaryrefslogtreecommitdiff
path: root/modules/tclsci/src/c/TCL_Command.c
blob: 5a68b3c150cde289d240c3e1f630d09b9a448988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2007-2008 - INRIA - Bruno JOFRET
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#include <stdlib.h>

#ifdef _MSC_VER
#include <Windows.h>
#include "strdup_windows.h"
#define usleep(micro) Sleep(micro/1000)
#else
#include <unistd.h>
#endif

#include "MALLOC.h"
#include "TCL_Command.h"
#include "GlobalTclInterp.h"

// Globla Tcl Slave Name
char *			TclSlave;
// Global Tcl Command Buffer
char *			TclCommand;
// Global Tcl Script to Evaluate
char *			TclFile;
// Global Tcl Return Code.
int			TclInterpReturn;
// Global Tcl Return Result.
char *			TclInterpResult;

// Single execution
__threadLock		singleExecutionLock;

// Wake Up
__threadSignal		wakeUp;
__threadSignalLock	wakeUpLock;

// Work is done
__threadSignal		workIsDone;

// Launch command
__threadSignalLock	launchCommand;

// ***********************
// ** Enable LocalDebug **
// **********************
//#define __LOCAL_DEBUG__


static Tcl_Interp *LocalTCLinterp;

static BOOL FileEvaluationInProgress;
static BOOL CommandEvaluationInProgress;

extern BOOL TK_Started;

/*
** This function is a timer to periodicly wake
** up the loop in startTclLoop.
** Called within a thread.
*/
static void *sleepAndSignal(void* in)
{
    while (TK_Started)
    {
#ifdef __LOCAL_DEBUG__
        //printf(".");
        //fflush(NULL);
#endif
        usleep(TIME_TO_SLEEP);
        __LockSignal(&wakeUpLock);
        __Signal(&wakeUp);
        __UnLockSignal(&wakeUpLock);
    }
    return NULL;
}

/*
** Evaluate the TclCommand inside Global interp
*/
static void evaluateTclCommand()
{
#ifdef __LOCAL_DEBUG__
    printf("[TCL Daemon] Eval : %s\n", TclCommand);
    fflush(NULL);
#endif
    //CommandEvaluationInProgress = TRUE;
    TclInterpReturn = Tcl_Eval(LocalTCLinterp, TclCommand);
    //CommandEvaluationInProgress = FALSE;
    FREE(TclCommand);
    TclCommand = NULL;
}

/*
** Evaluate the TclCommand inside Global interp
*/
static void evaluateTclFile()
{
#ifdef __LOCAL_DEBUG__
    printf("[TCL Daemon] Load : %s\n", TclFile);
    fflush(NULL);
#endif
    FileEvaluationInProgress = TRUE;
    TclInterpReturn = Tcl_EvalFile(LocalTCLinterp, TclFile);
    FileEvaluationInProgress = FALSE;
    FREE(TclFile);
    TclFile = NULL;
}

/*
** This function start an endless Tcl Loop
** in order the Scilab Global Tcl Interpreter
** do some "update" and let the Tcl Applications live.
*/
void startTclLoop()
{
    __threadId sleepThreadId;

    __InitLock(&singleExecutionLock);

    __InitSignal(&wakeUp);
    __InitSignalLock(&wakeUpLock);
    __InitSignal(&workIsDone);
    __InitSignalLock(&launchCommand);

    __CreateThread(&sleepThreadId, sleepAndSignal);

    __LockSignal(&InterpReadyLock);
    __Signal(&InterpReady);
    __UnLockSignal(&InterpReadyLock);
    /*
    ** TCL Event Loop : Threaded
    */
    while (TK_Started)
    {
#ifdef __LOCAL_DEBUG__
        //printf(".");
        //fflush(NULL);
#endif
        /*
        ** -= IF =-
        ** there is a command to run
        ** RUN IT.
        **
        ** -= or =-
        ** there is a script to load
        ** LOAD IT.
        */
        if (TclCommand != NULL || TclFile != NULL)
        {
#ifdef __LOCAL_DEBUG__
            printf("[LOCK] launch Command\n");
            fflush(NULL);
#endif
            __LockSignal(&launchCommand);

            /* Reinit local interpreter,
               default is the biggest one. */
            LocalTCLinterp = getTclInterp();

            /* Check if it's supposed to be run in root or slave */
            if (TclSlave != NULL)
            {
                LocalTCLinterp = Tcl_GetSlave(LocalTCLinterp, TclSlave);
                releaseTclInterp();
                FREE(TclSlave);
                TclSlave = NULL;
            }
            /*
            ** Do the evaluation
            */
            if (TclCommand != NULL)
            {
                evaluateTclCommand();
            }
            /*
            ** Do the evaluation
            */
            else if (TclFile != NULL)
            {
                evaluateTclFile();
            }
            /* Update return value and result */
            if (Tcl_GetStringResult(LocalTCLinterp) && strlen(Tcl_GetStringResult(LocalTCLinterp)) != 0)
            {
                TclInterpResult = strdup(Tcl_GetStringResult(LocalTCLinterp));
            }
            else
            {
                TclInterpResult = NULL;
            }
#ifdef __LOCAL_DEBUG__
            printf("[SIGNAL] executionDone\n");
            fflush(NULL);
#endif
            releaseTclInterp();
            Tcl_Eval(getTclInterp(), "update");
            releaseTclInterp();
            __Signal(&workIsDone);
            __UnLockSignal(&launchCommand);
        }
        /*
        ** -= ELSE =-
        ** there is no command
        ** do an update and wait to be waked up.
        */
        else
        {
            __LockSignal(&wakeUpLock);
            Tcl_Eval(getTclInterp(), "update");
            releaseTclInterp();
#ifdef __LOCAL_DEBUG__
            //printf("[TCL Daemon] Wait\n");
            //fflush(NULL);
#endif
            __Wait(&wakeUp, &wakeUpLock);
            __UnLockSignal(&wakeUpLock);
        }
    }
    /* TK is stopped... Kill interpreter. */
    deleteTclInterp();

}

/*
** Send Tcl_EvalFile
** Ask the interpreter to execute some command
** by send a SIGNAL to the other thread.
*/
int sendTclFileToSlave(char* file, char* slave)
{
#ifdef __LOCAL_DEBUG__
    printf("[TCL %s SendFile] File : %s\n", slave, file);
    fflush(NULL);
#endif
    __Lock(&singleExecutionLock);
    {
        __LockSignal(&launchCommand);
        TclFile = strdup(file);
        if (slave != NULL)
        {
            TclSlave = strdup(slave);
        }
        else
        {
            TclSlave = NULL;
        }
        __LockSignal(&wakeUpLock);
        __Signal(&wakeUp);
        __UnLockSignal(&wakeUpLock);
#ifdef __LOCAL_DEBUG__
        printf("[TCL Main] Wait EXECUTION DONE\n");
        fflush(NULL);
#endif
        __Wait(&workIsDone, &launchCommand);
#ifdef __LOCAL_DEBUG__
        printf("[TCL SendFile] DONE\n");
        fflush(NULL);
#endif
        __UnLockSignal(&launchCommand);
    }
    __UnLock(&singleExecutionLock);
    return getTclCommandReturn();
}

/*
** Send Tcl Command To Slave
** Ask the interpreter to execute some command
** by send a SIGNAL to the other thread.
*/
int sendTclCommandToSlave(char* command, char* slave)
{
    /*
    ** Shortcut for evaluation if there is some file evaluation
    ** in progress.
    */
    if (!FileEvaluationInProgress && !CommandEvaluationInProgress)
    {
#ifdef __LOCAL_DEBUG__
        printf("[TCL %s Send] Eval : %s\n", slave, command);
        fflush(NULL);
#endif

        CommandEvaluationInProgress = TRUE;
        __Lock(&singleExecutionLock);

        __LockSignal(&launchCommand);
        TclCommand = strdup(command);
        if (slave != NULL)
        {
            TclSlave = strdup(slave);
        }
        else
        {
            TclSlave = NULL;
        }
        __LockSignal(&wakeUpLock);
        __Signal(&wakeUp);
        __UnLockSignal(&wakeUpLock);

#ifdef __LOCAL_DEBUG__
        printf("[TCL Main]Wait EXECUTION DONE\n");
        fflush(NULL);
#endif
        __Wait(&workIsDone, &launchCommand);
#ifdef __LOCAL_DEBUG__
        printf("[TCL Send] DONE\n");
        fflush(NULL);
#endif
        __UnLockSignal(&launchCommand);
        __UnLock(&singleExecutionLock);
        CommandEvaluationInProgress = FALSE;
        return getTclCommandReturn();
    }
    else
    {
#ifdef __LOCAL_DEBUG__
        printf("[TCL %s Send] Eval : %s -- Shortcut\n", slave, command);
        fflush(NULL);
#endif
        /*
        ** File Evaluation in progress
        */
        TclCommand = strdup(command);
        if (slave != NULL)
        {
            TclSlave = strdup(slave);
        }
        else
        {
            TclSlave = NULL;
        }
        evaluateTclCommand();
    }
    return TCL_OK;
}


/*
** Get Tcl Command Return Value
** Propagate return value of Tcl Interpreter
** caught in thread.
*/
int getTclCommandReturn(void)
{
    int commandResult = TclInterpReturn;

#ifdef __LOCAL_DEBUG__
    printf("[TCL getTclCommandReturn]\n");
    fflush(NULL);
#endif

    TclInterpReturn = 0;
    return commandResult;
}

/*
** Get Tcl Command Result
** Propagate return message of Tcl Interpreter
** caught in thread.
** This can be an error or a string value.
*/
char *getTclCommandResult(void)
{
#ifdef __LOCAL_DEBUG__
    printf("[TCL getTclCommandResult]\n");
    fflush(NULL);
#endif
    if (TclInterpResult != NULL)
    {
        char *result = strdup(TclInterpResult);
        TclInterpResult = NULL;
        return result;
    }
    return strdup("\0");
}