Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Checking data consistency for unlocking of mutexes

Closed / Invalid0f439992eff158ca991ee191f89a6bd4c4eb51e9Created 8 years agoUpdated 8 years ago
Descriptionplain2019-05-06 09:27:07
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
   }
…
sebreswiki2019-05-06 11:48:30

Firstly, this is a wrong repository for that (please use thread-repo for all thread-related issues).

Secondly, I assume your proc "perform_command" is running multi-threaded...
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:

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]
...

Or even still better via "singletone" similar variable:

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
...

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).

elfringplain2019-05-06 12:45:58
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
sebresplain2019-05-06 13:01:38
> 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.
elfringplain2019-05-06 13:54:56
See also:
https://core.tcl-lang.org/thread/tktview/90de9ddf94ecfb6bacd5e754919a21d31fe3f25a
elfringplain2019-05-06 14:21:14
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?
Log in (anonymous is fine) to add a comment.