This commit is contained in:
aozhiwei 2019-12-13 20:21:05 +08:00
parent fa3d6d840b
commit 1fe3f046e7
2 changed files with 96 additions and 11 deletions

View File

@ -4,6 +4,12 @@ public class ListHead {
public ListHead next;
public ListHead prev;
public Object data;
public ListHead() {
next = this;
prev = this;
}
public void del() {
@ -17,7 +23,29 @@ public class ListHead {
}
public boolean empty() {
return false;
public Object firstEntry() {
return next.data;
}
public void replace(ListHead pnew) {
pnew.next = next;
pnew.next.prev = pnew;
pnew.prev = prev;
pnew.prev.next = pnew;
pnew.data = data;
}
public void replaceInit(ListHead pnew) {
replace(pnew);
init();
}
public boolean empty() {
return next == this;
}
public void init() {
}
}

View File

@ -20,25 +20,38 @@ public class Timer {
public long GetTickCount(Object context);
}
private enum TimerType
{
DEADLINE,
REPEAT,
FIXED
}
public class TimerList
{
private ListHead entry = new ListHead();
private ListHead attachEntry;
private int timerType = 0;
private TimerType timerType = TimerType.DEADLINE;
private int milli_seconds = 0;
private long expires = 0;
private int fixedTimerExecuteTimes = 0;
private ITimerFunc timerFunc;
private ITimerAfterFunc timerAfterFunc;
private XParams param;
private void initTimerList(Timer timer, int timer_type, int milli_seconds,
private void initTimerList(Timer timer, TimerType timer_type, int milli_seconds,
XParams param, ITimerFunc timerFunc, ITimerAfterFunc afterFunc)
{
}
}
private static final int TVN_SIZE = 1 << 6;
private static final int TVR_SIZE = 1 << 8;
private static final int TVN_MASK = TVN_SIZE - 1;
private static final int TVR_MASK = TVR_SIZE - 1;
private int freeTimerNum = 0;
private ListHead freeTimer = new ListHead();
private TimerList runningTimer;
@ -47,6 +60,12 @@ public class Timer {
private Object content;
private int gcTime = 0;
private int cacheTimerNum = 0;
private ListHead[] tv1 = new ListHead[TVR_SIZE];
private ListHead[] tv2 = new ListHead[TVN_SIZE];
private ListHead[] tv3 = new ListHead[TVN_SIZE];
private ListHead[] tv4 = new ListHead[TVN_SIZE];
private ListHead[] tv5 = new ListHead[TVN_SIZE];
private ListHead workList = new ListHead();
public void init(IGetTickCountFunc func, Object content, int gc_time, int cache_timer_num) {
this.getTickCount = func;
@ -60,7 +79,45 @@ public class Timer {
}
public void update() {
while (getTickCount.GetTickCount(content) >= timerTick) {
int index = (int)(timerTick & TVR_MASK);
++timerTick;
tv1[index].replaceInit(workList);
while (!workList.empty()) {
TimerList timer = (TimerList)workList.firstEntry();
runningTimer = timer;
if (timer.timerFunc != null) {
timer.timerFunc.OnTimer(timer.param);
}
if (timer.timerAfterFunc != null) {
timer.timerAfterFunc.OnTimerAfter(timer.param);
}
if (runningTimer != null) {
switch (runningTimer.timerType) {
case REPEAT:
case FIXED:
{
if (timer.timerType == TimerType.FIXED) {
++timer.fixedTimerExecuteTimes;
}
modifyTimer(timer, timer.milli_seconds);
}
break;
case DEADLINE:
{
detachTimer(timer);
if (timer.attachEntry != null && !timer.attachEntry.empty()) {
timer.attachEntry.delInit();
}
addToFreeList(timer);
}
break;
}
}
}
}
runningTimer = null;
}
public TimerList addDeadLineTimer(int expire_time, XParams param, ITimerFunc timer_func) {
@ -70,7 +127,7 @@ public class Timer {
public TimerList addDeadLineTimer(int expire_time, XParams param, ITimerFunc timer_func,
ITimerAfterFunc after_func) {
TimerList timer = newTimerList();
timer.initTimerList(this, 0, expire_time, param, timer_func, after_func);
timer.initTimerList(this, TimerType.DEADLINE, expire_time, param, timer_func, after_func);
return timer;
}
@ -88,14 +145,14 @@ public class Timer {
public TimerList addRepeatTimer(int expire_time, XParams param, ITimerFunc timer_func) {
TimerList timer = newTimerList();
timer.initTimerList(this, 1, expire_time, param, timer_func, null);
timer.initTimerList(this, TimerType.REPEAT, expire_time, param, timer_func, null);
modifyTimer(timer, expire_time);
return timer;
}
public TimerList addFixedTimer(int expire_time, XParams param, ITimerFunc timer_func) {
TimerList timer = newTimerList();
timer.initTimerList(this, 2, expire_time, param, timer_func, null);
timer.initTimerList(this, TimerType.FIXED, expire_time, param, timer_func, null);
modifyTimer(timer, expire_time);
return timer;
}
@ -105,11 +162,11 @@ public class Timer {
}
public TimerList getTimerByAttach(ListHead attach_entry) {
return null;
return (TimerList)attach_entry.data;
}
public XParams getMutableParams(TimerList timer_list) {
return null;
return timer_list.param;
}
public long getRemainTime(TimerList timer_list) {
@ -117,7 +174,7 @@ public class Timer {
}
public TimerList getRunningTimer() {
return null;
return runningTimer;
}
public int getIdleableMillSeconds() {
@ -142,7 +199,7 @@ public class Timer {
}
private TimerList newTimerList() {
return null;
return new TimerList();
}
private void clear() {