summaryrefslogtreecommitdiff
path: root/lsp.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-27 13:13:49 -0500
committerpommicket <pommicket@gmail.com>2022-12-27 13:13:49 -0500
commit1379bfc2bfc8f96215d9187e72ac30dc5c94225f (patch)
treece23a710a70172593a97d04a14796bcbb10c1f1e /lsp.c
parent27b8b36aea3bb5913a600bc7e5b41f68557c2587 (diff)
fix LSP thread safety
Diffstat (limited to 'lsp.c')
-rw-r--r--lsp.c32
1 files changed, 23 insertions, 9 deletions
diff --git a/lsp.c b/lsp.c
index 025cc10..a490aad 100644
--- a/lsp.c
+++ b/lsp.c
@@ -309,15 +309,29 @@ static int lsp_communication_thread(void *data) {
}
u32 lsp_document_id(LSP *lsp, const char *path) {
- u32 *value = str_hash_table_get(&lsp->document_ids, path);
- if (!value) {
- u32 id = arr_len(lsp->document_data);
- value = str_hash_table_insert(&lsp->document_ids, path);
- *value = id;
- LSPDocumentData *data = arr_addp(lsp->document_data);
- data->path = str_dup(path);
- }
- return *value;
+ SDL_LockMutex(lsp->document_mutex);
+ u32 *value = str_hash_table_get(&lsp->document_ids, path);
+ if (!value) {
+ u32 id = arr_len(lsp->document_data);
+ value = str_hash_table_insert(&lsp->document_ids, path);
+ *value = id;
+ LSPDocumentData *data = arr_addp(lsp->document_data);
+ data->path = str_dup(path);
+ }
+ u32 id = *value;
+ SDL_UnlockMutex(lsp->document_mutex);
+ return id;
+}
+
+const char *lsp_document_path(LSP *lsp, LSPDocumentID document) {
+ SDL_LockMutex(lsp->document_mutex);
+ if (document >= arr_len(lsp->document_data)) {
+ assert(0);
+ return "";
+ }
+ const char *path = lsp->document_data[document].path;
+ SDL_UnlockMutex(lsp->document_mutex);
+ return path;
}
LSP *lsp_create(const char *root_dir, Language language, const char *analyzer_command) {