This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: add locker util and fix thread-unsafe problems for cdn
Signed-off-by: Starnop <[email protected]>
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"sync" | ||
|
||
cutil "github.com/dragonflyoss/Dragonfly/common/util" | ||
) | ||
|
||
type countRWMutex struct { | ||
count *cutil.AtomicInt | ||
sync.RWMutex | ||
} | ||
|
||
func newCountRWMutex() *countRWMutex { | ||
return &countRWMutex{ | ||
count: cutil.NewAtomicInt(0), | ||
} | ||
} | ||
|
||
func (cr *countRWMutex) reset() { | ||
cr.count.Set(0) | ||
} | ||
|
||
func (cr *countRWMutex) increaseCount() int32 { | ||
cr.count.Add(1) | ||
return cr.count.Get() | ||
} | ||
|
||
func (cr *countRWMutex) decreaseCount() int32 { | ||
cr.count.Add(-1) | ||
return cr.count.Get() | ||
} | ||
|
||
func (cr *countRWMutex) lock(ro bool) { | ||
if ro { | ||
cr.RLock() | ||
return | ||
} | ||
cr.Lock() | ||
} | ||
|
||
func (cr *countRWMutex) unlock(ro bool) { | ||
if ro { | ||
cr.RUnlock() | ||
return | ||
} | ||
cr.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// LockerPool is a set of reader/writer mutual exclusion locks. | ||
type LockerPool struct { | ||
// use syncPool to cache allocated but unused *countRWMutex items for later reuse | ||
syncPool *sync.Pool | ||
|
||
lockerMap map[string]*countRWMutex | ||
sync.Mutex | ||
} | ||
|
||
// NewLockerPool returns a *LockerPool with self-defined prefix. | ||
func NewLockerPool() *LockerPool { | ||
return &LockerPool{ | ||
syncPool: &sync.Pool{ | ||
New: func() interface{} { | ||
return newCountRWMutex() | ||
}, | ||
}, | ||
lockerMap: make(map[string]*countRWMutex), | ||
} | ||
} | ||
|
||
// GetLock locks key. | ||
// If ro(readonly) is true, then it locks key for reading. | ||
// Otherwise, locks key for writing. | ||
func (l *LockerPool) GetLock(key string, ro bool) { | ||
l.Lock() | ||
|
||
locker, ok := l.lockerMap[key] | ||
if !ok { | ||
locker = l.syncPool.Get().(*countRWMutex) | ||
l.lockerMap[key] = locker | ||
} | ||
|
||
locker.increaseCount() | ||
l.Unlock() | ||
|
||
locker.lock(ro) | ||
} | ||
|
||
// ReleaseLock unlocks key. | ||
// If ro(readonly) is true, then it unlocks key for reading. | ||
// Otherwise, unlocks key for writing. | ||
func (l *LockerPool) ReleaseLock(key string, ro bool) { | ||
l.Lock() | ||
defer l.Unlock() | ||
|
||
locker, ok := l.lockerMap[key] | ||
if !ok { | ||
return | ||
} | ||
|
||
locker.unlock(ro) | ||
if locker.decreaseCount() < 1 { | ||
locker.reset() | ||
l.syncPool.Put(locker) | ||
delete(l.lockerMap, key) | ||
} | ||
} |