Skip to content

Commit

Permalink
Improved threadsafety for FileIndexTable.FileToIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinRansom committed Dec 13, 2024
1 parent f0f6c65 commit 844e2ec
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/Compiler/Utilities/range.fs
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,29 @@ type FileIndexTable() =
match fileToIndexTable.TryGetValue filePath with
| true, idx -> idx
| _ ->
// If a write operation can happen, we have to lock the whole read-check-write to avoid race conditions
lock fileToIndexTable
<| fun () ->
match fileToIndexTable.TryGetValue filePath with
| true, idx -> idx
| _ ->

// Try again looking for a normalized entry.
let normalizedFilePath =
if normalize then
FileSystem.NormalizePathShim filePath
else
filePath

match fileToIndexTable.TryGetValue normalizedFilePath with
| true, idx ->
// Record the non-normalized entry if necessary
if filePath <> normalizedFilePath then
fileToIndexTable[filePath] <- idx

// Return the index
idx

// Try again looking for a normalized entry.
let normalizedFilePath =
if normalize then
FileSystem.NormalizePathShim filePath
else
filePath

match fileToIndexTable.TryGetValue normalizedFilePath with
| true, idx ->
// Record the non-normalized entry if necessary
if filePath <> normalizedFilePath then
fileToIndexTable[filePath] <- idx

// Return the index
idx

| _ ->
lock indexToFileTable (fun () ->
// See if it was added on another thread
match fileToIndexTable.TryGetValue filePath with
| true, idx -> idx
| _ ->
// Get the new index
// Okay it's really not there
let idx = indexToFileTable.Count

// Record the normalized entry
Expand All @@ -232,7 +230,7 @@ type FileIndexTable() =
fileToIndexTable[filePath] <- idx

// Return the index
idx
idx)

member t.IndexToFile n =
if n < 0 then
Expand Down

0 comments on commit 844e2ec

Please sign in to comment.