src/hotspot/os/bsd/threadCritical_bsd.cpp
author mgerdin
Mon, 25 Sep 2017 21:25:46 -0400
changeset 47524 97569cf468f4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8187040: ThreadCritical crashes on Solaris if used between os::init and os::init_2 Reviewed-by: dholmes, stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     4
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     8
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    13
 * accompanied this code).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    14
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    18
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    21
 * questions.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    22
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    23
 */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    24
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    25
#include "precompiled.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 10565
diff changeset
    26
#include "runtime/thread.inline.hpp"
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    27
#include "runtime/threadCritical.hpp"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    28
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    29
// put OS-includes here
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    30
# include <pthread.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    31
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    32
//
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    33
// See threadCritical.hpp for details of this class.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    34
//
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    35
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    36
static pthread_t             tc_owner = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    37
static pthread_mutex_t       tc_mutex = PTHREAD_MUTEX_INITIALIZER;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    38
static int                   tc_count = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    39
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    40
ThreadCritical::ThreadCritical() {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    41
  pthread_t self = pthread_self();
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    42
  if (self != tc_owner) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    43
    int ret = pthread_mutex_lock(&tc_mutex);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    44
    guarantee(ret == 0, "fatal error with pthread_mutex_lock()");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    45
    assert(tc_count == 0, "Lock acquired with illegal reentry count.");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    46
    tc_owner = self;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    47
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    48
  tc_count++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    49
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    50
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    51
ThreadCritical::~ThreadCritical() {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    52
  assert(tc_owner == pthread_self(), "must have correct owner");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    53
  assert(tc_count > 0, "must have correct count");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    54
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    55
  tc_count--;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    56
  if (tc_count == 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    57
    tc_owner = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    58
    int ret = pthread_mutex_unlock(&tc_mutex);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    59
    guarantee(ret == 0, "fatal error with pthread_mutex_unlock()");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    60
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    61
}