1 /*
   2  * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // This is embedded via include into the class OSThread
  26 
  27  private:
  28 
  29   thread_t _thread_id;      // Solaris thread id
  30   unsigned int  _lwp_id;    // lwp ID, only used with bound threads
  31   sigset_t _caller_sigmask; // Caller's signal mask
  32   bool _vm_created_thread;  // true if the VM create this thread
  33                             // false if primary thread or attached thread
  34  public:
  35 
  36   thread_t thread_id() const      { return _thread_id; }
  37 
  38   unsigned int lwp_id() const     { return _lwp_id; }
  39 
  40   // Set and get state of _vm_created_thread flag
  41   void set_vm_created()           { _vm_created_thread = true; }
  42   bool is_vm_created()            { return _vm_created_thread; }
  43 
  44   // Methods to save/restore caller's signal mask
  45   sigset_t  caller_sigmask() const       { return _caller_sigmask; }
  46   void    set_caller_sigmask(sigset_t sigmask)  { _caller_sigmask = sigmask; }
  47 
  48 #ifndef PRODUCT
  49   // Used for debugging, return a unique integer for each thread.
  50   int thread_identifier() const   { return _thread_id; }
  51 #endif
  52 #ifdef ASSERT
  53   // On solaris reposition can fail in two ways:
  54   // 1: a mismatched pc, because signal is delivered too late, target thread
  55   //    is resumed.
  56   // 2: on a timeout where signal is lost, target thread is resumed.
  57   bool valid_reposition_failure() {
  58     // only 1 and 2 can happen and we can handle both of them
  59     return true;
  60   }
  61 #endif
  62   void set_thread_id(thread_t id) { _thread_id = id;   }
  63   void set_lwp_id(unsigned int id){ _lwp_id = id;   }
  64 
  65  // ***************************************************************
  66  // interrupt support.  interrupts (using signals) are used to get
  67  // the thread context (get_thread_pc), to set the thread context
  68  // (set_thread_pc), and to implement java.lang.Thread.interrupt.
  69  // ***************************************************************
  70 
  71  public:
  72 
  73   class InterruptArguments : StackObj {
  74    private:
  75     Thread*     _thread;   // the thread to signal was dispatched to
  76     ucontext_t* _ucontext; // the machine context at the time of the signal
  77 
  78    public:
  79     InterruptArguments(Thread* thread, ucontext_t* ucontext) {
  80       _thread   = thread;
  81       _ucontext = ucontext;
  82     }
  83 
  84     Thread*     thread()   const { return _thread;   }
  85     ucontext_t* ucontext() const { return _ucontext; }
  86   };
  87 
  88   // There are currently no asynchronous callbacks - and we'd better not
  89   // support them in the future either, as they need to be deallocated from
  90   // the interrupt handler, which is not safe; they also require locks to
  91   // protect the callback queue.
  92 
  93   class Sync_Interrupt_Callback : private StackObj {
  94    protected:
  95     volatile bool _is_done;
  96     Monitor*      _sync;
  97     Thread*       _target;
  98    public:
  99     Sync_Interrupt_Callback(Monitor * sync) {
 100       _is_done = false;  _target = NULL;  _sync = sync;
 101     }
 102 
 103     bool is_done() const               { return _is_done; }
 104     Thread* target() const             { return _target;  }
 105 
 106     int interrupt(Thread * target, int timeout);
 107 
 108     // override to implement the callback.
 109     virtual void execute(InterruptArguments *args) = 0;
 110 
 111     void leave_callback();
 112   };
 113 
 114  private:
 115 
 116   Sync_Interrupt_Callback * volatile _current_callback;
 117   enum {
 118     callback_in_progress = 1
 119   };
 120   Mutex * _current_callback_lock;       // only used on v8
 121 
 122  public:
 123 
 124   int set_interrupt_callback    (Sync_Interrupt_Callback * cb);
 125   void remove_interrupt_callback(Sync_Interrupt_Callback * cb);
 126   void do_interrupt_callbacks_at_interrupt(InterruptArguments *args);
 127 
 128  // ***************************************************************
 129  // java.lang.Thread.interrupt state.
 130  // ***************************************************************
 131 
 132  private:
 133 
 134   JavaThreadState      _saved_interrupt_thread_state;       // the thread state before a system call -- restored afterward
 135 
 136  public:
 137 
 138 
 139   JavaThreadState   saved_interrupt_thread_state()                              { return _saved_interrupt_thread_state; }
 140   void              set_saved_interrupt_thread_state(JavaThreadState state)     { _saved_interrupt_thread_state = state; }
 141 
 142   static void       handle_spinlock_contention(int tries);                      // Used for thread local eden locking
 143 
 144   // ***************************************************************
 145   // Platform dependent initialization and cleanup
 146   // ***************************************************************
 147 
 148 private:
 149 
 150   void pd_initialize();
 151   void pd_destroy();