-
Notifications
You must be signed in to change notification settings - Fork 39
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
StreamHandle interop #711
StreamHandle interop #711
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I have some minor comments. Generally this LGTM.
Cesium.Runtime/StdIoFunctions.cs
Outdated
catch (AccessViolationException) | ||
{ | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no point in trying to catch this. The program should fail if an exception is thrown here.
Cesium.Runtime/StdIoFunctions.cs
Outdated
var handel = GCHandle.ToIntPtr(gch); | ||
|
||
var ptr = Marshal.AllocHGlobal(sizeof(IntPtr)); | ||
Marshal.WriteIntPtr(ptr, handel); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var handel = GCHandle.ToIntPtr(gch); | |
var ptr = Marshal.AllocHGlobal(sizeof(IntPtr)); | |
Marshal.WriteIntPtr(ptr, handel); | |
var handle = GCHandle.ToIntPtr(gch); | |
var ptr = Marshal.AllocHGlobal(sizeof(IntPtr)); | |
Marshal.WriteIntPtr(ptr, handle); |
Cesium.Runtime/StdIoFunctions.cs
Outdated
} | ||
} | ||
|
||
internal static bool RemoveStream(IntPtr handle) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this FreeStream
?
Cesium.Runtime/StdIoFunctions.cs
Outdated
@@ -668,15 +667,62 @@ public static int Remove(byte* pathname) | |||
return 0; | |||
} | |||
|
|||
private static unsafe void* GetLastStream() | |||
internal static StreamHandle? GetStream(IntPtr handle) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we accept void* filePtr
parameter here and perform cast inside? I think otherwise it's unnescessary noise to convert everywhere.
I have included your suggestions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I believe we are getting closer to merging this.
Cesium.Runtime/StdIoFunctions.cs
Outdated
catch (InvalidOperationException) | ||
{ | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When does this get thrown? Looks suspicious. Maybe we shouldn't handle any exception here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually only AV occurs there. In this case, the error can occur due to an invalid pointer argument when converting to GCHandle, eventually the Target property will throw an AV if converting fail.
Cesium.Runtime/StdIoFunctions.cs
Outdated
lock (_locker) | ||
{ | ||
var gchAddr = Marshal.ReadIntPtr(handle); | ||
var gch = GCHandle.FromIntPtr(gchAddr); | ||
|
||
return gch.Target as StreamHandle; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, now wait, why we need this _locker
at all? It doesn't seem to defend any collection manipulation. Let's maybe get rid of the _locker
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, so far the lock is not needed here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Closes #681
GCHandles on StreamHandles are allocated on the unmanaged heap (
AllocHGlobal
). A pointer to the GCHandle is used to control the StreamHandler. Locks are also added for thread-safe interaction.Keeps all standard streams (stdin, stdout, stderr) in list
Handles