comparison src/share/vm/runtime/thread.cpp @ 1930:2d26b0046e0d

Merge.
author Thomas Wuerthinger <wuerthinger@ssw.jku.at>
date Tue, 30 Nov 2010 14:53:30 +0100
parents 7cf1952ec5fb 5caa30ea147b
children 06f017f7daa7
comparison
equal deleted inserted replaced
1484:6b7001391c97 1930:2d26b0046e0d
1 /* 1 /*
2 * Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved. 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
14 * 14 *
15 * You should have received a copy of the GNU General Public License version 15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation, 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 * 18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * CA 95054 USA or visit www.sun.com if you need additional information or 20 * or visit www.oracle.com if you need additional information or have any
21 * have any questions. 21 * questions.
22 * 22 *
23 */ 23 */
24 24
25 # include "incls/_precompiled.incl" 25 # include "incls/_precompiled.incl"
26 # include "incls/_thread.cpp.incl" 26 # include "incls/_thread.cpp.incl"
137 _current_waiting_monitor = NULL; 137 _current_waiting_monitor = NULL;
138 _num_nested_signal = 0; 138 _num_nested_signal = 0;
139 omFreeList = NULL ; 139 omFreeList = NULL ;
140 omFreeCount = 0 ; 140 omFreeCount = 0 ;
141 omFreeProvision = 32 ; 141 omFreeProvision = 32 ;
142 omInUseList = NULL ;
143 omInUseCount = 0 ;
142 144
143 _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true); 145 _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true);
144 _suspend_flags = 0; 146 _suspend_flags = 0;
145 147
146 // thread-specific hashCode stream generator state - Marsaglia shift-xor form 148 // thread-specific hashCode stream generator state - Marsaglia shift-xor form
803 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being 805 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
804 // used for compilation in the future. If that change is made, the need for these methods 806 // used for compilation in the future. If that change is made, the need for these methods
805 // should be revisited, and they should be removed if possible. 807 // should be revisited, and they should be removed if possible.
806 808
807 bool Thread::is_lock_owned(address adr) const { 809 bool Thread::is_lock_owned(address adr) const {
808 return (_stack_base >= adr && adr >= (_stack_base - _stack_size)); 810 return on_local_stack(adr);
809 } 811 }
810 812
811 bool Thread::set_as_starting_thread() { 813 bool Thread::set_as_starting_thread() {
812 // NOTE: this must be called inside the main thread. 814 // NOTE: this must be called inside the main thread.
813 return os::create_main_thread((JavaThread*)this); 815 return os::create_main_thread((JavaThread*)this);
1016 // The watcher thread exists to simulate timer interrupts. It should 1018 // The watcher thread exists to simulate timer interrupts. It should
1017 // be replaced by an abstraction over whatever native support for 1019 // be replaced by an abstraction over whatever native support for
1018 // timer interrupts exists on the platform. 1020 // timer interrupts exists on the platform.
1019 1021
1020 WatcherThread* WatcherThread::_watcher_thread = NULL; 1022 WatcherThread* WatcherThread::_watcher_thread = NULL;
1021 bool WatcherThread::_should_terminate = false; 1023 volatile bool WatcherThread::_should_terminate = false;
1022 1024
1023 WatcherThread::WatcherThread() : Thread() { 1025 WatcherThread::WatcherThread() : Thread() {
1024 assert(watcher_thread() == NULL, "we can only allocate one WatcherThread"); 1026 assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1025 if (os::create_thread(this, os::watcher_thread)) { 1027 if (os::create_thread(this, os::watcher_thread)) {
1026 _watcher_thread = this; 1028 _watcher_thread = this;
1048 assert(watcher_thread() == Thread::current(), "thread consistency check"); 1050 assert(watcher_thread() == Thread::current(), "thread consistency check");
1049 assert(watcher_thread() == this, "thread consistency check"); 1051 assert(watcher_thread() == this, "thread consistency check");
1050 1052
1051 // Calculate how long it'll be until the next PeriodicTask work 1053 // Calculate how long it'll be until the next PeriodicTask work
1052 // should be done, and sleep that amount of time. 1054 // should be done, and sleep that amount of time.
1053 const size_t time_to_wait = PeriodicTask::time_to_wait(); 1055 size_t time_to_wait = PeriodicTask::time_to_wait();
1054 os::sleep(this, time_to_wait, false); 1056
1057 // we expect this to timeout - we only ever get unparked when
1058 // we should terminate
1059 {
1060 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1061
1062 jlong prev_time = os::javaTimeNanos();
1063 for (;;) {
1064 int res= _SleepEvent->park(time_to_wait);
1065 if (res == OS_TIMEOUT || _should_terminate)
1066 break;
1067 // spurious wakeup of some kind
1068 jlong now = os::javaTimeNanos();
1069 time_to_wait -= (now - prev_time) / 1000000;
1070 if (time_to_wait <= 0)
1071 break;
1072 prev_time = now;
1073 }
1074 }
1055 1075
1056 if (is_error_reported()) { 1076 if (is_error_reported()) {
1057 // A fatal error has happened, the error handler(VMError::report_and_die) 1077 // A fatal error has happened, the error handler(VMError::report_and_die)
1058 // should abort JVM after creating an error log file. However in some 1078 // should abort JVM after creating an error log file. However in some
1059 // rare cases, the error handler itself might deadlock. Here we try to 1079 // rare cases, the error handler itself might deadlock. Here we try to
1111 1131
1112 void WatcherThread::stop() { 1132 void WatcherThread::stop() {
1113 // it is ok to take late safepoints here, if needed 1133 // it is ok to take late safepoints here, if needed
1114 MutexLocker mu(Terminator_lock); 1134 MutexLocker mu(Terminator_lock);
1115 _should_terminate = true; 1135 _should_terminate = true;
1136 OrderAccess::fence(); // ensure WatcherThread sees update in main loop
1137
1138 Thread* watcher = watcher_thread();
1139 if (watcher != NULL)
1140 watcher->_SleepEvent->unpark();
1141
1116 while(watcher_thread() != NULL) { 1142 while(watcher_thread() != NULL) {
1117 // This wait should make safepoint checks, wait without a timeout, 1143 // This wait should make safepoint checks, wait without a timeout,
1118 // and wait as a suspend-equivalent condition. 1144 // and wait as a suspend-equivalent condition.
1119 // 1145 //
1120 // Note: If the FlatProfiler is running, then this thread is waiting 1146 // Note: If the FlatProfiler is running, then this thread is waiting
1155 set_vm_result_2(NULL); 1181 set_vm_result_2(NULL);
1156 set_vframe_array_head(NULL); 1182 set_vframe_array_head(NULL);
1157 set_vframe_array_last(NULL); 1183 set_vframe_array_last(NULL);
1158 set_deferred_locals(NULL); 1184 set_deferred_locals(NULL);
1159 set_deopt_mark(NULL); 1185 set_deopt_mark(NULL);
1186 set_deopt_nmethod(NULL);
1160 clear_must_deopt_id(); 1187 clear_must_deopt_id();
1161 set_monitor_chunks(NULL); 1188 set_monitor_chunks(NULL);
1162 set_next(NULL); 1189 set_next(NULL);
1163 set_thread_state(_thread_new); 1190 set_thread_state(_thread_new);
1164 _terminated = _not_terminated; 1191 _terminated = _not_terminated;
1170 _stack_guard_state = stack_guard_unused; 1197 _stack_guard_state = stack_guard_unused;
1171 _exception_oop = NULL; 1198 _exception_oop = NULL;
1172 _exception_pc = 0; 1199 _exception_pc = 0;
1173 _exception_handler_pc = 0; 1200 _exception_handler_pc = 0;
1174 _exception_stack_size = 0; 1201 _exception_stack_size = 0;
1202 _is_method_handle_return = 0;
1175 _jvmti_thread_state= NULL; 1203 _jvmti_thread_state= NULL;
1176 _should_post_on_exceptions_flag = JNI_FALSE; 1204 _should_post_on_exceptions_flag = JNI_FALSE;
1177 _jvmti_get_loaded_classes_closure = NULL; 1205 _jvmti_get_loaded_classes_closure = NULL;
1178 _interp_only_mode = 0; 1206 _interp_only_mode = 0;
1179 _special_runtime_exit_condition = _no_async_condition; 1207 _special_runtime_exit_condition = _no_async_condition;
1359 1387
1360 // Initialize thread local storage; set before calling MutexLocker 1388 // Initialize thread local storage; set before calling MutexLocker
1361 this->initialize_thread_local_storage(); 1389 this->initialize_thread_local_storage();
1362 1390
1363 this->create_stack_guard_pages(); 1391 this->create_stack_guard_pages();
1392
1393 this->cache_global_variables();
1364 1394
1365 // Thread is now sufficient initialized to be handled by the safepoint code as being 1395 // Thread is now sufficient initialized to be handled by the safepoint code as being
1366 // in the VM. Change thread state from _thread_new to _thread_in_vm 1396 // in the VM. Change thread state from _thread_new to _thread_in_vm
1367 ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm); 1397 ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1368 1398
1614 // Flush G1-related queues. 1644 // Flush G1-related queues.
1615 void JavaThread::flush_barrier_queues() { 1645 void JavaThread::flush_barrier_queues() {
1616 satb_mark_queue().flush(); 1646 satb_mark_queue().flush();
1617 dirty_card_queue().flush(); 1647 dirty_card_queue().flush();
1618 } 1648 }
1619 #endif 1649
1650 void JavaThread::initialize_queues() {
1651 assert(!SafepointSynchronize::is_at_safepoint(),
1652 "we should not be at a safepoint");
1653
1654 ObjPtrQueue& satb_queue = satb_mark_queue();
1655 SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1656 // The SATB queue should have been constructed with its active
1657 // field set to false.
1658 assert(!satb_queue.is_active(), "SATB queue should not be active");
1659 assert(satb_queue.is_empty(), "SATB queue should be empty");
1660 // If we are creating the thread during a marking cycle, we should
1661 // set the active field of the SATB queue to true.
1662 if (satb_queue_set.is_active()) {
1663 satb_queue.set_active(true);
1664 }
1665
1666 DirtyCardQueue& dirty_queue = dirty_card_queue();
1667 // The dirty card queue should have been constructed with its
1668 // active field set to true.
1669 assert(dirty_queue.is_active(), "dirty card queue should be active");
1670 }
1671 #endif // !SERIALGC
1620 1672
1621 void JavaThread::cleanup_failed_attach_current_thread() { 1673 void JavaThread::cleanup_failed_attach_current_thread() {
1622 if (get_thread_profiler() != NULL) { 1674 if (get_thread_profiler() != NULL) {
1623 get_thread_profiler()->disengage(); 1675 get_thread_profiler()->disengage();
1624 ResourceMark rm; 1676 ResourceMark rm;
2082 while ( f.id() != thread->must_deopt_id() && ! f.is_first_frame()) { 2134 while ( f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2083 f = f.sender(&map); 2135 f = f.sender(&map);
2084 } 2136 }
2085 if (f.id() == thread->must_deopt_id()) { 2137 if (f.id() == thread->must_deopt_id()) {
2086 thread->clear_must_deopt_id(); 2138 thread->clear_must_deopt_id();
2087 // Since we know we're safe to deopt the current state is a safe state 2139 f.deoptimize(thread);
2088 f.deoptimize(thread, true);
2089 } else { 2140 } else {
2090 fatal("missed deoptimization!"); 2141 fatal("missed deoptimization!");
2091 } 2142 }
2092 } 2143 }
2093 } 2144 }
2698 void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) { 2749 void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) {
2699 assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments"); 2750 assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments");
2700 if (in_bytes(size_in_bytes) != 0) { 2751 if (in_bytes(size_in_bytes) != 0) {
2701 _popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes)); 2752 _popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes));
2702 _popframe_preserved_args_size = in_bytes(size_in_bytes); 2753 _popframe_preserved_args_size = in_bytes(size_in_bytes);
2703 Copy::conjoint_bytes(start, _popframe_preserved_args, _popframe_preserved_args_size); 2754 Copy::conjoint_jbytes(start, _popframe_preserved_args, _popframe_preserved_args_size);
2704 } 2755 }
2705 } 2756 }
2706 2757
2707 void* JavaThread::popframe_preserved_args() { 2758 void* JavaThread::popframe_preserved_args() {
2708 return _popframe_preserved_args; 2759 return _popframe_preserved_args;
2798 _log = NULL; 2849 _log = NULL;
2799 _task = NULL; 2850 _task = NULL;
2800 _queue = queue; 2851 _queue = queue;
2801 _counters = counters; 2852 _counters = counters;
2802 _is_compiling = false; 2853 _is_compiling = false;
2854 _buffer_blob = NULL;
2803 2855
2804 #ifndef PRODUCT 2856 #ifndef PRODUCT
2805 _ideal_graph_printer = NULL; 2857 _ideal_graph_printer = NULL;
2806 #endif 2858 #endif
2807 } 2859 }
2871 Arguments::init_system_properties(); 2923 Arguments::init_system_properties();
2872 2924
2873 // So that JDK version can be used as a discrimintor when parsing arguments 2925 // So that JDK version can be used as a discrimintor when parsing arguments
2874 JDK_Version_init(); 2926 JDK_Version_init();
2875 2927
2928 // Update/Initialize System properties after JDK version number is known
2929 Arguments::init_version_specific_system_properties();
2930
2876 // Parse arguments 2931 // Parse arguments
2877 jint parse_result = Arguments::parse(args); 2932 jint parse_result = Arguments::parse(args);
2878 if (parse_result != JNI_OK) return parse_result; 2933 if (parse_result != JNI_OK) return parse_result;
2879 2934
2880 if (PauseAtStartup) { 2935 if (PauseAtStartup) {
2942 2997
2943 // Enable guard page *after* os::create_main_thread(), otherwise it would 2998 // Enable guard page *after* os::create_main_thread(), otherwise it would
2944 // crash Linux VM, see notes in os_linux.cpp. 2999 // crash Linux VM, see notes in os_linux.cpp.
2945 main_thread->create_stack_guard_pages(); 3000 main_thread->create_stack_guard_pages();
2946 3001
2947 // Initialize Java-Leve synchronization subsystem 3002 // Initialize Java-Level synchronization subsystem
2948 ObjectSynchronizer::Initialize() ; 3003 ObjectMonitor::Initialize() ;
2949 3004
2950 // Initialize global modules 3005 // Initialize global modules
2951 jint status = init_globals(); 3006 jint status = init_globals();
2952 if (status != JNI_OK) { 3007 if (status != JNI_OK) {
2953 delete main_thread; 3008 delete main_thread;
2954 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again 3009 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
2955 return status; 3010 return status;
2956 } 3011 }
3012
3013 // Should be done after the heap is fully created
3014 main_thread->cache_global_variables();
2957 3015
2958 HandleMark hm; 3016 HandleMark hm;
2959 3017
2960 { MutexLocker mu(Threads_lock); 3018 { MutexLocker mu(Threads_lock);
2961 Threads::add(main_thread); 3019 Threads::add(main_thread);
3228 // take a while to process their first tick). 3286 // take a while to process their first tick).
3229 if (PeriodicTask::num_tasks() > 0) { 3287 if (PeriodicTask::num_tasks() > 0) {
3230 WatcherThread::start(); 3288 WatcherThread::start();
3231 } 3289 }
3232 3290
3291 // Give os specific code one last chance to start
3292 os::init_3();
3293
3233 create_vm_timer.end(); 3294 create_vm_timer.end();
3234 return JNI_OK; 3295 return JNI_OK;
3235 } 3296 }
3236 3297
3237 // type for the Agent_OnLoad and JVM_OnLoad entry points 3298 // type for the Agent_OnLoad and JVM_OnLoad entry points
3247 3308
3248 if (library == NULL) { 3309 if (library == NULL) {
3249 char buffer[JVM_MAXPATHLEN]; 3310 char buffer[JVM_MAXPATHLEN];
3250 char ebuf[1024]; 3311 char ebuf[1024];
3251 const char *name = agent->name(); 3312 const char *name = agent->name();
3313 const char *msg = "Could not find agent library ";
3252 3314
3253 if (agent->is_absolute_path()) { 3315 if (agent->is_absolute_path()) {
3254 library = hpi::dll_load(name, ebuf, sizeof ebuf); 3316 library = hpi::dll_load(name, ebuf, sizeof ebuf);
3255 if (library == NULL) { 3317 if (library == NULL) {
3318 const char *sub_msg = " in absolute path, with error: ";
3319 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1;
3320 char *buf = NEW_C_HEAP_ARRAY(char, len);
3321 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3256 // If we can't find the agent, exit. 3322 // If we can't find the agent, exit.
3257 vm_exit_during_initialization("Could not find agent library in absolute path", name); 3323 vm_exit_during_initialization(buf, NULL);
3324 FREE_C_HEAP_ARRAY(char, buf);
3258 } 3325 }
3259 } else { 3326 } else {
3260 // Try to load the agent from the standard dll directory 3327 // Try to load the agent from the standard dll directory
3261 hpi::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), name); 3328 hpi::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), name);
3262 library = hpi::dll_load(buffer, ebuf, sizeof ebuf); 3329 library = hpi::dll_load(buffer, ebuf, sizeof ebuf);
3265 if (library == NULL && strcmp(name, "instrument") == 0) { 3332 if (library == NULL && strcmp(name, "instrument") == 0) {
3266 char *props = Arguments::get_kernel_properties(); 3333 char *props = Arguments::get_kernel_properties();
3267 char *home = Arguments::get_java_home(); 3334 char *home = Arguments::get_java_home();
3268 const char *fmt = "%s/bin/java %s -Dkernel.background.download=false" 3335 const char *fmt = "%s/bin/java %s -Dkernel.background.download=false"
3269 " sun.jkernel.DownloadManager -download client_jvm"; 3336 " sun.jkernel.DownloadManager -download client_jvm";
3270 int length = strlen(props) + strlen(home) + strlen(fmt) + 1; 3337 size_t length = strlen(props) + strlen(home) + strlen(fmt) + 1;
3271 char *cmd = AllocateHeap(length); 3338 char *cmd = NEW_C_HEAP_ARRAY(char, length);
3272 jio_snprintf(cmd, length, fmt, home, props); 3339 jio_snprintf(cmd, length, fmt, home, props);
3273 int status = os::fork_and_exec(cmd); 3340 int status = os::fork_and_exec(cmd);
3274 FreeHeap(props); 3341 FreeHeap(props);
3275 FreeHeap(cmd);
3276 if (status == -1) { 3342 if (status == -1) {
3277 warning(cmd); 3343 warning(cmd);
3278 vm_exit_during_initialization("fork_and_exec failed: %s", 3344 vm_exit_during_initialization("fork_and_exec failed: %s",
3279 strerror(errno)); 3345 strerror(errno));
3280 } 3346 }
3347 FREE_C_HEAP_ARRAY(char, cmd);
3281 // when this comes back the instrument.dll should be where it belongs. 3348 // when this comes back the instrument.dll should be where it belongs.
3282 library = hpi::dll_load(buffer, ebuf, sizeof ebuf); 3349 library = hpi::dll_load(buffer, ebuf, sizeof ebuf);
3283 } 3350 }
3284 #endif // KERNEL 3351 #endif // KERNEL
3285 if (library == NULL) { // Try the local directory 3352 if (library == NULL) { // Try the local directory
3286 char ns[1] = {0}; 3353 char ns[1] = {0};
3287 hpi::dll_build_name(buffer, sizeof(buffer), ns, name); 3354 hpi::dll_build_name(buffer, sizeof(buffer), ns, name);
3288 library = hpi::dll_load(buffer, ebuf, sizeof ebuf); 3355 library = hpi::dll_load(buffer, ebuf, sizeof ebuf);
3289 if (library == NULL) { 3356 if (library == NULL) {
3357 const char *sub_msg = " on the library path, with error: ";
3358 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1;
3359 char *buf = NEW_C_HEAP_ARRAY(char, len);
3360 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf);
3290 // If we can't find the agent, exit. 3361 // If we can't find the agent, exit.
3291 vm_exit_during_initialization("Could not find agent library on the library path or in the local directory", name); 3362 vm_exit_during_initialization(buf, NULL);
3363 FREE_C_HEAP_ARRAY(char, buf);
3292 } 3364 }
3293 } 3365 }
3294 } 3366 }
3295 agent->set_os_lib(library); 3367 agent->set_os_lib(library);
3296 } 3368 }
3582 3654
3583 3655
3584 void Threads::add(JavaThread* p, bool force_daemon) { 3656 void Threads::add(JavaThread* p, bool force_daemon) {
3585 // The threads lock must be owned at this point 3657 // The threads lock must be owned at this point
3586 assert_locked_or_safepoint(Threads_lock); 3658 assert_locked_or_safepoint(Threads_lock);
3659
3660 // See the comment for this method in thread.hpp for its purpose and
3661 // why it is called here.
3662 p->initialize_queues();
3587 p->set_next(_thread_list); 3663 p->set_next(_thread_list);
3588 _thread_list = p; 3664 _thread_list = p;
3589 _number_of_threads++; 3665 _number_of_threads++;
3590 oop threadObj = p->threadObj(); 3666 oop threadObj = p->threadObj();
3591 bool daemon = true; 3667 bool daemon = true;
3893 current->print_on_error(st, buf, buflen); 3969 current->print_on_error(st, buf, buflen);
3894 st->cr(); 3970 st->cr();
3895 } 3971 }
3896 } 3972 }
3897 3973
3898 3974 // Internal SpinLock and Mutex
3899 // Lifecycle management for TSM ParkEvents. 3975 // Based on ParkEvent
3900 // ParkEvents are type-stable (TSM). 3976
3901 // In our particular implementation they happen to be immortal. 3977 // Ad-hoc mutual exclusion primitives: SpinLock and Mux
3902 // 3978 //
3903 // We manage concurrency on the FreeList with a CAS-based 3979 // We employ SpinLocks _only for low-contention, fixed-length
3904 // detach-modify-reattach idiom that avoids the ABA problems 3980 // short-duration critical sections where we're concerned
3905 // that would otherwise be present in a simple CAS-based 3981 // about native mutex_t or HotSpot Mutex:: latency.
3906 // push-pop implementation. (push-one and pop-all) 3982 // The mux construct provides a spin-then-block mutual exclusion
3983 // mechanism.
3907 // 3984 //
3908 // Caveat: Allocate() and Release() may be called from threads 3985 // Testing has shown that contention on the ListLock guarding gFreeList
3909 // other than the thread associated with the Event! 3986 // is common. If we implement ListLock as a simple SpinLock it's common
3910 // If we need to call Allocate() when running as the thread in 3987 // for the JVM to devolve to yielding with little progress. This is true
3911 // question then look for the PD calls to initialize native TLS. 3988 // despite the fact that the critical sections protected by ListLock are
3912 // Native TLS (Win32/Linux/Solaris) can only be initialized or 3989 // extremely short.
3913 // accessed by the associated thread.
3914 // See also pd_initialize().
3915 // 3990 //
3916 // Note that we could defer associating a ParkEvent with a thread 3991 // TODO-FIXME: ListLock should be of type SpinLock.
3917 // until the 1st time the thread calls park(). unpark() calls to 3992 // We should make this a 1st-class type, integrated into the lock
3918 // an unprovisioned thread would be ignored. The first park() call 3993 // hierarchy as leaf-locks. Critically, the SpinLock structure
3919 // for a thread would allocate and associate a ParkEvent and return 3994 // should have sufficient padding to avoid false-sharing and excessive
3920 // immediately. 3995 // cache-coherency traffic.
3921 3996
3922 volatile int ParkEvent::ListLock = 0 ; 3997
3923 ParkEvent * volatile ParkEvent::FreeList = NULL ; 3998 typedef volatile int SpinLockT ;
3924 3999
3925 ParkEvent * ParkEvent::Allocate (Thread * t) { 4000 void Thread::SpinAcquire (volatile int * adr, const char * LockName) {
3926 // In rare cases -- JVM_RawMonitor* operations -- we can find t == null. 4001 if (Atomic::cmpxchg (1, adr, 0) == 0) {
3927 ParkEvent * ev ; 4002 return ; // normal fast-path return
3928 4003 }
3929 // Start by trying to recycle an existing but unassociated 4004
3930 // ParkEvent from the global free list. 4005 // Slow-path : We've encountered contention -- Spin/Yield/Block strategy.
4006 TEVENT (SpinAcquire - ctx) ;
4007 int ctr = 0 ;
4008 int Yields = 0 ;
3931 for (;;) { 4009 for (;;) {
3932 ev = FreeList ; 4010 while (*adr != 0) {
3933 if (ev == NULL) break ; 4011 ++ctr ;
3934 // 1: Detach - sequester or privatize the list 4012 if ((ctr & 0xFFF) == 0 || !os::is_MP()) {
3935 // Tantamount to ev = Swap (&FreeList, NULL) 4013 if (Yields > 5) {
3936 if (Atomic::cmpxchg_ptr (NULL, &FreeList, ev) != ev) { 4014 // Consider using a simple NakedSleep() instead.
3937 continue ; 4015 // Then SpinAcquire could be called by non-JVM threads
3938 } 4016 Thread::current()->_ParkEvent->park(1) ;
3939 4017 } else {
3940 // We've detached the list. The list in-hand is now 4018 os::NakedYield() ;
3941 // local to this thread. This thread can operate on the 4019 ++Yields ;
3942 // list without risk of interference from other threads. 4020 }
3943 // 2: Extract -- pop the 1st element from the list. 4021 } else {
3944 ParkEvent * List = ev->FreeNext ; 4022 SpinPause() ;
3945 if (List == NULL) break ; 4023 }
4024 }
4025 if (Atomic::cmpxchg (1, adr, 0) == 0) return ;
4026 }
4027 }
4028
4029 void Thread::SpinRelease (volatile int * adr) {
4030 assert (*adr != 0, "invariant") ;
4031 OrderAccess::fence() ; // guarantee at least release consistency.
4032 // Roach-motel semantics.
4033 // It's safe if subsequent LDs and STs float "up" into the critical section,
4034 // but prior LDs and STs within the critical section can't be allowed
4035 // to reorder or float past the ST that releases the lock.
4036 *adr = 0 ;
4037 }
4038
4039 // muxAcquire and muxRelease:
4040 //
4041 // * muxAcquire and muxRelease support a single-word lock-word construct.
4042 // The LSB of the word is set IFF the lock is held.
4043 // The remainder of the word points to the head of a singly-linked list
4044 // of threads blocked on the lock.
4045 //
4046 // * The current implementation of muxAcquire-muxRelease uses its own
4047 // dedicated Thread._MuxEvent instance. If we're interested in
4048 // minimizing the peak number of extant ParkEvent instances then
4049 // we could eliminate _MuxEvent and "borrow" _ParkEvent as long
4050 // as certain invariants were satisfied. Specifically, care would need
4051 // to be taken with regards to consuming unpark() "permits".
4052 // A safe rule of thumb is that a thread would never call muxAcquire()
4053 // if it's enqueued (cxq, EntryList, WaitList, etc) and will subsequently
4054 // park(). Otherwise the _ParkEvent park() operation in muxAcquire() could
4055 // consume an unpark() permit intended for monitorenter, for instance.
4056 // One way around this would be to widen the restricted-range semaphore
4057 // implemented in park(). Another alternative would be to provide
4058 // multiple instances of the PlatformEvent() for each thread. One
4059 // instance would be dedicated to muxAcquire-muxRelease, for instance.
4060 //
4061 // * Usage:
4062 // -- Only as leaf locks
4063 // -- for short-term locking only as muxAcquire does not perform
4064 // thread state transitions.
4065 //
4066 // Alternatives:
4067 // * We could implement muxAcquire and muxRelease with MCS or CLH locks
4068 // but with parking or spin-then-park instead of pure spinning.
4069 // * Use Taura-Oyama-Yonenzawa locks.
4070 // * It's possible to construct a 1-0 lock if we encode the lockword as
4071 // (List,LockByte). Acquire will CAS the full lockword while Release
4072 // will STB 0 into the LockByte. The 1-0 scheme admits stranding, so
4073 // acquiring threads use timers (ParkTimed) to detect and recover from
4074 // the stranding window. Thread/Node structures must be aligned on 256-byte
4075 // boundaries by using placement-new.
4076 // * Augment MCS with advisory back-link fields maintained with CAS().
4077 // Pictorially: LockWord -> T1 <-> T2 <-> T3 <-> ... <-> Tn <-> Owner.
4078 // The validity of the backlinks must be ratified before we trust the value.
4079 // If the backlinks are invalid the exiting thread must back-track through the
4080 // the forward links, which are always trustworthy.
4081 // * Add a successor indication. The LockWord is currently encoded as
4082 // (List, LOCKBIT:1). We could also add a SUCCBIT or an explicit _succ variable
4083 // to provide the usual futile-wakeup optimization.
4084 // See RTStt for details.
4085 // * Consider schedctl.sc_nopreempt to cover the critical section.
4086 //
4087
4088
4089 typedef volatile intptr_t MutexT ; // Mux Lock-word
4090 enum MuxBits { LOCKBIT = 1 } ;
4091
4092 void Thread::muxAcquire (volatile intptr_t * Lock, const char * LockName) {
4093 intptr_t w = Atomic::cmpxchg_ptr (LOCKBIT, Lock, 0) ;
4094 if (w == 0) return ;
4095 if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
4096 return ;
4097 }
4098
4099 TEVENT (muxAcquire - Contention) ;
4100 ParkEvent * const Self = Thread::current()->_MuxEvent ;
4101 assert ((intptr_t(Self) & LOCKBIT) == 0, "invariant") ;
4102 for (;;) {
4103 int its = (os::is_MP() ? 100 : 0) + 1 ;
4104
4105 // Optional spin phase: spin-then-park strategy
4106 while (--its >= 0) {
4107 w = *Lock ;
4108 if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
4109 return ;
4110 }
4111 }
4112
4113 Self->reset() ;
4114 Self->OnList = intptr_t(Lock) ;
4115 // The following fence() isn't _strictly necessary as the subsequent
4116 // CAS() both serializes execution and ratifies the fetched *Lock value.
4117 OrderAccess::fence();
4118 for (;;) {
4119 w = *Lock ;
4120 if ((w & LOCKBIT) == 0) {
4121 if (Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
4122 Self->OnList = 0 ; // hygiene - allows stronger asserts
4123 return ;
4124 }
4125 continue ; // Interference -- *Lock changed -- Just retry
4126 }
4127 assert (w & LOCKBIT, "invariant") ;
4128 Self->ListNext = (ParkEvent *) (w & ~LOCKBIT );
4129 if (Atomic::cmpxchg_ptr (intptr_t(Self)|LOCKBIT, Lock, w) == w) break ;
4130 }
4131
4132 while (Self->OnList != 0) {
4133 Self->park() ;
4134 }
4135 }
4136 }
4137
4138 void Thread::muxAcquireW (volatile intptr_t * Lock, ParkEvent * ev) {
4139 intptr_t w = Atomic::cmpxchg_ptr (LOCKBIT, Lock, 0) ;
4140 if (w == 0) return ;
4141 if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
4142 return ;
4143 }
4144
4145 TEVENT (muxAcquire - Contention) ;
4146 ParkEvent * ReleaseAfter = NULL ;
4147 if (ev == NULL) {
4148 ev = ReleaseAfter = ParkEvent::Allocate (NULL) ;
4149 }
4150 assert ((intptr_t(ev) & LOCKBIT) == 0, "invariant") ;
4151 for (;;) {
4152 guarantee (ev->OnList == 0, "invariant") ;
4153 int its = (os::is_MP() ? 100 : 0) + 1 ;
4154
4155 // Optional spin phase: spin-then-park strategy
4156 while (--its >= 0) {
4157 w = *Lock ;
4158 if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
4159 if (ReleaseAfter != NULL) {
4160 ParkEvent::Release (ReleaseAfter) ;
4161 }
4162 return ;
4163 }
4164 }
4165
4166 ev->reset() ;
4167 ev->OnList = intptr_t(Lock) ;
4168 // The following fence() isn't _strictly necessary as the subsequent
4169 // CAS() both serializes execution and ratifies the fetched *Lock value.
4170 OrderAccess::fence();
3946 for (;;) { 4171 for (;;) {
3947 // 3: Try to reattach the residual list 4172 w = *Lock ;
3948 guarantee (List != NULL, "invariant") ; 4173 if ((w & LOCKBIT) == 0) {
3949 ParkEvent * Arv = (ParkEvent *) Atomic::cmpxchg_ptr (List, &FreeList, NULL) ; 4174 if (Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
3950 if (Arv == NULL) break ; 4175 ev->OnList = 0 ;
3951 4176 // We call ::Release while holding the outer lock, thus
3952 // New nodes arrived. Try to detach the recent arrivals. 4177 // artificially lengthening the critical section.
3953 if (Atomic::cmpxchg_ptr (NULL, &FreeList, Arv) != Arv) { 4178 // Consider deferring the ::Release() until the subsequent unlock(),
3954 continue ; 4179 // after we've dropped the outer lock.
4180 if (ReleaseAfter != NULL) {
4181 ParkEvent::Release (ReleaseAfter) ;
4182 }
4183 return ;
3955 } 4184 }
3956 guarantee (Arv != NULL, "invariant") ; 4185 continue ; // Interference -- *Lock changed -- Just retry
3957 // 4: Merge Arv into List 4186 }
3958 ParkEvent * Tail = List ; 4187 assert (w & LOCKBIT, "invariant") ;
3959 while (Tail->FreeNext != NULL) Tail = Tail->FreeNext ; 4188 ev->ListNext = (ParkEvent *) (w & ~LOCKBIT );
3960 Tail->FreeNext = Arv ; 4189 if (Atomic::cmpxchg_ptr (intptr_t(ev)|LOCKBIT, Lock, w) == w) break ;
3961 } 4190 }
3962 break ; 4191
3963 } 4192 while (ev->OnList != 0) {
3964 4193 ev->park() ;
3965 if (ev != NULL) { 4194 }
3966 guarantee (ev->AssociatedWith == NULL, "invariant") ; 4195 }
3967 } else { 4196 }
3968 // Do this the hard way -- materialize a new ParkEvent. 4197
3969 // In rare cases an allocating thread might detach a long list -- 4198 // Release() must extract a successor from the list and then wake that thread.
3970 // installing null into FreeList -- and then stall or be obstructed. 4199 // It can "pop" the front of the list or use a detach-modify-reattach (DMR) scheme
3971 // A 2nd thread calling Allocate() would see FreeList == null. 4200 // similar to that used by ParkEvent::Allocate() and ::Release(). DMR-based
3972 // The list held privately by the 1st thread is unavailable to the 2nd thread. 4201 // Release() would :
3973 // In that case the 2nd thread would have to materialize a new ParkEvent, 4202 // (A) CAS() or swap() null to *Lock, releasing the lock and detaching the list.
3974 // even though free ParkEvents existed in the system. In this case we end up 4203 // (B) Extract a successor from the private list "in-hand"
3975 // with more ParkEvents in circulation than we need, but the race is 4204 // (C) attempt to CAS() the residual back into *Lock over null.
3976 // rare and the outcome is benign. Ideally, the # of extant ParkEvents 4205 // If there were any newly arrived threads and the CAS() would fail.
3977 // is equal to the maximum # of threads that existed at any one time. 4206 // In that case Release() would detach the RATs, re-merge the list in-hand
3978 // Because of the race mentioned above, segments of the freelist 4207 // with the RATs and repeat as needed. Alternately, Release() might
3979 // can be transiently inaccessible. At worst we may end up with the 4208 // detach and extract a successor, but then pass the residual list to the wakee.
3980 // # of ParkEvents in circulation slightly above the ideal. 4209 // The wakee would be responsible for reattaching and remerging before it
3981 // Note that if we didn't have the TSM/immortal constraint, then 4210 // competed for the lock.
3982 // when reattaching, above, we could trim the list. 4211 //
3983 ev = new ParkEvent () ; 4212 // Both "pop" and DMR are immune from ABA corruption -- there can be
3984 guarantee ((intptr_t(ev) & 0xFF) == 0, "invariant") ; 4213 // multiple concurrent pushers, but only one popper or detacher.
3985 } 4214 // This implementation pops from the head of the list. This is unfair,
3986 ev->reset() ; // courtesy to caller 4215 // but tends to provide excellent throughput as hot threads remain hot.
3987 ev->AssociatedWith = t ; // Associate ev with t 4216 // (We wake recently run threads first).
3988 ev->FreeNext = NULL ; 4217
3989 return ev ; 4218 void Thread::muxRelease (volatile intptr_t * Lock) {
3990 }
3991
3992 void ParkEvent::Release (ParkEvent * ev) {
3993 if (ev == NULL) return ;
3994 guarantee (ev->FreeNext == NULL , "invariant") ;
3995 ev->AssociatedWith = NULL ;
3996 for (;;) { 4219 for (;;) {
3997 // Push ev onto FreeList 4220 const intptr_t w = Atomic::cmpxchg_ptr (0, Lock, LOCKBIT) ;
3998 // The mechanism is "half" lock-free. 4221 assert (w & LOCKBIT, "invariant") ;
3999 ParkEvent * List = FreeList ; 4222 if (w == LOCKBIT) return ;
4000 ev->FreeNext = List ; 4223 ParkEvent * List = (ParkEvent *) (w & ~LOCKBIT) ;
4001 if (Atomic::cmpxchg_ptr (ev, &FreeList, List) == List) break ; 4224 assert (List != NULL, "invariant") ;
4002 } 4225 assert (List->OnList == intptr_t(Lock), "invariant") ;
4003 } 4226 ParkEvent * nxt = List->ListNext ;
4004 4227
4005 // Override operator new and delete so we can ensure that the 4228 // The following CAS() releases the lock and pops the head element.
4006 // least significant byte of ParkEvent addresses is 0. 4229 if (Atomic::cmpxchg_ptr (intptr_t(nxt), Lock, w) != w) {
4007 // Beware that excessive address alignment is undesirable 4230 continue ;
4008 // as it can result in D$ index usage imbalance as 4231 }
4009 // well as bank access imbalance on Niagara-like platforms, 4232 List->OnList = 0 ;
4010 // although Niagara's hash function should help. 4233 OrderAccess::fence() ;
4011 4234 List->unpark () ;
4012 void * ParkEvent::operator new (size_t sz) { 4235 return ;
4013 return (void *) ((intptr_t (CHeapObj::operator new (sz + 256)) + 256) & -256) ; 4236 }
4014 } 4237 }
4015 4238
4016 void ParkEvent::operator delete (void * a) {
4017 // ParkEvents are type-stable and immortal ...
4018 ShouldNotReachHere();
4019 }
4020
4021
4022 // 6399321 As a temporary measure we copied & modified the ParkEvent::
4023 // allocate() and release() code for use by Parkers. The Parker:: forms
4024 // will eventually be removed as we consolide and shift over to ParkEvents
4025 // for both builtin synchronization and JSR166 operations.
4026
4027 volatile int Parker::ListLock = 0 ;
4028 Parker * volatile Parker::FreeList = NULL ;
4029
4030 Parker * Parker::Allocate (JavaThread * t) {
4031 guarantee (t != NULL, "invariant") ;
4032 Parker * p ;
4033
4034 // Start by trying to recycle an existing but unassociated
4035 // Parker from the global free list.
4036 for (;;) {
4037 p = FreeList ;
4038 if (p == NULL) break ;
4039 // 1: Detach
4040 // Tantamount to p = Swap (&FreeList, NULL)
4041 if (Atomic::cmpxchg_ptr (NULL, &FreeList, p) != p) {
4042 continue ;
4043 }
4044
4045 // We've detached the list. The list in-hand is now
4046 // local to this thread. This thread can operate on the
4047 // list without risk of interference from other threads.
4048 // 2: Extract -- pop the 1st element from the list.
4049 Parker * List = p->FreeNext ;
4050 if (List == NULL) break ;
4051 for (;;) {
4052 // 3: Try to reattach the residual list
4053 guarantee (List != NULL, "invariant") ;
4054 Parker * Arv = (Parker *) Atomic::cmpxchg_ptr (List, &FreeList, NULL) ;
4055 if (Arv == NULL) break ;
4056
4057 // New nodes arrived. Try to detach the recent arrivals.
4058 if (Atomic::cmpxchg_ptr (NULL, &FreeList, Arv) != Arv) {
4059 continue ;
4060 }
4061 guarantee (Arv != NULL, "invariant") ;
4062 // 4: Merge Arv into List
4063 Parker * Tail = List ;
4064 while (Tail->FreeNext != NULL) Tail = Tail->FreeNext ;
4065 Tail->FreeNext = Arv ;
4066 }
4067 break ;
4068 }
4069
4070 if (p != NULL) {
4071 guarantee (p->AssociatedWith == NULL, "invariant") ;
4072 } else {
4073 // Do this the hard way -- materialize a new Parker..
4074 // In rare cases an allocating thread might detach
4075 // a long list -- installing null into FreeList --and
4076 // then stall. Another thread calling Allocate() would see
4077 // FreeList == null and then invoke the ctor. In this case we
4078 // end up with more Parkers in circulation than we need, but
4079 // the race is rare and the outcome is benign.
4080 // Ideally, the # of extant Parkers is equal to the
4081 // maximum # of threads that existed at any one time.
4082 // Because of the race mentioned above, segments of the
4083 // freelist can be transiently inaccessible. At worst
4084 // we may end up with the # of Parkers in circulation
4085 // slightly above the ideal.
4086 p = new Parker() ;
4087 }
4088 p->AssociatedWith = t ; // Associate p with t
4089 p->FreeNext = NULL ;
4090 return p ;
4091 }
4092
4093
4094 void Parker::Release (Parker * p) {
4095 if (p == NULL) return ;
4096 guarantee (p->AssociatedWith != NULL, "invariant") ;
4097 guarantee (p->FreeNext == NULL , "invariant") ;
4098 p->AssociatedWith = NULL ;
4099 for (;;) {
4100 // Push p onto FreeList
4101 Parker * List = FreeList ;
4102 p->FreeNext = List ;
4103 if (Atomic::cmpxchg_ptr (p, &FreeList, List) == List) break ;
4104 }
4105 }
4106 4239
4107 void Threads::verify() { 4240 void Threads::verify() {
4108 ALL_JAVA_THREADS(p) { 4241 ALL_JAVA_THREADS(p) {
4109 p->verify(); 4242 p->verify();
4110 } 4243 }