-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
snes: introduce UseDeviceMemory; introduce ExclusiveUse and BaseDevic…
…eDriver
- Loading branch information
1 parent
0734ce7
commit dafd48b
Showing
9 changed files
with
224 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package snes | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type BaseDeviceDriver struct { | ||
driver DeviceDriver | ||
|
||
// track opened devices by URI | ||
devicesRw sync.RWMutex | ||
devicesMap map[string]Device | ||
} | ||
|
||
func (b *BaseDeviceDriver) UseDevice(ctx context.Context, deviceKey string, openDevice func() (Device, error), use DeviceUser) (err error) { | ||
var device Device | ||
var ok bool | ||
|
||
b.devicesRw.RLock() | ||
device, ok = b.devicesMap[deviceKey] | ||
b.devicesRw.RUnlock() | ||
|
||
if !ok { | ||
device, err = openDevice() | ||
if err != nil { | ||
return | ||
} | ||
|
||
b.devicesRw.Lock() | ||
if b.devicesMap == nil { | ||
b.devicesMap = make(map[string]Device) | ||
} | ||
b.devicesMap[deviceKey] = device | ||
b.devicesRw.Unlock() | ||
} | ||
|
||
err = device.ExclusiveUse(ctx, use) | ||
|
||
if device.IsClosed() { | ||
b.devicesRw.Lock() | ||
if b.devicesMap == nil { | ||
b.devicesMap = make(map[string]Device) | ||
} | ||
delete(b.devicesMap, deviceKey) | ||
b.devicesRw.Unlock() | ||
} | ||
|
||
return | ||
} | ||
|
||
func (b *BaseDeviceDriver) Get(deviceKey string) (Device, bool) { | ||
b.devicesRw.RLock() | ||
device, ok := b.devicesMap[deviceKey] | ||
b.devicesRw.RUnlock() | ||
|
||
return device, ok | ||
} |
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
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
Oops, something went wrong.