never@3156: /* never@3156: * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. never@3156: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. never@3156: * never@3156: * This code is free software; you can redistribute it and/or modify it never@3156: * under the terms of the GNU General Public License version 2 only, as never@3156: * published by the Free Software Foundation. never@3156: * never@3156: * This code is distributed in the hope that it will be useful, but WITHOUT never@3156: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or never@3156: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License never@3156: * version 2 for more details (a copy is included in the LICENSE file that never@3156: * accompanied this code). never@3156: * never@3156: * You should have received a copy of the GNU General Public License version never@3156: * 2 along with this work; if not, write to the Free Software Foundation, never@3156: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. never@3156: * never@3156: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA never@3156: * or visit www.oracle.com if you need additional information or have any never@3156: * questions. never@3156: * never@3156: */ never@3156: never@3156: #include "precompiled.hpp" stefank@4299: #include "runtime/thread.inline.hpp" never@3156: #include "runtime/threadCritical.hpp" never@3156: never@3156: // put OS-includes here never@3156: # include never@3156: never@3156: // never@3156: // See threadCritical.hpp for details of this class. never@3156: // never@3156: never@3156: static pthread_t tc_owner = 0; never@3156: static pthread_mutex_t tc_mutex = PTHREAD_MUTEX_INITIALIZER; never@3156: static int tc_count = 0; never@3156: never@3156: void ThreadCritical::initialize() { never@3156: } never@3156: never@3156: void ThreadCritical::release() { never@3156: } never@3156: never@3156: ThreadCritical::ThreadCritical() { never@3156: pthread_t self = pthread_self(); never@3156: if (self != tc_owner) { never@3156: int ret = pthread_mutex_lock(&tc_mutex); never@3156: guarantee(ret == 0, "fatal error with pthread_mutex_lock()"); never@3156: assert(tc_count == 0, "Lock acquired with illegal reentry count."); never@3156: tc_owner = self; never@3156: } never@3156: tc_count++; never@3156: } never@3156: never@3156: ThreadCritical::~ThreadCritical() { never@3156: assert(tc_owner == pthread_self(), "must have correct owner"); never@3156: assert(tc_count > 0, "must have correct count"); never@3156: never@3156: tc_count--; never@3156: if (tc_count == 0) { never@3156: tc_owner = 0; never@3156: int ret = pthread_mutex_unlock(&tc_mutex); never@3156: guarantee(ret == 0, "fatal error with pthread_mutex_unlock()"); never@3156: } never@3156: }