Skip to content

Commit 956b25f

Browse files
committed
JSON output: prevent writer starvation on rwlock used for time window file rotation.
1 parent c9d1473 commit 956b25f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/plugins/output/json/src/File.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,28 @@ File::File(const struct cfg_file &cfg, ipx_ctx_t *ctx) : Output(cfg.name, ctx)
9797

9898
_thread->file = new_file;
9999

100-
if (pthread_rwlock_init(&_thread->rwlock, NULL) != 0) {
100+
pthread_rwlockattr_t attr;
101+
if (pthread_rwlockattr_init(&attr) != 0) {
101102
fclose(_thread->file);
102103
delete _thread;
103-
throw std::runtime_error("(File output) Mutex initialization failed!");
104+
throw std::runtime_error("(File output) Rwlockattr initialization failed!");
104105
}
105106

107+
if (pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) != 0) {
108+
fclose(_thread->file);
109+
pthread_rwlockattr_destroy(&attr);
110+
delete _thread;
111+
throw std::runtime_error("(File output) Rwlockattr setkind failed!");
112+
}
113+
114+
if (pthread_rwlock_init(&_thread->rwlock, &attr) != 0) {
115+
fclose(_thread->file);
116+
pthread_rwlockattr_destroy(&attr);
117+
delete _thread;
118+
throw std::runtime_error("(File output) Rwlock initialization failed!");
119+
}
120+
121+
pthread_rwlockattr_destroy(&attr);
106122
if (pthread_create(&_thread->thread, NULL, &File::thread_window, _thread) != 0) {
107123
fclose(_thread->file);
108124
pthread_rwlock_destroy(&_thread->rwlock);

0 commit comments

Comments
 (0)