Tcl Source Code

View Ticket
Login
Ticket UUID: 219097
Title: Tcl_AsyncMark yields the wrong thread (part 3)
Type: Bug Created on: 2000-10-26 01:10:42
Submitter: nobody Assigned to: nobody
Subsystem: 49. Threading Severity:
Priority: 2 Last modified: 2000-10-26 08:34:56
Status: Closed Closed by: welch
Resolution: Fixed Closed on: 2000-10-26 01:34:56
Version: obsolete: 8.4a1
Description:
OriginalBugID: 5791 Bug
Version: 8.4a1
SubmitDate: '2000-06-02'
LastModified: '2000-07-25'
Severity: SER
Status: Released
Submitter: techsupp
ChangedBy: davidg
RelatedBugIDs: 4826 3952 3951 3862 5758
OS: All
FixedDate: '2000-07-25'
FixedInVersion: 8.4a1
ClosedDate: '2000-07-25'


Name:

David Gravereaux


CVS:

updated today


Comments:

I barely touched tclTest.c.  I didn't add any new test cases except

    the above script using my own extension code.  All tests pass as

    before.  The API wasn't changed at all, but the behavior under

    threads is TOTALLY different on the inside.


ReproducibleScript:

C:\Projects\TclCvs\tcl\win\Debug>tclsh84d

% load ../../../thread/win/vc/debug/threadd.dll thread

% thread::id

1176

% thread::create {

  load c:/projects/ghe_cvs/ghe/ghe_dirwatch.dll

  GHE::startwatch c:/temp {gotcha [thread::id]}

  proc gotcha {id} {

    puts "c:/temp was written to and yielded notifier thread $id"

  }

  thread::wait

}

1532

% update

c:/temp was written to and yielded notifier thread 1176

% update

c:/temp was written to and yielded notifier thread 1176


1176 != 1532


ObservedBehavior:

Because TclpAsyncMark (on windows) is hard wired to only wake the

"main" thread, all hell breaks lose mixing Tcl_Async* with threads.


This should also fix the "Tcl_AsyncMark doesn't wake the UNIX notifier"

problem, but it's totally untested there.


DesiredBehavior:

with the patch, I now get this :)


C:\Projects\TclCvs\tcl\win\Debug>tclsh84d

% load ../../../thread/win/vc/debug/threadd.dll thread

% thread::id

1228

% thread::create {

  load c:/projects/ghe_cvs/ghe/ghe_dirwatch.dll

  GHE::startwatch c:/temp {gotcha [thread::id]}

  proc gotcha {id} {

    puts "c:/temp was written to and yielded notifier thread $id"

  }

  thread::wait

}

516

% c:/temp was written to and yielded notifier thread 516

c:/temp was written to and yielded notifier thread 516


the origin thread from where Tcl_AsyncCreate was called is now

the target for the AsyncProc.


Patch:

*** doc/Async.32000/04/14 23:01:481.4

--- doc/Async.32000/06/02 04:18:21

***************

*** 60,65 ****

--- 60,72 ----

  occurred, then handle the event later when the world has returned

  to a clean state, such as after the current Tcl command completes.

  .PP

+ \fBTcl_AsyncCreate\fR, \fBTcl_AsyncDelete\fR, and \fBTcl_AsyncReady\fR

+ are thread sensitive.  They access and/or set a thread-specific data

+ structure in the event of an --enable-thread built core.  The token

+ created by Tcl_AsyncCreate contains the needed thread information it

+ was called from so that calling Tcl_AsyncMark(token) will only yield

+ the origin thread into the AsyncProc.

+ .PP 

  \fBTcl_AsyncCreate\fR creates an asynchronous handler and returns

  a token for it.

  The asynchronous handler must be created before


*** generic/tclAsync.c1999/04/16 00:46:421.4

--- generic/tclAsync.c2000/06/02 04:18:32

***************

*** 18,23 ****

