Skip to content

Commit d641b43

Browse files
committed
Fix thread safety
Signed-off-by: Mark Chesney <[email protected]>
1 parent 0a03ab7 commit d641b43

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

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

Lines changed: 16 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,7 @@
1515

1616
import java.util.ArrayList;
1717
import java.util.List;
18+
import java.util.concurrent.atomic.AtomicInteger;
1819

1920
import ch.qos.logback.core.helpers.CyclicBuffer;
2021
import ch.qos.logback.core.spi.LogbackLock;
@@ -28,14 +29,14 @@ public class BasicStatusManager implements StatusManager {
2829
public static final int MAX_HEADER_COUNT = 150;
2930
public static final int TAIL_SIZE = 150;
3031

31-
int count = 0;
32+
final AtomicInteger count = new AtomicInteger();
3233

3334
// protected access was requested in http://jira.qos.ch/browse/LBCORE-36
3435
final protected List<Status> statusList = new ArrayList<Status>();
3536
final protected CyclicBuffer<Status> tailBuffer = new CyclicBuffer<Status>(TAIL_SIZE);
3637
final protected LogbackLock statusListLock = new LogbackLock();
3738

38-
int level = Status.INFO;
39+
final AtomicInteger level = new AtomicInteger(Status.INFO);
3940

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

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

6571
synchronized (statusListLock) {
6672
if (statusList.size() < MAX_HEADER_COUNT) {
@@ -90,18 +96,18 @@ private void fireStatusAddEvent(Status status) {
9096

9197
public void clear() {
9298
synchronized (statusListLock) {
93-
count = 0;
99+
count.set(0);
94100
statusList.clear();
95101
tailBuffer.clear();
96102
}
97103
}
98104

99105
public int getLevel() {
100-
return level;
106+
return level.get();
101107
}
102108

103109
public int getCount() {
104-
return count;
110+
return count.get();
105111
}
106112

107113
/**

0 commit comments

Comments
 (0)