Commit 6d215bb9 authored by djq2's avatar djq2
Browse files

more

parent 64a11a66
......@@ -96,7 +96,11 @@ func (host *Server) LockAccount(key string, ltype string) bool {
acc.Upgrade.Lock()
acc.RW.RLock()
acc.Upgrade.Unlock()
acc.ReaderLock.Lock()
acc.Readers++
acc.LockType = "R"
acc.ReaderLock.Unlock()
// fmt.Println("Couldnt get lock after 50 attempts.")
} else {
......@@ -122,6 +126,7 @@ func (host *Server) LockAccount(key string, ltype string) bool {
acc.Upgrade.Lock()
acc.RW.Lock()
acc.Upgrade.Unlock()
acc.LockType = "W"
// fmt.Println("Couldnt get lock after 50 attempts.")
......@@ -177,12 +182,24 @@ func (host *Server) Release(account string, commit bool) bool {
// Read Lock
if acc.LockType == "R" {
acc.LockType = ""
acc.ReaderLock.Lock()
acc.Readers--
if acc.Readers == 0 {
acc.LockType = ""
}
acc.ReaderLock.Unlock()
acc.RW.RUnlock()
// Write Lock
} else {
acc.LockType = ""
acc.ReaderLock.Lock()
if acc.Readers == 0 {
acc.LockType = ""
} else {
acc.LockType = "R"
}
acc.ReaderLock.Unlock()
acc.RW.Unlock()
}
......@@ -528,7 +545,7 @@ func (host *Server) withdraw(operation Client_msg) {
val.Balance -= operation.Amount
host.Tr.Accounts[local_key] = val
// Lock not "W", need upgrade
// Lock not "W", need upgrade
} else {
// Local upgrade
if operation.Branch == host.Id {
......@@ -537,14 +554,14 @@ func (host *Server) withdraw(operation Client_msg) {
val.LockType = "W"
host.Tr.Accounts[local_key] = val
// Remote upgrade
// Remote upgrade
} else {
req_out.Request = "UPGRADE"
go host.server_write(operation.Branch, req_out)
resp_in := <-host.Trans_chan
if resp_in.Status {
val.Balance += operation.Amount
val.Balance -= operation.Amount
val.LockType = "W"
host.Tr.Accounts[local_key] = val
} else {
......@@ -646,7 +663,7 @@ func (host *Server) balance(operation Client_msg) {
host.abort()
return
// Else grab read lock
// Else grab read lock
} else {
realAcc := cast.(*Account)
......@@ -770,6 +787,7 @@ func (host *Server) commit() {
// POSSIBLE TIMING ISSUE IF FUNCTION ENDS WHILE STILL WAITING ON CHANNEL RESPONSES
go host.server_write(data.Branch, msg_out)
}
}
......
......@@ -74,6 +74,7 @@ type Account struct{
LockType string // "R" or "W"
Readers int
Committed bool // True if account has new, but uncommitted - May need to add conditional wait
ReaderLock *sync.Mutex
}
// type Msg struct{ // For testing
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment