wait/notifyAll like in Java
Juan Jose Comellas
jcomellas at gmail.com
Thu Mar 1 04:56:01 PST 2007
To achieve what you mention we need to combine D's Object monitor with a
condition variable. We can create this in Tango based on the
tango.util.locks.Condition class, but we'd need access to each Object's
hidden monitor member variable. There is an Object struct in the
src/phobos/internal/mars.h file present in DMD's distribution that holds
the pointer to the monitor (which is implemented as a pthread_mutex_t on
Linux and a CRITICAL_SECTION on Windows) and the monitor functions are in
the monitor.c file in the same directory. If we could add a function like
the following one to this file we'd have all we need:
void *_d_monitorget(Object *);
Another possibility would be to add this behavior directly to DMD and have
it available everywhere. It would mean making each Object that is
synchronized a little bit more heavyweight. On Linux we would have to add a
pthread_cond_t to the monitor and on Windows we'd have to emulate the
condition variable with an extra mutex, semaphore and event.
Which option seems more attractive to everybody?
Frank Benoit (keinfarbton) wrote:
> While porting java stuff, i come to this:
>
> How can I implement a JObject class as a base of all the ported classes
> to have wait/notify/notifyAll with the behaviour like in Java.
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html
>
> The "synchronized" of D objects, already uses some monitor.
> Is it possible to access and use it for such an implementation?
More information about the Digitalmars-d
mailing list