So THAT is how implicit lambdas work. :)
Also even though it does work in one case (or two = mutex and mapping), it doesn't work for other types. Naturally one can make such a method more universal by checking the type of the first attribute, or by using a mapping with <lockedobj>:mutex:
Synchronized.pike:
private mapping locks = ([]); void create() { set_weak_flag(locks, Pike.WEAK_INDICES); }
mixed synchronized(mixed var, function f) { Thread.Mutex m = locks[var] || (locks[var]= Thread.Mutex()); object key = m->lock(); mixed r = f(); destruct(key); return r; }
A similar construct that can be done would use Synchronized class:
class Synchronized { Thread.Mutex mutex = Thread.Mutex(); object wrapee void create(object o) { wrapee = o; }
mixed `[](mixed var) { object key = mutex->lock(); mixed ret = wrapee[var]; destruct(key); return ret; } [... wrap other methods ...] }
object sync_foo = Synchronized(Foo());
...
The above would be nice to have generally available, perhaps as Thread.Synchronized (which you can inherot to use synchronized(foo) { }) with Thread.Synchronized.Wrapper for the class.
Comments on that?
/ David Hedbor
Previous text:
2003-02-04 22:56: Subject: synchronized
mixed synchronized(Thread.Mutex|mapping m, function f) { mixed r; if(mappingp(m)) m = m->_mutex; object key = m->lock(); r = f(); destruct(key); return r; }
mapping foo; void do_something() { synchronized(foo) { work on foo }; }
should work already, provided you place a mutex in the mapping under the index "_mutex" (or whatever index you choose to look under in the method "synchronized").
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)