Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deadlock behavior: reader could not unlock when writer is waiting for the lock #16

Open
adamstupid opened this issue Aug 17, 2023 · 0 comments

Comments

@adamstupid
Copy link

the scenario is,

  1. reader take the read lock
  2. writer comes in and would like to take the write lock, while it cant, since there is already reader here. thus writer has to wait for the condition, without releasing the main lock
  3. reader now finish work, and want to return the read lock, but it cant, since the main lock is already taken by writer
  4. hence the dead lock

starts at line 80

function lock!(write_lock::WriteLock)
    rwlock = write_lock.rwlock
    lock(rwlock.lock)

    try
        while rwlock.readers > 0 || rwlock.writer
            wait(rwlock.condition)
        end

        rwlock.writer = true
    finally
        unlock(rwlock.lock)
    end

    return nothing
end

and here is the mre(or i'm using it wrong?)

include("ReadWriteLocks.jl")

using .ReadWriteLocks

function readerTask(rwLock)
	rLock = read_lock(rwLock)
	lock!(rLock)
	try
		println("$(current_task()): using read lock")
	finally
		unlock!(rLock)
	end
end

function writerTask(rwLock)
	wLock = write_lock(rwLock)
	lock!(wLock)
	try
		println("$(current_task()): using write lock")
	finally
		unlock!(wLock)
	end
end
	
function main()
	l = ReadWriteLock()
	tasks = Array{Task, 1}(undef, 3)

	# freeze everyone
	lock!(write_lock(l))

	try
		tasks[1] = Threads.@task readerTask(l)
		tasks[2] = Threads.@task readerTask(l)
		tasks[3] = Threads.@task writerTask(l)
		schedule.(tasks)
	finally
		# now release
		unlock!(write_lock(l))
	end	

	(l, tasks)
end

(l, t) = main()

istaskstarted.(t)
istaskdone.(t)

run with julia -t 2 main.jl
juliaversion: 1.8.0-rc1
expected output: all tasks should be finished, program exits immediately
actual output: program hangs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant