1 // System/System.IOSelector.cs
4 // Ludovic Henry <ludovic@xamarin.com>
6 // Copyright (C) 2015 Xamarin, Inc. (https://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Runtime.CompilerServices;
30 using System.Runtime.InteropServices;
31 using System.Runtime.Remoting.Messaging;
32 using System.Threading;
36 internal enum IOOperation : int
38 /* Keep in sync with MonoIOOperation in mono/metadata/threadpool-ms-io.c */
44 internal delegate void IOAsyncCallback (IOAsyncResult ioares);
46 [StructLayout (LayoutKind.Sequential)]
47 internal abstract class IOAsyncResult : IAsyncResult
49 AsyncCallback async_callback;
52 ManualResetEvent wait_handle;
53 bool completed_synchronously;
56 protected IOAsyncResult ()
60 protected void Init (AsyncCallback async_callback, object async_state)
62 this.async_callback = async_callback;
63 this.async_state = async_state;
66 completed_synchronously = false;
68 if (wait_handle != null)
72 protected IOAsyncResult (AsyncCallback async_callback, object async_state)
74 this.async_callback = async_callback;
75 this.async_state = async_state;
78 public AsyncCallback AsyncCallback
80 get { return async_callback; }
83 public object AsyncState
85 get { return async_state; }
88 public WaitHandle AsyncWaitHandle
92 if (wait_handle == null)
93 wait_handle = new ManualResetEvent (completed);
99 public bool CompletedSynchronously
102 return completed_synchronously;
105 completed_synchronously = value;
109 public bool IsCompleted
117 if (value && wait_handle != null)
123 internal abstract void CompleteDisposed();
126 [StructLayout (LayoutKind.Sequential)]
127 internal class IOSelectorJob : IThreadPoolWorkItem
129 /* Keep in sync with MonoIOSelectorJob in mono/metadata/threadpool-ms-io.c */
130 IOOperation operation;
131 IOAsyncCallback callback;
134 public IOSelectorJob (IOOperation operation, IOAsyncCallback callback, IOAsyncResult state)
136 this.operation = operation;
137 this.callback = callback;
141 void IThreadPoolWorkItem.ExecuteWorkItem ()
143 this.callback (this.state);
146 void IThreadPoolWorkItem.MarkAborted (ThreadAbortException tae)
150 public void MarkDisposed ()
152 state.CompleteDisposed ();
156 internal static class IOSelector
158 [MethodImplAttribute(MethodImplOptions.InternalCall)]
159 public static extern void Add (IntPtr handle, IOSelectorJob job);
161 [MethodImplAttribute(MethodImplOptions.InternalCall)]
162 public static extern void Remove (IntPtr handle);