1 | /* |
2 | * Copyright (c) 2015, 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 | #include "precompiled.hpp" |
25 | #include "logging/logLevel.hpp" |
26 | #include "logging/logOutputList.hpp" |
27 | #include "memory/allocation.inline.hpp" |
28 | #include "runtime/atomic.hpp" |
29 | #include "runtime/orderAccess.hpp" |
30 | #include "utilities/globalDefinitions.hpp" |
31 | |
32 | jint LogOutputList::increase_readers() { |
33 | jint result = Atomic::add(1, &_active_readers); |
34 | assert(_active_readers > 0, "Ensure we have consistent state" ); |
35 | return result; |
36 | } |
37 | |
38 | jint LogOutputList::decrease_readers() { |
39 | jint result = Atomic::add(-1, &_active_readers); |
40 | assert(result >= 0, "Ensure we have consistent state" ); |
41 | return result; |
42 | } |
43 | |
44 | void LogOutputList::wait_until_no_readers() const { |
45 | OrderAccess::storeload(); |
46 | while (_active_readers != 0) { |
47 | // Busy wait |
48 | } |
49 | } |
50 | |
51 | void LogOutputList::set_output_level(LogOutput* output, LogLevelType level) { |
52 | LogOutputNode* node = find(output); |
53 | if (level == LogLevel::Off && node != NULL) { |
54 | remove_output(node); |
55 | } else if (level != LogLevel::Off && node == NULL) { |
56 | add_output(output, level); |
57 | } else if (node != NULL) { |
58 | update_output_level(node, level); |
59 | } |
60 | } |
61 | |
62 | LogOutputList::LogOutputNode* LogOutputList::find(const LogOutput* output) const { |
63 | for (LogOutputNode* node = _level_start[LogLevel::Last]; node != NULL; node = node->_next) { |
64 | if (output == node->_value) { |
65 | return node; |
66 | } |
67 | } |
68 | return NULL; |
69 | } |
70 | |
71 | void LogOutputList::remove_output(LogOutputList::LogOutputNode* node) { |
72 | assert(node != NULL, "Node must be non-null" ); |
73 | |
74 | // Remove node from _level_start first |
75 | bool found = false; |
76 | for (uint level = LogLevel::First; level < LogLevel::Count; level++) { |
77 | if (_level_start[level] == node) { |
78 | found = true; |
79 | _level_start[level] = node->_next; |
80 | } |
81 | } |
82 | |
83 | // Now remove it from the linked list |
84 | for (LogOutputNode* cur = _level_start[LogLevel::Last]; cur != NULL; cur = cur->_next) { |
85 | if (cur->_next == node) { |
86 | found = true; |
87 | cur->_next = node->_next; |
88 | break; |
89 | } |
90 | } |
91 | assert(found, "Node to be removed should always be found" ); |
92 | |
93 | wait_until_no_readers(); |
94 | delete node; |
95 | } |
96 | |
97 | void LogOutputList::add_output(LogOutput* output, LogLevelType level) { |
98 | LogOutputNode* node = new LogOutputNode(); |
99 | node->_value = output; |
100 | node->_level = level; |
101 | |
102 | // Set the next pointer to the first node of a lower level |
103 | for (node->_next = _level_start[level]; |
104 | node->_next != NULL && node->_next->_level == level; |
105 | node->_next = node->_next->_next) { |
106 | } |
107 | |
108 | // Update the _level_start index |
109 | for (int l = LogLevel::Last; l >= level; l--) { |
110 | if (_level_start[l] == NULL || _level_start[l]->_level < level) { |
111 | _level_start[l] = node; |
112 | } |
113 | } |
114 | |
115 | // Add the node the list |
116 | for (LogOutputNode* cur = _level_start[LogLevel::Last]; cur != NULL; cur = cur->_next) { |
117 | if (cur != node && cur->_next == node->_next) { |
118 | cur->_next = node; |
119 | break; |
120 | } |
121 | } |
122 | } |
123 | |
124 | void LogOutputList::update_output_level(LogOutputList::LogOutputNode* node, LogLevelType level) { |
125 | add_output(node->_value, level); |
126 | wait_until_no_readers(); |
127 | remove_output(node); |
128 | } |
129 | |