mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-18 15:40:12 +01:00
🎨 Improve kernel stability by eliminating some data races https://github.com/siyuan-note/siyuan/issues/9842
This commit is contained in:
parent
071f3f9af8
commit
535f72afdb
2 changed files with 23 additions and 2 deletions
|
|
@ -94,5 +94,8 @@ func pushTransactions(app, session string, transactions []*model.Transaction) {
|
||||||
evt.AppId = app
|
evt.AppId = app
|
||||||
evt.SessionId = session
|
evt.SessionId = session
|
||||||
evt.Data = transactions
|
evt.Data = transactions
|
||||||
|
for _, tx := range transactions {
|
||||||
|
tx.WaitForCommit()
|
||||||
|
}
|
||||||
util.PushEvent(evt)
|
util.PushEvent(evt)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
|
|
@ -128,6 +129,7 @@ func flushTx(tx *Transaction) {
|
||||||
|
|
||||||
func PerformTransactions(transactions *[]*Transaction) {
|
func PerformTransactions(transactions *[]*Transaction) {
|
||||||
for _, tx := range *transactions {
|
for _, tx := range *transactions {
|
||||||
|
tx.m = &sync.Mutex{}
|
||||||
txQueue <- tx
|
txQueue <- tx
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -1168,8 +1170,6 @@ type Operation struct {
|
||||||
Format string `json:"format"` // 属性视图列格式化
|
Format string `json:"format"` // 属性视图列格式化
|
||||||
KeyID string `json:"keyID"` // 属性视列 ID
|
KeyID string `json:"keyID"` // 属性视列 ID
|
||||||
RowID string `json:"rowID"` // 属性视图行 ID
|
RowID string `json:"rowID"` // 属性视图行 ID
|
||||||
|
|
||||||
discard bool // 用于标识是否在事务合并中丢弃
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
|
|
@ -1181,6 +1181,18 @@ type Transaction struct {
|
||||||
nodes map[string]*ast.Node
|
nodes map[string]*ast.Node
|
||||||
|
|
||||||
luteEngine *lute.Lute
|
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) {
|
func (tx *Transaction) begin() (err error) {
|
||||||
|
|
@ -1190,6 +1202,8 @@ func (tx *Transaction) begin() (err error) {
|
||||||
tx.trees = map[string]*parse.Tree{}
|
tx.trees = map[string]*parse.Tree{}
|
||||||
tx.nodes = map[string]*ast.Node{}
|
tx.nodes = map[string]*ast.Node{}
|
||||||
tx.luteEngine = util.NewLute()
|
tx.luteEngine = util.NewLute()
|
||||||
|
tx.m.Lock()
|
||||||
|
tx.state.Store(0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1202,11 +1216,15 @@ func (tx *Transaction) commit() (err error) {
|
||||||
refreshDynamicRefTexts(tx.nodes, tx.trees)
|
refreshDynamicRefTexts(tx.nodes, tx.trees)
|
||||||
IncSync()
|
IncSync()
|
||||||
tx.trees = nil
|
tx.trees = nil
|
||||||
|
tx.state.Store(1)
|
||||||
|
tx.m.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Transaction) rollback() {
|
func (tx *Transaction) rollback() {
|
||||||
tx.trees, tx.nodes = nil, nil
|
tx.trees, tx.nodes = nil, nil
|
||||||
|
tx.state.Store(2)
|
||||||
|
tx.m.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue