Path: blob/master/src/hotspot/os/linux/threadCritical_linux.cpp
40951 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// put OS-includes here29# include <pthread.h>3031//32// See threadCritical.hpp for details of this class.33//3435static pthread_t tc_owner = 0;36static pthread_mutex_t tc_mutex = PTHREAD_MUTEX_INITIALIZER;37static int tc_count = 0;3839ThreadCritical::ThreadCritical() {40pthread_t self = pthread_self();41if (self != tc_owner) {42int ret = pthread_mutex_lock(&tc_mutex);43guarantee(ret == 0, "fatal error with pthread_mutex_lock()");44assert(tc_count == 0, "Lock acquired with illegal reentry count.");45tc_owner = self;46}47tc_count++;48}4950ThreadCritical::~ThreadCritical() {51assert(tc_owner == pthread_self(), "must have correct owner");52assert(tc_count > 0, "must have correct count");5354tc_count--;55if (tc_count == 0) {56tc_owner = 0;57int ret = pthread_mutex_unlock(&tc_mutex);58guarantee(ret == 0, "fatal error with pthread_mutex_unlock()");59}60}616263