Skip to content
This repository has been archived by the owner on May 29, 2022. It is now read-only.

Commit

Permalink
async_loop, async_w, async_wloop, future, promise, clearing pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
slowcheet4h committed Mar 9, 2021
1 parent 9cc23f2 commit efeb789
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 150 deletions.
77 changes: 71 additions & 6 deletions pisi/unitedmeows/meowlib/async/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,94 @@ public static void _return(CoID uuid, Object result) {
public static <X> Future<X> async_f(IAsyncAction action) {
// change this uuid alternative
final CoID pointer = newPointer();

Future<X> future = new Future<>(pointer);
Task<?> task = new Task<Object>(action) {
@Override
public void run() {
action.start(pointer);
future.post();
}
};

pointers.put(pointer, task);
MeowLib.getTaskPool().queue_f(task);
return new Future<>(pointer);
return future;
}

public static <X> Future<X> async(IAsyncAction action) {
// change this uuid alternative
final CoID pointer = newPointer();


Future<X> future = new Future<>(pointer);
Task<?> task = new Task<Object>(action) {
@Override
public void run() {
action.start(pointer);
future.post();
}
};

pointers.put(pointer, task);
MeowLib.getTaskPool().queue(task);
return future;
}


public static <X> Future<X> async_w(IAsyncAction action, final long after) {
// change this uuid alternative
final CoID pointer = newPointer();

Future<X> future = new Future<>(pointer);
Task<?> task = new Task<Object>(action) {
@Override
public void run() {
action.start(pointer);
future.post();
}
};

pointers.put(pointer, task);
MeowLib.getTaskPool().queue_w(task, after);
return future;
}

/* async_loop but waits before first call */
public static Promise async_wloop(IAsyncAction action, final long repeatDelay) {
final Promise promise = new Promise();
Task<?> task = new Task<Object>(action) {
@Override
public void run() {
if (promise.isValid()) {
action.start(null);
MeowLib.getTaskPool().queue_w(this, repeatDelay);
}
}
};

MeowLib.getTaskPool().queue_w(task, repeatDelay);
return promise;
}

public static Promise async_loop(IAsyncAction action, final long repeatDelay) {
final Promise promise = new Promise();
Task<?> task = new Task<Object>(action) {
@Override
public void run() {
if (promise.isValid()) {
action.start(null);
MeowLib.getTaskPool().queue_w(this, repeatDelay);
}
}
};

MeowLib.getTaskPool().queue(task);
return new Future<>(pointer);
return promise;
}





public static <X> X await(Future<?> future) {
return (X) await(pointers.get(future.pointer())).result();
}
Expand All @@ -84,15 +140,16 @@ public void run() {
action.start(pointer);
}
};

Future<X> future = new Future<>(pointer);
pointers.put(pointer, task);
new Thread(()-> {
task.pre();
task.run();
task.post();
future.post();
}).start();

return new Future<X>(pointer);
return future;
}

private static CoID newPointer() {
Expand All @@ -103,5 +160,13 @@ private static CoID newPointer() {
return pointer;
}

public static void removePointer(CoID coid) {
pointers.remove(coid);
}

public static int pointerCount() {
return pointers.size();
}


}
28 changes: 23 additions & 5 deletions pisi/unitedmeows/meowlib/async/BasicTaskPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import pisi.unitedmeows.meowlib.etc.MLibSettings;
import pisi.unitedmeows.meowlib.thread.kThread;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.*;
import java.util.concurrent.*;

public class BasicTaskPool implements ITaskPool {


private HashMap<Task<?>, Long> waitQueue;
private List<TaskWorker> taskWorkers;
private LinkedBlockingDeque<Task<?>> taskQueue;
private Thread workerCThread;
Expand All @@ -21,10 +20,11 @@ public class BasicTaskPool implements ITaskPool {
public void setup() {
taskWorkers = new ArrayList<>();
taskQueue = new LinkedBlockingDeque<Task<?>>();
waitQueue = new HashMap<>();
workerCThread = new Thread(this::workerC);
workerCThread.start();

for (int i = 0; i < 3; i++) {
for (int i = 0; i < 5; i++) {
TaskWorker taskWorker = new TaskWorker(this);
taskWorkers.add(taskWorker);
taskWorker.startWorker();
Expand Down Expand Up @@ -59,6 +59,19 @@ public void workerC() {
}
}

final long currentTime = System.currentTimeMillis();
List<Task<?>> addQueue = new ArrayList<>();
for (Map.Entry<Task<?>, Long> waitQEntry : waitQueue.entrySet()) {
if (currentTime >= waitQEntry.getValue()) {
addQueue.add(waitQEntry.getKey());
queue(waitQEntry.getKey());
}
}

for (Task<?> task : addQueue) {
waitQueue.remove(task);
}

kThread.sleep((long) MeowLib.settings().get(MLibSettings.ASYNC_CHECK_BUSY).getValue());
}
}
Expand All @@ -73,6 +86,11 @@ public void queue_f(Task<?> task) {
taskQueue.addFirst(task);
}

@Override
public void queue_w(Task<?> task, long after) {
waitQueue.put(task, System.currentTimeMillis() + after);
}

@Override
public Task<?> poll() {
if (!taskQueue.isEmpty()) {
Expand Down
23 changes: 23 additions & 0 deletions pisi/unitedmeows/meowlib/async/Future.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import pisi.unitedmeows.meowlib.etc.CoID;

import java.util.ArrayList;
import java.util.List;

public class Future<X> {

private X value;
private CoID pointer;
private Task task;
private transient List<IAsyncAction> afterTasks;

public Future(CoID pointer) {
this.pointer = pointer;
Expand All @@ -20,6 +24,25 @@ public Task<X> task() {
return task;
}

public void post() {
if (afterTasks != null) {
for (IAsyncAction afterTask : afterTasks) {
Async.async(afterTask);
}
afterTasks.clear();
}
task = Async.task(pointer);
Async.removePointer(pointer);
}

public void after(IAsyncAction task) {
if (afterTasks == null) {
afterTasks = new ArrayList<>();
}
afterTasks.add(task);
}


public <x> x await() {
return (x) Async.await(this);
}
Expand Down
25 changes: 13 additions & 12 deletions pisi/unitedmeows/meowlib/async/ITaskPool.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package pisi.unitedmeows.meowlib.async;

public interface ITaskPool {

void queue(Task<?> task);
/* queue first */
void queue_f(Task<?> task);
Task<?> poll();
int workerCount();
void close();
void setup();
}
package pisi.unitedmeows.meowlib.async;

public interface ITaskPool {

void queue(Task<?> task);
/* queue first */
void queue_f(Task<?> task);
void queue_w(Task<?> task, long after);
Task<?> poll();
int workerCount();
void close();
void setup();
}
17 changes: 17 additions & 0 deletions pisi/unitedmeows/meowlib/async/Promise.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pisi.unitedmeows.meowlib.async;

public class Promise {
private boolean valid = true;

public void stop() {
valid = false;
}

public void start() {
valid = true;
}

public boolean isValid() {
return valid;
}
}
Loading

0 comments on commit efeb789

Please sign in to comment.