Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement preprocessor for NET 9 locks #309

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Fuyu.Common/Collections/ThreadDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.Collections.Generic;
#if NET9_0_OR_GREATER
using Lock = System.Threading.Lock;
#else
using Lock = object;
#endif

namespace Fuyu.Common.Collections;

Expand All @@ -10,18 +15,18 @@ namespace Fuyu.Common.Collections;
public class ThreadDictionary<T1, T2>
{
private readonly Dictionary<T1, T2> _dictionary;
private readonly object _lock;
private readonly Lock _lock;

public ThreadDictionary()
{
_dictionary = new Dictionary<T1, T2>();
_lock = new object();
_lock = new Lock();
}

public ThreadDictionary(IDictionary<T1, T2> enumerable)
{
_dictionary = new Dictionary<T1, T2>(enumerable);
_lock = new object();
_lock = new Lock();
}

public Dictionary<T1, T2> ToDictionary()
Expand Down
9 changes: 7 additions & 2 deletions Fuyu.Common/Collections/ThreadList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.Collections.Generic;
#if NET9_0_OR_GREATER
using Lock = System.Threading.Lock;
#else
using Lock = object;
#endif

namespace Fuyu.Common.Collections;

Expand All @@ -11,12 +16,12 @@ namespace Fuyu.Common.Collections;
public class ThreadList<T>
{
private readonly List<T> _list;
private readonly object _lock;
private readonly Lock _lock;

public ThreadList()
{
_list = new List<T>();
_lock = new object();
_lock = new Lock();
}

public List<T> ToList()
Expand Down
9 changes: 7 additions & 2 deletions Fuyu.Common/Collections/ThreadObject.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
namespace Fuyu.Common.Collections;
#if NET9_0_OR_GREATER
using Lock = System.Threading.Lock;
#else
using Lock = object;
#endif

public class ThreadObject<T>
{
private T _object;
private readonly object _lock;
private readonly Lock _lock;

public ThreadObject(T value)
{
_object = value;
_lock = new object();
_lock = new Lock();
}

public T Get()
Expand Down
7 changes: 6 additions & 1 deletion Fuyu.Common/IO/Terminal.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System;
#if NET9_0_OR_GREATER
using Lock = System.Threading.Lock;
#else
using Lock = object;
#endif

namespace Fuyu.Common.IO;

public static class Terminal
{
private static readonly object _lock = new object();
private static readonly Lock _lock = new Lock();
private static string _filepath;

static Terminal()
Expand Down
11 changes: 8 additions & 3 deletions Fuyu.Common/IO/VFS.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System;
using System.Collections.Concurrent;
using System.IO;
#if NET9_0_OR_GREATER
using Lock = System.Threading.Lock;
#else
using Lock = object;
#endif

namespace Fuyu.Common.IO;

public static class VFS
{
private static readonly ConcurrentDictionary<string, object> _writeLock;
private static readonly ConcurrentDictionary<string, Lock> _writeLock;

static VFS()
{
_writeLock = new ConcurrentDictionary<string, object>();
_writeLock = new ConcurrentDictionary<string, Lock>();
}

public static string GetWorkingDirectory()
Expand Down Expand Up @@ -116,7 +121,7 @@ public static void WriteTextFile(string filepath, string text, bool append = fal
}

// get thread lock
_writeLock.TryAdd(filepath, new object());
_writeLock.TryAdd(filepath, new Lock());

// write text
lock (_writeLock[filepath])
Expand Down