From 36d0006a85823a31e6fe199388723be542c1ec55 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 9 Apr 2024 21:26:59 +0800 Subject: [PATCH] 1 --- async_task.go | 75 +++------------------------------------------- lock_async_task.go | 45 ++++++---------------------- 2 files changed, 13 insertions(+), 107 deletions(-) diff --git a/async_task.go b/async_task.go index 583b152..9133454 100644 --- a/async_task.go +++ b/async_task.go @@ -5,14 +5,11 @@ import ( type AsyncTask struct { status int32 - debugInfo string cb func(*AsyncTask) succCb func(*AsyncTask) failCb func(*AsyncTask) exitCb func(*AsyncTask) - preExecTimes int64 execTimes int64 - lockKeys map[string]*taskLock } func (this *AsyncTask) init(cb func(*AsyncTask)) *AsyncTask { @@ -20,28 +17,15 @@ func (this *AsyncTask) init(cb func(*AsyncTask)) *AsyncTask { return this } -func (this *AsyncTask) GetExecTimes() int64 { - return this.execTimes -} - -func (this *AsyncTask) IsRunning() bool { +func (this *AsyncTask) isRunning() bool { return this.status == 0 } -func (this *AsyncTask) IsSucc() bool { - return this.status > 0 -} - -func (this *AsyncTask) IsFail() bool { - return this.status < 0 -} - func (this *AsyncTask) SetSucc() { - if !this.IsRunning() { + if !this.isRunning() { panic("task is not runing") } this.status = 1 - this.ClearLocks() if this.succCb != nil { this.succCb(this) } @@ -51,11 +35,10 @@ func (this *AsyncTask) SetSucc() { } func (this *AsyncTask) SetFail() { - if !this.IsRunning() { + if !this.isRunning() { panic("task is not runing") } this.status = -1 - this.ClearLocks() if this.failCb != nil { this.failCb(this) } @@ -65,12 +48,9 @@ func (this *AsyncTask) SetFail() { } func (this *AsyncTask) Continue() *AsyncTask { - if !this.IsRunning() { + if !this.isRunning() { panic("task is not runing") } - if len(this.lockKeys) > 0 { - panic("lockTask not support continue") - } GetApp().RegisterMainThreadCb( func () { this.cb(this) @@ -79,29 +59,6 @@ func (this *AsyncTask) Continue() *AsyncTask { return this } -func (this *AsyncTask) checkDo() *AsyncTask { - if !this.IsRunning() { - panic("task is not runing") - } - if this.allIsReady() { - if this.preExecTimes <= 0 { - this.preExecTimes++ - if this.preExecTimes > 1 { - panic("locktask preExectimes > 0") - } - GetApp().RegisterMainThreadCb( - func () { - this.cb(this) - if this.execTimes > 0 { - panic("locktask only run once") - } - this.execTimes += 1 - }) - } - } - return this -} - func (this *AsyncTask) OnSucc(cb func(*AsyncTask)) *AsyncTask { this.succCb = cb return this @@ -117,31 +74,7 @@ func (this *AsyncTask) OnExit(cb func(*AsyncTask)) *AsyncTask { return this } -func (this *AsyncTask) allIsReady() bool { - var allReady = true - for _, lock := range(this.lockKeys) { - if !_app.isFirstAsyncTask(lock.key, &lock.entry) { - allReady = false - break - } - } - return allReady -} - -func (this *AsyncTask) ClearLocks() { - for _, lock := range(this.lockKeys) { - lock.entry.DelInit() - } - for _, lock := range(this.lockKeys) { - _app.clearEmptyPendingAsyncTask(lock.key) - } - for _, lock := range(this.lockKeys) { - _app.notifyNextTask(lock.key) - } -} - func NewAsyncTask(cb func(*AsyncTask)) *AsyncTask { p := new(AsyncTask) - p.lockKeys = make(map[string]*taskLock) return p.init(cb).Continue() } diff --git a/lock_async_task.go b/lock_async_task.go index 059a3fa..ebde976 100644 --- a/lock_async_task.go +++ b/lock_async_task.go @@ -16,8 +16,8 @@ type LockAsyncTask struct { succCb func(*LockAsyncTask) failCb func(*LockAsyncTask) exitCb func(*LockAsyncTask) - preExecTimes int64 execTimes int64 + preExecTimes int64 lockKeys map[string]*taskLock } @@ -26,28 +26,16 @@ func (this *LockAsyncTask) init(cb func(*LockAsyncTask)) *LockAsyncTask { return this } -func (this *LockAsyncTask) GetExecTimes() int64 { - return this.execTimes -} - -func (this *LockAsyncTask) IsRunning() bool { +func (this *LockAsyncTask) isRunning() bool { return this.status == 0 } -func (this *LockAsyncTask) IsSucc() bool { - return this.status > 0 -} - -func (this *LockAsyncTask) IsFail() bool { - return this.status < 0 -} - func (this *LockAsyncTask) SetSucc() { - if !this.IsRunning() { + if !this.isRunning() { panic("task is not runing") } this.status = 1 - this.ClearLocks() + this.clearLocks() if this.succCb != nil { this.succCb(this) } @@ -57,11 +45,11 @@ func (this *LockAsyncTask) SetSucc() { } func (this *LockAsyncTask) SetFail() { - if !this.IsRunning() { + if !this.isRunning() { panic("task is not runing") } this.status = -1 - this.ClearLocks() + this.clearLocks() if this.failCb != nil { this.failCb(this) } @@ -70,23 +58,8 @@ func (this *LockAsyncTask) SetFail() { } } -func (this *LockAsyncTask) Continue() *LockAsyncTask { - if !this.IsRunning() { - panic("task is not runing") - } - if len(this.lockKeys) > 0 { - panic("lockTask not support continue") - } - GetApp().RegisterMainThreadCb( - func () { - this.cb(this) - this.execTimes += 1 - }) - return this -} - func (this *LockAsyncTask) checkDo() *LockAsyncTask { - if !this.IsRunning() { + if !this.isRunning() { panic("task is not runing") } if this.allIsReady() { @@ -134,7 +107,7 @@ func (this *LockAsyncTask) allIsReady() bool { return allReady } -func (this *LockAsyncTask) ClearLocks() { +func (this *LockAsyncTask) clearLocks() { for _, lock := range(this.lockKeys) { lock.entry.DelInit() } @@ -168,7 +141,7 @@ func NewLockAsyncTask(keys [][]string, cb func(*LockAsyncTask)) *LockAsyncTask { 1000 * 2, func (e int32, args* q5.Args) { if e == q5.TIMER_EXEC_EVENT { - if p.IsRunning() { + if p.isRunning() { _sysLog.Warning("LockAsyncaskTimeOut %s", p.debugInfo) panic("LockAsyncaskTimeOut") }