hotspot/src/os_cpu/solaris_x86/vm/threadLS_solaris_x86.cpp
author chegar
Thu, 06 May 2010 17:17:09 +0100
changeset 5463 ad2d35ddcf37
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6946825: com.sun.net.httpserver.HttpServer; Memory Leak on Non HTTP conform open socket Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include "incls/_threadLS_solaris_x86.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
extern "C" Thread*  fs_load(ptrdiff_t tlsOffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
extern "C" intptr_t fs_thread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// From solaris_i486.s
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
extern "C" Thread*  gs_load(ptrdiff_t tlsOffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
extern "C" intptr_t gs_thread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// tlsMode encoding:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// pd_tlsAccessUndefined : uninitialized
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// pd_tlsAccessSlow      : not available
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// pd_tlsAccessIndirect  :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//   old-style indirect access - present in "T1" libthread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
//   use thr_slot_sync_allocate() to attempt to allocate a slot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
// pd_tlsAccessDirect    :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
//   new-style direct access - present in late-model "T2" libthread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
//   Allocate the offset (slot) via _thr_slot_offset() or by
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
//   defining an IE- or LE-mode TLS/TSD slot in the launcher and then passing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
//   that offset into libjvm.so.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
//   See http://sac.eng/Archives/CaseLog/arc/PSARC/2003/159/.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
// Note that we have a capability gap - some early model T2 forms
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// (e.g., unpatched S9) have neither _thr_slot_sync_allocate() nor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
// _thr_slot_offset().  In that case we revert to the usual
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// thr_getspecific accessor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
static ThreadLocalStorage::pd_tlsAccessMode tlsMode = ThreadLocalStorage::pd_tlsAccessUndefined ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
static ptrdiff_t tlsOffset = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
static thread_key_t tlsKey ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
typedef int (*TSSA_Entry) (ptrdiff_t *, int, int) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
typedef ptrdiff_t (*TSO_Entry) (int) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
ThreadLocalStorage::pd_tlsAccessMode ThreadLocalStorage::pd_getTlsAccessMode ()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
   guarantee (tlsMode != pd_tlsAccessUndefined, "tlsMode not set") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
   return tlsMode ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
ptrdiff_t ThreadLocalStorage::pd_getTlsOffset () {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
   guarantee (tlsMode != pd_tlsAccessUndefined, "tlsMode not set") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
   return tlsOffset ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
// TODO: Consider the following improvements:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// 1.   Convert from thr_*specific* to pthread_*specific*.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
//      The pthread_ forms are slightly faster.  Also, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
//      pthread_ forms have a pthread_key_delete() API which
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
//      would aid in clean JVM shutdown and the eventual goal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
//      of permitting a JVM to reinstantiate itself withing a process.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
// 2.   See ThreadLocalStorage::init().  We end up allocating
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
//      two TLS keys during VM startup.  That's benign, but we could collapse
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
//      down to one key without too much trouble.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
// 3.   MacroAssembler::get_thread() currently emits calls to thr_getspecific().
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
//      Modify get_thread() to call Thread::current() instead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
// 4.   Thread::current() currently uses a cache keyed by %gs:[0].
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
//      (The JVM has PSARC permission to use %g7/%gs:[0]
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
//      as an opaque temporally unique thread identifier).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
//      For C++ access to a thread's reflexive "self" pointer we
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
//      should consider using one of the following:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
//      a. a radix tree keyed by %esp - as in EVM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
//         This requires two loads (the 2nd dependent on the 1st), but
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
//         is easily inlined and doesn't require a "miss" slow path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
//      b. a fast TLS/TSD slot allocated by _thr_slot_offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
//         or _thr_slot_sync_allocate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
// 5.   'generate_code_for_get_thread' is a misnomer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
//      We should change it to something more general like
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
//      pd_ThreadSelf_Init(), for instance.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
static void AllocateTLSOffset ()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
   int rslt ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
   TSSA_Entry tssa ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
   TSO_Entry  tso ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
   ptrdiff_t off ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
   guarantee (tlsMode == ThreadLocalStorage::pd_tlsAccessUndefined, "tlsMode not set") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
   tlsMode = ThreadLocalStorage::pd_tlsAccessSlow ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
   tlsOffset = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
#ifndef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
   tssa = (TSSA_Entry) dlsym (RTLD_DEFAULT, "thr_slot_sync_allocate") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
   if (tssa != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
        off = -1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
        rslt = (*tssa)(&off, NULL, NULL) ;                // (off,dtor,darg)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
        if (off != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
           tlsOffset = off ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
           tlsMode = ThreadLocalStorage::pd_tlsAccessIndirect ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
           return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    rslt = thr_keycreate (&tlsKey, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    if (rslt != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        tlsMode = ThreadLocalStorage::pd_tlsAccessSlow ;   // revert to slow mode
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
        return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    tso = (TSO_Entry) dlsym (RTLD_DEFAULT, "_thr_slot_offset") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    if (tso != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
        off = (*tso)(tlsKey) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
        if (off >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
           tlsOffset = off ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
           tlsMode = ThreadLocalStorage::pd_tlsAccessDirect ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
           return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    // Failure: Too bad ... we've allocated a TLS slot we don't need and there's
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    // no provision in the ABI for returning the slot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    // If we didn't find a slot then then:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    // 1. We might be on liblwp.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    // 2. We might be on T2 libthread, but all "fast" slots are already
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    //    consumed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    // 3. We might be on T1, and all TSD (thr_slot_sync_allocate) slots are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    //    consumed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    // 4. We might be on T2 libthread, but it's be re-architected
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    //    so that fast slots are no longer g7-relative.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    tlsMode = ThreadLocalStorage::pd_tlsAccessSlow ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
void ThreadLocalStorage::generate_code_for_get_thread() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    AllocateTLSOffset() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
void ThreadLocalStorage::set_thread_in_slot(Thread *thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  guarantee (tlsMode != pd_tlsAccessUndefined, "tlsMode not set") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  if (tlsMode == pd_tlsAccessIndirect) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
        intptr_t tbase = fs_thread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
        intptr_t tbase = gs_thread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
        *((Thread**) (tbase + tlsOffset)) = thread ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  if (tlsMode == pd_tlsAccessDirect) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
        thr_setspecific (tlsKey, (void *) thread) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
        // set with thr_setspecific and then readback with gs_load to validate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
#ifdef AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
        guarantee (thread == fs_load(tlsOffset), "tls readback failure") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
        guarantee (thread == gs_load(tlsOffset), "tls readback failure") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
#endif // AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
extern "C" Thread* get_thread() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  return ThreadLocalStorage::thread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
}