Skip to content

Commit 5b484a7

Browse files
authored
Merge pull request #947 from Unity-Technologies/unity-master-abort-w32-sync-io
Interrupt syncronous file read/write IO on Win32 by registering inter…
2 parents 34dc12d + e5769df commit 5b484a7

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

mono/metadata/w32file-win32.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,49 @@ mono_w32file_delete (const gunichar2 *name)
8282
return res;
8383
}
8484

85+
static void
86+
cancel_w32_io (HANDLE file_handle)
87+
{
88+
CancelIoEx (file_handle, NULL);
89+
}
90+
8591
gboolean
86-
mono_w32file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
92+
mono_w32file_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
8793
{
88-
gboolean res;
89-
MONO_ENTER_GC_SAFE;
90-
res = ReadFile (handle, buffer, numbytes, bytesread, NULL);
91-
MONO_PROFILER_RAISE (fileio, (1, *bytesread));
92-
MONO_EXIT_GC_SAFE;
94+
gboolean res = FALSE;
95+
gboolean interrupted;
96+
97+
mono_thread_info_install_interrupt (cancel_w32_io, handle, &interrupted);
98+
if (!interrupted)
99+
{
100+
MONO_ENTER_GC_SAFE;
101+
res = ReadFile (handle, buffer, numbytes, bytesread, NULL);
102+
MONO_PROFILER_RAISE (fileio, (1, *bytesread));
103+
MONO_EXIT_GC_SAFE;
104+
105+
mono_thread_info_uninstall_interrupt (&interrupted);
106+
}
107+
93108
return res;
94109
}
95110

96111
gboolean
97112
mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
98113
{
99-
gboolean res;
100-
MONO_ENTER_GC_SAFE;
101-
res = WriteFile (handle, buffer, numbytes, byteswritten, NULL);
102-
MONO_PROFILER_RAISE (fileio, (0, *byteswritten));
103-
MONO_EXIT_GC_SAFE;
114+
gboolean res = FALSE;
115+
gboolean interrupted;
116+
117+
mono_thread_info_install_interrupt (cancel_w32_io, handle, &interrupted);
118+
if (!interrupted)
119+
{
120+
MONO_ENTER_GC_SAFE;
121+
res = WriteFile (handle, buffer, numbytes, byteswritten, NULL);
122+
MONO_PROFILER_RAISE (fileio, (0, *byteswritten));
123+
MONO_EXIT_GC_SAFE;
124+
125+
mono_thread_info_uninstall_interrupt (&interrupted);
126+
}
127+
104128
return res;
105129
}
106130

0 commit comments

Comments
 (0)