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!)
Previous text:
2003-02-04 19:14: Subject: synchronized
One thing that's nice in Java is the 'synchronized keyword. It would be nice to have that functionality in Pike since it's easier than manually using mutex locks (nicer code):
synchronized void do_something() { ... }
and
mapping foo; void do_something() { synchronized(foo) { work on foo } }
and
void do_something() { ... synchronized { locked region } ... }
would be nice. The first one creates an implicit mutex lock for the entire method, the second one creates a lock bound to a specific variable (i.e you can have multiple methods "locking" the same variable) and the third method works just like the first, except for a specific region rather than the entire method.
Another possibility is to have synchronized classes:
synchronized class { ... }
and variable instanses:
synchronized mapping foo = ([]);
But the first three versions are IMHO more interesting.
How hard would this be to implement in pike? I'm guessing "quite hard", but... :)
/ David Hedbor
Hmmm. BEGIN HUBBE Another reason is that now I can hopefully avoid implementing a bunch of keywords that can be done as function calls instead, example: syncronized { }
becomes: mutex->lock() { }
(I am fixing mutex->lock() to allow a function argument) END HUBBE
mutex->lock doesn't take a function argument yet, does it?
/ Martin Nilsson (Åskblod)
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!)
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!)
pike-devel@lists.lysator.liu.se