I am having some issues understanding how the Atomic_RW_Mutex is supposed to work in Odin. I have attached a minimal example that helps illustrate the problem.
In the below code I would expect that tast1 would run and acquire the RW Mutex for reading. Then task2 would try to acquire the RW Mutex for writing, would wait for task1 to release it then would continue though the code.
main :: proc()
{
wg : sync.Wait_Group
t1 := thread.create(task1)
t1.init_context = context
t1.user_index = 1
t1.data = &wg
t2 := thread.create(task2)
t2.init_context = context
t2.user_index = 1
t2.data = &wg
sync.wait_group_add(&wg, 2)
thread.start(t1)
thread.start(t2)
sync.wait_group_wait(&wg)
}
rw_mutex : sync.Atomic_RW_Mutex
task1 :: proc(t: ^thread.Thread)
{
sync.shared_lock(&rw_mutex)
time.sleep(time.Second * time.Duration(2))
sync.shared_unlock(&rw_mutex)
sync.wait_group_done((cast(^sync.Wait_Group)t.data))
fmt.println("task1 done")
}
task2 :: proc(t: ^thread.Thread)
{
time.sleep(time.Second * time.Duration(1))
fmt.println("before lock")
sync.lock(&rw_mutex)
fmt.println("after lock")
time.sleep(time.Second * time.Duration(1))
sync.unlock(&rw_mutex)
sync.wait_group_done((cast(^sync.Wait_Group)t.data))
fmt.println("task2 done")
}
However when I run the code I get
before lock
task1 done
Then the program appears to wait on acquiring the lock for writing forever. Is this the expected behaviour for the Atomic_RW_Mutex or is a bug?
Turns out this was indeed a bug. Fixed by this pr https://github.com/odin-lang/Odin/pull/5267.