浏览代码

eliminated the data race between the callback and script goroutines

postman0 9 年之前
父节点
当前提交
c7ef3658bd
共有 1 个文件被更改,包括 6 次插入1 次删除
  1. 6 1
      muc-client/luaexecutor/executor.go

+ 6 - 1
muc-client/luaexecutor/executor.go

@@ -3,7 +3,7 @@ package luaexecutor
 import (
 import (
 	"fmt"
 	"fmt"
 	"github.com/kpmy/go-lua"
 	"github.com/kpmy/go-lua"
-	"log"
+	"sync"
 	"time"
 	"time"
 	"xep/c2s/stream"
 	"xep/c2s/stream"
 	"xep/entity"
 	"xep/entity"
@@ -23,6 +23,7 @@ type Executor struct {
 	incomingScripts chan string
 	incomingScripts chan string
 	outgoingMsgs    chan string
 	outgoingMsgs    chan string
 	incomingMsgs    chan IncomingMessage
 	incomingMsgs    chan IncomingMessage
+	stateMutex      sync.Mutex
 	state           *lua.State
 	state           *lua.State
 	xmppStream      stream.Stream
 	xmppStream      stream.Stream
 }
 }
@@ -62,6 +63,7 @@ func NewExecutor(s stream.Stream) *Executor {
 
 
 func (e *Executor) execute() {
 func (e *Executor) execute() {
 	for script := range e.incomingScripts {
 	for script := range e.incomingScripts {
+		e.stateMutex.Lock()
 		err := lua.DoString(e.state, script)
 		err := lua.DoString(e.state, script)
 		if err != nil {
 		if err != nil {
 			fmt.Printf("lua fucking shit error: %s\n", err)
 			fmt.Printf("lua fucking shit error: %s\n", err)
@@ -70,6 +72,7 @@ func (e *Executor) execute() {
 			m.Body = err.Error()
 			m.Body = err.Error()
 			e.xmppStream.Write(entity.ProduceStatic(m))
 			e.xmppStream.Write(entity.ProduceStatic(m))
 		}
 		}
+		e.stateMutex.Unlock()
 	}
 	}
 }
 }
 
 
@@ -88,6 +91,7 @@ func (e *Executor) sendingRoutine() {
 
 
 func (e *Executor) processIncomingMsgs() {
 func (e *Executor) processIncomingMsgs() {
 	for msg := range e.incomingMsgs {
 	for msg := range e.incomingMsgs {
+		e.stateMutex.Lock()
 		e.state.PushString(callbackLocation)
 		e.state.PushString(callbackLocation)
 		e.state.Table(lua.RegistryIndex)
 		e.state.Table(lua.RegistryIndex)
 		if e.state.IsFunction(-1) {
 		if e.state.IsFunction(-1) {
@@ -103,6 +107,7 @@ func (e *Executor) processIncomingMsgs() {
 		} else {
 		} else {
 			e.state.Pop(1)
 			e.state.Pop(1)
 		}
 		}
+		e.stateMutex.Unlock()
 	}
 	}
 }
 }