Skip to content

Commit 3deacb2

Browse files
lateralusXUnityAlex
authored andcommitted
Windows support to abort blocking system calls. (mono#12654)
* Fix race condtion in socket altertable wait implementation on Windows. * Add support to abort blocking Windows mono_w32file_read/write methods.
1 parent b04ac91 commit 3deacb2

File tree

7 files changed

+321
-373
lines changed

7 files changed

+321
-373
lines changed

mono/metadata/w32file-win32.c

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,49 +92,46 @@ cancel_w32_io (HANDLE file_handle)
9292
gboolean
9393
mono_w32file_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
9494
{
95-
gboolean res = FALSE;
95+
9696
gboolean interrupted;
97+
guint32 last_error;
98+
gboolean res;
99+
100+
MonoThreadInfo *info = mono_thread_info_current ();
101+
102+
mono_win32_enter_blocking_io_call (info, (HANDLE)handle);
103+
MONO_ENTER_GC_SAFE;
104+
res = ReadFile ((HANDLE)handle, buffer, numbytes, (PDWORD)bytesread, NULL);
105+
/* need to save and restore since clients expect error code set for
106+
* failed IO calls and mono_thread_info_uninstall_interrupt overwrites value */
107+
last_error = mono_w32error_get_last ();
108+
109+
MONO_EXIT_GC_SAFE;
110+
mono_win32_leave_blocking_io_call (info, (HANDLE)handle);
111+
mono_w32error_set_last (last_error);
97112

98-
mono_thread_info_install_interrupt (cancel_w32_io, handle, &interrupted);
99-
if (!interrupted)
100-
{
101-
guint32 last_error;
102-
MONO_ENTER_GC_SAFE;
103-
res = ReadFile (handle, buffer, numbytes, bytesread, NULL);
104-
MONO_PROFILER_RAISE (fileio, (1, *bytesread));
105-
MONO_EXIT_GC_SAFE;
106-
107-
/* need to save and restore since clients expect error code set for
108-
* failed IO calls and mono_thread_info_uninstall_interrupt overwrites value */
109-
last_error = mono_w32error_get_last ();
110-
mono_thread_info_uninstall_interrupt (&interrupted);
111-
mono_w32error_set_last (last_error);
112-
}
113113

114114
return res;
115115
}
116116

117117
gboolean
118118
mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
119119
{
120-
gboolean res = FALSE;
121120
gboolean interrupted;
121+
guint32 last_error;
122+
gboolean res;
123+
124+
MonoThreadInfo *info = mono_thread_info_current ();
122125

123-
mono_thread_info_install_interrupt (cancel_w32_io, handle, &interrupted);
124-
if (!interrupted)
125-
{
126-
guint32 last_error;
127-
MONO_ENTER_GC_SAFE;
128-
res = WriteFile (handle, buffer, numbytes, byteswritten, NULL);
129-
MONO_PROFILER_RAISE (fileio, (0, *byteswritten));
130-
MONO_EXIT_GC_SAFE;
131-
132-
/* need to save and restore since clients expect error code set for
133-
* failed IO calls and mono_thread_info_uninstall_interrupt overwrites value */
134-
last_error = mono_w32error_get_last ();
135-
mono_thread_info_uninstall_interrupt (&interrupted);
136-
mono_w32error_set_last (last_error);
137-
}
126+
mono_win32_enter_blocking_io_call (info, (HANDLE)handle);
127+
MONO_ENTER_GC_SAFE;
128+
res = WriteFile ((HANDLE)handle, buffer, numbytes, (PDWORD)byteswritten, NULL);
129+
/* need to save and restore since clients expect error code set for
130+
* failed IO calls and mono_thread_info_uninstall_interrupt overwrites value */
131+
last_error = mono_w32error_get_last ();
132+
MONO_EXIT_GC_SAFE;
133+
mono_win32_leave_blocking_io_call (info, (HANDLE)handle);
134+
mono_w32error_set_last (last_error);
138135

139136
return res;
140137
}

0 commit comments

Comments
 (0)