Reworking locks api

This commit is contained in:
Dan Ballard 2011-03-31 22:42:52 -07:00
parent 5c5d71e064
commit 05e650c541
1 changed files with 149 additions and 84 deletions

View File

@ -6,10 +6,11 @@
* NOTES * NOTES
*/ */
/* locks -> [ name:[type, locks], ] */ /* locks -> [ class: [type, locks], ] */
var locks = new Array(); var locks = new Array();
var lockTrys = new Array(); /* -> [ class : lock] (you can only req/acq 1 lock per class */
var myLocks = new Array(); var locksReq = new Array();
var locksAcq = new Array();
function copyobj(arr) { function copyobj(arr) {
c = new Object(); c = new Object();
@ -18,40 +19,69 @@ function copyobj(arr) {
} }
} }
function locksClassType(klass) {
return locks[klass]['type'];
}
function locksClassLocks(klass) {
return locks[klass]['locks'];
}
function registerLockFn(ltype, fnName, fn) { function registerLockFn(ltype, fnName, fn) {
registerFn("locks." +ltype+"."+fnName, fn); registerFn("locks." +ltype+"."+fnName, fn);
} }
/*
function execLockFn(fnName, args) {
type = locksClassType(args['class']);
args['type'] = type;
locks = locksClassLocks(args['class']);
args['locks'] = locks;
return execFn("locks."+type+"."+fnName, args);
}*/
function execLockFn(family, fnName, args) { function locksInitClass(klass, type) {
args["family"] = family; locks[klass]['type'] = type;
ltype = locks[family].type; locks[klass]['locks'] = new Array();
args["locks"] = locks[family].locks;
return execFn("locks."+ltype+"."+fnName, args); locksReq[klass] = new Array();
locksAcq[klass] = new Array();
} }
/**** Lock Functions
* BR lookup(details) # get lock data struct (lookup?)
* BR remove(addr/owner) /**** Core Lock Functions
* BR isLocked(details) * get(table, info) BR -> getLock
* BR aquire(details) # aquire lock (aquire?) * equal(a, b) - > locksEqual
* BR equals(a, b) * ?msg(lock)
* release
* make?
*/ */
registerLockFn("basic", "lookup", function getLock(info) {
type = locksClassType(info['class']);
info['type'] = type;
locks = locksClassLocks(info['class']);
info['locks'] = locks;
return execFn("locks."+type+".get", info);
}
registerLockFn("basic", "get",
function (args) { function (args) {
return args["locks"][args["name"]]; lock = args['locks'][args['name']];
if ( ! lock.name) {
lock.name =args['name'];
lock.locked = false;
lock.type = 'basic';
}
return lock;
}); });
function genRangeCell(name, start, end, next) { function genRangeCell(name, start, end, next) {
var c = new Object(); var c = new Object();
c.name = name; c.name = name;
c.type = 'range';
c.start=start; c.start=start;
c.end = end; c.end = end;
c.next = next; c.next = next;
c.locked = false; c.locked = false;
c.done = false;
return c; return c;
} }
@ -106,7 +136,7 @@ function getRangeLock(rangeLocks, name, start, end) {
} }
registerLockFn("range", "lookup", registerLockFn("range", "get",
function (args) { function (args) {
var l = getRangeLock(arg['locks'] , args['name'], args['start'], args'[end']); var l = getRangeLock(arg['locks'] , args['name'], args['start'], args'[end']);
if (l == null) if (l == null)
@ -114,25 +144,71 @@ function (args) {
return l; return l;
}); });
function lockTypesEqual(a, b) {
registerLockFn("basic", "isLocked", if(a == null || b == null)
function(args) {
var locks = args['locks'];
if (locks[args['name']] == null)
return false; return false;
else if(a['type'] != b['type'])
return locks[args['name']].locked; return false;
}); return true;
}
registerLockFn("range", "isLocked", registerLockFn("basic", "equals",
function (args) { function(args) { // locksEqual(a, b)
var rlock = getRangeLock(args['locks'], args['name'], args['start'], args['end']); var a = args['a'];
if (rlock) var b = arbs['b'];
return rlock.locked; if (!lockTypesEqual(a, b))
return false;
if (a.name == b.name)
return true;
else else
return false; return false;
}); });
registerLockFn("range", "equals",
function(args) { // locksEqual(a, b)
var a = args['a'];
var b = arbs['b'];
if (!lockTypesEqual(a, b))
return false;
log("locksEqual? " + a.name + " (" + a.start + " to " + a.end + ") and " + b.name + " (" + b.start + " to " + b.end + ")");
if (a.name == b.name && a.start == b.start && a.end == b.end) {
log("true");
return true;
} else {
log("false");
return false;
}
});
functions locksEqual(a, b) {
args = new Array();
args['a'] = a;
args['b'] = b;
return execFn("locks."+a['type']+".equals", args);
}
/*** Non Networked Lock functions
* lock
* release
* isLocked
* removeLocks
*/
function isLocked(lock) {
return lock.locked;
}
function lock(lock) {
lock['locked'] = true;
}
function release(lock) {
lock['locked'] = false;
}
/************** REVIEW HERE ******************/
registerLockFn("basic", "remove", registerLockFn("basic", "remove",
function(args) { function(args) {
@ -183,6 +259,43 @@ function genGetLockMsg() {
return m; return m;
} }
registerLockFn("basic", "release",
function (args) {
var lock = myLocks[type];
myLocks[type] = null;
lock.locked = false;
var m = new Object();
m["query"] = "releaseLock";
m["type"] = type;
m["name"] = lock.name;
if (type == "range") {
m["start"] = lock.start;
m["end"] = lock.end;
}
bcastMsg(m);
}
/**** Netowkr Lock using functions
* aquire
* grant(resp, lock
* deny(resp, lock
* handleLockReq
* handleLockResp
* lockGranted
* checkLocks
* ...
*/
registerLockFn("basic", "aquire", registerLockFn("basic", "aquire",
function(args) { function(args) {
var locks = args['locks']; var locks = args['locks'];
@ -234,54 +347,6 @@ function(args) {
bcastMsg(m); bcastMsg(m);
}); });
function lockTypesEqual(a, b) {
if(a == null || b == null)
return false;
if(a['type'] != b['type'])
return false;
return true;
}
registerLockFn("basic", "equals",
function(args) { // locksEqual(a, b)
var a = args['a'];
var b = arbs['b'];
if (!lockTypesEqual(a, b))
return false;
if (a.name == b.name)
return true;
else
return false;
});
registerLockFn("range", "equals",
function(args) { // locksEqual(a, b)
var a = args['a'];
var b = arbs['b'];
if (!lockTypesEqual(a, b))
return false;
log("locksEqual? " + a.name + " (" + a.start + " to " + a.end + ") and " + b.name + " (" + b.start + " to " + b.end + ")");
if (a.name == b.name && a.start == b.start && a.end == b.end) {
log("true");
return true;
} else {
log("false");
return false;
}
});
/**** Logical Lock using functions
* grant(resp, lock
* deny(resp, lock
* handleLockReq
* handleLockResp
* lockGranted
* checkLocks
* ...
*/
function sendReqs(addr) { function sendReqs(addr) {
for(ltype in lockTrys) { for(ltype in lockTrys) {
for (i in lockTrys[ltype]) { for (i in lockTrys[ltype]) {