-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
buzzibaer
committed
Jun 3, 2023
1 parent
d577d37
commit 7df17f9
Showing
3 changed files
with
234 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.IO.MemoryMappedFiles; | ||
using System.Threading; | ||
|
||
namespace FanPlugin.Wrapper | ||
{ | ||
|
||
public class SharedMemory<T> where T : struct | ||
{ | ||
// Constructor | ||
public SharedMemory(string name, int size) | ||
{ | ||
smName = name; | ||
smSize = size; | ||
} | ||
|
||
// Methods | ||
public bool Open() | ||
{ | ||
try | ||
{ | ||
// Create named MMF | ||
mmf = MemoryMappedFile.CreateOrOpen(smName, smSize); | ||
|
||
// Create accessors to MMF | ||
accessor = mmf.CreateViewAccessor(0, smSize, | ||
MemoryMappedFileAccess.ReadWrite); | ||
|
||
// Create lock | ||
smLock = new Mutex(true, "SM_LOCK", out locked); | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void Close() | ||
{ | ||
accessor.Dispose(); | ||
mmf.Dispose(); | ||
smLock.Close(); | ||
} | ||
|
||
public T Data | ||
{ | ||
get | ||
{ | ||
T dataStruct; | ||
accessor.Read<T>(0, out dataStruct); | ||
return dataStruct; | ||
} | ||
set | ||
{ | ||
smLock.WaitOne(); | ||
accessor.Write<T>(0, ref value); | ||
smLock.ReleaseMutex(); | ||
} | ||
} | ||
|
||
// Data | ||
private string smName; | ||
private Mutex smLock; | ||
private int smSize; | ||
private bool locked; | ||
private MemoryMappedFile mmf; | ||
private MemoryMappedViewAccessor accessor; | ||
} | ||
|
||
} |