src/os/windows/vm/threadCritical_windows.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "runtime/thread.inline.hpp"
aoqi@0 27 #include "runtime/threadCritical.hpp"
aoqi@0 28
aoqi@0 29 // OS-includes here
aoqi@0 30 # include <windows.h>
aoqi@0 31 # include <winbase.h>
aoqi@0 32
aoqi@0 33 //
aoqi@0 34 // See threadCritical.hpp for details of this class.
aoqi@0 35 //
aoqi@0 36
aoqi@0 37 static bool initialized = false;
aoqi@0 38 static volatile jint lock_count = -1;
aoqi@0 39 static HANDLE lock_event;
aoqi@0 40 static DWORD lock_owner = -1;
aoqi@0 41
aoqi@0 42 //
aoqi@0 43 // Note that Microsoft's critical region code contains a race
aoqi@0 44 // condition, and is not suitable for use. A thread holding the
aoqi@0 45 // critical section cannot safely suspend a thread attempting
aoqi@0 46 // to enter the critical region. The failure mode is that both
aoqi@0 47 // threads are permanently suspended.
aoqi@0 48 //
aoqi@0 49 // I experiemented with the use of ordinary windows mutex objects
aoqi@0 50 // and found them ~30 times slower than the critical region code.
aoqi@0 51 //
aoqi@0 52
aoqi@0 53 void ThreadCritical::initialize() {
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 void ThreadCritical::release() {
aoqi@0 57 assert(lock_owner == -1, "Mutex being deleted while owned.");
aoqi@0 58 assert(lock_count == -1, "Mutex being deleted while recursively locked");
aoqi@0 59 assert(lock_event != NULL, "Sanity check");
aoqi@0 60 CloseHandle(lock_event);
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 ThreadCritical::ThreadCritical() {
aoqi@0 64 DWORD current_thread = GetCurrentThreadId();
aoqi@0 65
aoqi@0 66 if (lock_owner != current_thread) {
aoqi@0 67 // Grab the lock before doing anything.
aoqi@0 68 while (Atomic::cmpxchg(0, &lock_count, -1) != -1) {
aoqi@0 69 if (initialized) {
aoqi@0 70 DWORD ret = WaitForSingleObject(lock_event, INFINITE);
aoqi@0 71 assert(ret == WAIT_OBJECT_0, "unexpected return value from WaitForSingleObject");
aoqi@0 72 }
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 // Make sure the event object is allocated.
aoqi@0 76 if (!initialized) {
aoqi@0 77 // Locking will not work correctly unless this is autoreset.
aoqi@0 78 lock_event = CreateEvent(NULL, false, false, NULL);
aoqi@0 79 initialized = true;
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 assert(lock_owner == -1, "Lock acquired illegally.");
aoqi@0 83 lock_owner = current_thread;
aoqi@0 84 } else {
aoqi@0 85 // Atomicity isn't required. Bump the recursion count.
aoqi@0 86 lock_count++;
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 assert(lock_owner == GetCurrentThreadId(), "Lock acquired illegally.");
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 ThreadCritical::~ThreadCritical() {
aoqi@0 93 assert(lock_owner == GetCurrentThreadId(), "unlock attempt by wrong thread");
aoqi@0 94 assert(lock_count >= 0, "Attempt to unlock when already unlocked");
aoqi@0 95
aoqi@0 96 if (lock_count == 0) {
aoqi@0 97 // We're going to unlock
aoqi@0 98 lock_owner = -1;
aoqi@0 99 lock_count = -1;
aoqi@0 100 // No lost wakeups, lock_event stays signaled until reset.
aoqi@0 101 DWORD ret = SetEvent(lock_event);
aoqi@0 102 assert(ret != 0, "unexpected return value from SetEvent");
aoqi@0 103 } else {
aoqi@0 104 // Just unwinding a recursive lock;
aoqi@0 105 lock_count--;
aoqi@0 106 }
aoqi@0 107 }

mercurial