Example #1
0
// Locks a table in exclusive mode.
func (tr *Transaction) ELock(t *table.Table) int {
	existingLocks, status := LocksOf(t)
	if status != st.OK {
		return status
	}
	// Do not lock if other transaction(s) have shared or exclusively locked the table.
	if (existingLocks.Exclusive != 0 && existingLocks.Exclusive != tr.id) ||
		(len(existingLocks.Shared) == 1 && existingLocks.Shared[0] != tr.id) ||
		(len(existingLocks.Shared) > 1) {
		return st.CannotLockInExclusive
	}
	// Release previously acquired shared lock, if any.
	if len(existingLocks.Shared) == 1 && existingLocks.Shared[0] == tr.id {
		status = tr.unlock(t)
		if status != st.OK {
			return status
		}
	}
	// Create exclusive lock file.
	status = util.CreateAndWrite(t.Path+t.Name+".exclusive", tr.ID)
	if status != st.OK {
		return status
	}
	tr.Locked = append(tr.Locked[:], t)
	return st.OK
}
Example #2
0
func (tr *Transaction) SLock(t *table.Table) int {
	existingLocks, status := LocksOf(t)
	if status != st.OK {
		return status
	}
	// Do not lock if another transaction has locked the table exclusively.
	if existingLocks.Exclusive != 0 && existingLocks.Exclusive != tr.id {
		return st.CannotLockInShared
	}
	// Release previously acquired exclusive lock, if any.
	if existingLocks.Exclusive == tr.id {
		status = tr.unlock(t)
		if status != st.OK {
			return status
		}
	}
	// Create shared lock file.
	status = util.CreateAndWrite(t.Path+t.Name+".shared/"+tr.ID, "")
	if status != st.OK {
		return status
	}
	tr.Locked = append(tr.Locked[:], t)
	return st.OK
}