Skip to content

Commit c5b6a33

Browse files
committed
fix encoding
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
1 parent e8135c0 commit c5b6a33

File tree

2 files changed

+154
-154
lines changed

2 files changed

+154
-154
lines changed

internal/syncutil/quay.go

+57-57
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
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-
// Quay is a quay with scalable wharves.
21-
type Quay struct {
22-
gate sync.Mutex
23-
wharves map[any]*Wharf
24-
}
25-
26-
// NewQuay creates a virtual scalable quay.
27-
func NewQuay() *Quay {
28-
return &Quay{}
29-
}
30-
31-
// Enter enters a specific wharf, holding a ticket.
32-
// A captain gopher is responsible to dispose the wharf if it is no longer
33-
// needed, using the returned function.
34-
func (q *Quay) Enter(wharfID, ticket any) (*Wharf, <-chan bool, func()) {
35-
q.gate.Lock()
36-
defer q.gate.Unlock()
37-
38-
wharf, ok := q.wharves[wharfID]
39-
if !ok {
40-
wharf = NewWharf()
41-
if q.wharves == nil {
42-
q.wharves = map[any]*Wharf{
43-
wharfID: wharf,
44-
}
45-
} else {
46-
q.wharves[wharfID] = wharf
47-
}
48-
}
49-
50-
return wharf, wharf.Enter(ticket), func() {
51-
q.gate.Lock()
52-
defer q.gate.Unlock()
53-
if wharf.idle() {
54-
delete(q.wharves, wharfID)
55-
}
56-
}
57-
}
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+
// Quay is a quay with scalable wharves.
21+
type Quay struct {
22+
gate sync.Mutex
23+
wharves map[any]*Wharf
24+
}
25+
26+
// NewQuay creates a virtual scalable quay.
27+
func NewQuay() *Quay {
28+
return &Quay{}
29+
}
30+
31+
// Enter enters a specific wharf, holding a ticket.
32+
// A captain gopher is responsible to dispose the wharf if it is no longer
33+
// needed, using the returned function.
34+
func (q *Quay) Enter(wharfID, ticket any) (*Wharf, <-chan bool, func()) {
35+
q.gate.Lock()
36+
defer q.gate.Unlock()
37+
38+
wharf, ok := q.wharves[wharfID]
39+
if !ok {
40+
wharf = NewWharf()
41+
if q.wharves == nil {
42+
q.wharves = map[any]*Wharf{
43+
wharfID: wharf,
44+
}
45+
} else {
46+
q.wharves[wharfID] = wharf
47+
}
48+
}
49+
50+
return wharf, wharf.Enter(ticket), func() {
51+
q.gate.Lock()
52+
defer q.gate.Unlock()
53+
if wharf.idle() {
54+
delete(q.wharves, wharfID)
55+
}
56+
}
57+
}

internal/syncutil/wharf.go

+97-97
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,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-
}
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

Comments
 (0)