| ︙ | | | ︙ | |
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
*
* Copyright (c) 1998 by Sun Microsystems, Inc.
* Copyright (c) 2006-2008 by Joe Mistachkin. All rights reserved.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclThreadTest.c,v 1.16.4.16 2009/11/25 16:20:12 dgp Exp $
*/
#ifndef USE_TCL_STUBS
# define USE_TCL_STUBS
#endif
#include "tclInt.h"
|
|
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
*
* Copyright (c) 1998 by Sun Microsystems, Inc.
* Copyright (c) 2006-2008 by Joe Mistachkin. All rights reserved.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclThreadTest.c,v 1.16.4.17 2010/12/01 15:27:27 dgp Exp $
*/
#ifndef USE_TCL_STUBS
# define USE_TCL_STUBS
#endif
#include "tclInt.h"
|
| ︙ | | | ︙ | |
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
Tcl_Interp *interp) /* The current Tcl interpreter */
{
/*
* If the main thread Id has not been set, do it now.
*/
Tcl_MutexLock(&threadMutex);
if ((long) mainThreadId == 0) {
mainThreadId = Tcl_GetCurrentThread();
}
Tcl_MutexUnlock(&threadMutex);
Tcl_CreateObjCommand(interp, "testthread", ThreadObjCmd, NULL, NULL);
return TCL_OK;
}
|
|
|
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
Tcl_Interp *interp) /* The current Tcl interpreter */
{
/*
* If the main thread Id has not been set, do it now.
*/
Tcl_MutexLock(&threadMutex);
if (mainThreadId == 0) {
mainThreadId = Tcl_GetCurrentThread();
}
Tcl_MutexUnlock(&threadMutex);
Tcl_CreateObjCommand(interp, "testthread", ThreadObjCmd, NULL, NULL);
return TCL_OK;
}
|
| ︙ | | | ︙ | |
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
}
arg++;
if (arg < objc) {
result = Tcl_GetString(objv[arg]);
} else {
result = NULL;
}
return ThreadCancel(interp, (Tcl_ThreadId) id, result, flags);
}
case THREAD_CREATE: {
const char *script;
int joinable, len;
if (objc == 2) {
/*
|
|
|
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
}
arg++;
if (arg < objc) {
result = Tcl_GetString(objv[arg]);
} else {
result = NULL;
}
return ThreadCancel(interp, (Tcl_ThreadId) (size_t) id, result, flags);
}
case THREAD_CREATE: {
const char *script;
int joinable, len;
if (objc == 2) {
/*
|
| ︙ | | | ︙ | |
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
Tcl_Obj *idObj;
/*
* Check if they want the main thread id or the current thread id.
*/
if (objc == 2) {
idObj = Tcl_NewLongObj((long) Tcl_GetCurrentThread());
} else if (objc == 3
&& strcmp("-main", Tcl_GetString(objv[2])) == 0) {
Tcl_MutexLock(&threadMutex);
idObj = Tcl_NewLongObj((long) mainThreadId);
Tcl_MutexUnlock(&threadMutex);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
Tcl_SetObjResult(interp, idObj);
|
|
|
|
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
Tcl_Obj *idObj;
/*
* Check if they want the main thread id or the current thread id.
*/
if (objc == 2) {
idObj = Tcl_NewLongObj((long)(size_t)Tcl_GetCurrentThread());
} else if (objc == 3
&& strcmp("-main", Tcl_GetString(objv[2])) == 0) {
Tcl_MutexLock(&threadMutex);
idObj = Tcl_NewLongObj((long)(size_t)mainThreadId);
Tcl_MutexUnlock(&threadMutex);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
Tcl_SetObjResult(interp, idObj);
|
| ︙ | | | ︙ | |
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
Tcl_WrongNumArgs(interp, 2, objv, "id");
return TCL_ERROR;
}
if (Tcl_GetLongFromObj(interp, objv[2], &id) != TCL_OK) {
return TCL_ERROR;
}
result = Tcl_JoinThread((Tcl_ThreadId) id, &status);
if (result == TCL_OK) {
Tcl_SetIntObj(Tcl_GetObjResult(interp), status);
} else {
char buf[20];
sprintf(buf, "%ld", id);
Tcl_AppendResult(interp, "cannot join thread ", buf, NULL);
}
return result;
}
case THREAD_NAMES:
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
|
|
|
|
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
Tcl_WrongNumArgs(interp, 2, objv, "id");
return TCL_ERROR;
}
if (Tcl_GetLongFromObj(interp, objv[2], &id) != TCL_OK) {
return TCL_ERROR;
}
result = Tcl_JoinThread((Tcl_ThreadId)(size_t)id, &status);
if (result == TCL_OK) {
Tcl_SetIntObj(Tcl_GetObjResult(interp), status);
} else {
char buf[20];
TclFormatInt(buf, id);
Tcl_AppendResult(interp, "cannot join thread ", buf, NULL);
}
return result;
}
case THREAD_NAMES:
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
|
| ︙ | | | ︙ | |
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
arg = 2;
}
if (Tcl_GetLongFromObj(interp, objv[arg], &id) != TCL_OK) {
return TCL_ERROR;
}
arg++;
script = Tcl_GetString(objv[arg]);
return ThreadSend(interp, (Tcl_ThreadId) id, script, wait);
}
case THREAD_EVENT: {
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(
|
|
|
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
arg = 2;
}
if (Tcl_GetLongFromObj(interp, objv[arg], &id) != TCL_OK) {
return TCL_ERROR;
}
arg++;
script = Tcl_GetString(objv[arg]);
return ThreadSend(interp, (Tcl_ThreadId)(size_t)id, script, wait);
}
case THREAD_EVENT: {
if (objc > 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(
|
| ︙ | | | ︙ | |
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
/*
* Wait for the thread to start because it is using something on our stack!
*/
Tcl_ConditionWait(&ctrl.condWait, &threadMutex, NULL);
Tcl_MutexUnlock(&threadMutex);
Tcl_ConditionFinalize(&ctrl.condWait);
Tcl_SetObjResult(interp, Tcl_NewLongObj((long)id));
return TCL_OK;
}
/*
*------------------------------------------------------------------------
*
* NewTestThread --
|
|
|
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
/*
* Wait for the thread to start because it is using something on our stack!
*/
Tcl_ConditionWait(&ctrl.condWait, &threadMutex, NULL);
Tcl_MutexUnlock(&threadMutex);
Tcl_ConditionFinalize(&ctrl.condWait);
Tcl_SetObjResult(interp, Tcl_NewLongObj((long)(size_t)id));
return TCL_OK;
}
/*
*------------------------------------------------------------------------
*
* NewTestThread --
|
| ︙ | | | ︙ | |
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
Tcl_Interp *interp) /* Interp that failed */
{
Tcl_Channel errChannel;
const char *errorInfo, *argv[3];
char *script;
char buf[TCL_DOUBLE_SPACE+1];
sprintf(buf, "%ld", (long) Tcl_GetCurrentThread());
errorInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
if (errorProcString == NULL) {
errChannel = Tcl_GetStdChannel(TCL_STDERR);
Tcl_WriteChars(errChannel, "Error from thread ", -1);
Tcl_WriteChars(errChannel, buf, -1);
Tcl_WriteChars(errChannel, "\n", 1);
|
|
|
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
Tcl_Interp *interp) /* Interp that failed */
{
Tcl_Channel errChannel;
const char *errorInfo, *argv[3];
char *script;
char buf[TCL_DOUBLE_SPACE+1];
TclFormatInt(buf, (size_t) Tcl_GetCurrentThread());
errorInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
if (errorProcString == NULL) {
errChannel = Tcl_GetStdChannel(TCL_STDERR);
Tcl_WriteChars(errChannel, "Error from thread ", -1);
Tcl_WriteChars(errChannel, buf, -1);
Tcl_WriteChars(errChannel, "\n", 1);
|
| ︙ | | | ︙ | |
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
ThreadSpecificData *tsdPtr;
Tcl_Obj *listPtr;
listPtr = Tcl_NewListObj(0, NULL);
Tcl_MutexLock(&threadMutex);
for (tsdPtr = threadList ; tsdPtr ; tsdPtr = tsdPtr->nextPtr) {
Tcl_ListObjAppendElement(interp, listPtr,
Tcl_NewLongObj((long) tsdPtr->threadId));
}
Tcl_MutexUnlock(&threadMutex);
Tcl_SetObjResult(interp, listPtr);
return TCL_OK;
}
/*
|
|
|
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
ThreadSpecificData *tsdPtr;
Tcl_Obj *listPtr;
listPtr = Tcl_NewListObj(0, NULL);
Tcl_MutexLock(&threadMutex);
for (tsdPtr = threadList ; tsdPtr ; tsdPtr = tsdPtr->nextPtr) {
Tcl_ListObjAppendElement(interp, listPtr,
Tcl_NewLongObj((long)(size_t)tsdPtr->threadId));
}
Tcl_MutexUnlock(&threadMutex);
Tcl_SetObjResult(interp, listPtr);
return TCL_OK;
}
/*
|
| ︙ | | | ︙ | |