Skip to content

Commit

Permalink
Fix thread safety
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Chesney <mches@users.noreply.github.com>
  • Loading branch information
mches committed Sep 9, 2024
1 parent 0a03ab7 commit 99a3726
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
Expand All @@ -15,6 +15,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

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

int count = 0;
final AtomicInteger count = new AtomicInteger(0);

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

int level = Status.INFO;
final AtomicInteger level = new AtomicInteger(Status.INFO);

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

count++;
if (newStatus.getLevel() > level) {
level = newStatus.getLevel();
}
count.getAndIncrement();
int currentLevel;
int newLevel = newStatus.getLevel();
do {
currentLevel = level.get();
if (newLevel <= currentLevel) {
break;
}
} while (!level.compareAndSet(currentLevel, newLevel));

synchronized (statusListLock) {
if (statusList.size() < MAX_HEADER_COUNT) {
Expand Down Expand Up @@ -90,18 +96,18 @@ private void fireStatusAddEvent(Status status) {

public void clear() {
synchronized (statusListLock) {
count = 0;
count.set(0);
statusList.clear();
tailBuffer.clear();
}
}

public int getLevel() {
return level;
return level.get();
}

public int getCount() {
return count;
return count.get();
}

/**
Expand Down

0 comments on commit 99a3726

Please sign in to comment.