Skip to content

Commit 505b583

Browse files
committed
Removed useless logic from AsyncHandler
1 parent 956770a commit 505b583

File tree

4 files changed

+29
-53
lines changed

4 files changed

+29
-53
lines changed

Diff for: handler/builder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ func (handler *InoHandler) rebuildEnvironmentLoop() {
9494
}
9595
}()
9696

97-
handler.synchronizer.DataMux.Lock()
97+
handler.dataMux.Lock()
9898
handler.initializeWorkbench(nil)
99-
handler.synchronizer.DataMux.Unlock()
99+
handler.dataMux.Unlock()
100100
done <- true
101101
close(done)
102102
}

Diff for: handler/handler.go

+24-18
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,24 @@ import (
2727
var globalCliPath string
2828
var globalClangdPath string
2929
var enableLogging bool
30-
var asyncProcessing bool
3130

3231
// Setup initializes global variables.
33-
func Setup(cliPath string, clangdPath string, _enableLogging bool, _asyncProcessing bool) {
32+
func Setup(cliPath string, clangdPath string, _enableLogging bool) {
3433
globalCliPath = cliPath
3534
globalClangdPath = clangdPath
3635
enableLogging = _enableLogging
37-
asyncProcessing = _asyncProcessing
3836
}
3937

4038
// CLangdStarter starts clangd and returns its stdin/out/err
4139
type CLangdStarter func() (stdin io.WriteCloser, stdout io.ReadCloser, stderr io.ReadCloser)
4240

4341
// InoHandler is a JSON-RPC handler that delegates messages to clangd.
4442
type InoHandler struct {
45-
StdioConn *jsonrpc2.Conn
46-
ClangdConn *jsonrpc2.Conn
43+
StdioConn *jsonrpc2.Conn
44+
ClangdConn *jsonrpc2.Conn
45+
46+
clangdStarted *sync.Cond
47+
dataMux sync.RWMutex
4748
lspInitializeParams *lsp.InitializeParams
4849
buildPath *paths.Path
4950
buildSketchRoot *paths.Path
@@ -61,8 +62,7 @@ type InoHandler struct {
6162
docs map[string]*lsp.TextDocumentItem
6263
inoDocsWithDiagnostics map[string]bool
6364

64-
config lsp.BoardConfig
65-
synchronizer Synchronizer
65+
config lsp.BoardConfig
6666
}
6767

6868
// NewInoHandler creates and configures an InoHandler.
@@ -74,15 +74,9 @@ func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
7474
SelectedBoard: board,
7575
},
7676
}
77-
77+
handler.clangdStarted = sync.NewCond(&handler.dataMux)
7878
stdStream := jsonrpc2.NewBufferedStream(stdio, jsonrpc2.VSCodeObjectCodec{})
7979
var stdHandler jsonrpc2.Handler = jsonrpc2.HandlerWithError(handler.HandleMessageFromIDE)
80-
if asyncProcessing {
81-
stdHandler = AsyncHandler{
82-
handler: stdHandler,
83-
synchronizer: &handler.synchronizer,
84-
}
85-
}
8680
handler.StdioConn = jsonrpc2.NewConn(context.Background(), stdStream, stdHandler)
8781
if enableLogging {
8882
log.Println("Initial board configuration:", board)
@@ -118,11 +112,23 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
118112
"textDocument/didClose": true,
119113
}
120114
if needsWriteLock[req.Method] {
121-
handler.synchronizer.DataMux.Lock()
122-
defer handler.synchronizer.DataMux.Unlock()
115+
handler.dataMux.Lock()
116+
defer handler.dataMux.Unlock()
123117
} else {
124-
handler.synchronizer.DataMux.RLock()
125-
defer handler.synchronizer.DataMux.RUnlock()
118+
handler.dataMux.RLock()
119+
defer handler.dataMux.RUnlock()
120+
}
121+
122+
// Wait for clangd start-up
123+
doNotNeedClangd := map[string]bool{
124+
"initialize": true,
125+
"initialized": true,
126+
}
127+
if handler.ClangdConn == nil && !doNotNeedClangd[req.Method] {
128+
handler.clangdStarted.Wait()
129+
if handler.ClangdConn == nil {
130+
return nil, errors.New("could not run clangd, aborted")
131+
}
126132
}
127133

128134
// Handle LSP methods: transform parameters and send to clangd

Diff for: handler/syncer.go

+2-32
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,16 @@ package handler
22

33
import (
44
"context"
5-
"sync"
65

76
"github.com/sourcegraph/jsonrpc2"
87
)
98

10-
// Synchronizer is used to block message processing while an edit or config change is applied.
11-
type Synchronizer struct {
12-
// FileMux is a read/write mutex for file access. It is locked during the processing of
13-
// messages that modify target files for clangd.
14-
FileMux sync.RWMutex
15-
// DataMux is a mutex for document metadata access, i.e. source-target URI mappings and line mappings.
16-
DataMux sync.RWMutex
17-
}
18-
199
// AsyncHandler wraps a Handler such that each request is handled in its own goroutine.
2010
type AsyncHandler struct {
21-
handler jsonrpc2.Handler
22-
synchronizer *Synchronizer
11+
handler jsonrpc2.Handler
2312
}
2413

2514
// Handle handles a request or notification
2615
func (ah AsyncHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
27-
needsWriteLock := req.Method == "textDocument/didOpen" || req.Method == "textDocument/didChange"
28-
if needsWriteLock {
29-
go func() {
30-
ah.synchronizer.FileMux.Lock()
31-
defer ah.synchronizer.FileMux.Unlock()
32-
if enableLogging {
33-
// log.Println("Message processing locked for", req.Method)
34-
}
35-
ah.handler.Handle(ctx, conn, req)
36-
if enableLogging {
37-
// log.Println("Message processing unlocked for", req.Method)
38-
}
39-
}()
40-
} else {
41-
go func() {
42-
ah.synchronizer.FileMux.RLock()
43-
ah.handler.Handle(ctx, conn, req)
44-
ah.synchronizer.FileMux.RUnlock()
45-
}()
46-
}
16+
go ah.handler.Handle(ctx, conn, req)
4717
}

Diff for: main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func main() {
4444
log.SetOutput(os.Stderr)
4545
}
4646

47-
handler.Setup(cliPath, clangdPath, enableLogging, true)
47+
handler.Setup(cliPath, clangdPath, enableLogging)
4848
initialBoard := lsp.Board{Fqbn: initialFqbn, Name: initialBoardName}
4949

5050
stdio := streams.NewReadWriteCloser(os.Stdin, os.Stdout)

0 commit comments

Comments
 (0)