Skip to content

Commit 0d3cea9

Browse files
committed
semfor
1 parent dcde526 commit 0d3cea9

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

src/main/java/me/cortex/voxy/common/thread/MultiThreadPrioritySemaphore.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,26 @@ public void release(int permits) {
2626
this.blockSemaphore.release(permits);
2727
}
2828

29+
/*
2930
public void acquire() {
3031
this.acquire(true);
3132
}
3233
public void acquire(boolean runJob) {//Block until a permit for this block is availbe, other jobs maybe executed while we wait
33-
/*
34-
while (true) {
35-
this.blockSemaphore.acquireUninterruptibly();//Block on all
36-
if (this.localSemaphore.tryAcquire()) {//We prioritize locals first
37-
return;
38-
}
39-
if (runJob) {
40-
//It wasnt a local job so run
41-
this.man.tryRun(this);
42-
} else {
43-
this.blockSemaphore.release(1);
44-
Thread.onSpinWait();
45-
Thread.yield();
46-
}
47-
}*/
34+
35+
//while (true) {
36+
// this.blockSemaphore.acquireUninterruptibly();//Block on all
37+
// if (this.localSemaphore.tryAcquire()) {//We prioritize locals first
38+
// return;
39+
// }
40+
// if (runJob) {
41+
// //It wasnt a local job so run
42+
// this.man.tryRun(this);
43+
// } else {
44+
// this.blockSemaphore.release(1);
45+
// Thread.onSpinWait();
46+
// Thread.yield();
47+
// }
48+
//}
4849
4950
//Absolutly no idea if this shitty thing functions correctly... at all, it very much probably doesnt
5051
while (true) {
@@ -64,9 +65,31 @@ public void acquire(boolean runJob) {//Block until a permit for this block is av
6465
break;
6566
}
6667
}
68+
}*/
69+
70+
71+
public void acquire() {
72+
this.acquire(true);
73+
}
74+
public void acquire(boolean contributeToPool) {
75+
if (contributeToPool) {
76+
while (true) {
77+
this.blockSemaphore.acquireUninterruptibly();//Block on all
78+
if (this.localSemaphore.tryAcquire()) {//We prioritize locals first
79+
return;
80+
}
81+
if (this.man.tryRun(this)) {//Returns true if it captured a local job
82+
break;
83+
}
84+
}
85+
} else {
86+
this.localSemaphore.acquireUninterruptibly();//We acquire local first
87+
this.blockSemaphore.tryAcquire();//Try acquire a block, if not its... "fine"
88+
}
6789
}
6890

6991

92+
7093
public void free() {
7194
this.man.freeBlock(this);
7295
this.free0();

0 commit comments

Comments
 (0)