OSDN Git Service

PR target/20754
[pf3gnuchains/gcc-fork.git] / libjava / posix-threads.cc
index f77e2f4..6ef50bd 100644 (file)
@@ -92,14 +92,33 @@ _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
     return _JV_NOT_OWNER;
 
   struct timespec ts;
-  jlong m, startTime;
 
   if (millis > 0 || nanos > 0)
     {
-      startTime = java::lang::System::currentTimeMillis();
-      m = millis + startTime;
-      ts.tv_sec = m / 1000; 
-      ts.tv_nsec = ((m % 1000) * 1000000) + nanos; 
+      // Calculate the abstime corresponding to the timeout.
+      // Everything is in milliseconds.
+      //
+      // We use `unsigned long long' rather than jlong because our
+      // caller may pass up to Long.MAX_VALUE millis.  This would
+      // overflow the range of a jlong when added to the current time.
+      
+      unsigned long long startTime 
+       = (unsigned long long)java::lang::System::currentTimeMillis();
+      unsigned long long m = (unsigned long long)millis + startTime;
+      unsigned long long seconds = m / 1000; 
+
+      ts.tv_sec = seconds;
+      if (ts.tv_sec < 0 || (unsigned long long)ts.tv_sec != seconds)
+        {
+          // We treat a timeout that won't fit into a struct timespec
+          // as a wait forever.
+          millis = nanos = 0;
+        }
+      else
+        {
+          m %= 1000;
+          ts.tv_nsec = m * 1000000 + (unsigned long long)nanos;
+        }
     }
 
   _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
@@ -311,6 +330,21 @@ _Jv_InitThreads (void)
   // Block SIGCHLD here to ensure that any non-Java threads inherit the new 
   // signal mask.
   block_sigchld();
+
+  // Check/set the thread stack size.
+  size_t min_ss = 32 * 1024;
+  
+  if (sizeof (void *) == 8)
+    // Bigger default on 64-bit systems.
+    min_ss *= 2;
+
+#ifdef PTHREAD_STACK_MIN
+  if (min_ss < PTHREAD_STACK_MIN)
+    min_ss = PTHREAD_STACK_MIN;
+#endif
+  
+  if (gcj::stack_size > 0 && gcj::stack_size < min_ss)
+    gcj::stack_size = min_ss;
 }
 
 _Jv_Thread_t *
@@ -430,6 +464,14 @@ _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
   pthread_attr_init (&attr);
   pthread_attr_setschedparam (&attr, &param);
   pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
+  
+  // Set stack size if -Xss option was given.
+  if (gcj::stack_size > 0)
+    {
+      int e = pthread_attr_setstacksize (&attr, gcj::stack_size);
+      if (e != 0)
+       JvFail (strerror (e));
+    }
 
   info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
   info->method = meth;