Skip to content

Commit b9d7337

Browse files
Xin LiuPaul Hohensee
Xin Liu
authored and
Paul Hohensee
committedJun 18, 2021
8268638: semaphores of AsyncLogWriter may be broken when JVM is exiting.
Backport-of: fa3b44d
1 parent 8caeca0 commit b9d7337

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed
 

‎src/hotspot/share/logging/logAsyncWriter.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,18 @@
2828
#include "logging/logHandle.hpp"
2929
#include "runtime/atomic.hpp"
3030

31-
Semaphore AsyncLogWriter::_sem(0);
32-
Semaphore AsyncLogWriter::_io_sem(1);
33-
34-
class AsyncLogLocker : public StackObj {
35-
private:
36-
static Semaphore _lock;
31+
class AsyncLogWriter::AsyncLogLocker : public StackObj {
3732
public:
3833
AsyncLogLocker() {
39-
_lock.wait();
34+
assert(_instance != nullptr, "AsyncLogWriter::_lock is unavailable");
35+
_instance->_lock.wait();
4036
}
4137

4238
~AsyncLogLocker() {
43-
_lock.signal();
39+
_instance->_lock.signal();
4440
}
4541
};
4642

47-
Semaphore AsyncLogLocker::_lock(1);
48-
4943
void AsyncLogWriter::enqueue_locked(const AsyncLogMessage& msg) {
5044
if (_buffer.size() >= _buffer_max_size) {
5145
bool p_created;
@@ -64,15 +58,15 @@ void AsyncLogWriter::enqueue(LogFileOutput& output, const LogDecorations& decora
6458
AsyncLogMessage m(output, decorations, os::strdup(msg));
6559

6660
{ // critical area
67-
AsyncLogLocker lock;
61+
AsyncLogLocker locker;
6862
enqueue_locked(m);
6963
}
7064
}
7165

7266
// LogMessageBuffer consists of a multiple-part/multiple-line messsage.
7367
// The lock here guarantees its integrity.
7468
void AsyncLogWriter::enqueue(LogFileOutput& output, LogMessageBuffer::Iterator msg_iterator) {
75-
AsyncLogLocker lock;
69+
AsyncLogLocker locker;
7670

7771
for (; !msg_iterator.is_at_end(); msg_iterator++) {
7872
AsyncLogMessage m(output, msg_iterator.decorations(), os::strdup(msg_iterator.message()));
@@ -81,7 +75,8 @@ void AsyncLogWriter::enqueue(LogFileOutput& output, LogMessageBuffer::Iterator m
8175
}
8276

8377
AsyncLogWriter::AsyncLogWriter()
84-
: _initialized(false),
78+
: _lock(1), _sem(0), _io_sem(1),
79+
_initialized(false),
8580
_stats(17 /*table_size*/) {
8681
if (os::create_thread(this, os::asynclog_thread)) {
8782
_initialized = true;
@@ -125,7 +120,7 @@ void AsyncLogWriter::write() {
125120
bool own_io = false;
126121

127122
{ // critical region
128-
AsyncLogLocker lock;
123+
AsyncLogLocker locker;
129124

130125
_buffer.pop_all(&logs);
131126
// append meta-messages of dropped counters

‎src/hotspot/share/logging/logAsyncWriter.hpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,16 @@ typedef KVHashtable<LogFileOutput*, uint32_t, mtLogging> AsyncLogMap;
133133
// times. It is no-op if async logging is not established.
134134
//
135135
class AsyncLogWriter : public NonJavaThread {
136+
class AsyncLogLocker;
137+
136138
static AsyncLogWriter* _instance;
139+
// _lock(1) denotes a critional region.
140+
Semaphore _lock;
137141
// _sem is a semaphore whose value denotes how many messages have been enqueued.
138142
// It decreases in AsyncLogWriter::run()
139-
static Semaphore _sem;
143+
Semaphore _sem;
140144
// A lock of IO
141-
static Semaphore _io_sem;
145+
Semaphore _io_sem;
142146

143147
volatile bool _initialized;
144148
AsyncLogMap _stats; // statistics for dropped messages

0 commit comments

Comments
 (0)
Please sign in to comment.