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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
|
/*
* threadSpCmd.c --
*
* This file implements commands for script-level access to thread
* synchronization primitives. Currently, the mutex and condition
* variable objects are exposed to the script programmer.
*
* Additionaly, a locked eval is also implemented. This is a practical
* convenience function which relieves the programmer from the need
* to take care about unlocking some mutex after evaluating a protected
* part of code.
*
* Copyright (c) 2002 by Zoran Vasiljevic.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: threadSpCmd.c,v 1.15 2003/05/31 10:46:00 vasiljevic Exp $
* ----------------------------------------------------------------------------
*/
#include "tclThread.h"
/*
* This global (static) data is used to map opaque Tcl-level handles
* to pointers of their corresponding synchronization objects.
*/
static Tcl_HashTable syncHandles; /* Handles/pointers to sync objects */
static Tcl_Mutex syncHandlesMutex; /* Protects the above table */
static unsigned int syncHandlesCounter; /* Counter for unique object id's */
#define GRAB_SYNCMUTEX Tcl_MutexLock(&syncHandlesMutex)
#define RELEASE_SYNCMUTEX Tcl_MutexUnlock(&syncHandlesMutex)
#define MUTEXID 'm' /* First letter of the mutex handle */
#define CONDID 'c' /* First letter of the condition-variable handle */
/*
* Functions implementing Tcl commands
*/
static Tcl_ObjCmdProc ThreadMutexObjCmd;
static Tcl_ObjCmdProc ThreadCondObjCmd;
static Tcl_ObjCmdProc ThreadEvalObjCmd;
/*
* Forward declaration of functions used only within this file
*/
static int GetObjFromHandle _ANSI_ARGS_((Tcl_Interp *interp,
int type,
char *id,
void **addrPtrPtr));
static void SetHandleFromObj _ANSI_ARGS_((Tcl_Interp *interp,
int type,
void *addrPtr));
static void DeleteObjHandle _ANSI_ARGS_((char *id));
static void FinalizeSp _ANSI_ARGS_((ClientData clientData));
/*
*----------------------------------------------------------------------
*
* ThreadMutexObjCmd --
*
* This procedure is invoked to process "thread::mutex" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
static int
ThreadMutexObjCmd(dummy, interp, objc, objv)
ClientData dummy; /* Not used. */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj *CONST objv[]; /* Argument objects. */
{
int opt, ret;
char *mutexHandle;
Tcl_Mutex *mutexPtr = NULL;
static CONST84 char *cmdOpts[] = {
"create", "destroy", "lock", "unlock", NULL
};
enum options {
m_CREATE, m_DESTROY, m_LOCK, m_UNLOCK
};
/*
* Syntax:
*
* thread::mutex create
* thread::mutex destroy <mutexHandle>
* thread::mutex lock <mutexHandle>
* thread::mutex unlock <mutexHandle>
*/
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?args?");
return TCL_ERROR;
}
ret = Tcl_GetIndexFromObj(interp, objv[1], cmdOpts, "option", 0, &opt);
if (ret != TCL_OK) {
return TCL_ERROR;
}
/*
* Cover the "create" option first, since it needs no existing handle
*/
if (opt == (int)m_CREATE) {
mutexPtr = (Tcl_Mutex*)Tcl_Alloc(sizeof(Tcl_Mutex));
*mutexPtr = 0;
SetHandleFromObj(interp, MUTEXID, (void*)mutexPtr);
return TCL_OK;
}
/*
* All other options require a valid handle. See if we got any...
*/
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "mutexHandle");
return TCL_ERROR;
}
mutexHandle = Tcl_GetString(objv[2]);
ret = GetObjFromHandle(interp, MUTEXID, mutexHandle, (void**) &mutexPtr);
if (ret != TCL_OK) {
return TCL_ERROR;
}
/*
* Having found the correct handle, take care about other options.
*/
switch ((enum options)opt) {
case m_LOCK:
Tcl_MutexLock(mutexPtr);
break;
case m_UNLOCK:
Tcl_MutexUnlock(mutexPtr);
break;
case m_DESTROY:
Tcl_MutexFinalize(mutexPtr);
Tcl_Free((char*)mutexPtr);
DeleteObjHandle(mutexHandle);
break;
case m_CREATE: /* Already handled above */
break;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* ThreadEvalObjCmd --
*
* This procedure is invoked to process "thread::eval" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
static int
ThreadEvalObjCmd(dummy, interp, objc, objv)
ClientData dummy; /* Not used. */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj *CONST objv[]; /* Argument objects. */
{
int ret, optx;
char *mutexHandle;
Tcl_Obj *scriptObj;
Tcl_Mutex *mutexPtr = NULL;
static Tcl_Mutex evalMutex;
/*
* Syntax:
*
* thread::eval ?-lock <mutexHandle>? arg ?arg ...?
*/
if (objc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
Tcl_GetString(objv[0]),
" ?-lock <mutexHandle>? arg ?arg...?\"", NULL);
return TCL_ERROR;
}
if (objc > 3 && OPT_CMP(Tcl_GetString(objv[1]), "-lock")) {
mutexHandle = Tcl_GetString(objv[2]);
ret = GetObjFromHandle(interp, MUTEXID, mutexHandle, (void**)&mutexPtr);
if (ret != TCL_OK) {
return TCL_ERROR;
}
optx = 3;
} else {
optx = 1;
mutexPtr = &evalMutex;
}
objc -= optx;
Tcl_MutexLock(mutexPtr);
/*
* Evaluate passed arguments as Tcl script. Note that
* Tcl_EvalObjEx throws away the passed object by
* doing an decrement reference count on it. This also
* means we need not build object bytecode rep.
*/
if (objc == 1) {
scriptObj = Tcl_DuplicateObj(objv[optx]);
} else {
scriptObj = Tcl_ConcatObj(objc, objv + optx);
}
ret = Tcl_EvalObjEx(interp, scriptObj, TCL_EVAL_DIRECT);
if (ret == TCL_ERROR) {
char msg[32 + TCL_INTEGER_SPACE];
sprintf(msg, "\n (\"eval\" body line %d)", interp->errorLine);
Tcl_AddObjErrorInfo(interp, msg, -1);
}
/*
* Double-check the mutex handle in case somebody
* has deleted the mutex in the evaluated script.
* This is considered to be A Bad Thing.
*/
if (mutexPtr != &evalMutex) {
if (GetObjFromHandle(NULL, MUTEXID, mutexHandle, (void**)&mutexPtr)
== TCL_OK) {
Tcl_MutexUnlock(mutexPtr);
}
} else {
Tcl_MutexUnlock(mutexPtr);
}
return ret;
}
/*
*----------------------------------------------------------------------
*
* ThreadCondObjCmd --
*
* This procedure is invoked to process "thread::cond" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
static int
ThreadCondObjCmd(dummy, interp, objc, objv)
ClientData dummy; /* Not used. */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj *CONST objv[]; /* Argument objects. */
{
int opt, ret;
char *condHandle, *mutexHandle;
Tcl_Time waitTime;
Tcl_Mutex *mutexPtr = NULL;
Tcl_Condition *condPtr = NULL;
static CONST84 char *cmdOpts[] = {
"create", "destroy", "notify", "wait", NULL
};
enum options {
c_CREATE, c_DESTROY, c_NOTIFY, c_WAIT
};
/*
* Syntax:
*
* thread::cond create
* thread::cond destroy <condHandle>
* thread::cond broadcast <condHandle>
* thread::cond wait <condHandle> <mutexHandle> ?timeout?
*/
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?args?");
return TCL_ERROR;
}
ret = Tcl_GetIndexFromObj(interp, objv[1], cmdOpts, "option", 0, &opt);
if (ret != TCL_OK) {
return TCL_ERROR;
}
/*
* Cover the "create" option since it needs no existing handle.
*/
if (opt == (int)c_CREATE) {
condPtr = (Tcl_Condition*)Tcl_Alloc(sizeof(Tcl_Condition));
*condPtr = 0;
SetHandleFromObj(interp, CONDID, (void*)condPtr);
return TCL_OK;
}
/*
* All others require at least a valid handle. See if we got any...
*/
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "condHandle ?args?");
return TCL_ERROR;
}
condHandle = Tcl_GetString(objv[2]);
ret = GetObjFromHandle(interp, CONDID, condHandle, (void**)&condPtr);
if (ret != TCL_OK) {
return TCL_ERROR;
}
switch ((enum options)opt) {
case c_WAIT:
/*
* May improve the Tcl_ConditionWait() to report timeouts so we can
* inform script programmer about this interesting fact. I think
* there is still a place for something like Tcl_ConditionWaitEx()
* or similar in the core.
*/
if (objc < 4 || objc > 5) {
Tcl_WrongNumArgs(interp, 3, objv, "mutexHandle ?timeout?");
return TCL_ERROR;
}
mutexHandle = Tcl_GetString(objv[3]);
ret = GetObjFromHandle(interp,MUTEXID,mutexHandle,(void**)&mutexPtr);
if (ret != TCL_OK) {
return TCL_ERROR;
}
if (objc == 5) {
int timeMsec;
if (Tcl_GetIntFromObj(interp, objv[4], &timeMsec) != TCL_OK) {
return TCL_ERROR;
}
waitTime.sec = timeMsec/1000;
waitTime.usec = (timeMsec%1000) * 1000;
Tcl_ConditionWait(condPtr, mutexPtr, &waitTime);
} else {
Tcl_ConditionWait(condPtr, mutexPtr, NULL);
}
break;
case c_NOTIFY:
Tcl_ConditionNotify(condPtr);
break;
case c_DESTROY:
Tcl_ConditionFinalize(condPtr);
Tcl_Free((char*)condPtr);
DeleteObjHandle(condHandle);
break;
case c_CREATE: /* Already handled above */
break;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* SetHandleFromObj --
*
* Take an pointer and convert it into a opaque object Tcl handle.
* Returns the string-handle in interpreter result.
*
* Results:
* None.
*
* Side effects:
* Modifies the interpreter result.
*
*----------------------------------------------------------------------
*/
static void
SetHandleFromObj(interp, type, addrPtr)
Tcl_Interp *interp; /* Interpreter to set result. */
int type; /* Type: MUTEXID or CONDID. */
void *addrPtr; /* Object address */
{
int new;
char handle[20];
Tcl_HashEntry *hashEntryPtr;
#ifdef NS_AOLSERVER
sprintf(handle, "%cid%p", type, addrPtr);
#else
sprintf(handle, "%cid%u", type, syncHandlesCounter++);
/*
* By their very nature, object handles are always unique
* (the counter!) so no need to check for duplicates.
*/
GRAB_SYNCMUTEX;
hashEntryPtr = Tcl_CreateHashEntry(&syncHandles, handle, &new);
Tcl_SetHashValue(hashEntryPtr, (ClientData)addrPtr);
RELEASE_SYNCMUTEX;
#endif /* NS_AOLSERVER */
Tcl_SetObjResult(interp, Tcl_NewStringObj(handle, -1));
return;
}
/*
*----------------------------------------------------------------------
*
* GetObjFromHandle --
*
* Take an opaque object Tcl handle and convert it into a pointer.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* On error, error message will be left in interpreter result.
*
*----------------------------------------------------------------------
*/
static int
GetObjFromHandle(interp, type, handle, addrPtrPtr)
Tcl_Interp *interp; /* Interpreter for error msg. */
int type; /* Type: MUTEXID or CONDID. */
char *handle; /* Tcl string handle */
void **addrPtrPtr; /* Return object address here */
{
#ifdef NS_AOLSERVER
void *addrPtr = NULL;
#endif
Tcl_HashEntry *hashEntryPtr;
if (handle[0] != type || handle[1] != 'i' || handle[2] != 'd'
#ifdef NS_AOLSERVER
|| sscanf(handle+3, "%p", &addrPtr) != 1 || addrPtr == NULL
#endif
) {
goto badhandle;
}
#ifdef NS_AOLSERVER
*addrPtrPtr = addrPtr;
#else
GRAB_SYNCMUTEX;
hashEntryPtr = Tcl_FindHashEntry(&syncHandles, handle);
if (hashEntryPtr == (Tcl_HashEntry *)NULL) {
RELEASE_SYNCMUTEX;
goto badhandle;
}
*addrPtrPtr = (void *)Tcl_GetHashValue(hashEntryPtr);
RELEASE_SYNCMUTEX;
#endif /* NS_AOLSERVER */
return TCL_OK;
badhandle:
if (interp != NULL) {
Tcl_AppendResult(interp, "invalid handle \"", handle, "\"", NULL);
}
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* DeleteObjHandle --
*
* Deletes the object handle from internal hash table.
* Caller has to take care about disposing the object.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Handle gets deleted from the hash table
*
*----------------------------------------------------------------------
*/
static void
DeleteObjHandle(handle)
char *handle; /* Tcl string handle */
{
Tcl_HashEntry *hashEntryPtr;
GRAB_SYNCMUTEX;
hashEntryPtr = Tcl_FindHashEntry(&syncHandles, handle);
Tcl_DeleteHashEntry(hashEntryPtr);
RELEASE_SYNCMUTEX;
}
/*
*----------------------------------------------------------------------
*
* Sp_Init --
*
* Create commands in current interpreter..
*
* Results:
* None.
*
* Side effects:
* Creates new commands in current interpreter, initializes
* shared hash table for storing sync primitives handles/pointers.
*
*----------------------------------------------------------------------
*/
int
Sp_Init (interp)
Tcl_Interp *interp; /* Interp where to create cmds */
{
static int initialized;
if (!initialized) {
GRAB_SYNCMUTEX;
if (!initialized) {
Tcl_InitHashTable(&syncHandles, TCL_STRING_KEYS);
/*
* We should not be garbage-collecting sync
* primitives because there is no way to
* find out who/if is using them. Generally,
* this should be delegated to the programmer.
*/
initialized = 1;
}
RELEASE_SYNCMUTEX;
}
TCL_CMD(interp, THNS"::mutex", ThreadMutexObjCmd);
TCL_CMD(interp, THNS"::cond", ThreadCondObjCmd);
TCL_CMD(interp, THNS"::eval", ThreadEvalObjCmd);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* FinalizeSp --
*
* Garbage-collect hash table on application exit.
* NOTE: this function should not be used!
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Memory gets reclaimed.
*
*----------------------------------------------------------------------
*/
static void
FinalizeSp (clientData)
ClientData clientData; /* Not used. */
{
char *objHdl;
void *objPtr;
Tcl_HashEntry *entryPtr;
Tcl_HashSearch search;
GRAB_SYNCMUTEX;
for (entryPtr = Tcl_FirstHashEntry(&syncHandles, &search);
entryPtr; entryPtr = Tcl_NextHashEntry(&search)) {
objHdl = (char*)Tcl_GetHashKey(&syncHandles, entryPtr);
objPtr = (void*)Tcl_GetHashValue(entryPtr);
switch((char)*objHdl) {
case MUTEXID: Tcl_MutexFinalize((Tcl_Mutex*)objPtr);
case CONDID: Tcl_ConditionFinalize((Tcl_Condition*)objPtr);
}
Tcl_Free((char*)objPtr);
Tcl_DeleteHashEntry(entryPtr);
}
Tcl_DeleteHashTable(&syncHandles);
RELEASE_SYNCMUTEX;
}
/* EOF $RCSfile: threadSpCmd.c,v $ */
/* Emacs Setup Variables */
/* Local Variables: */
/* mode: C */
/* indent-tabs-mode: nil */
/* c-basic-offset: 4 */
/* End: */
|