1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -72,18 +72,31 @@ int LogFileStreamOutput::write_decorations(const LogDecorations& decorations) {
72
72
return total_written;
73
73
}
74
74
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
+
75
89
int LogFileStreamOutput::write (const LogDecorations& decorations, const char * msg) {
76
90
const bool use_decorations = !_decorators.is_empty ();
77
91
78
92
int written = 0 ;
79
- os::flockfile (_stream);
93
+ FileLocker flocker (_stream);
80
94
if (use_decorations) {
81
95
written += write_decorations (decorations);
82
96
written += jio_fprintf (_stream, " " );
83
97
}
84
98
written += jio_fprintf (_stream, " %s\n " , msg);
85
99
fflush (_stream);
86
- os::funlockfile (_stream);
87
100
88
101
return written;
89
102
}
@@ -92,7 +105,7 @@ int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
92
105
const bool use_decorations = !_decorators.is_empty ();
93
106
94
107
int written = 0 ;
95
- os::flockfile (_stream);
108
+ FileLocker flocker (_stream);
96
109
for (; !msg_iterator.is_at_end (); msg_iterator++) {
97
110
if (use_decorations) {
98
111
written += write_decorations (msg_iterator.decorations ());
@@ -101,7 +114,6 @@ int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
101
114
written += jio_fprintf (_stream, " %s\n " , msg_iterator.message ());
102
115
}
103
116
fflush (_stream);
104
- os::funlockfile (_stream);
105
117
106
118
return written;
107
119
}
0 commit comments