Signed-off-by: Tomek Grabiec <tgrab...@gmail.com>
---
 vm/monitor.c |   78 +++++++++++++++++++++++++--------------------------------
 1 files changed, 34 insertions(+), 44 deletions(-)

diff --git a/vm/monitor.c b/vm/monitor.c
index 4a22584..5705954 100644
--- a/vm/monitor.c
+++ b/vm/monitor.c
@@ -141,10 +141,9 @@ int vm_monitor_unlock(struct vm_monitor *mon)
        return err;
 }
 
-int vm_monitor_timed_wait(struct vm_monitor *mon, long long ms, int ns)
+static int vm_monitor_do_wait(struct vm_monitor *mon, struct timespec 
*timespec)
 {
        struct vm_thread *self;
-       struct timespec timespec;
        int old_lock_count;
        int err;
 
@@ -154,33 +153,28 @@ int vm_monitor_timed_wait(struct vm_monitor *mon, long 
long ms, int ns)
                return -1;
        }
 
-       /*
-        * XXX: we must use CLOCK_REALTIME here because
-        * pthread_cond_timedwait() uses this clock.
-        */
-       clock_gettime(CLOCK_REALTIME, &timespec);
-
-       timespec.tv_sec += ms / 1000;
-       timespec.tv_nsec += (long)ns + (long)(ms % 1000) * 1000000l;
-
-       if (timespec.tv_nsec >= 1000000000l) {
-               timespec.tv_sec++;
-               timespec.tv_nsec -= 1000000000l;
-       }
-
        old_lock_count = mon->lock_count;
-
        mon->lock_count = 0;
        vm_monitor_set_owner(mon, NULL);
 
        self = vm_thread_self();
 
-       vm_thread_set_state(self, VM_THREAD_STATE_TIMED_WAITING);
-       err = pthread_cond_timedwait(&mon->cond, &mon->mutex, &timespec);
-       vm_thread_set_state(self, VM_THREAD_STATE_RUNNABLE);
+       pthread_mutex_lock(&self->mutex);
+       if (timespec != NULL)
+               self->state = VM_THREAD_STATE_TIMED_WAITING;
+       else
+               self->state = VM_THREAD_STATE_WAITING;
+
+       pthread_mutex_unlock(&self->mutex);
 
-       if (err == ETIMEDOUT)
-               err = 0;
+       if (timespec) {
+               err = pthread_cond_timedwait(&mon->cond, &mon->mutex, timespec);
+               if (err == ETIMEDOUT)
+                       err = 0;
+       } else
+               err = pthread_cond_wait(&mon->cond, &mon->mutex);
+
+       vm_thread_set_state(self, VM_THREAD_STATE_RUNNABLE);
 
        vm_monitor_set_owner(mon, self);
        mon->lock_count = old_lock_count;
@@ -189,34 +183,30 @@ int vm_monitor_timed_wait(struct vm_monitor *mon, long 
long ms, int ns)
        return err;
 }
 
-int vm_monitor_wait(struct vm_monitor *mon)
+int vm_monitor_timed_wait(struct vm_monitor *mon, long long ms, int ns)
 {
-       struct vm_thread *self;
-       int old_lock_count;
-       int err;
-
-       self = vm_thread_self();
-
-       if (vm_monitor_get_owner(mon) != vm_thread_self()) {
-               signal_new_exception(vm_java_lang_IllegalMonitorStateException,
-                                    NULL);
-               return -1;
-       }
+       struct timespec timespec;
 
-       old_lock_count = mon->lock_count;
+       /*
+        * XXX: we must use CLOCK_REALTIME here because
+        * pthread_cond_timedwait() uses this clock.
+        */
+       clock_gettime(CLOCK_REALTIME, &timespec);
 
-       mon->lock_count = 0;
-       vm_monitor_set_owner(mon, NULL);
+       timespec.tv_sec += ms / 1000;
+       timespec.tv_nsec += (long)ns + (long)(ms % 1000) * 1000000l;
 
-       vm_thread_set_state(self, VM_THREAD_STATE_WAITING);
-       err = pthread_cond_wait(&mon->cond, &mon->mutex);
-       vm_thread_set_state(self, VM_THREAD_STATE_RUNNABLE);
+       if (timespec.tv_nsec >= 1000000000l) {
+               timespec.tv_sec++;
+               timespec.tv_nsec -= 1000000000l;
+       }
 
-       vm_monitor_set_owner(mon, self);
-       mon->lock_count = old_lock_count;
+       return vm_monitor_do_wait(mon, &timespec);
+}
 
-       /* TODO: check if thread has been interrupted. */
-       return err;
+int vm_monitor_wait(struct vm_monitor *mon)
+{
+       return vm_monitor_do_wait(mon, NULL);
 }
 
 int vm_monitor_notify(struct vm_monitor *mon)
-- 
1.6.0.4


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to