1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-08-08 06:53:46 +00:00

removed unnecessary atomic semantics inside the lock

This commit is contained in:
zelva 2022-09-02 22:00:40 +03:00 committed by TobiGr
parent 16a83dc448
commit b41f52e5d6

View File

@ -40,8 +40,8 @@ import io.reactivex.rxjava3.subjects.BehaviorSubject;
*/ */
public abstract class PlayQueue implements Serializable { public abstract class PlayQueue implements Serializable {
public static final boolean DEBUG = MainActivity.DEBUG; public static final boolean DEBUG = MainActivity.DEBUG;
@NonNull
private final AtomicInteger queueIndex; private volatile int queueIndex;
private final List<PlayQueueItem> history; private final List<PlayQueueItem> history;
// volatile is needed for the isShuffled method // volatile is needed for the isShuffled method
@ -64,7 +64,7 @@ public abstract class PlayQueue implements Serializable {
streams = new ArrayList<>(startWith); streams = new ArrayList<>(startWith);
queueIndex = new AtomicInteger(index); queueIndex = index;
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
@ -123,7 +123,7 @@ public abstract class PlayQueue implements Serializable {
* @return the current index that should be played * @return the current index that should be played
*/ */
public int getIndex() { public int getIndex() {
return queueIndex.get(); return queueIndex;
} }
/** /**
@ -159,7 +159,7 @@ public abstract class PlayQueue implements Serializable {
newIndex = streams.size() - 1; newIndex = streams.size() - 1;
} }
queueIndex.set(newIndex); queueIndex = newIndex;
if (oldIndex != newIndex) { if (oldIndex != newIndex) {
history.add(streams.get(newIndex)); history.add(streams.get(newIndex));
@ -329,8 +329,8 @@ public abstract class PlayQueue implements Serializable {
* </p> * </p>
*/ */
public synchronized void error() { public synchronized void error() {
final int oldIndex = queueIndex.getAndIncrement(); final int oldIndex = queueIndex;
final int nextIndex = oldIndex + 1; final int nextIndex = queueIndex = oldIndex + 1;
if (streams.size() > nextIndex) { if (streams.size() > nextIndex) {
history.add(streams.get(nextIndex)); history.add(streams.get(nextIndex));
} }
@ -338,20 +338,18 @@ public abstract class PlayQueue implements Serializable {
} }
private synchronized void removeInternal(final int removeIndex) { private synchronized void removeInternal(final int removeIndex) {
final int currentIndex = queueIndex.get(); final int currentIndex = queueIndex;
final int size = size(); final int size = size();
int nextIndex = currentIndex; int nextIndex = currentIndex;
if (currentIndex > removeIndex) { if (currentIndex > removeIndex) {
nextIndex = currentIndex - 1; queueIndex = nextIndex = currentIndex - 1;
queueIndex.set(nextIndex);
} else if (currentIndex >= size) { } else if (currentIndex >= size) {
nextIndex = currentIndex % (size - 1); queueIndex = nextIndex = currentIndex % (size - 1);
queueIndex.set(nextIndex);
} else if (currentIndex == removeIndex && currentIndex == size - 1) { } else if (currentIndex == removeIndex && currentIndex == size - 1) {
queueIndex.set(nextIndex = 0); queueIndex = nextIndex = 0;
} }
if (backup != null) { if (backup != null) {
@ -385,13 +383,13 @@ public abstract class PlayQueue implements Serializable {
return; return;
} }
final int current = getIndex(); final int current = queueIndex;
if (source == current) { if (source == current) {
queueIndex.set(target); queueIndex = target;
} else if (source < current && target >= current) { } else if (source < current && target >= current) {
queueIndex.decrementAndGet(); queueIndex = current - 1;
} else if (source > current && target <= current) { } else if (source > current && target <= current) {
queueIndex.incrementAndGet(); queueIndex = current + 1;
} }
final PlayQueueItem playQueueItem = streams.remove(source); final PlayQueueItem playQueueItem = streams.remove(source);
@ -465,7 +463,7 @@ public abstract class PlayQueue implements Serializable {
// Move currentItem to the head of the queue // Move currentItem to the head of the queue
streams.remove(currentItem); streams.remove(currentItem);
streams.add(0, currentItem); streams.add(0, currentItem);
queueIndex.set(0); queueIndex = 0;
history.add(currentItem); history.add(currentItem);
@ -495,7 +493,7 @@ public abstract class PlayQueue implements Serializable {
final int newIndex = streams.indexOf(current); final int newIndex = streams.indexOf(current);
final int nextIndex = newIndex != -1 ? newIndex : 0; final int nextIndex = newIndex != -1 ? newIndex : 0;
queueIndex.set(nextIndex); queueIndex = nextIndex;
if (streams.size() > nextIndex) { if (streams.size() > nextIndex) {
history.add(streams.get(nextIndex)); history.add(streams.get(nextIndex));