author | redestad |
Thu, 20 Dec 2018 19:44:08 +0100 | |
changeset 53091 | 5bf5b773fc41 |
parent 52332 | d2a3503c72f7 |
child 53244 | 9807daeb47c4 |
permissions | -rw-r--r-- |
49800 | 1 |
/* |
2 |
* Copyright (c) 2018, 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_UTILITIES_GLOBAL_COUNTER_HPP |
|
26 |
#define SHARE_UTILITIES_GLOBAL_COUNTER_HPP |
|
27 |
||
28 |
#include "memory/allocation.hpp" |
|
29 |
#include "memory/padded.hpp" |
|
30 |
||
31 |
class Thread; |
|
32 |
||
33 |
// The GlobalCounter provides a synchronization mechanism between threads for |
|
34 |
// safe memory reclamation and other ABA problems. All readers must call |
|
35 |
// critical_section_begin before reading the volatile data and |
|
52332
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
36 |
// critical_section_end afterwards. Such read-side critical sections may |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
37 |
// be properly nested. The write side must call write_synchronize |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
38 |
// before reclaming the memory. The read-path only does an uncontended store |
49800 | 39 |
// to a thread-local-storage and fence to stop any loads from floating up, thus |
40 |
// light weight and wait-free. The write-side is more heavy since it must check |
|
41 |
// all readers and wait until they have left the generation. (a system memory |
|
42 |
// barrier can be used on write-side to remove fence in read-side, |
|
43 |
// not implemented). |
|
44 |
class GlobalCounter : public AllStatic { |
|
45 |
private: |
|
46 |
// Since do not know what we will end up next to in BSS, we make sure the |
|
47 |
// counter is on a seperate cacheline. |
|
48 |
struct PaddedCounter { |
|
52227 | 49 |
DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, 0); |
49800 | 50 |
volatile uintx _counter; |
52227 | 51 |
DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile uintx)); |
49800 | 52 |
}; |
53 |
||
54 |
// The global counter |
|
55 |
static PaddedCounter _global_counter; |
|
56 |
||
57 |
// Bit 0 is active bit. |
|
58 |
static const uintx COUNTER_ACTIVE = 1; |
|
59 |
// Thus we increase counter by 2. |
|
60 |
static const uintx COUNTER_INCREMENT = 2; |
|
61 |
||
62 |
// The per thread scanning closure. |
|
63 |
class CounterThreadCheck; |
|
64 |
||
65 |
public: |
|
52332
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
66 |
// The type of the critical section context passed from |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
67 |
// critical_section_begin() to critical_section_end(). |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
68 |
typedef uintx CSContext; |
49800 | 69 |
|
52332
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
70 |
// Must be called before accessing the data. The result must be passed |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
71 |
// to the associated call to critical_section_end(). Acts as a full |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
72 |
// memory barrier before the code within the critical section. |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
73 |
static CSContext critical_section_begin(Thread *thread); |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
74 |
|
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
75 |
// Must be called after finished accessing the data. The context |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
76 |
// must be the result of the associated initiating critical_section_begin(). |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
77 |
// Acts as a release memory barrier after the code within the critical |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
78 |
// section. |
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
79 |
static void critical_section_end(Thread *thread, CSContext context); |
49800 | 80 |
|
81 |
// Make the data inaccessible to readers before calling. When this call |
|
52332
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
82 |
// returns it's safe to reclaim the data. Acts as a full memory barrier. |
49800 | 83 |
static void write_synchronize(); |
84 |
||
52332
d2a3503c72f7
8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
52227
diff
changeset
|
85 |
// A scoped object for a read-side critical-section. |
49800 | 86 |
class CriticalSection; |
87 |
}; |
|
88 |
||
89 |
#endif // include guard |