jdk/src/share/instrument/Reentrancy.c
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 282 bca3e5a71df1
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 282
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Copyright 2003 Wily Technology, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include    <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include    <jvmti.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include    "JPLISAssert.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include    "Reentrancy.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include    "JPLISAgent.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  This module provides some utility functions to support the "same thread" re-entrancy management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  Uses JVMTI TLS to store a single bit per thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  Non-zero means the thread is already inside; zero means the thread is not inside.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  Local prototypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/* Wrapper around set that does the set then re-fetches to make sure it worked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Degenerates to a simple set when assertions are disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This routine is only here because of a bug in the JVMTI where set to 0 fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
confirmingTLSSet(   jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                    jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    const void *    newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/* Confirmation routine only; used to assure that the TLS slot holds the value we expect it to. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
assertTLSValue( jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                const void *    expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#define JPLIS_CURRENTLY_INSIDE_TOKEN                ((void *) 0x7EFFC0BB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#define JPLIS_CURRENTLY_OUTSIDE_TOKEN               ((void *) 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
confirmingTLSSet(   jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    const void *    newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    jvmtiError  error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    error = (*jvmtienv)->SetThreadLocalStorage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                    jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                    thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                    newValue);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
    77
    check_phase_ret_blob(error, error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#if JPLISASSERT_ENABLEASSERTIONS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    assertTLSValue( jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
assertTLSValue( jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                const void *    expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    jvmtiError  error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    void *      test = (void *) 0x99999999;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /* now check if we do a fetch we get what we wrote */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    error = (*jvmtienv)->GetThreadLocalStorage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                &test);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   100
    check_phase_ret(error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    jplis_assert(test == expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
tryToAcquireReentrancyToken(    jvmtiEnv *  jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                jthread     thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    jboolean    result      = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    jvmtiError  error       = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    void *      storedValue = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    error = (*jvmtienv)->GetThreadLocalStorage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                &storedValue);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   116
    check_phase_ret_false(error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    if ( error == JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* if this thread is already inside, just return false and short-circuit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if ( storedValue == JPLIS_CURRENTLY_INSIDE_TOKEN ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            result = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            /* stuff in the sentinel and return true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
#if JPLISASSERT_ENABLEASSERTIONS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            assertTLSValue( jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                            JPLIS_CURRENTLY_OUTSIDE_TOKEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            error = confirmingTLSSet (  jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                        thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                        JPLIS_CURRENTLY_INSIDE_TOKEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if ( error != JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                result = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                result = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
releaseReentrancyToken(         jvmtiEnv *  jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                jthread     thread)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    jvmtiError  error       = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
/* assert we hold the token */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
#if JPLISASSERT_ENABLEASSERTIONS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    assertTLSValue( jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    JPLIS_CURRENTLY_INSIDE_TOKEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    error = confirmingTLSSet(   jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                JPLIS_CURRENTLY_OUTSIDE_TOKEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}