595 bool os::create_thread(Thread* thread, ThreadType thr_type,
596 size_t stack_size) {
597 unsigned thread_id;
598
599 // Allocate the OSThread object
600 OSThread* osthread = new OSThread(NULL, NULL);
601 if (osthread == NULL) {
602 return false;
603 }
604
605 // Initialize the JDK library's interrupt event.
606 // This should really be done when OSThread is constructed,
607 // but there is no way for a constructor to report failure to
608 // allocate the event.
609 HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
610 if (interrupt_event == NULL) {
611 delete osthread;
612 return false;
613 }
614 osthread->set_interrupt_event(interrupt_event);
615 osthread->set_interrupted(false);
616
617 thread->set_osthread(osthread);
618
619 if (stack_size == 0) {
620 switch (thr_type) {
621 case os::java_thread:
622 // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
623 if (JavaThread::stack_size_at_create() > 0) {
624 stack_size = JavaThread::stack_size_at_create();
625 }
626 break;
627 case os::compiler_thread:
628 if (CompilerThreadStackSize > 0) {
629 stack_size = (size_t)(CompilerThreadStackSize * K);
630 break;
631 } // else fall through:
632 // use VMThreadStackSize if CompilerThreadStackSize is not defined
633 case os::vm_thread:
634 case os::pgc_thread:
635 case os::cgc_thread:
667 (unsigned (__stdcall *)(void*)) thread_native_entry,
668 thread,
669 initflag,
670 &thread_id);
671
672 char buf[64];
673 if (thread_handle != NULL) {
674 log_info(os, thread)("Thread started (tid: %u, attributes: %s)",
675 thread_id, describe_beginthreadex_attributes(buf, sizeof(buf), stack_size, initflag));
676 } else {
677 log_warning(os, thread)("Failed to start thread - _beginthreadex failed (%s) for attributes: %s.",
678 os::errno_name(errno), describe_beginthreadex_attributes(buf, sizeof(buf), stack_size, initflag));
679 // Log some OS information which might explain why creating the thread failed.
680 log_info(os, thread)("Number of threads approx. running in the VM: %d", Threads::number_of_threads());
681 LogStream st(Log(os, thread)::info());
682 os::print_memory_info(&st);
683 }
684
685 if (thread_handle == NULL) {
686 // Need to clean up stuff we've allocated so far
687 CloseHandle(osthread->interrupt_event());
688 thread->set_osthread(NULL);
689 delete osthread;
690 return false;
691 }
692
693 Atomic::inc(&os::win32::_os_thread_count);
694
695 // Store info on the Win32 thread into the OSThread
696 osthread->set_thread_handle(thread_handle);
697 osthread->set_thread_id(thread_id);
698
699 // Initial thread state is INITIALIZED, not SUSPENDED
700 osthread->set_state(INITIALIZED);
701
702 // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
703 return true;
704 }
705
706
707 // Free Win32 resources related to the OSThread
708 void os::free_thread(OSThread* osthread) {
709 assert(osthread != NULL, "osthread not set");
710
711 // We are told to free resources of the argument thread,
712 // but we can only really operate on the current thread.
713 assert(Thread::current()->osthread() == osthread,
714 "os::free_thread but not current thread");
715
716 CloseHandle(osthread->thread_handle());
717 CloseHandle(osthread->interrupt_event());
718 delete osthread;
719 }
720
721 static jlong first_filetime;
722 static jlong initial_performance_count;
723 static jlong performance_frequency;
724
725
726 jlong as_long(LARGE_INTEGER x) {
727 jlong result = 0; // initialization to avoid warning
728 set_high(&result, x.HighPart);
729 set_low(&result, x.LowPart);
730 return result;
731 }
732
733
734 jlong os::elapsed_counter() {
735 LARGE_INTEGER count;
736 QueryPerformanceCounter(&count);
737 return as_long(count) - initial_performance_count;
3468 char* os::non_memory_address_word() {
3469 // Must never look like an address returned by reserve_memory,
3470 // even in its subfields (as defined by the CPU immediate fields,
3471 // if the CPU splits constants across multiple instructions).
3472 return (char*)-1;
3473 }
3474
3475 #define MAX_ERROR_COUNT 100
3476 #define SYS_THREAD_ERROR 0xffffffffUL
3477
3478 void os::pd_start_thread(Thread* thread) {
3479 DWORD ret = ResumeThread(thread->osthread()->thread_handle());
3480 // Returns previous suspend state:
3481 // 0: Thread was not suspended
3482 // 1: Thread is running now
3483 // >1: Thread is still suspended.
3484 assert(ret != SYS_THREAD_ERROR, "StartThread failed"); // should propagate back
3485 }
3486
3487
3488
3489 // Short sleep, direct OS call.
3490 //
3491 // ms = 0, means allow others (if any) to run.
3492 //
3493 void os::naked_short_sleep(jlong ms) {
3494 assert(ms < 1000, "Un-interruptable sleep, short time use only");
3495 Sleep(ms);
3496 }
3497
3498 // Windows does not provide sleep functionality with nanosecond resolution, so we
3499 // try to approximate this with spinning combined with yielding if another thread
3500 // is ready to run on the current processor.
3501 void os::naked_short_nanosleep(jlong ns) {
3502 assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");
3503
3504 int64_t start = os::javaTimeNanos();
3505 do {
3506 if (SwitchToThread() == 0) {
3507 // Nothing else is ready to run on this cpu, spin a little
3508 SpinPause();
3576 if (!UseThreadPriorities) return OS_OK;
3577 bool ret = SetThreadPriority(thread->osthread()->thread_handle(), priority) != 0;
3578 return ret ? OS_OK : OS_ERR;
3579 }
3580
3581 OSReturn os::get_native_priority(const Thread* const thread,
3582 int* priority_ptr) {
3583 if (!UseThreadPriorities) {
3584 *priority_ptr = java_to_os_priority[NormPriority];
3585 return OS_OK;
3586 }
3587 int os_prio = GetThreadPriority(thread->osthread()->thread_handle());
3588 if (os_prio == THREAD_PRIORITY_ERROR_RETURN) {
3589 assert(false, "GetThreadPriority failed");
3590 return OS_ERR;
3591 }
3592 *priority_ptr = os_prio;
3593 return OS_OK;
3594 }
3595
3596 void os::interrupt(Thread* thread) {
3597 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
3598 assert(thread->is_Java_thread(), "invariant");
3599 JavaThread* jt = (JavaThread*) thread;
3600 OSThread* osthread = thread->osthread();
3601 osthread->set_interrupted(true);
3602 // More than one thread can get here with the same value of osthread,
3603 // resulting in multiple notifications. We do, however, want the store
3604 // to interrupted() to be visible to other threads before we post
3605 // the interrupt event.
3606 OrderAccess::release();
3607 SetEvent(osthread->interrupt_event());
3608 // For JSR166: unpark after setting status
3609 jt->parker()->unpark();
3610
3611 ParkEvent * ev = thread->_ParkEvent;
3612 if (ev != NULL) ev->unpark();
3613
3614 ev = jt->_SleepEvent;
3615 if (ev != NULL) ev->unpark();
3616 }
3617
3618
3619 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
3620 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
3621
3622 OSThread* osthread = thread->osthread();
3623 // There is no synchronization between the setting of the interrupt
3624 // and it being cleared here. It is critical - see 6535709 - that
3625 // we only clear the interrupt state, and reset the interrupt event,
3626 // if we are going to report that we were indeed interrupted - else
3627 // an interrupt can be "lost", leading to spurious wakeups or lost wakeups
3628 // depending on the timing. By checking thread interrupt event to see
3629 // if the thread gets real interrupt thus prevent spurious wakeup.
3630 bool interrupted = osthread->interrupted() && (WaitForSingleObject(osthread->interrupt_event(), 0) == WAIT_OBJECT_0);
3631 if (interrupted && clear_interrupted) {
3632 osthread->set_interrupted(false);
3633 ResetEvent(osthread->interrupt_event());
3634 } // Otherwise leave the interrupted state alone
3635
3636 return interrupted;
3637 }
3638
3639 // GetCurrentThreadId() returns DWORD
3640 intx os::current_thread_id() { return GetCurrentThreadId(); }
3641
3642 static int _initial_pid = 0;
3643
3644 int os::current_process_id() {
3645 return (_initial_pid ? _initial_pid : _getpid());
3646 }
3647
3648 int os::win32::_vm_page_size = 0;
3649 int os::win32::_vm_allocation_granularity = 0;
3650 int os::win32::_processor_type = 0;
3651 // Processor level is not available on non-NT systems, use vm_version instead
3652 int os::win32::_processor_level = 0;
3653 julong os::win32::_physical_memory = 0;
3654 size_t os::win32::_default_stack_size = 0;
3655
3656 intx os::win32::_os_thread_limit = 0;
3657 volatile intx os::win32::_os_thread_count = 0;
3658
5329 // First, demultiplex/decode time arguments
5330 if (time < 0) { // don't wait
5331 return;
5332 } else if (time == 0 && !isAbsolute) {
5333 time = INFINITE;
5334 } else if (isAbsolute) {
5335 time -= os::javaTimeMillis(); // convert to relative time
5336 if (time <= 0) { // already elapsed
5337 return;
5338 }
5339 } else { // relative
5340 time /= 1000000; // Must coarsen from nanos to millis
5341 if (time == 0) { // Wait for the minimal time unit if zero
5342 time = 1;
5343 }
5344 }
5345
5346 JavaThread* thread = JavaThread::current();
5347
5348 // Don't wait if interrupted or already triggered
5349 if (Thread::is_interrupted(thread, false) ||
5350 WaitForSingleObject(_ParkEvent, 0) == WAIT_OBJECT_0) {
5351 ResetEvent(_ParkEvent);
5352 return;
5353 } else {
5354 ThreadBlockInVM tbivm(thread);
5355 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
5356 thread->set_suspend_equivalent();
5357
5358 WaitForSingleObject(_ParkEvent, time);
5359 ResetEvent(_ParkEvent);
5360
5361 // If externally suspended while waiting, re-suspend
5362 if (thread->handle_special_suspend_equivalent_condition()) {
5363 thread->java_suspend_self();
5364 }
5365 }
5366 }
5367
5368 void Parker::unpark() {
5369 guarantee(_ParkEvent != NULL, "invariant");
|
595 bool os::create_thread(Thread* thread, ThreadType thr_type,
596 size_t stack_size) {
597 unsigned thread_id;
598
599 // Allocate the OSThread object
600 OSThread* osthread = new OSThread(NULL, NULL);
601 if (osthread == NULL) {
602 return false;
603 }
604
605 // Initialize the JDK library's interrupt event.
606 // This should really be done when OSThread is constructed,
607 // but there is no way for a constructor to report failure to
608 // allocate the event.
609 HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
610 if (interrupt_event == NULL) {
611 delete osthread;
612 return false;
613 }
614 osthread->set_interrupt_event(interrupt_event);
615 // We don't call set_interrupted(false) as it will trip the assert in there
616 // as we are not operating on the current thread. We don't need to call it
617 // because the initial state is already correct.
618
619 thread->set_osthread(osthread);
620
621 if (stack_size == 0) {
622 switch (thr_type) {
623 case os::java_thread:
624 // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
625 if (JavaThread::stack_size_at_create() > 0) {
626 stack_size = JavaThread::stack_size_at_create();
627 }
628 break;
629 case os::compiler_thread:
630 if (CompilerThreadStackSize > 0) {
631 stack_size = (size_t)(CompilerThreadStackSize * K);
632 break;
633 } // else fall through:
634 // use VMThreadStackSize if CompilerThreadStackSize is not defined
635 case os::vm_thread:
636 case os::pgc_thread:
637 case os::cgc_thread:
669 (unsigned (__stdcall *)(void*)) thread_native_entry,
670 thread,
671 initflag,
672 &thread_id);
673
674 char buf[64];
675 if (thread_handle != NULL) {
676 log_info(os, thread)("Thread started (tid: %u, attributes: %s)",
677 thread_id, describe_beginthreadex_attributes(buf, sizeof(buf), stack_size, initflag));
678 } else {
679 log_warning(os, thread)("Failed to start thread - _beginthreadex failed (%s) for attributes: %s.",
680 os::errno_name(errno), describe_beginthreadex_attributes(buf, sizeof(buf), stack_size, initflag));
681 // Log some OS information which might explain why creating the thread failed.
682 log_info(os, thread)("Number of threads approx. running in the VM: %d", Threads::number_of_threads());
683 LogStream st(Log(os, thread)::info());
684 os::print_memory_info(&st);
685 }
686
687 if (thread_handle == NULL) {
688 // Need to clean up stuff we've allocated so far
689 thread->set_osthread(NULL);
690 delete osthread;
691 return false;
692 }
693
694 Atomic::inc(&os::win32::_os_thread_count);
695
696 // Store info on the Win32 thread into the OSThread
697 osthread->set_thread_handle(thread_handle);
698 osthread->set_thread_id(thread_id);
699
700 // Initial thread state is INITIALIZED, not SUSPENDED
701 osthread->set_state(INITIALIZED);
702
703 // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
704 return true;
705 }
706
707
708 // Free Win32 resources related to the OSThread
709 void os::free_thread(OSThread* osthread) {
710 assert(osthread != NULL, "osthread not set");
711
712 // We are told to free resources of the argument thread,
713 // but we can only really operate on the current thread.
714 assert(Thread::current()->osthread() == osthread,
715 "os::free_thread but not current thread");
716
717 CloseHandle(osthread->thread_handle());
718 delete osthread;
719 }
720
721 static jlong first_filetime;
722 static jlong initial_performance_count;
723 static jlong performance_frequency;
724
725
726 jlong as_long(LARGE_INTEGER x) {
727 jlong result = 0; // initialization to avoid warning
728 set_high(&result, x.HighPart);
729 set_low(&result, x.LowPart);
730 return result;
731 }
732
733
734 jlong os::elapsed_counter() {
735 LARGE_INTEGER count;
736 QueryPerformanceCounter(&count);
737 return as_long(count) - initial_performance_count;
3468 char* os::non_memory_address_word() {
3469 // Must never look like an address returned by reserve_memory,
3470 // even in its subfields (as defined by the CPU immediate fields,
3471 // if the CPU splits constants across multiple instructions).
3472 return (char*)-1;
3473 }
3474
3475 #define MAX_ERROR_COUNT 100
3476 #define SYS_THREAD_ERROR 0xffffffffUL
3477
3478 void os::pd_start_thread(Thread* thread) {
3479 DWORD ret = ResumeThread(thread->osthread()->thread_handle());
3480 // Returns previous suspend state:
3481 // 0: Thread was not suspended
3482 // 1: Thread is running now
3483 // >1: Thread is still suspended.
3484 assert(ret != SYS_THREAD_ERROR, "StartThread failed"); // should propagate back
3485 }
3486
3487
3488 // Short sleep, direct OS call.
3489 //
3490 // ms = 0, means allow others (if any) to run.
3491 //
3492 void os::naked_short_sleep(jlong ms) {
3493 assert(ms < 1000, "Un-interruptable sleep, short time use only");
3494 Sleep(ms);
3495 }
3496
3497 // Windows does not provide sleep functionality with nanosecond resolution, so we
3498 // try to approximate this with spinning combined with yielding if another thread
3499 // is ready to run on the current processor.
3500 void os::naked_short_nanosleep(jlong ns) {
3501 assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");
3502
3503 int64_t start = os::javaTimeNanos();
3504 do {
3505 if (SwitchToThread() == 0) {
3506 // Nothing else is ready to run on this cpu, spin a little
3507 SpinPause();
3575 if (!UseThreadPriorities) return OS_OK;
3576 bool ret = SetThreadPriority(thread->osthread()->thread_handle(), priority) != 0;
3577 return ret ? OS_OK : OS_ERR;
3578 }
3579
3580 OSReturn os::get_native_priority(const Thread* const thread,
3581 int* priority_ptr) {
3582 if (!UseThreadPriorities) {
3583 *priority_ptr = java_to_os_priority[NormPriority];
3584 return OS_OK;
3585 }
3586 int os_prio = GetThreadPriority(thread->osthread()->thread_handle());
3587 if (os_prio == THREAD_PRIORITY_ERROR_RETURN) {
3588 assert(false, "GetThreadPriority failed");
3589 return OS_ERR;
3590 }
3591 *priority_ptr = os_prio;
3592 return OS_OK;
3593 }
3594
3595 // GetCurrentThreadId() returns DWORD
3596 intx os::current_thread_id() { return GetCurrentThreadId(); }
3597
3598 static int _initial_pid = 0;
3599
3600 int os::current_process_id() {
3601 return (_initial_pid ? _initial_pid : _getpid());
3602 }
3603
3604 int os::win32::_vm_page_size = 0;
3605 int os::win32::_vm_allocation_granularity = 0;
3606 int os::win32::_processor_type = 0;
3607 // Processor level is not available on non-NT systems, use vm_version instead
3608 int os::win32::_processor_level = 0;
3609 julong os::win32::_physical_memory = 0;
3610 size_t os::win32::_default_stack_size = 0;
3611
3612 intx os::win32::_os_thread_limit = 0;
3613 volatile intx os::win32::_os_thread_count = 0;
3614
5285 // First, demultiplex/decode time arguments
5286 if (time < 0) { // don't wait
5287 return;
5288 } else if (time == 0 && !isAbsolute) {
5289 time = INFINITE;
5290 } else if (isAbsolute) {
5291 time -= os::javaTimeMillis(); // convert to relative time
5292 if (time <= 0) { // already elapsed
5293 return;
5294 }
5295 } else { // relative
5296 time /= 1000000; // Must coarsen from nanos to millis
5297 if (time == 0) { // Wait for the minimal time unit if zero
5298 time = 1;
5299 }
5300 }
5301
5302 JavaThread* thread = JavaThread::current();
5303
5304 // Don't wait if interrupted or already triggered
5305 if (thread->is_interrupted(false) ||
5306 WaitForSingleObject(_ParkEvent, 0) == WAIT_OBJECT_0) {
5307 ResetEvent(_ParkEvent);
5308 return;
5309 } else {
5310 ThreadBlockInVM tbivm(thread);
5311 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
5312 thread->set_suspend_equivalent();
5313
5314 WaitForSingleObject(_ParkEvent, time);
5315 ResetEvent(_ParkEvent);
5316
5317 // If externally suspended while waiting, re-suspend
5318 if (thread->handle_special_suspend_equivalent_condition()) {
5319 thread->java_suspend_self();
5320 }
5321 }
5322 }
5323
5324 void Parker::unpark() {
5325 guarantee(_ParkEvent != NULL, "invariant");
|