Skip to content

Commit cfa92a5

Browse files
committed
8256178: Add RAII object for file lock
Reviewed-by: dholmes, coleenp
1 parent 2b15571 commit cfa92a5

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/hotspot/share/logging/logFileStreamOutput.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -72,18 +72,31 @@ int LogFileStreamOutput::write_decorations(const LogDecorations& decorations) {
7272
return total_written;
7373
}
7474

75+
class FileLocker : public StackObj {
76+
private:
77+
FILE *_file;
78+
79+
public:
80+
FileLocker(FILE *file) : _file(file) {
81+
os::flockfile(_file);
82+
}
83+
84+
~FileLocker() {
85+
os::funlockfile(_file);
86+
}
87+
};
88+
7589
int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) {
7690
const bool use_decorations = !_decorators.is_empty();
7791

7892
int written = 0;
79-
os::flockfile(_stream);
93+
FileLocker flocker(_stream);
8094
if (use_decorations) {
8195
written += write_decorations(decorations);
8296
written += jio_fprintf(_stream, " ");
8397
}
8498
written += jio_fprintf(_stream, "%s\n", msg);
8599
fflush(_stream);
86-
os::funlockfile(_stream);
87100

88101
return written;
89102
}
@@ -92,7 +105,7 @@ int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
92105
const bool use_decorations = !_decorators.is_empty();
93106

94107
int written = 0;
95-
os::flockfile(_stream);
108+
FileLocker flocker(_stream);
96109
for (; !msg_iterator.is_at_end(); msg_iterator++) {
97110
if (use_decorations) {
98111
written += write_decorations(msg_iterator.decorations());
@@ -101,7 +114,6 @@ int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
101114
written += jio_fprintf(_stream, "%s\n", msg_iterator.message());
102115
}
103116
fflush(_stream);
104-
os::funlockfile(_stream);
105117

106118
return written;
107119
}

0 commit comments

Comments
 (0)