Skip to content
Tcl Source Code History Of Ticket 0f439992eff158ca
Login

History Of Ticket 0f439992eff158ca

Artifacts Associated With Ticket 0f439992eff158ca

  1. Ticket change [f04fb47697] (rid 171360) by elfring on 2019-05-06 09:27:07:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      I am trying again to get a multi-threaded data processing approach working also together with the programming language “TCL 8.6.9-1.5” on an openSUSE system.
      Unfortunately, I have detected another questionable software behaviour during my tests.
      
      Test output example:
      …
      3. run
      mutex is not locked
          while executing
      "thread::mutex unlock [tsv::get shared pcm]"
          (procedure "main_thread_unlock_start" line 1)
          invoked from within
      "main_thread_unlock_start"
          (procedure "perform_command" line 92)
          invoked from within
      "perform_command"
          (procedure "repeat_command" line 4)
          invoked from within
      "repeat_command"
          (procedure "run_analysis" line 2)
          invoked from within
      "run_analysis"
          (file "test-statistic-server2c.tcl" line 221)
      
      
      How can such a software surprise happen when this script provides the following source code at the affected place and similar command calls at related places?
      (By the way: Scoped locks could reduce the programming efforts for the desired data consistency, couldn't they?)
      
      …
      proc perform_command {} \
      {
      tsv::set shared pcm [thread::mutex create]
      thread::mutex lock [tsv::get shared pcm]
      
      try \
         {
         tsv::set shared pcc [thread::cond create]
      …
         } \
      finally \
         {
         proc main_thread_unlock_start {} \
         {thread::mutex unlock [tsv::get shared pcm]}
         main_thread_unlock_start
         }
      …
      
    5. foundin initialized to: "8.6.9"
    6. is_private initialized to: "0"
    7. login: "elfring"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "elfring"
    13. subsystem initialized to: "80. Thread Package"
    14. title initialized to: "Checking data consistency for unlocking of mutexes"
    15. type initialized to: "Bug"
  2. Ticket change [4507d7dd81] (rid 171361) by sebres on 2019-05-06 11:48:30:

    1. closedate changed to: "2458609.99202232"
    2. closer changed to: "sebres"
    3. icomment:
      Firstly, this is a wrong repository for that (please use [/../thread/|thread-repo] for all thread-related issues).
      
      Secondly, I assume your proc "perform_command" is running multi-threaded...<br/>
      Then you've a grave error there - you've a race condition on the mutex creation and locking/unlocking - the mutex stored in tsv shared pcm could be created with thread 1, during a thread 2 still trying to lock another (freshly created) mutex, so it will then try to unlock mutex created within thread 1 later in finally block (because overwritten in tsv).
      
      So in order to avoid a race conditions like that, either don't use mutex (tsv-lock is your friend), or you should move the initialization of mutex into master thread (to initialize it once), or you should create mutex like here:
      <code><pre style="padding-left:10pt">
      tsv::lock shared {
        if {![tsv::exists shared pcm]} {
          tsv::set shared pcm [thread::mutex create]
        }
      }
      ...
      thread::mutex lock [tsv::get shared pcm]
      ...
      thread::mutex unlock [tsv::get shared pcm]
      ...
      </pre></code>
      
      Or even still better via "singletone" similar variable:
      <code><pre style="padding-left:10pt">
      if {![tsv::get shared pcm mutex]} {
        tsv::lock shared {
          if {![tsv::exists shared pcm]} {
            tsv::set shared pcm [thread::mutex create]
          }
          tsv::get shared pcm mutex
        }
      }
      ...
      thread::mutex lock $mutex
      ...
      thread::mutex unlock $mutex
      ...
      </pre></code>
      
      
      Anyway no matter how you would use that, you MUST always ensure that the mutex is created (and referenced) only once in your processing. Otherwise you would get the same RC (so your threads may operate with different mutexes).
      
    4. login: "sebres"
    5. mimetype: "text/x-fossil-wiki"
    6. resolution changed to: "Invalid"
    7. status changed to: "Closed"
  3. Ticket change [865fc5a69d] (rid 171363) by elfring on 2019-05-06 12:45:58:

    1. closer changed to: "nobody"
    2. icomment:
      I find the software situation not so clear as desired.
      
      
      > Anyway no matter how you would use that, you MUST always ensure that the mutex is created (and referenced) only once in your processing.
      
      My expectations might be different for these usage constraints.
      
      
      How do you think about to check if any more software surprises can be found by an example script like the following?
      
      
      package require Thread
      set results {}
      
      proc perform_command {} \
      {
      tsv::set [tsv::get shared context] pcm [thread::mutex create]
      thread::mutex lock [tsv::get [tsv::get shared context] pcm]
      tsv::set [tsv::get shared context] pcc [thread::cond create]
      
      try \
         {
         tsv::set [tsv::get shared context] working 0
         tsv::set [tsv::get shared context] finish_notification 0
         } \
      finally \
         {
         proc main_thread_unlock_start {} \
         {thread::mutex unlock [tsv::get [tsv::get shared context] pcm]}
         main_thread_unlock_start
         }
      
      for {set x 1} {$x <= 6} {incr x} \
         {
         set t($x) [thread::create {
                                   proc handle_input {} \
                                   {
                                   thread::mutex lock [tsv::get [tsv::get shared context] pcm]
      
                                   try \
                                      {
                                      puts stderr "[tsv::incr [tsv::get shared context] working] threads are working."
                                      } \
                                   finally \
                                      {
                                      proc handle_input_unlock_begin {} \
                                      {thread::mutex unlock [tsv::get [tsv::get shared context] pcm]}
                                      handle_input_unlock_begin
                                      }
      
                                   thread::mutex lock [tsv::get [tsv::get shared context] pcm]
      
                                   try \
                                      {
                                      tsv::incr [tsv::get shared context] working -1
      
                                      if [tsv::get [tsv::get shared context] finish_notification] \
                                         {
                                         puts stderr {thread::cond notify finish}
                                         thread::cond notify [tsv::get [tsv::get shared context] pcc]
                                         }
                                      } \
                                   finally \
                                      {
                                      proc handle_input_unlock_end {} \
                                      {thread::mutex unlock [tsv::get [tsv::get shared context] pcm]}
                                      handle_input_unlock_end
                                      }
                                   }
      
                                   handle_input
                                   }]
         }
      
      puts stderr "Worker threads were started."
      thread::mutex lock [tsv::get [tsv::get shared context] pcm]
      
      try \
         {
         tsv::set [tsv::get shared context] finish_notification 1
      
         while {[set w [tsv::get [tsv::get shared context] working]] > 0} \
            {
            puts stderr "cond wait finish for $w threads"
            thread::cond wait [tsv::get [tsv::get shared context] pcc] [tsv::get [tsv::get shared context] pcm]
            }
         } \
      finally \
         {
         proc main_thread_unlock_finish {} \
         {thread::mutex unlock [tsv::get [tsv::get shared context] pcm]}
         main_thread_unlock_finish
         }
      
      global results
      lappend results [llength [thread::names]]
      #thread::cond destroy [tsv::get [tsv::get shared context] pcc]
      #thread::mutex destroy [tsv::get [tsv::get shared context] pcm]
      }
      
      proc repeat_command {} \
      {
      for {set x 1} {$x <= 12} {incr x} \
         {
         puts stderr "$x. run"
         tsv::set shared context $x
         perform_command
         }
      }
      
      proc run_analysis {} \
      {
      repeat_command
      global results
      
      if [llength ${results}] \
         {
         proc report {} \
         {
         global results
      
         foreach k ${results} \
            {
            dict incr counts $k
            }
      
         set delimiter |
         puts [join {{"still running threads"} incidence} ${delimiter}]
      
         dict for {k v} ${counts} \
            {
             puts "$k${delimiter}$v"
            }
         }
      
         report
         } \
      else \
         {
         puts stderr {No result for this analysis!}
         }
      }
      
      run_analysis
      
    3. login: "elfring"
    4. mimetype: "text/plain"
    5. resolution changed to: "Later"
    6. severity changed to: "Important"
    7. status changed to: "Open"
  4. Ticket change [8b14f4c6c1] (rid 171365) by sebres on 2019-05-06 13:01:38:

    1. closedate changed to: "2458610.0428065"
    2. closer changed to: "sebres"
    3. icomment:
      > My expectations might be different for these usage constraints.
      
      It does no matter what your expectations are here if you basically don't understand how mutex or tsv are working...
      
      If your script running multi-threaded and two threads write a tsv variable, one of that will overwrite the value with another mutex-handle sooner or later.
      
      If you don't know how the mutex basically work use another locking mechanisms e. g. tsv::lock.
      If you want still do that what you are trying to do in your perform_command, you MUST guarantee that in ALL threads you'll use still the same mutex handle (and I wrote already a proper example for that).
      
      And again, this is a wrong repository for the thread-package issues. 
      So please let the ticket closed and reopen your concerns in the thread-repo.
      
    4. login: "sebres"
    5. mimetype: "text/plain"
    6. resolution changed to: "Invalid"
    7. status changed to: "Closed"
  5. Ticket change [abcc39ba79] (rid 171372) by elfring on 2019-05-06 13:54:56:

    1. closedate changed to: "2458610.07982499"
    2. closer changed to: "elfring"
    3. icomment:
      See also:
      https://core.tcl-lang.org/thread/tktview/90de9ddf94ecfb6bacd5e754919a21d31fe3f25a
      
    4. login: "elfring"
    5. mimetype: "text/plain"
  6. Ticket change [74ae424577] (rid 171373) by elfring on 2019-05-06 14:21:14:

    1. closedate changed to: "2458610.09808057"
    2. icomment:
      I am curious then how the software clarification will be continued in the issue tracker for the other repository.
      
      * Would you like to try the shown test script out a bit more?
      
      * Does it indicate a sufficient understanding for required data synchronisation according to the supported programming interfaces?
      
    3. login: "elfring"
    4. mimetype: "text/plain"