Path: blob/master/src/hotspot/os/windows/threadCritical_windows.cpp
40930 views
/*1* Copyright (c) 2001, 2016, 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/atomic.hpp"26#include "runtime/thread.inline.hpp"27#include "runtime/threadCritical.hpp"2829// OS-includes here30# include <windows.h>31# include <winbase.h>3233//34// See threadCritical.hpp for details of this class.35//3637static INIT_ONCE initialized = INIT_ONCE_STATIC_INIT;38static int lock_count = 0;39static HANDLE lock_event;40static DWORD lock_owner = 0;4142//43// Note that Microsoft's critical region code contains a race44// condition, and is not suitable for use. A thread holding the45// critical section cannot safely suspend a thread attempting46// to enter the critical region. The failure mode is that both47// threads are permanently suspended.48//49// I experiemented with the use of ordinary windows mutex objects50// and found them ~30 times slower than the critical region code.51//5253static BOOL WINAPI initialize(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) {54lock_event = CreateEvent(NULL, false, true, NULL);55assert(lock_event != NULL, "unexpected return value from CreateEvent");56return true;57}5859ThreadCritical::ThreadCritical() {60InitOnceExecuteOnce(&initialized, &initialize, NULL, NULL);6162DWORD current_thread = GetCurrentThreadId();63if (lock_owner != current_thread) {64// Grab the lock before doing anything.65DWORD ret = WaitForSingleObject(lock_event, INFINITE);66assert(ret == WAIT_OBJECT_0, "unexpected return value from WaitForSingleObject");67lock_owner = current_thread;68}69// Atomicity isn't required. Bump the recursion count.70lock_count++;71}7273ThreadCritical::~ThreadCritical() {74assert(lock_owner == GetCurrentThreadId(), "unlock attempt by wrong thread");75assert(lock_count >= 0, "Attempt to unlock when already unlocked");7677lock_count--;78if (lock_count == 0) {79// We're going to unlock80lock_owner = 0;81// No lost wakeups, lock_event stays signaled until reset.82DWORD ret = SetEvent(lock_event);83assert(ret != 0, "unexpected return value from SetEvent");84}85}868788