+ /* These next two statements cannot overflow. */
+ usec += nanos / 1000;
+ usec += (millis % 1000) * 1000;
+ /* These two statements could overflow only if tv.tv_sec was
+ insanely large. */
+ seconds += millis / 1000;
+ seconds += usec / 1000000;
+
+ 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
+ /* This next statement also cannot overflow. */
+ ts.tv_nsec = (usec % 1000000) * 1000 + (nanos % 1000);
+ }
+
+ _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
+ java::lang::Thread *current_obj = _Jv_ThreadCurrent ();
+
+ pthread_mutex_lock (¤t->wait_mutex);
+
+ // Now that we hold the wait mutex, check if this thread has been
+ // interrupted already.
+ if (current_obj->interrupt_flag)
+ {
+ pthread_mutex_unlock (¤t->wait_mutex);
+ return _JV_INTERRUPTED;
+ }
+
+ // Set the thread's state.
+ JvSetThreadState holder (current_obj, new_state);
+
+ // Add this thread to the cv's wait set.
+ current->next = NULL;
+
+ if (cv->first == NULL)
+ cv->first = current;