Skip to content

Commit 07a7905

Browse files
author
Xin Liu
committed
Use RAII to protect _rotation_semaphore.
1 parent a52019d commit 07a7905

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/hotspot/share/logging/logConfiguration.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,6 @@ void LogConfiguration::configure_output(size_t idx, const LogSelectionList& sele
275275
// 2. asynclog buffer may be holding some log messages targeting to the output 'idx'. It has been disabled by new setting,
276276
// eg. all=off and is about to be purged in delete_output(idx).
277277
//
278-
279-
#ifdef ASSERT
280-
if (!enabled) {
281-
for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
282-
assert(!ts->has_output(output), "The node of output should been removed in ts->set_output_level.");
283-
}
284-
}
285-
#endif
286-
287278
AsyncLogWriter::flush();
288279

289280
// It is now safe to set the new decorators for the actual output

src/hotspot/share/logging/logFileOutput.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@
3333
#include "utilities/globalDefinitions.hpp"
3434
#include "utilities/defaultStream.hpp"
3535

36+
class RotationLocker : public StackObj {
37+
Semaphore& _sem;
38+
39+
public:
40+
RotationLocker(Semaphore& sem) : _sem(sem) {
41+
sem.wait();
42+
}
43+
44+
~RotationLocker() {
45+
_sem.signal();
46+
}
47+
};
48+
3649
const char* const LogFileOutput::Prefix = "file=";
3750
const char* const LogFileOutput::FileOpenMode = "a";
3851
const char* const LogFileOutput::PidFilenamePlaceholder = "%p";
@@ -286,7 +299,7 @@ bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
286299
}
287300

288301
int LogFileOutput::write_blocking(const LogDecorations& decorations, const char* msg) {
289-
_rotation_semaphore.wait();
302+
RotationLocker lock(_rotation_semaphore);
290303
if (_stream == NULL) {
291304
// An error has occurred with this output, avoid writing to it.
292305
return 0;
@@ -300,7 +313,6 @@ int LogFileOutput::write_blocking(const LogDecorations& decorations, const char*
300313
rotate();
301314
}
302315
}
303-
_rotation_semaphore.signal();
304316

305317
return written;
306318
}
@@ -332,7 +344,7 @@ int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
332344
return 0;
333345
}
334346

335-
_rotation_semaphore.wait();
347+
RotationLocker lock(_rotation_semaphore);
336348
int written = LogFileStreamOutput::write(msg_iterator);
337349
if (written > 0) {
338350
_current_size += written;
@@ -341,7 +353,6 @@ int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
341353
rotate();
342354
}
343355
}
344-
_rotation_semaphore.signal();
345356

346357
return written;
347358
}
@@ -368,13 +379,11 @@ void LogFileOutput::force_rotate() {
368379
// Rotation not possible
369380
return;
370381
}
371-
_rotation_semaphore.wait();
382+
RotationLocker lock(_rotation_semaphore);
372383
rotate();
373-
_rotation_semaphore.signal();
374384
}
375385

376386
void LogFileOutput::rotate() {
377-
378387
if (fclose(_stream)) {
379388
jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
380389
_file_name, os::strerror(errno));

test/hotspot/gtest/logging/test_logConfiguration.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,10 @@ TEST_VM_F(LogConfigurationTest, reconfigure_tags_MT) {
302302
break;
303303
}
304304

305-
// Take turn logging with different decorators, either None or All.
305+
// turn on/off the tagset 'logging'.
306306
set_log_config(TestLogFileName, "logging=off");
307307
set_log_config(TestLogFileName, "logging=debug", "", "filecount=0");
308+
// sleep a prime number milliseconds to allow concurrent logsites write logs
308309
os::naked_short_nanosleep(137);
309310
}
310311

0 commit comments

Comments
 (0)