Remove lock

This commit is contained in:
giongto35 2019-05-11 01:49:01 +08:00
parent fc0f4d6e75
commit d32b34c7db
5 changed files with 81 additions and 80 deletions

View file

@ -51,9 +51,9 @@ func (console *Console) Step() int {
console.PPU.Step()
console.Mapper.Step()
}
for i := 0; i < cpuCycles; i++ {
console.APU.Step()
}
//for i := 0; i < cpuCycles; i++ {
//console.APU.Step()
//}
return cpuCycles
}

View file

@ -77,6 +77,7 @@ func (s *Session) RouteOverlord() {
log.Println("Received a start request from overlord")
log.Println("Add the connection to current room on the host")
log.Println("SessionID : ", resp.SessionID)
peerconnection := oclient.peerconnections[resp.SessionID]
log.Println("start session")

View file

@ -26,54 +26,52 @@ func (r *Room) startAudio() {
// fanout Audio
for {
select {
case <-r.Done:
sample, ok := <-r.audioChannel
if !ok {
// Just for guarding
log.Println("Warn: Room ", r.ID, " audio channel closed unexpectedly")
return
}
if r.Done {
log.Println("Room ", r.ID, " audio channel closed")
return
case sample, ok := <-r.audioChannel:
if !ok {
// Just for guarding
log.Println("Warn: Room ", r.ID, " audio channel closed unexpectedly")
return
}
// TODO: Use worker pool for encoding
pcm[idx] = sample
idx++
if idx == len(pcm) {
data := make([]byte, 640)
n, err := enc.EncodeFloat32(pcm, data)
if err != nil {
log.Println("[!] Failed to decode")
continue
}
data = data[:n]
data = append(data, count)
// TODO: Use worker pool for encoding
pcm[idx] = sample
idx++
if idx == len(pcm) {
data := make([]byte, 640)
// TODO: r.rtcSessions is rarely updated. Lock will hold down perf
//r.sessionsLock.Lock()
for _, webRTC := range r.rtcSessions {
// Client stopped
//if !webRTC.IsClosed() {
//continue
//}
n, err := enc.EncodeFloat32(pcm, data)
if err != nil {
log.Println("[!] Failed to decode")
continue
// encode frame
// fanout audioChannel
if webRTC.IsConnected() {
// NOTE: can block here
webRTC.AudioChannel <- data
}
data = data[:n]
data = append(data, count)
// TODO: r.rtcSessions is rarely updated. Lock will hold down perf
//r.sessionsLock.Lock()
for _, webRTC := range r.rtcSessions {
// Client stopped
//if !webRTC.IsClosed() {
//continue
//}
// encode frame
// fanout audioChannel
if webRTC.IsConnected() {
// NOTE: can block here
webRTC.AudioChannel <- data
}
//isRoomRunning = true
}
//r.sessionsLock.Unlock()
idx = 0
count = (count + 1) & 0xff
//isRoomRunning = true
}
//r.sessionsLock.Unlock()
idx = 0
count = (count + 1) & 0xff
}
}
}
@ -81,35 +79,34 @@ func (r *Room) startAudio() {
func (r *Room) startVideo() {
// fanout Screen
for {
select {
case <-r.Done:
image, ok := <-r.imageChannel
if !ok {
// Just for guarding, should not reached
log.Println("Warn: Room ", r.ID, " video channel closed unexpectedly")
return
}
if r.Done {
log.Println("Room ", r.ID, " video channel closed")
return
case image, ok := <-r.imageChannel:
if !ok {
// Just for guarding, should not reached
log.Println("Warn: Room ", r.ID, " video channel closed unexpectedly")
return
}
// TODO: Use worker pool for encoding
yuv := util.RgbaToYuv(image)
// TODO: r.rtcSessions is rarely updated. Lock will hold down perf
//r.sessionsLock.Lock()
for _, webRTC := range r.rtcSessions {
// Client stopped
//if webRTC.IsClosed() {
//continue
//}
// encode frame
// fanout imageChannel
if webRTC.IsConnected() {
// NOTE: can block here
webRTC.ImageChannel <- yuv
}
}
//r.sessionsLock.Unlock()
}
// TODO: Use worker pool for encoding
yuv := util.RgbaToYuv(image)
// TODO: r.rtcSessions is rarely updated. Lock will hold down perf
//r.sessionsLock.Lock()
for _, webRTC := range r.rtcSessions {
// Client stopped
//if webRTC.IsClosed() {
//continue
//}
// encode frame
// fanout imageChannel
if webRTC.IsConnected() {
// NOTE: can block here
webRTC.ImageChannel <- yuv
}
}
//r.sessionsLock.Unlock()
}
}

View file

@ -24,7 +24,7 @@ type Room struct {
audioChannel chan float32
inputChannel chan int
// Done channel is to fire exit event when there is no webRTC session running
Done chan struct{}
Done bool
rtcSessions []*webrtc.WebRTC
sessionsLock *sync.Mutex
@ -58,7 +58,7 @@ func NewRoom(roomID, gamepath, gameName string, onlineStorage *storage.Client) *
rtcSessions: []*webrtc.WebRTC{},
sessionsLock: &sync.Mutex{},
director: director,
Done: make(chan struct{}),
Done: false,
onlineStorage: onlineStorage,
}
@ -103,6 +103,7 @@ func (r *Room) isGameOnLocal(savepath string) bool {
func (r *Room) AddConnectionToRoom(peerconnection *webrtc.WebRTC, playerIndex int) {
peerconnection.AttachRoomID(r.ID)
r.rtcSessions = append(r.rtcSessions, peerconnection)
fmt.Println("Peerconnection ID", peerconnection.ID, " numSessions ", len(r.rtcSessions))
go r.startWebRTCSession(peerconnection, playerIndex)
}
@ -115,12 +116,14 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int
}()
for {
select {
case <-r.Done:
if r.Done {
log.Println("Detach peerconnection from room", r.ID)
return
case <-peerconnection.Done:
}
if peerconnection.Done {
r.removeSession(peerconnection)
}
select {
case input, ok := <-peerconnection.InputChannel:
if !ok {
return
@ -150,8 +153,8 @@ func (r *Room) CleanSession(peerconnection *webrtc.WebRTC) {
func (r *Room) removeSession(w *webrtc.WebRTC) {
fmt.Println("Cleaning session: ", w)
r.sessionsLock.Lock()
defer r.sessionsLock.Unlock()
//r.sessionsLock.Lock()
//defer r.sessionsLock.Unlock()
fmt.Println("Sessions list", r.rtcSessions)
for i, s := range r.rtcSessions {
fmt.Println("found session: ", s, w)
@ -174,7 +177,7 @@ func (r *Room) removeSession(w *webrtc.WebRTC) {
func (r *Room) Close() {
log.Println("Closing room", r.ID)
close(r.Done)
r.Done = true
log.Println("Closing director of room ", r.ID)
close(r.director.Done)
log.Println("Closing input of room ", r.ID)

View file

@ -84,7 +84,7 @@ type WebRTC struct {
AudioChannel chan []byte
InputChannel chan int
Done chan struct{}
Done bool
lastTime time.Time
curFPS int