1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-30 00:12:50 -05:00

core/extworkspace: fix some thread-safety issues

This commit is contained in:
bbedward
2025-11-13 23:52:32 -05:00
parent 9d1fac3570
commit 77e6c16bd2

View File

@@ -151,9 +151,8 @@ func (m *Manager) handleWorkspaceGroup(e ext_workspace.ExtWorkspaceManagerV1Work
outputID := e.Output.ID() outputID := e.Output.ID()
log.Debugf("ExtWorkspace: Group %d output enter (output=%d)", groupID, outputID) log.Debugf("ExtWorkspace: Group %d output enter (output=%d)", groupID, outputID)
group.outputIDs[outputID] = true
m.post(func() { m.post(func() {
group.outputIDs[outputID] = true
m.updateState() m.updateState()
}) })
}) })
@@ -161,8 +160,8 @@ func (m *Manager) handleWorkspaceGroup(e ext_workspace.ExtWorkspaceManagerV1Work
handle.SetOutputLeaveHandler(func(e ext_workspace.ExtWorkspaceGroupHandleV1OutputLeaveEvent) { handle.SetOutputLeaveHandler(func(e ext_workspace.ExtWorkspaceGroupHandleV1OutputLeaveEvent) {
outputID := e.Output.ID() outputID := e.Output.ID()
log.Debugf("ExtWorkspace: Group %d output leave (output=%d)", groupID, outputID) log.Debugf("ExtWorkspace: Group %d output leave (output=%d)", groupID, outputID)
delete(group.outputIDs, outputID)
m.post(func() { m.post(func() {
delete(group.outputIDs, outputID)
m.updateState() m.updateState()
}) })
}) })
@@ -171,6 +170,7 @@ func (m *Manager) handleWorkspaceGroup(e ext_workspace.ExtWorkspaceManagerV1Work
workspaceID := e.Workspace.ID() workspaceID := e.Workspace.ID()
log.Debugf("ExtWorkspace: Group %d workspace enter (workspace=%d)", groupID, workspaceID) log.Debugf("ExtWorkspace: Group %d workspace enter (workspace=%d)", groupID, workspaceID)
m.post(func() {
m.workspacesMutex.Lock() m.workspacesMutex.Lock()
if ws, exists := m.workspaces[workspaceID]; exists { if ws, exists := m.workspaces[workspaceID]; exists {
ws.groupID = groupID ws.groupID = groupID
@@ -178,7 +178,6 @@ func (m *Manager) handleWorkspaceGroup(e ext_workspace.ExtWorkspaceManagerV1Work
m.workspacesMutex.Unlock() m.workspacesMutex.Unlock()
group.workspaceIDs = append(group.workspaceIDs, workspaceID) group.workspaceIDs = append(group.workspaceIDs, workspaceID)
m.post(func() {
m.updateState() m.updateState()
}) })
}) })
@@ -187,6 +186,7 @@ func (m *Manager) handleWorkspaceGroup(e ext_workspace.ExtWorkspaceManagerV1Work
workspaceID := e.Workspace.ID() workspaceID := e.Workspace.ID()
log.Debugf("ExtWorkspace: Group %d workspace leave (workspace=%d)", groupID, workspaceID) log.Debugf("ExtWorkspace: Group %d workspace leave (workspace=%d)", groupID, workspaceID)
m.post(func() {
m.workspacesMutex.Lock() m.workspacesMutex.Lock()
if ws, exists := m.workspaces[workspaceID]; exists { if ws, exists := m.workspaces[workspaceID]; exists {
ws.groupID = 0 ws.groupID = 0
@@ -199,20 +199,20 @@ func (m *Manager) handleWorkspaceGroup(e ext_workspace.ExtWorkspaceManagerV1Work
break break
} }
} }
m.post(func() {
m.updateState() m.updateState()
}) })
}) })
handle.SetRemovedHandler(func(e ext_workspace.ExtWorkspaceGroupHandleV1RemovedEvent) { handle.SetRemovedHandler(func(e ext_workspace.ExtWorkspaceGroupHandleV1RemovedEvent) {
log.Debugf("ExtWorkspace: Group %d removed", groupID) log.Debugf("ExtWorkspace: Group %d removed", groupID)
m.post(func() {
group.removed = true group.removed = true
m.groupsMutex.Lock() m.groupsMutex.Lock()
delete(m.groups, groupID) delete(m.groups, groupID)
m.groupsMutex.Unlock() m.groupsMutex.Unlock()
m.post(func() {
m.wlMutex.Lock() m.wlMutex.Lock()
handle.Destroy() handle.Destroy()
m.wlMutex.Unlock() m.wlMutex.Unlock()
@@ -240,16 +240,16 @@ func (m *Manager) handleWorkspace(e ext_workspace.ExtWorkspaceManagerV1Workspace
handle.SetIdHandler(func(e ext_workspace.ExtWorkspaceHandleV1IdEvent) { handle.SetIdHandler(func(e ext_workspace.ExtWorkspaceHandleV1IdEvent) {
log.Debugf("ExtWorkspace: Workspace %d id: %s", workspaceID, e.Id) log.Debugf("ExtWorkspace: Workspace %d id: %s", workspaceID, e.Id)
ws.workspaceID = e.Id
m.post(func() { m.post(func() {
ws.workspaceID = e.Id
m.updateState() m.updateState()
}) })
}) })
handle.SetNameHandler(func(e ext_workspace.ExtWorkspaceHandleV1NameEvent) { handle.SetNameHandler(func(e ext_workspace.ExtWorkspaceHandleV1NameEvent) {
log.Debugf("ExtWorkspace: Workspace %d name: %s", workspaceID, e.Name) log.Debugf("ExtWorkspace: Workspace %d name: %s", workspaceID, e.Name)
ws.name = e.Name
m.post(func() { m.post(func() {
ws.name = e.Name
m.updateState() m.updateState()
}) })
}) })
@@ -266,16 +266,16 @@ func (m *Manager) handleWorkspace(e ext_workspace.ExtWorkspaceManagerV1Workspace
} }
} }
log.Debugf("ExtWorkspace: Workspace %d coordinates: %v", workspaceID, coords) log.Debugf("ExtWorkspace: Workspace %d coordinates: %v", workspaceID, coords)
ws.coordinates = coords
m.post(func() { m.post(func() {
ws.coordinates = coords
m.updateState() m.updateState()
}) })
}) })
handle.SetStateHandler(func(e ext_workspace.ExtWorkspaceHandleV1StateEvent) { handle.SetStateHandler(func(e ext_workspace.ExtWorkspaceHandleV1StateEvent) {
log.Debugf("ExtWorkspace: Workspace %d state: %d", workspaceID, e.State) log.Debugf("ExtWorkspace: Workspace %d state: %d", workspaceID, e.State)
ws.state = e.State
m.post(func() { m.post(func() {
ws.state = e.State
m.updateState() m.updateState()
}) })
}) })
@@ -286,13 +286,14 @@ func (m *Manager) handleWorkspace(e ext_workspace.ExtWorkspaceManagerV1Workspace
handle.SetRemovedHandler(func(e ext_workspace.ExtWorkspaceHandleV1RemovedEvent) { handle.SetRemovedHandler(func(e ext_workspace.ExtWorkspaceHandleV1RemovedEvent) {
log.Debugf("ExtWorkspace: Workspace %d removed", workspaceID) log.Debugf("ExtWorkspace: Workspace %d removed", workspaceID)
m.post(func() {
ws.removed = true ws.removed = true
m.workspacesMutex.Lock() m.workspacesMutex.Lock()
delete(m.workspaces, workspaceID) delete(m.workspaces, workspaceID)
m.workspacesMutex.Unlock() m.workspacesMutex.Unlock()
m.post(func() {
m.wlMutex.Lock() m.wlMutex.Lock()
handle.Destroy() handle.Destroy()
m.wlMutex.Unlock() m.wlMutex.Unlock()
@@ -422,6 +423,9 @@ func (m *Manager) notifier() {
} }
func (m *Manager) ActivateWorkspace(groupID, workspaceID string) error { func (m *Manager) ActivateWorkspace(groupID, workspaceID string) error {
errChan := make(chan error, 1)
m.post(func() {
m.workspacesMutex.RLock() m.workspacesMutex.RLock()
defer m.workspacesMutex.RUnlock() defer m.workspacesMutex.RUnlock()
@@ -444,14 +448,21 @@ func (m *Manager) ActivateWorkspace(groupID, workspaceID string) error {
err = m.manager.Commit() err = m.manager.Commit()
} }
m.wlMutex.Unlock() m.wlMutex.Unlock()
return err errChan <- err
return
} }
} }
return fmt.Errorf("workspace not found: %s in group %s", workspaceID, groupID) errChan <- fmt.Errorf("workspace not found: %s in group %s", workspaceID, groupID)
})
return <-errChan
} }
func (m *Manager) DeactivateWorkspace(groupID, workspaceID string) error { func (m *Manager) DeactivateWorkspace(groupID, workspaceID string) error {
errChan := make(chan error, 1)
m.post(func() {
m.workspacesMutex.RLock() m.workspacesMutex.RLock()
defer m.workspacesMutex.RUnlock() defer m.workspacesMutex.RUnlock()
@@ -474,14 +485,21 @@ func (m *Manager) DeactivateWorkspace(groupID, workspaceID string) error {
err = m.manager.Commit() err = m.manager.Commit()
} }
m.wlMutex.Unlock() m.wlMutex.Unlock()
return err errChan <- err
return
} }
} }
return fmt.Errorf("workspace not found: %s in group %s", workspaceID, groupID) errChan <- fmt.Errorf("workspace not found: %s in group %s", workspaceID, groupID)
})
return <-errChan
} }
func (m *Manager) RemoveWorkspace(groupID, workspaceID string) error { func (m *Manager) RemoveWorkspace(groupID, workspaceID string) error {
errChan := make(chan error, 1)
m.post(func() {
m.workspacesMutex.RLock() m.workspacesMutex.RLock()
defer m.workspacesMutex.RUnlock() defer m.workspacesMutex.RUnlock()
@@ -504,14 +522,21 @@ func (m *Manager) RemoveWorkspace(groupID, workspaceID string) error {
err = m.manager.Commit() err = m.manager.Commit()
} }
m.wlMutex.Unlock() m.wlMutex.Unlock()
return err errChan <- err
return
} }
} }
return fmt.Errorf("workspace not found: %s in group %s", workspaceID, groupID) errChan <- fmt.Errorf("workspace not found: %s in group %s", workspaceID, groupID)
})
return <-errChan
} }
func (m *Manager) CreateWorkspace(groupID, workspaceName string) error { func (m *Manager) CreateWorkspace(groupID, workspaceName string) error {
errChan := make(chan error, 1)
m.post(func() {
m.groupsMutex.RLock() m.groupsMutex.RLock()
defer m.groupsMutex.RUnlock() defer m.groupsMutex.RUnlock()
@@ -523,11 +548,15 @@ func (m *Manager) CreateWorkspace(groupID, workspaceName string) error {
err = m.manager.Commit() err = m.manager.Commit()
} }
m.wlMutex.Unlock() m.wlMutex.Unlock()
return err errChan <- err
return
} }
} }
return fmt.Errorf("workspace group not found: %s", groupID) errChan <- fmt.Errorf("workspace group not found: %s", groupID)
})
return <-errChan
} }
func (m *Manager) Close() { func (m *Manager) Close() {