Make code lock-less on critical path

This commit is contained in:
Kamil Trzciński 2022-11-28 20:29:32 +01:00
parent 190b14e66f
commit 558d37e940
3 changed files with 43 additions and 50 deletions

View file

@ -35,3 +35,4 @@ It does help this project on being supported.
- Fix crash on startup when Steam is missing
- Fix crash on resume when X360 Controller is in use
- Provide currated list of mapping keys in settings (might be extended in the future)
- Improve performance on critical loop (code is lock-less)

View file

@ -128,63 +128,62 @@ namespace SteamController
}
}
public Profiles.Profile? FindProfile(String name)
private bool SelectProfile(Profiles.Profile profile)
{
return Profiles.Find((profile) => profile.Name == name);
lock (this)
{
var list = OrderedProfiles.ToList();
if (!list.Remove(profile))
return false;
list.Insert(0, profile);
orderedProfiles = list;
RequestDesktopMode = profile.IsDesktop;
if (profile.Selected(this))
{
ProfileChanged(profile);
return true;
}
return false;
}
}
public bool SelectProfile(String name, bool enable = true)
public bool SelectProfile(String name)
{
var profile = FindProfile(name);
var profile = Profiles.Find((profile) => profile.Name == name);
if (profile is null)
return false;
var list = OrderedProfiles;
list.Remove(profile);
list.Insert(0, profile);
RequestDesktopMode = profile.IsDesktop;
if (profile.Selected(this))
ProfileChanged(profile);
return true;
return SelectProfile(profile);
}
public bool SelectNext()
{
bool firstSelected = false;
var profile = OrderedProfiles.
Where((profile) => profile.Selected(this)).
Skip(1).
FirstOrDefault();
var list = OrderedProfiles;
foreach (var profile in list)
{
if (!profile.Selected(this))
continue;
if (!firstSelected)
{
firstSelected = true;
continue;
}
list.Remove(profile);
list.Insert(0, profile);
ProfileChanged(profile);
return true;
}
if (profile is not null)
return SelectProfile(profile);
return false;
}
public void ToggleDesktopMode(bool? forceState = null)
{
var oldProfile = GetCurrentProfile();
if (forceState is null)
RequestDesktopMode = !RequestDesktopMode;
else
RequestDesktopMode = forceState.Value;
lock (this)
{
var oldProfile = GetCurrentProfile();
if (forceState is null)
RequestDesktopMode = !RequestDesktopMode;
else
RequestDesktopMode = forceState.Value;
var newProfile = GetCurrentProfile();
if (oldProfile != newProfile && newProfile is not null)
ProfileChanged(newProfile);
var newProfile = GetCurrentProfile();
if (oldProfile != newProfile && newProfile is not null)
ProfileChanged(newProfile);
}
}
}
}

View file

@ -59,7 +59,7 @@ namespace SteamController
continue;
var profileItem = new ToolStripMenuItem(profile.Name);
profileItem.Click += delegate { lock (context) { context.SelectProfile(profile.Name); } };
profileItem.Click += delegate { context.SelectProfile(profile.Name); };
contextMenu.Opening += delegate { profileItem.Checked = context.GetCurrentProfile() == profile; };
contextMenu.Items.Add(profileItem);
}
@ -149,12 +149,8 @@ namespace SteamController
}
updatesReceived++;
lock (context)
{
context.Update();
context.Debug();
}
context.Update();
context.Debug();
if (!context.Enabled)
{
@ -179,11 +175,8 @@ namespace SteamController
private void ContextStateUpdate_Tick(object? sender, EventArgs e)
{
lock (context)
{
context.Tick();
SharedData_Update();
}
context.Tick();
SharedData_Update();
if (!context.Mouse.Valid)
{