--- 18,26 ----

  #include "tclInt.h"

  #include "tclPort.h"


+ /* Forward declaration */

+ struct ThreadSpecificData;

+ 

  /*

   * One of the following structures exists for each asynchronous

   * handler:

***************

*** 33,66 ****

   * is invoked. */

      ClientData clientData;/* Value to pass to handler when it

   * is invoked. */

  } AsyncHandler;


- /*

-  * The variables below maintain a list of all existing handlers.

-  */


! static AsyncHandler *firstHandler;/* First handler defined for process,

!  * or NULL if none. */

! static AsyncHandler *lastHandler;/* Last handler or NULL. */


! TCL_DECLARE_MUTEX(asyncMutex)           /* Process-wide async handler lock */


! /*

!  * The variable below is set to 1 whenever a handler becomes ready and

!  * it is cleared to zero whenever Tcl_AsyncInvoke is called.  It can be

!  * checked elsewhere in the application by calling Tcl_AsyncReady to see

!  * if Tcl_AsyncInvoke should be invoked.

!  */


! static int asyncReady = 0;


! /*

!  * The variable below indicates whether Tcl_AsyncInvoke is currently

!  * working.  If so then we won't set asyncReady again until

!  * Tcl_AsyncInvoke returns.

!  */


! static int asyncActive = 0;

  

  /*

   *----------------------------------------------------------------------

--- 36,84 ----

   * is invoked. */

      ClientData clientData;/* Value to pass to handler when it

   * is invoked. */

+     struct ThreadSpecificData *originTsd;

+ /* Used in Tcl_AsyncMark to modify thread-

+  * specific data from outside the thread

+  * it is associated to. */

+     Tcl_ThreadId originThrdId;/* Origin thread where this token was

+  * created and where it will be

+  * yielded. */

  } AsyncHandler;


! typedef struct ThreadSpecificData {

!     /*

!      * The variables below maintain a list of all existing handlers

!      * specific to the calling thread.

!      */

!     AsyncHandler *firstHandler;    /* First handler defined for process,

!      * or NULL if none. */

!     AsyncHandler *lastHandler;    /* Last handler or NULL. */


!     /*

!      * The variable below is set to 1 whenever a handler becomes ready and

!      * it is cleared to zero whenever Tcl_AsyncInvoke is called.  It can be

!      * checked elsewhere in the application by calling Tcl_AsyncReady to see

!      * if Tcl_AsyncInvoke should be invoked.

!      */


!     int asyncReady;


!     /*

!      * The variable below indicates whether Tcl_AsyncInvoke is currently

!      * working.  If so then we won't set asyncReady again until

!      * Tcl_AsyncInvoke returns.

!      */


!     int asyncActive;

! 

! #ifdef TCL_THREADS

!     Tcl_Mutex asyncMutex;   /* Thread-specific AsyncHandler linked-list lock */

! #endif


! } ThreadSpecificData;

! static Tcl_ThreadDataKey dataKey;

! 

  

  /*

   *----------------------------------------------------------------------

***************

*** 88,107 ****

      ClientData clientData;/* Argument to pass to handler. */

  {

      AsyncHandler *asyncPtr;


      asyncPtr = (AsyncHandler *) ckalloc(sizeof(AsyncHandler));

      asyncPtr->ready = 0;

      asyncPtr->nextPtr = NULL;

      asyncPtr->proc = proc;

      asyncPtr->clientData = clientData;

!     Tcl_MutexLock(&asyncMutex);

!     if (firstHandler == NULL) {

! firstHandler = asyncPtr;

      } else {

! lastHandler->nextPtr = asyncPtr;

      }

!     lastHandler = asyncPtr;

!     Tcl_MutexUnlock(&asyncMutex);

      return (Tcl_AsyncHandler) asyncPtr;

  }

  

