1 /*
2  * Copyright (c) 2017, 2020, 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 #ifndef SHARE_RUNTIME_SAFEPOINTMECHANISM_HPP
26 #define SHARE_RUNTIME_SAFEPOINTMECHANISM_HPP
27 
28 #include "runtime/globals.hpp"
29 #include "utilities/globalDefinitions.hpp"
30 #include "utilities/macros.hpp"
31 #include "utilities/sizes.hpp"
32 
33 class JavaThread;
34 class Thread;
35 
36 // This is the abstracted interface for the safepoint implementation
37 class SafepointMechanism : public AllStatic {
38   friend class StackWatermark;
39   static uintptr_t _poll_page_armed_value;
40   static uintptr_t _poll_page_disarmed_value;
41 
42   static uintptr_t _poll_word_armed_value;
43   static uintptr_t _poll_word_disarmed_value;
44 
45   static address _polling_page;
46 
47 
48   static inline void disarm_local_poll(JavaThread* thread);
49 
50   static inline bool local_poll(Thread* thread);
51   static inline bool global_poll();
52 
53   static void process(JavaThread *thread);
54   static void process_if_requested_slow(JavaThread *thread);
55 
56   static void default_initialize();
57 
58   static void pd_initialize() NOT_AIX({ default_initialize(); });
59 
60   static uintptr_t compute_poll_word(bool armed, uintptr_t stack_watermark);
61 
62   const static intptr_t _poll_bit = 1;
63 public:
64   static inline bool local_poll_armed(JavaThread* thread);
poll_bit()65   static intptr_t poll_bit() { return _poll_bit; }
66 
get_polling_page()67   static address get_polling_page()             { return _polling_page; }
is_poll_address(address addr)68   static bool    is_poll_address(address addr)  { return addr >= _polling_page && addr < (_polling_page + os::vm_page_size()); }
69 
70   struct ThreadData {
71     volatile uintptr_t _polling_word;
72     volatile uintptr_t _polling_page;
73 
74     inline void set_polling_word(uintptr_t poll_value);
75     inline uintptr_t get_polling_word();
76 
77     inline void set_polling_page(uintptr_t poll_value);
78     inline uintptr_t get_polling_page();
79   };
80 
81   // Call this method to see if this thread should block for a safepoint or process handshake.
82   static inline bool should_process(Thread* thread);
83 
84   // Processes a pending requested operation.
85   static inline void process_if_requested(JavaThread* thread);
86   static inline void process_if_requested_with_exit_check(JavaThread* thread, bool check_asyncs);
87   // Compute what the poll values should be and install them.
88   static void update_poll_values(JavaThread* thread);
89 
90   // Caller is responsible for using a memory barrier if needed.
91   static inline void arm_local_poll(JavaThread* thread);
92   // Release semantics
93   static inline void arm_local_poll_release(JavaThread* thread);
94 
95   // Setup the selected safepoint mechanism
96   static void initialize();
97   static void initialize_header(JavaThread* thread);
98 };
99 
100 #endif // SHARE_RUNTIME_SAFEPOINTMECHANISM_HPP
101