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