--- 106,129 ----

      ClientData clientData;/* Argument to pass to handler. */

  {

      AsyncHandler *asyncPtr;

+     ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);


      asyncPtr = (AsyncHandler *) ckalloc(sizeof(AsyncHandler));

      asyncPtr->ready = 0;

      asyncPtr->nextPtr = NULL;

      asyncPtr->proc = proc;

      asyncPtr->clientData = clientData;

!     asyncPtr->originTsd = tsdPtr;

!     asyncPtr->originThrdId = Tcl_GetCurrentThread();

! 

!     Tcl_MutexLock(&tsdPtr->asyncMutex);

!     if (tsdPtr->firstHandler == NULL) {

! tsdPtr->firstHandler = asyncPtr;

      } else {

! tsdPtr->lastHandler->nextPtr = asyncPtr;

      }

!     tsdPtr->lastHandler = asyncPtr;

!     Tcl_MutexUnlock(&tsdPtr->asyncMutex);

      return (Tcl_AsyncHandler) asyncPtr;

  }

  

***************

*** 128,140 ****

  Tcl_AsyncMark(async)

      Tcl_AsyncHandler async;/* Token for handler. */

  {

!     Tcl_MutexLock(&asyncMutex);

!     ((AsyncHandler *) async)->ready = 1;

!     if (!asyncActive) {

! asyncReady = 1;

! TclpAsyncMark(async);

      }

!     Tcl_MutexUnlock(&asyncMutex);

  }

  

  /*

--- 150,164 ----

  Tcl_AsyncMark(async)

      Tcl_AsyncHandler async;/* Token for handler. */

  {

!     AsyncHandler *token = (AsyncHandler *) async;

! 

!     Tcl_MutexLock(&token->originTsd->asyncMutex);

!     token->ready = 1;

!     if (!token->originTsd->asyncActive) {

! token->originTsd->asyncReady = 1;

! Tcl_ThreadAlert(token->originThrdId);

      }

!     Tcl_MutexUnlock(&token->originTsd->asyncMutex);

  }

  

  /*

***************

*** 167,180 ****

   * just completed. */

  {

      AsyncHandler *asyncPtr;

!     Tcl_MutexLock(&asyncMutex);


!     if (asyncReady == 0) {

! Tcl_MutexUnlock(&asyncMutex);

  return code;

      }

!     asyncReady = 0;

!     asyncActive = 1;

      if (interp == NULL) {

  code = 0;

      }

--- 191,206 ----

   * just completed. */

  {

      AsyncHandler *asyncPtr;

!     ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

! 

!     Tcl_MutexLock(&tsdPtr->asyncMutex);


!     if (tsdPtr->asyncReady == 0) {

! Tcl_MutexUnlock(&tsdPtr->asyncMutex);

  return code;

      }

!     tsdPtr->asyncReady = 0;

!     tsdPtr->asyncActive = 1;

      if (interp == NULL) {

  code = 0;

      }

***************

*** 191,197 ****

       */


      while (1) {

! for (asyncPtr = firstHandler; asyncPtr != NULL;

  asyncPtr = asyncPtr->nextPtr) {

      if (asyncPtr->ready) {

  break;

--- 217,223 ----

       */


      while (1) {

! for (asyncPtr = tsdPtr->firstHandler; asyncPtr != NULL;

  asyncPtr = asyncPtr->nextPtr) {

      if (asyncPtr->ready) {

  break;

***************

*** 201,212 ****

      break;

  }

  asyncPtr->ready = 0;

! Tcl_MutexUnlock(&asyncMutex);

  code = (*asyncPtr->proc)(asyncPtr->clientData, interp, code);

! Tcl_MutexLock(&asyncMutex);

      }

!     asyncActive = 0;

!     Tcl_MutexUnlock(&asyncMutex);

      return code;

  }

  

--- 227,238 ----

      break;

  }

  asyncPtr->ready = 0;

! Tcl_MutexUnlock(&tsdPtr->asyncMutex);

  code = (*asyncPtr->proc)(asyncPtr->clientData, interp, code);

! Tcl_MutexLock(&tsdPtr->asyncMutex);

      }

!     tsdPtr->asyncActive = 0;

!     Tcl_MutexUnlock(&tsdPtr->asyncMutex);

      return code;

  }

  

***************

*** 231,256 ****

  Tcl_AsyncDelete(async)

      Tcl_AsyncHandler async;/* Token for handler to delete. */

  {

      AsyncHandler *asyncPtr = (AsyncHandler *) async;

      AsyncHandler *prevPtr;


!     Tcl_MutexLock(&asyncMutex);

!     if (firstHandler == asyncPtr) {

! firstHandler = asyncPtr->nextPtr;

! if (firstHandler == NULL) {

!     lastHandler = NULL;

  }

      } else {

! prevPtr = firstHandler;

  while (prevPtr->nextPtr != asyncPtr) {

      prevPtr = prevPtr->nextPtr;

  }

  prevPtr->nextPtr = asyncPtr->nextPtr;

! if (lastHandler == asyncPtr) {

!     lastHandler = prevPtr;

  }

      }

!     Tcl_MutexUnlock(&asyncMutex);

      ckfree((char *) asyncPtr);

  }

  

--- 257,283 ----

  Tcl_AsyncDelete(async)

      Tcl_AsyncHandler async;/* Token for handler to delete. */

  {

+     ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

      AsyncHandler *asyncPtr = (AsyncHandler *) async;

      AsyncHandler *prevPtr;


!     Tcl_MutexLock(&tsdPtr->asyncMutex);

!     if (tsdPtr->firstHandler == asyncPtr) {

! tsdPtr->firstHandler = asyncPtr->nextPtr;

! if (tsdPtr->firstHandler == NULL) {

!     tsdPtr->lastHandler = NULL;

  }

      } else {

! prevPtr = tsdPtr->firstHandler;

  while (prevPtr->nextPtr != asyncPtr) {

      prevPtr = prevPtr->nextPtr;

  }

  prevPtr->nextPtr = asyncPtr->nextPtr;

! if (tsdPtr->lastHandler == asyncPtr) {

!     tsdPtr->lastHandler = prevPtr;

  }

      }

!     Tcl_MutexUnlock(&tsdPtr->asyncMutex);

      ckfree((char *) asyncPtr);

  }

  

***************

*** 261,267 ****

   *

   *This procedure can be used to tell whether Tcl_AsyncInvoke

   *needs to be called.  This procedure is the external interface

!  *for checking the internal asyncReady variable.

   *

   * Results:

   * The return value is 1 whenever a handler is ready and is 0

--- 288,294 ----

   *

   *This procedure can be used to tell whether Tcl_AsyncInvoke

   *needs to be called.  This procedure is the external interface

!  *for checking the thread-specific asyncReady variable.

   *

   * Results:

   * The return value is 1 whenever a handler is ready and is 0

***************

*** 276,280 ****

  int

  Tcl_AsyncReady()

  {

!     return asyncReady;

  }

--- 303,308 ----

  int

  Tcl_AsyncReady()

  {

!     ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

!     return tsdPtr->asyncReady;

  }


*** generic/tclInt.decls2000/05/19 21:30:161.21

--- generic/tclInt.decls2000/06/02 04:19:00

***************

*** 797,805 ****

  declare 20 win {

      void TclWinAddProcess(HANDLE hProcess, DWORD id)

  }

! declare 21 win {

!     void TclpAsyncMark(Tcl_AsyncHandler async)

! }


  # Added in 8.1:

  declare 22 win {

--- 797,807 ----

  declare 20 win {

      void TclWinAddProcess(HANDLE hProcess, DWORD id)

  }

! 

! # removed permanently for 8.4

! #declare 21 win {

! #    void TclpAsyncMark(Tcl_AsyncHandler async)

! #}


  # Added in 8.1:

  declare 22 win {


*** generic/tclTest.c2000/05/19 21:30:161.18

--- generic/tclTest.c2000/06/02 04:20:07

***************

*** 586,592 ****

      listArgv[2] = string;

      listArgv[3] = NULL;

      cmd = Tcl_Merge(3, listArgv);

!     code = Tcl_Eval(interp, cmd);

      ckfree(cmd);

      return code;

  }

--- 586,600 ----

      listArgv[2] = string;

      listArgv[3] = NULL;

      cmd = Tcl_Merge(3, listArgv);

!     if (interp != NULL) {

! code = Tcl_Eval(interp, cmd);

!     } else {

! /*

!  * this should not happen, but by definition of how async

!  * handlers are invoked, it's possible.  Better error

!  * checking is needed here.

!  */

!     }

      ckfree(cmd);

      return code;

  }


*** mac/tclMacPort.h1999/12/08 03:50:041.10

--- mac/tclMacPort.h2000/06/02 04:20:12

***************

*** 224,230 ****

   * address platform-specific issues.

   */


- #define TclpAsyncMark(async)

  #define TclpGetPid(pid)    ((unsigned long) (pid))

  #define TclSetSystemEnv(a,b)

  #define tzset()

--- 224,229 ----


*** unix/tclUnixPort.h2000/04/21 04:04:121.15

--- unix/tclUnixPort.h2000/06/02 04:20:18

***************

*** 457,463 ****

   * address platform-specific issues.

   */


- #define TclpAsyncMark(async)

  #define TclpGetPid(pid)((unsigned long) (pid))

  #define TclpReleaseFile(file)/* Nothing. */


--- 457,462 ----


*** win/tclWinInit.c2000/03/31 08:52:311.22

--- win/tclWinInit.c2000/06/02 04:20:37

***************

*** 73,84 ****

  };


  /*

-  * Thread id used for asynchronous notification from signal handlers.

-  */

- 

- static DWORD mainThreadId;

- 

- /*

   * The Init script (common to Windows and Unix platforms) is

   * defined in tkInitScript.h

   */

--- 73,78 ----

***************

*** 129,144 ****


      SetErrorMode(SetErrorMode(0) | SEM_FAILCRITICALERRORS);


-     /*

-      * Save the id of the first thread to intialize the Tcl library.  This

-      * thread will be used to handle notifications from async event

-      * procedures.  This is not strictly correct.  A better solution involves

-      * using a designated "main" notifier that is kept up to date as threads

-      * come and go.

-      */

- 

-     mainThreadId = GetCurrentThreadId();

- 

  #ifdef STATIC_BUILD

      /*

       * If we are in a statically linked executable, then we need to

--- 123,128 ----

***************

*** 814,845 ****

  }

          Tcl_DStringFree(&temp);

      }

- }

- 

- /*

-  *----------------------------------------------------------------------

-  *

-  * TclpAsyncMark --

-  *

-  *Wake up the main thread from a signal handler.

-  *

-  * Results:

-  *None.

-  *

-  * Side effects:

-  *Sends a message to the main thread.

-  *

-  *----------------------------------------------------------------------

-  */

- 

- void

- TclpAsyncMark(async)

-     Tcl_AsyncHandler async;/* Token for handler. */

- {

-     /*

-      * Need a way to kick the Windows event loop and tell it to go look at

-      * asynchronous events.

-      */

- 

-     PostThreadMessage(mainThreadId, WM_USER, 0, 0);

  }

--- 798,801 ----


PatchFiles:

  doc/Async.3

  generic/tclAsync.c

  generic/tclInt.decls

  generic/tclTest.c

  mac/tclMacPort.h

  unix/tclUnixPort.h

  win/tclWinInit.c




This needs a good code review, please.  And don't forget to invite me :)
--davygrvy 
-- 06/23/2000 davidg

decided to add it to the core. 
-- 07/25/2000 davidg