2022-02-09 19:42:28 +03:00
|
|
|
/*
|
|
|
|
_____ _____ _____ ____ ______ _____ ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| --- | | | | |-----| |---- | | |-----| |----- ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
|
|
|
|
2024-01-02 13:10:39 +03:00
|
|
|
Copyright © 2020-2024 Microsoft Corporation. All rights reserved.
|
2022-02-09 19:42:28 +03:00
|
|
|
Author : <blobfusedev@microsoft.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE
|
|
|
|
*/
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2023-05-10 15:37:10 +03:00
|
|
|
"time"
|
2022-02-09 19:42:28 +03:00
|
|
|
)
|
|
|
|
|
2022-03-25 06:00:33 +03:00
|
|
|
// Lock item for each file
|
2022-02-09 19:42:28 +03:00
|
|
|
type LockMapItem struct {
|
2023-05-10 15:37:10 +03:00
|
|
|
handleCount uint32
|
|
|
|
exLocked bool
|
|
|
|
mtx sync.Mutex
|
|
|
|
downloadTime time.Time
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
2022-03-25 06:00:33 +03:00
|
|
|
|
|
|
|
// Map holding locks for all the files
|
2022-02-09 19:42:28 +03:00
|
|
|
type LockMap struct {
|
|
|
|
locks sync.Map
|
|
|
|
}
|
|
|
|
|
2022-03-25 06:00:33 +03:00
|
|
|
func NewLockMap() *LockMap {
|
|
|
|
return &LockMap{}
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 06:00:33 +03:00
|
|
|
// Map level operations
|
|
|
|
|
|
|
|
// Get the lock item based on file name, if item does not exists create it
|
|
|
|
func (l *LockMap) Get(name string) *LockMapItem {
|
|
|
|
lockIntf, _ := l.locks.LoadOrStore(name, &LockMapItem{handleCount: 0, exLocked: false})
|
|
|
|
item := lockIntf.(*LockMapItem)
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete item from file lock map
|
|
|
|
func (l *LockMap) Delete(name string) {
|
|
|
|
l.locks.Delete(name)
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 06:00:33 +03:00
|
|
|
// Check if this file is already exLocked or not
|
2022-02-09 19:42:28 +03:00
|
|
|
func (l *LockMap) Locked(name string) bool {
|
|
|
|
lockIntf, ok := l.locks.Load(name)
|
|
|
|
if ok {
|
2022-03-25 06:00:33 +03:00
|
|
|
item := lockIntf.(*LockMapItem)
|
|
|
|
return item.exLocked
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-25 06:00:33 +03:00
|
|
|
// Lock Item level operation
|
|
|
|
// Lock this file exclusively
|
|
|
|
func (l *LockMapItem) Lock() {
|
|
|
|
l.mtx.Lock()
|
|
|
|
l.exLocked = true
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 06:00:33 +03:00
|
|
|
// UnLock this file exclusively
|
|
|
|
func (l *LockMapItem) Unlock() {
|
|
|
|
l.exLocked = false
|
|
|
|
l.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the handle count
|
|
|
|
func (l *LockMapItem) Inc() {
|
|
|
|
l.handleCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement the handle count
|
|
|
|
func (l *LockMapItem) Dec() {
|
|
|
|
l.handleCount--
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current handle count
|
|
|
|
func (l *LockMapItem) Count() uint32 {
|
|
|
|
return l.handleCount
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
2023-05-10 15:37:10 +03:00
|
|
|
|
|
|
|
// Set the download time of the file
|
|
|
|
func (l *LockMapItem) SetDownloadTime() {
|
|
|
|
l.downloadTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the download time of the file
|
|
|
|
func (l *LockMapItem) DownloadTime() time.Time {
|
|
|
|
return l.downloadTime
|
|
|
|
}
|