Include options.h/statistics.h.
[cacao.git] / threads / locks.c
index 805690b48cdf9032f833d3c1a614036e95130c38..b550f3208cdd73da201e81b78531103e4e34294d 100644 (file)
 
 #include "thread.h"
 #include "locks.h"
+#include "builtin.h"
 
 #include "tables.h"
 #include "native.h"
 #include "loader.h"
+#include "toolbox/memory.h"
 
-static classinfo *class_java_lang_IllegalMonitorStateException;
 
-#if 1
-#define DBG(s)
-#else
-#define DBG(s)   s
-#endif
+#if !defined(NATIVE_THREADS)
 
 extern thread* currentThread;
 
-#if defined(USE_INTERNAL_THREADS)
-
 mutexHashEntry *mutexHashTable;
 int mutexHashTableSize;
 long mutexHashMask;
@@ -57,7 +52,7 @@ initLocks (void)
     int i;
 
     mutexHashTableSize = MUTEX_HASH_TABLE_SIZE;
-    mutexHashTable = (mutexHashEntry*)malloc(sizeof(mutexHashEntry) * mutexHashTableSize);
+    mutexHashTable = MNEW(mutexHashEntry, mutexHashTableSize);
     mutexHashMask = (mutexHashTableSize - 1) << 3;
 
     for (i = 0; i < mutexHashTableSize; ++i)
@@ -71,8 +66,7 @@ initLocks (void)
     }
 
     mutexOverflowTableSize = MUTEX_OVERFLOW_TABLE_SIZE;
-    mutexOverflowTable = (mutexHashEntry*)malloc(sizeof(mutexHashEntry)
-                                                                                                * mutexOverflowTableSize);
+    mutexOverflowTable = MNEW(mutexHashEntry, mutexOverflowTableSize);
 
     firstFreeOverflowEntry = &mutexOverflowTable[0];
 
@@ -88,8 +82,7 @@ initLocks (void)
     mutexOverflowTable[i - 1].next = 0;
 
     conditionHashTableSize = CONDITION_HASH_TABLE_SIZE;
-    conditionHashTable = (conditionHashEntry*)malloc(sizeof(conditionHashEntry)
-                                                                                                        * conditionHashTableSize);
+    conditionHashTable = MNEW(conditionHashEntry, conditionHashTableSize);
     conditionHashMask = (conditionHashTableSize - 1) << 3;
 
     for (i = 0; i < conditionHashTableSize; ++i)
@@ -98,12 +91,9 @@ initLocks (void)
                conditionHashTable[i].condition.cvWaiters = 0;
                conditionHashTable[i].condition.mux = 0;
     }
-
-       /* Load exception classes */
-       class_java_lang_IllegalMonitorStateException =
-               loader_load(utf_new_char("java/lang/IllegalMonitorStateException"));
 }
 
+
 /*
  * Reorders part of the condition hash table. Must be called after an entry has been deleted.
  */
@@ -421,7 +411,7 @@ broadcast_cond (iCv *cond)
 void
 internal_lock_mutex(iMux* mux)
 {
-       assert(blockInts == 1);
+       assert(blockInts > 0);
 
     if (mux->holder == 0)
     {
@@ -452,7 +442,7 @@ internal_unlock_mutex(iMux* mux)
 {
     thread* tid;
 
-       assert(blockInts == 1);
+       assert(blockInts > 0);
 
     assert(mux->holder == currentThread);
     
@@ -463,7 +453,7 @@ internal_unlock_mutex(iMux* mux)
                if (mux->muxWaiters != 0)
                {
                        tid = mux->muxWaiters;
-                       mux->muxWaiters = tid->next;
+                       mux->muxWaiters = tid->vmThread->next;
                        iresumeThread(tid);
                }
     }
@@ -482,10 +472,10 @@ internal_wait_cond(iMux* mux, iCv* cv, s8 timeout)
     DBG( fprintf(stderr, "waiting on %p\n", cv); );
 
     if (mux->holder != currentThread) {
-               exceptionptr = native_new_and_init(class_java_lang_IllegalMonitorStateException);
+               *exceptionptr = new_exception(string_java_lang_IllegalMonitorStateException);
     }
 
-       assert(blockInts == 1);
+       assert(blockInts > 0);
 
     count = mux->count;
     mux->holder = 0;
@@ -495,7 +485,7 @@ internal_wait_cond(iMux* mux, iCv* cv, s8 timeout)
     /* If there's anyone waiting here, wake them up */
     if (mux->muxWaiters != 0) {
                tid = mux->muxWaiters;
-               mux->muxWaiters = tid->next;
+               mux->muxWaiters = tid->vmThread->next;
                iresumeThread(tid);
     }
 
@@ -526,20 +516,20 @@ internal_signal_cond (iCv* cv)
     }
 
     if (cv->mux->holder != currentThread) {
-               exceptionptr = native_new_and_init(class_java_lang_IllegalMonitorStateException);
+               *exceptionptr = new_exception(string_java_lang_IllegalMonitorStateException);
     }
 
-       assert(blockInts == 1);
+       assert(blockInts > 0);
 
     /* Remove one thread from cv list */
     if (cv->cvWaiters != 0) {
                DBG( fprintf(stderr, "releasing a waiter\n"); );
 
                tid = cv->cvWaiters;
-               cv->cvWaiters = tid->next;
+               cv->cvWaiters = tid->vmThread->next;
 
                /* Place it on mux list */
-               tid->next = cv->mux->muxWaiters;
+               tid->vmThread->next = cv->mux->muxWaiters;
                cv->mux->muxWaiters = tid;
     }
 }
@@ -558,14 +548,14 @@ internal_broadcast_cond (iCv* cv)
     }
 
     if (cv->mux->holder != currentThread) {
-               exceptionptr = native_new_and_init(class_java_lang_IllegalMonitorStateException);
+               *exceptionptr = new_exception(string_java_lang_IllegalMonitorStateException);
     }
 
-       assert(blockInts == 1);
+       assert(blockInts > 0);
 
     /* Find the end of the cv list */
     if (cv->cvWaiters) {
-               for (tidp = &cv->cvWaiters; *tidp != 0; tidp = &(*tidp)->next)
+               for (tidp = &cv->cvWaiters; *tidp != 0; tidp = &(*tidp)->vmThread->next)
                        ;
 
                /* Place entire cv list on mux list */