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

[Serialization] support AbstractLock and GenericCondition #43325

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,21 @@ function deepcopy_internal(x::Union{Dict,IdDict}, stackdict::IdDict)
end
dest
end

function deepcopy_internal(x::AbstractLock, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
y = typeof(x)()
stackdict[x] = y
return y
end

function deepcopy_internal(x::GenericCondition, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
y = typeof(x)(deepcopy_internal(x.lock))
stackdict[x] = y
return y
end
26 changes: 26 additions & 0 deletions stdlib/Serialization/src/Serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1522,4 +1522,30 @@ function deserialize(s::AbstractSerializer, ::Type{Base.StackTraces.StackFrame})
return Base.StackTraces.StackFrame(func, file, line, nothing, from_c, inlined, pointer)
end

function serialize(s::AbstractSerializer, lock::Base.AbstractLock)
# assert_havelock(lock)
serialize_cycle_header(s, lock)
nothing
end

function deserialize(s::AbstractSerializer, ::Type{T}) where T<:Base.AbstractLock
lock = T()
Comment on lines +1531 to +1532
Copy link
Member

@tkf tkf Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that all fields of an AbstractLock are lock states, right? I think it's conceivable to have locks with parameters (e.g., back-off delays), and these values have to be serialized. Maybe we can serialize only const fields? It's somewhat too magical but kinda cool too.

In a long run, it'd be nice to have a Serialization API to declare the part of the value that shouldn't be serialized. I needed a rather ugly hack to avoid serialization of process-local scratch space:
https://github.com/JuliaFolds/FLoops.jl/blob/255382d84c187ec10521c0166412e5dfb213471b/src/scratchspace.jl#L45-L48 I don't think we need to solve it here but AbstractLock sounds like a good case study.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked to Jeff, and we discussed how this is better than the current situation, and if someone needs something fancier, they can still implement that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree this PR is an improvement overall.

deserialize_cycle(s, lock)
return lock
end

function serialize(s::AbstractSerializer, cond::Base.GenericCondition)
serialize_cycle_header(s, cond) && return
serialize(s, cond.lock)
nothing
end

function deserialize(s::AbstractSerializer, ::Type{T}) where T<:Base.GenericCondition
lock = deserialize(s)
cond = T(lock)
deserialize_cycle(s, cond)
return cond
end


end
18 changes: 18 additions & 0 deletions stdlib/Serialization/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,21 @@ end
@test_broken f(1) == 2
end
end

let c1 = Threads.Condition()
c2 = Threads.Condition(c1.lock)
lock(c2)
t = @task nothing
Base._wait2(c1, t)
c3, c4 = deserialize(IOBuffer(sprint(serialize, [c1, c2])))::Vector{Threads.Condition}
@test c3.lock === c4.lock
@test islocked(c1)
@test !islocked(c3)
@test !isempty(c1.waitq)
@test isempty(c2.waitq)
@test isempty(c3.waitq)
@test isempty(c4.waitq)
notify(c1)
unlock(c2)
wait(t)
end