Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread safety #851

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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