package configurationLockExample import ( "sync" )
type _lockReference struct { fileIdentifier map[string]*sync.Mutex var ( _configurationLock _lockReference _lockReferenceLock sync.RWMutex ) func init() { _configurationLock.fileIdentifier = make(map[string]*sync.Mutex) } func (lockReference *_lockReference) Lock(identifier string) { _lockReferenceLock.Lock() if _, hasKey := lockReference.fileIdentifier[identifier]; hasKey { // - reserved for future development; intentionally left blank } else { // mutex not already constructed in referenced map; construct it lockReference.fileIdentifier[identifier] = &sync.Mutex{} } lockReference.fileIdentifier[identifier].Lock() _lockReferenceLock.Unlock() } func (lockReference *_lockReference) Unlock(identifier string) { _lockReferenceLock.Lock() defer func() { errorReturn := recover() _lockReferenceLock.Unlock() }() lockReference.fileIdentifier[identifier].Unlock() } func (lockReference *_lockReference) Delete(identifier string) { _lockReferenceLock.Lock() if _, hasKey := lockReference.fileIdentifier[identifier]; hasKey delete(lockReference.fileIdentifier, identifier) } _lockReferenceLock.Unlock() } func StoreConfiguration(configurationFile string, recordName string, recordContent string) error { _configurationLock.Lock(configurationFile) defer _configurationLock.Delete(configurationFile) defer _configurationLock.Unlock(configurationFile) // handle configuration stuff here } On Thursday, December 29, 2016 at 1:13:50 PM UTC-6, Dave Cheney wrote: > > Could you show some code, perhaps we can spot the point you're going wrong. > > On Friday, 30 December 2016 05:43:19 UTC+11, Eric Brown wrote: >> >> Thanks Val, I actually tried that approach... and failed. Perhaps I >> incorporated it wrong. I put the handling of locking/unlocking of the >> map[string]*sync.Mutex in it's own function, which was encapsulated in the >> global mutex like you suggested. Only problem was, was that the database >> function ran once... because when it went to call the function that handled >> the unlocking of the map[string]*sync.Mutex, it was already locked by a >> previous goroutine trying to lock the map[string]*sync.Mutex. >> >> Hahaha... I hope I made sense. My brain is hurting here. It doesn't >> help that I'm fairly new to this type of stuff in Go. Appreciate you >> assistance, however. Thank you! >> >> >> >> On Thursday, December 29, 2016 at 1:28:34 AM UTC-6, Val wrote: >>> >>> Hello Eric >>> You may use a map[string]*sync.Mutex where key is the DB filepath, and >>> protect that map with a global mutex. >>> >>> Note that a mutex value must not be copied, often it is necessary to use >>> pointers. >>> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.