🎨 Improve kernel stability by eliminating some data races https://github.com/siyuan-note/siyuan/issues/9842

This commit is contained in:
Daniel 2023-12-08 20:28:57 +08:00
parent 071f3f9af8
commit 535f72afdb
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 23 additions and 2 deletions

View file

@ -94,5 +94,8 @@ func pushTransactions(app, session string, transactions []*model.Transaction) {
evt.AppId = app
evt.SessionId = session
evt.Data = transactions
for _, tx := range transactions {
tx.WaitForCommit()
}
util.PushEvent(evt)
}

View file

@ -23,6 +23,7 @@ import (
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/88250/gulu"
@ -128,6 +129,7 @@ func flushTx(tx *Transaction) {
func PerformTransactions(transactions *[]*Transaction) {
for _, tx := range *transactions {
tx.m = &sync.Mutex{}
txQueue <- tx
}
return
@ -1168,8 +1170,6 @@ type Operation struct {
Format string `json:"format"` // 属性视图列格式化
KeyID string `json:"keyID"` // 属性视列 ID
RowID string `json:"rowID"` // 属性视图行 ID
discard bool // 用于标识是否在事务合并中丢弃
}
type Transaction struct {
@ -1181,6 +1181,18 @@ type Transaction struct {
nodes map[string]*ast.Node
luteEngine *lute.Lute
m *sync.Mutex
state atomic.Int32 // 0: 未提交1: 已提交2: 已回滚
}
func (tx *Transaction) WaitForCommit() {
for {
if 0 == tx.state.Load() {
time.Sleep(10 * time.Millisecond)
continue
}
return
}
}
func (tx *Transaction) begin() (err error) {
@ -1190,6 +1202,8 @@ func (tx *Transaction) begin() (err error) {
tx.trees = map[string]*parse.Tree{}
tx.nodes = map[string]*ast.Node{}
tx.luteEngine = util.NewLute()
tx.m.Lock()
tx.state.Store(0)
return
}
@ -1202,11 +1216,15 @@ func (tx *Transaction) commit() (err error) {
refreshDynamicRefTexts(tx.nodes, tx.trees)
IncSync()
tx.trees = nil
tx.state.Store(1)
tx.m.Unlock()
return
}
func (tx *Transaction) rollback() {
tx.trees, tx.nodes = nil, nil
tx.state.Store(2)
tx.m.Unlock()
return
}