Skip to content

Commit 05f70bf

Browse files
committed
Fix thread safety
Signed-off-by: Mark Chesney <[email protected]>
1 parent ae464f0 commit 05f70bf

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

logback-core/src/main/java/ch/qos/logback/core/BasicStatusManager.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Logback: the reliable, generic, fast and flexible logging framework.
3-
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
3+
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
44
*
55
* This program and the accompanying materials are dual-licensed under
66
* either the terms of the Eclipse Public License v1.0 as published by
@@ -15,6 +15,8 @@
1515

1616
import java.util.ArrayList;
1717
import java.util.List;
18+
import java.util.concurrent.atomic.AtomicInteger;
19+
import java.util.concurrent.atomic.LongAdder;
1820

1921
import ch.qos.logback.core.helpers.CyclicBuffer;
2022
import ch.qos.logback.core.spi.LogbackLock;
@@ -28,14 +30,14 @@ public class BasicStatusManager implements StatusManager {
2830
public static final int MAX_HEADER_COUNT = 150;
2931
public static final int TAIL_SIZE = 150;
3032

31-
int count = 0;
33+
final LongAdder count = new LongAdder();
3234

3335
// protected access was requested in http://jira.qos.ch/browse/LBCORE-36
3436
final protected List<Status> statusList = new ArrayList<Status>();
3537
final protected CyclicBuffer<Status> tailBuffer = new CyclicBuffer<Status>(TAIL_SIZE);
3638
final protected LogbackLock statusListLock = new LogbackLock();
3739

38-
int level = Status.INFO;
40+
final AtomicInteger level = new AtomicInteger(Status.INFO);
3941

4042
// protected access was requested in http://jira.qos.ch/browse/LBCORE-36
4143
final protected List<StatusListener> statusListenerList = new ArrayList<StatusListener>();
@@ -57,10 +59,15 @@ public void add(Status newStatus) {
5759
// LBCORE-72: fire event before the count check
5860
fireStatusAddEvent(newStatus);
5961

60-
count++;
61-
if (newStatus.getLevel() > level) {
62-
level = newStatus.getLevel();
63-
}
62+
count.increment();
63+
int newLevel = newStatus.getLevel();
64+
int currentLevel;
65+
do {
66+
currentLevel = level.get();
67+
if (newLevel <= currentLevel) {
68+
break;
69+
}
70+
} while (!level.compareAndSet(currentLevel, newLevel));
6471

6572
synchronized (statusListLock) {
6673
if (statusList.size() < MAX_HEADER_COUNT) {
@@ -90,18 +97,18 @@ private void fireStatusAddEvent(Status status) {
9097

9198
public void clear() {
9299
synchronized (statusListLock) {
93-
count = 0;
100+
count.reset();
94101
statusList.clear();
95102
tailBuffer.clear();
96103
}
97104
}
98105

99106
public int getLevel() {
100-
return level;
107+
return level.get();
101108
}
102109

103110
public int getCount() {
104-
return count;
111+
return count.intValue();
105112
}
106113

107114
/**

0 commit comments

Comments
 (0)