Skip to content

Commit 7cbc76f

Browse files
authored
Change lock to use time.After (#190)
1 parent 19422b4 commit 7cbc76f

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

testdata/examples/channel/channel.gold.v

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,39 +1668,19 @@ Definition Lock__LockWithTimeoutⁱᵐᵖˡ {ext : ffi_syntax} {go_gctx : GoGlob
16681668
(if: Convert go.untyped_bool go.bool ((![time.Duration] "d") ≤⟨time.Duration⟩ #(W64 0))
16691669
then return: (#false)
16701670
else do: #());;;
1671-
let: "done" := (GoAlloc (go.ChannelType go.sendrecv (go.StructType [
1672-
1673-
])) (GoZeroVal (go.ChannelType go.sendrecv (go.StructType [
1674-
1675-
])) #())) in
1676-
let: "$r0" := ((FuncResolve go.make1 [go.ChannelType go.sendrecv (go.StructType [
1677-
1678-
])] #()) #()) in
1679-
do: ("done" <-[go.ChannelType go.sendrecv (go.StructType [
1680-
1681-
])] "$r0");;;
1682-
let: "$go" := (λ: <>,
1683-
exception_do (do: (let: "$a0" := (![time.Duration] "d") in
1684-
(FuncResolve time.Sleep [] #()) "$a0");;;
1685-
do: (let: "$a0" := (![go.ChannelType go.sendrecv (go.StructType [
1686-
1687-
])] "done") in
1688-
(FuncResolve go.close [go.ChannelType go.sendrecv (go.StructType [
1689-
1690-
])] #()) "$a0");;;
1691-
return: #())
1692-
) in
1693-
do: (Fork ("$go" #()));;;
1694-
return: (let: "$a0" := (Convert (go.ChannelType go.sendrecv (go.StructType [
1671+
let: "$v0" := (CompositeLiteral (go.StructType [
16951672

1696-
])) (go.ChannelType go.recvonly (go.StructType [
1673+
]) (LiteralValue [])) in
1674+
let: "$ch0" := (![go.ChannelType go.sendrecv (go.StructType [
16971675

1698-
])) (![go.ChannelType go.sendrecv (go.StructType [
1676+
])] (StructFieldRef Lock "ch"%go (![go.PointerType Lock] "l"))) in
1677+
let: "$ch1" := (let: "$a0" := (![time.Duration] "d") in
1678+
(FuncResolve time.After [] #()) "$a0") in
1679+
SelectStmt (SelectStmtClauses None [(CommClause (SendCase (go.StructType [
16991680

1700-
])] "done")) in
1701-
(MethodResolve (go.PointerType Lock) "LockIfNotCancelled"%go (![go.PointerType Lock] "l")) "$a0")).
1681+
]) "$ch0" "$v0") (return: (#true))); (CommClause (RecvCase time.Time "$ch1") (return: (#false)))])).
17021682

1703-
(* go: lock.go:69:16 *)
1683+
(* go: lock.go:70:16 *)
17041684
Definition Lock__LockWithDeadlineⁱᵐᵖˡ {ext : ffi_syntax} {go_gctx : GoGlobalContext} : val :=
17051685
λ: "l" "deadline",
17061686
exception_do (let: "l" := (GoAlloc (go.PointerType Lock) "l") in

testdata/examples/channel/lock.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ func (l *Lock) LockWithTimeout(d time.Duration) bool {
5858
if d <= 0 {
5959
return false
6060
}
61-
done := make(chan struct{})
62-
go func() {
63-
time.Sleep(d)
64-
close(done)
65-
}()
66-
return l.LockIfNotCancelled(done)
61+
62+
select {
63+
case l.ch <- struct{}{}:
64+
return true
65+
case <-time.After(d):
66+
return false
67+
}
6768
}
6869

6970
func (l *Lock) LockWithDeadline(deadline time.Time) bool {

0 commit comments

Comments
 (0)