Skip to content

Commit 64cbd5f

Browse files
authored
Make the syntax highlighter more thread safety aware (WerWolv#2585)
By creating copies of the required inputs on the main thread just before the task is spawned. A;so if task is still running when new data can be copied then the task is interrupted thus avoiding concurrency without mutexes. Atomics are used to signal state information used to determine what and when to spawn. Also includes update to pattern editor library and some fixes to syntax highlighting error when custom types defined inside namespaces were used inside the namespaces without the full qualified name and other small changes mostly to improve the current style.
1 parent ba7e789 commit 64cbd5f

File tree

8 files changed

+871
-811
lines changed

8 files changed

+871
-811
lines changed

plugins/builtin/include/content/text_highlighting/pattern_language.hpp

Lines changed: 90 additions & 218 deletions
Large diffs are not rendered by default.

plugins/builtin/include/content/views/view_pattern_editor.hpp

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <ui/text_editor.hpp>
1111
#include <content/text_highlighting/pattern_language.hpp>
12-
#include <hex/api/content_registry/settings.hpp>
1312
#include <hex/helpers/magic.hpp>
1413
#include <ui/pattern_drawer.hpp>
1514

@@ -32,52 +31,34 @@ namespace hex::plugin::builtin {
3231
std::string m_sharedSource;
3332
};
3433

34+
using TextHighlighter = hex::plugin::builtin::TextHighlighter;
3535
class ViewPatternEditor : public View::Window {
3636
public:
3737
ViewPatternEditor();
3838
~ViewPatternEditor() override;
3939

4040
void drawAlwaysVisibleContent() override;
41-
std::unique_ptr<pl::PatternLanguage> *getPatternLanguage() {
42-
return &m_editorRuntime;
43-
}
44-
45-
ui::TextEditor *getTextEditor() {
46-
auto provider = ImHexApi::Provider::get();
47-
if (provider == nullptr)
48-
return nullptr;
49-
50-
return &m_textEditor.get(provider);
51-
}
52-
53-
bool getChangesWereParsed() const {
54-
return m_changesWereParsed;
55-
}
56-
57-
u32 getRunningParsers () const {
58-
return m_runningParsers;
59-
}
60-
61-
u32 getRunningEvaluators () const {
62-
return m_runningEvaluators;
63-
}
64-
65-
void setChangesWereParsed(bool changesWereParsed) {
66-
m_changesWereParsed = changesWereParsed;
67-
}
68-
41+
std::unique_ptr<pl::PatternLanguage> *getPatternLanguage() { return &m_editorRuntime; }
42+
ui::TextEditor *getTextEditor();
43+
u32 getRunningParsers () const { return m_runningParsers;}
44+
u32 getRunningEvaluators () const { return m_runningEvaluators;}
45+
u32 getRunningHighlighters () const { return m_runningHighlighters;}
46+
void incrementRunningParsers( i32 amount) { m_runningParsers += amount; }
47+
void incrementRunningEvaluators( i32 amount) { m_runningEvaluators += amount; }
48+
void incrementRunningHighlighters( i32 amount) { m_runningHighlighters += amount; }
49+
bool hasUnevaluatedChanges(prv::Provider *provider) const;
50+
void setChangesWereParsed(bool changesWereParsed) { m_changesWereParsed = changesWereParsed;}
51+
void setChangesWereColored(bool changesWereColored) { m_changesWereColored = changesWereColored; }
6952
void drawContent() override;
70-
7153
void setPopupWindowHeight(u32 height) { m_popupWindowHeight = height; }
7254
u32 getPopupWindowHeight() const { return m_popupWindowHeight; }
73-
74-
enum class DangerousFunctionPerms : u8 {
75-
Ask,
76-
Allow,
77-
Deny
78-
};
55+
enum class DangerousFunctionPerms : u8 { Ask, Allow, Deny };
7956

8057
void drawHelpText() override;
58+
void setWasInterrupted(bool wasInterrupted) { m_wasInterrupted = wasInterrupted; }
59+
bool wasInterrupted() const { return m_wasInterrupted;}
60+
void resetInterrupt() { m_wasInterrupted = false; m_interrupt = false;}
61+
void interrupt() { m_interrupt = true; }
8162

8263
private:
8364
class PopupAcceptPattern;
@@ -127,19 +108,26 @@ namespace hex::plugin::builtin {
127108

128109
std::atomic<u32> m_runningEvaluators = 0;
129110
std::atomic<u32> m_runningParsers = 0;
111+
std::atomic<u32> m_runningHighlighters = 0;
130112

131-
std::atomic<bool> m_changesWereParsed = false;
132113
PerProvider<bool> m_hasUnevaluatedChanges;
114+
std::atomic<bool> m_changesWereParsed;
115+
PerProvider<bool> m_hasUncoloredChanges;
116+
std::atomic<bool> m_changesWereColored;
117+
std::atomic<bool> m_wasInterrupted;
118+
std::atomic<bool> m_interrupt;
119+
133120
std::chrono::time_point<std::chrono::steady_clock> m_lastEditorChangeTime;
134121

135122
PerProvider<ui::TextEditor> m_textEditor, m_consoleEditor;
123+
PerProvider<TextHighlighter> m_textHighlighter;
136124
std::atomic<bool> m_consoleNeedsUpdate = false;
137125

138126
std::atomic<bool> m_dangerousFunctionCalled = false;
139127
std::atomic<DangerousFunctionPerms> m_dangerousFunctionsAllowed = DangerousFunctionPerms::Ask;
140128

141-
ContentRegistry::Settings::SettingsVariable<bool, "hex.builtin.setting.general", "hex.builtin.setting.general.suggest_patterns"> m_suggestSupportedPatterns = true;
142-
ContentRegistry::Settings::SettingsVariable<bool, "hex.builtin.setting.general", "hex.builtin.setting.general.auto_apply_patterns"> m_autoApplyPatterns = false;
129+
bool m_suggestSupportedPatterns = true;
130+
bool m_autoApplyPatterns = false;
143131

144132
PerProvider<ui::VisualizerDrawer> m_visualizerDrawer;
145133
bool m_tooltipJustOpened = false;
@@ -178,7 +166,7 @@ namespace hex::plugin::builtin {
178166

179167
std::array<AccessData, 512> m_accessHistory = {};
180168
u32 m_accessHistoryIndex = 0;
181-
ContentRegistry::Settings::SettingsVariable<bool, "hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.pattern_parent_highlighting"> m_parentHighlightingEnabled = false;
169+
bool m_parentHighlightingEnabled = true;
182170
bool m_replaceMode = false;
183171
bool m_openFindReplacePopUp = false;
184172
bool m_openGotoLinePopUp = false;
@@ -188,13 +176,14 @@ namespace hex::plugin::builtin {
188176
PerProvider<bool> m_ignoreNextChangeEvent;
189177
PerProvider<bool> m_changeEventAcknowledgementPending;
190178
PerProvider<bool> m_patternFileDirty;
179+
PerProvider<bool> m_patternFileInitialized;
191180

192181
ImRect m_textEditorHoverBox;
193182
ImRect m_consoleHoverBox;
194183
std::string m_focusedSubWindowName;
195184
float m_popupWindowHeight = 0;
196185
float m_popupWindowHeightChange = 0;
197-
bool m_frPopupIsClosed = true;
186+
bool m_findReplacePopupIsClosed = true;
198187
bool m_gotoPopupIsClosed = true;
199188

200189
static inline std::array<std::string,256> m_findHistory;
@@ -204,7 +193,6 @@ namespace hex::plugin::builtin {
204193
static inline u32 m_replaceHistorySize = 0;
205194
static inline u32 m_replaceHistoryIndex = 0;
206195

207-
TextHighlighter m_textHighlighter = TextHighlighter(this,&this->m_editorRuntime);
208196
private:
209197
void drawConsole(ImVec2 size);
210198
void drawDebugger(ImVec2 size);

0 commit comments

Comments
 (0)