|
1 |
| -/* |
2 |
| -Copyright The ORAS Authors. |
3 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| -you may not use this file except in compliance with the License. |
5 |
| -You may obtain a copy of the License at |
6 |
| -
|
7 |
| -http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| -
|
9 |
| -Unless required by applicable law or agreed to in writing, software |
10 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| -See the License for the specific language governing permissions and |
13 |
| -limitations under the License. |
14 |
| -*/ |
15 |
| - |
16 |
| -package syncutil |
17 |
| - |
18 |
| -import "sync" |
19 |
| - |
20 |
| -// Wharf is a wharf with ferries commanded by an elected gopher. |
21 |
| -type Wharf struct { |
22 |
| - gate sync.Mutex |
23 |
| - closed bool |
24 |
| - ferry []any |
25 |
| - ferryCaptain chan bool |
26 |
| - platform []any |
27 |
| - platformCaptain chan bool |
28 |
| -} |
29 |
| - |
30 |
| -// NewWharf creates a virtual wharf with ferries. |
31 |
| -func NewWharf() *Wharf { |
32 |
| - return &Wharf{} |
33 |
| -} |
34 |
| - |
35 |
| -// Enter enters the wharf, holding a ticket. |
36 |
| -// A channel is returned to indicate if the current gopher is elected as a |
37 |
| -// captain. |
38 |
| -// If a gopher is elected as a caption, it is responsible to Close() the gate |
39 |
| -// and set sail to process the tickets of gophers on boarded, and Arrive() once |
40 |
| -// all tickets are checked. |
41 |
| -// Otherwise, a gopher is known to be a passenger and it can check its ticket. |
42 |
| -func (w *Wharf) Enter(ticket any) <-chan bool { |
43 |
| - w.gate.Lock() |
44 |
| - defer w.gate.Unlock() |
45 |
| - |
46 |
| - if w.closed { |
47 |
| - if w.platformCaptain == nil { |
48 |
| - w.platformCaptain = make(chan bool, 1) |
49 |
| - } |
50 |
| - w.platform = append(w.platform, ticket) |
51 |
| - return w.platformCaptain |
52 |
| - } |
53 |
| - |
54 |
| - if w.ferryCaptain == nil { |
55 |
| - w.ferryCaptain = make(chan bool, 1) |
56 |
| - w.ferryCaptain <- true |
57 |
| - } |
58 |
| - w.ferry = append(w.ferry, ticket) |
59 |
| - return w.ferryCaptain |
60 |
| -} |
61 |
| - |
62 |
| -// Close closes the gate for onboarding, returning the tickets of all on boarded |
63 |
| -// gophers. |
64 |
| -func (w *Wharf) Close() []any { |
65 |
| - w.gate.Lock() |
66 |
| - defer w.gate.Unlock() |
67 |
| - |
68 |
| - w.closed = true |
69 |
| - return w.ferry |
70 |
| -} |
71 |
| - |
72 |
| -// Arrive notifies all passengers that the ferry has arrived its destination. |
73 |
| -// Onboarding gate is now open. |
74 |
| -func (w *Wharf) Arrive() { |
75 |
| - close(w.ferryCaptain) |
76 |
| - |
77 |
| - w.gate.Lock() |
78 |
| - defer w.gate.Unlock() |
79 |
| - |
80 |
| - w.closed = false |
81 |
| - w.ferry = w.platform |
82 |
| - w.ferryCaptain = w.platformCaptain |
83 |
| - w.platform = nil |
84 |
| - w.platformCaptain = nil |
85 |
| - |
86 |
| - if w.ferryCaptain != nil { |
87 |
| - w.ferryCaptain <- true |
88 |
| - } |
89 |
| -} |
90 |
| - |
91 |
| -// idle returns true if the wharf is not actively used. |
92 |
| -func (w *Wharf) idle() bool { |
93 |
| - w.gate.Lock() |
94 |
| - defer w.gate.Unlock() |
95 |
| - |
96 |
| - return (w.closed && w.platform == nil) || (!w.closed && w.ferry == nil) |
97 |
| -} |
| 1 | +/* |
| 2 | +Copyright The ORAS Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package syncutil |
| 17 | + |
| 18 | +import "sync" |
| 19 | + |
| 20 | +// Wharf is a wharf with ferries commanded by an elected gopher. |
| 21 | +type Wharf struct { |
| 22 | + gate sync.Mutex |
| 23 | + closed bool |
| 24 | + ferry []any |
| 25 | + ferryCaptain chan bool |
| 26 | + platform []any |
| 27 | + platformCaptain chan bool |
| 28 | +} |
| 29 | + |
| 30 | +// NewWharf creates a virtual wharf with ferries. |
| 31 | +func NewWharf() *Wharf { |
| 32 | + return &Wharf{} |
| 33 | +} |
| 34 | + |
| 35 | +// Enter enters the wharf, holding a ticket. |
| 36 | +// A channel is returned to indicate if the current gopher is elected as a |
| 37 | +// captain. |
| 38 | +// If a gopher is elected as a caption, it is responsible to Close() the gate |
| 39 | +// and set sail to process the tickets of gophers on boarded, and Arrive() once |
| 40 | +// all tickets are checked. |
| 41 | +// Otherwise, a gopher is known to be a passenger and it can check its ticket. |
| 42 | +func (w *Wharf) Enter(ticket any) <-chan bool { |
| 43 | + w.gate.Lock() |
| 44 | + defer w.gate.Unlock() |
| 45 | + |
| 46 | + if w.closed { |
| 47 | + if w.platformCaptain == nil { |
| 48 | + w.platformCaptain = make(chan bool, 1) |
| 49 | + } |
| 50 | + w.platform = append(w.platform, ticket) |
| 51 | + return w.platformCaptain |
| 52 | + } |
| 53 | + |
| 54 | + if w.ferryCaptain == nil { |
| 55 | + w.ferryCaptain = make(chan bool, 1) |
| 56 | + w.ferryCaptain <- true |
| 57 | + } |
| 58 | + w.ferry = append(w.ferry, ticket) |
| 59 | + return w.ferryCaptain |
| 60 | +} |
| 61 | + |
| 62 | +// Close closes the gate for onboarding, returning the tickets of all on boarded |
| 63 | +// gophers. |
| 64 | +func (w *Wharf) Close() []any { |
| 65 | + w.gate.Lock() |
| 66 | + defer w.gate.Unlock() |
| 67 | + |
| 68 | + w.closed = true |
| 69 | + return w.ferry |
| 70 | +} |
| 71 | + |
| 72 | +// Arrive notifies all passengers that the ferry has arrived its destination. |
| 73 | +// Onboarding gate is now open. |
| 74 | +func (w *Wharf) Arrive() { |
| 75 | + close(w.ferryCaptain) |
| 76 | + |
| 77 | + w.gate.Lock() |
| 78 | + defer w.gate.Unlock() |
| 79 | + |
| 80 | + w.closed = false |
| 81 | + w.ferry = w.platform |
| 82 | + w.ferryCaptain = w.platformCaptain |
| 83 | + w.platform = nil |
| 84 | + w.platformCaptain = nil |
| 85 | + |
| 86 | + if w.ferryCaptain != nil { |
| 87 | + w.ferryCaptain <- true |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// idle returns true if the wharf is not actively used. |
| 92 | +func (w *Wharf) idle() bool { |
| 93 | + w.gate.Lock() |
| 94 | + defer w.gate.Unlock() |
| 95 | + |
| 96 | + return (w.closed && w.platform == nil) || (!w.closed && w.ferry == nil) |
| 97 | +} |
0 commit comments