Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/windows/vm/threadCritical_windows.cpp
32284 views
/*1* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "runtime/thread.inline.hpp"26#include "runtime/threadCritical.hpp"2728// OS-includes here29# include <windows.h>30# include <winbase.h>3132//33// See threadCritical.hpp for details of this class.34//3536static bool initialized = false;37static volatile jint lock_count = -1;38static HANDLE lock_event;39static DWORD lock_owner = -1;4041//42// Note that Microsoft's critical region code contains a race43// condition, and is not suitable for use. A thread holding the44// critical section cannot safely suspend a thread attempting45// to enter the critical region. The failure mode is that both46// threads are permanently suspended.47//48// I experiemented with the use of ordinary windows mutex objects49// and found them ~30 times slower than the critical region code.50//5152void ThreadCritical::initialize() {53}5455void ThreadCritical::release() {56assert(lock_owner == -1, "Mutex being deleted while owned.");57assert(lock_count == -1, "Mutex being deleted while recursively locked");58assert(lock_event != NULL, "Sanity check");59CloseHandle(lock_event);60}6162ThreadCritical::ThreadCritical() {63DWORD current_thread = GetCurrentThreadId();6465if (lock_owner != current_thread) {66// Grab the lock before doing anything.67while (Atomic::cmpxchg(0, &lock_count, -1) != -1) {68if (initialized) {69DWORD ret = WaitForSingleObject(lock_event, INFINITE);70assert(ret == WAIT_OBJECT_0, "unexpected return value from WaitForSingleObject");71}72}7374// Make sure the event object is allocated.75if (!initialized) {76// Locking will not work correctly unless this is autoreset.77lock_event = CreateEvent(NULL, false, false, NULL);78initialized = true;79}8081assert(lock_owner == -1, "Lock acquired illegally.");82lock_owner = current_thread;83} else {84// Atomicity isn't required. Bump the recursion count.85lock_count++;86}8788assert(lock_owner == GetCurrentThreadId(), "Lock acquired illegally.");89}9091ThreadCritical::~ThreadCritical() {92assert(lock_owner == GetCurrentThreadId(), "unlock attempt by wrong thread");93assert(lock_count >= 0, "Attempt to unlock when already unlocked");9495if (lock_count == 0) {96// We're going to unlock97lock_owner = -1;98lock_count = -1;99// No lost wakeups, lock_event stays signaled until reset.100DWORD ret = SetEvent(lock_event);101assert(ret != 0, "unexpected return value from SetEvent");102} else {103// Just unwinding a recursive lock;104lock_count--;105}106}107108109