unsafe public MockLibuv()
        {
            _uv_write = UvWrite;

            _uv_async_send = postHandle =>
            {
                _loopWh.Set();

                return 0;
            };

            _uv_async_init = (loop, postHandle, callback) =>
            {
                _postHandle = postHandle;
                _onPost = callback;

                return 0;
            };

            _uv_run = (loopHandle, mode) =>
            {
                while (!_stopLoop)
                {
                    _loopWh.Wait();
                    _loopWh.Reset();
                    _onPost(_postHandle.InternalGetHandle());
                }

                return 0;
            };

            _uv_ref = handle => { };
            _uv_unref = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_stop = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_req_size = reqType => IntPtr.Size;
            _uv_loop_size = () => IntPtr.Size;
            _uv_handle_size = handleType => IntPtr.Size;
            _uv_loop_init = loop => 0;
            _uv_tcp_init = (loopHandle, tcpHandle) => 0;
            _uv_close = (handle, callback) => callback(handle);
            _uv_loop_close = handle => 0;
            _uv_walk = (loop, callback, ignore) => 0;
        }
Exemple #2
0
 public void async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb)
 {
     loop.Validate();
     handle.Validate();
     Check(_uv_async_init(loop, handle, cb));
 }
Exemple #3
0
 internal static extern int uv_async_init(EventLooper looper, Async handle, uv_async_cb callback);
Exemple #4
0
 public void async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb)
 {
     loop.Validate();
     handle.Validate();
     Check(_uv_async_init(loop, handle, cb));
 }
 public static extern int uv_async_init(UvLoopHandle loop,UvAsyncHandle handle,uv_async_cb cb);
 public static extern int uv_async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb);
 public void async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb)
 {
     loop.Validate();
     handle.Validate();
     ThrowIfErrored(_uv_async_init(loop, handle, cb));
 }
Exemple #8
0
        public unsafe MockLibuv()
            : base(onlyForTesting: true)
        {
            _stackTrace = Environment.StackTrace;

            OnWrite = (socket, buffers, triggerCompleted) =>
            {
                triggerCompleted(0);
                return(0);
            };

            _uv_write = UvWrite;

            _uv_async_send = postHandle =>
            {
                lock (_postLock)
                {
                    if (_completedOnPostTcs)
                    {
                        _onPostTcs          = new TaskCompletionSource();
                        _completedOnPostTcs = false;
                    }

                    PostCount++;

                    _loopWh.Set();
                }

                return(0);
            };

            _uv_async_init = (loop, postHandle, callback) =>
            {
                _postHandle = postHandle;
                _onPost     = callback;

                return(0);
            };

            _uv_run = (loopHandle, mode) =>
            {
                while (!_stopLoop)
                {
                    _loopWh.Wait();
                    KestrelThreadBlocker.Wait();

                    lock (_postLock)
                    {
                        _loopWh.Reset();
                    }

                    _onPost(_postHandle.InternalGetHandle());

                    lock (_postLock)
                    {
                        // Allow the loop to be run again before completing
                        // _onPostTcs given a nested uv_async_send call.
                        if (!_loopWh.IsSet)
                        {
                            // Ensure any subsequent calls to uv_async_send
                            // create a new _onPostTcs to be completed.
                            _completedOnPostTcs = true;

                            // Calling TrySetResult outside the lock to avoid deadlock
                            // when the code attempts to call uv_async_send after awaiting
                            // OnPostTask. Task.Run so the run loop doesn't block either.
                            var onPostTcs = _onPostTcs;
                            Task.Run(() => onPostTcs.TrySetResult());
                        }
                    }
                }

                return(0);
            };

            _uv_ref   = handle => { };
            _uv_unref = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_stop = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_req_size    = reqType => IntPtr.Size;
            _uv_loop_size   = () => IntPtr.Size;
            _uv_handle_size = handleType => IntPtr.Size;
            _uv_loop_init   = loop => 0;
            _uv_tcp_init    = (loopHandle, tcpHandle) => 0;
            _uv_close       = (handle, callback) => callback(handle);
            _uv_loop_close  = handle => 0;
            _uv_walk        = (loop, callback, ignore) => 0;
            _uv_err_name    = errno => IntPtr.Zero;
            _uv_strerror    = errno => IntPtr.Zero;
            _uv_read_start  = UvReadStart;
            _uv_read_stop   = (handle) =>
            {
                AllocCallback = null;
                ReadCallback  = null;
                return(0);
            };
            _uv_unsafe_async_send = handle =>
            {
                throw new Exception($"Why is this getting called?{Environment.NewLine}{_stackTrace}");
            };

            _uv_timer_init  = (loop, handle) => 0;
            _uv_timer_start = (handle, callback, timeout, repeat) => 0;
            _uv_timer_stop  = handle => 0;
            _uv_now         = (loop) => DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
        }
Exemple #9
0
 public void async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb)
 {
     loop.Validate();
     handle.Validate();
     ThrowIfErrored(_uv_async_init(loop, handle, cb));
 }
Exemple #10
0
        unsafe public MockLibuv()
            : base(onlyForTesting: true)
        {
            _stackTrace = Environment.StackTrace;

            OnWrite = (socket, buffers, triggerCompleted) =>
            {
                triggerCompleted(0);
                return(0);
            };

            _uv_write = UvWrite;

            _uv_async_send = postHandle =>
            {
                // Attempt to run the async send logic inline; this should succeed most of the time.
                // In the rare cases where it fails to acquire the lock, use Task.Run() so this call
                // never blocks, since the real libuv never blocks.
                if (Monitor.TryEnter(_postLock))
                {
                    try
                    {
                        UvAsyncSend();
                    }
                    finally
                    {
                        Monitor.Exit(_postLock);
                    }
                }
                else
                {
                    Task.Run(() =>
                    {
                        lock (_postLock)
                        {
                            UvAsyncSend();
                        }
                    });
                }

                return(0);
            };

            _uv_async_init = (loop, postHandle, callback) =>
            {
                _postHandle = postHandle;
                _onPost     = callback;

                return(0);
            };

            _uv_run = (loopHandle, mode) =>
            {
                while (!_stopLoop)
                {
                    _loopWh.Wait();
                    KestrelThreadBlocker.Wait();

                    lock (_postLock)
                    {
                        _sendCalled = false;
                        _loopWh.Reset();
                        _onPost(_postHandle.InternalGetHandle());

                        // Allow the loop to be run again before completing
                        // _onPostTcs given a nested uv_async_send call.
                        if (!_sendCalled)
                        {
                            // Ensure any subsequent calls to uv_async_send
                            // create a new _onPostTcs to be completed.
                            _completedOnPostTcs = true;

                            // Calling TrySetResult outside the lock to avoid deadlock
                            // when the code attempts to call uv_async_send after awaiting
                            // OnPostTask. Task.Run so the run loop doesn't block either.
                            var onPostTcs = _onPostTcs;
                            Task.Run(() => onPostTcs.TrySetResult(null));
                        }
                    }
                }

                return(0);
            };

            _uv_ref   = handle => { };
            _uv_unref = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_stop = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_req_size    = reqType => IntPtr.Size;
            _uv_loop_size   = () => IntPtr.Size;
            _uv_handle_size = handleType => IntPtr.Size;
            _uv_loop_init   = loop => 0;
            _uv_tcp_init    = (loopHandle, tcpHandle) => 0;
            _uv_close       = (handle, callback) => callback(handle);
            _uv_loop_close  = handle => 0;
            _uv_walk        = (loop, callback, ignore) => 0;
            _uv_err_name    = errno => IntPtr.Zero;
            _uv_strerror    = errno => IntPtr.Zero;
            _uv_read_start  = UvReadStart;
            _uv_read_stop   = (handle) =>
            {
                AllocCallback = null;
                ReadCallback  = null;
                return(0);
            };
            _uv_unsafe_async_send = handle =>
            {
                throw new Exception($"Why is this getting called?{Environment.NewLine}{_stackTrace}");
            };

            _uv_timer_init  = (loop, handle) => 0;
            _uv_timer_start = (handle, callback, timeout, repeat) => 0;
            _uv_timer_stop  = handle => 0;
            _uv_now         = (loop) => DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
        }
        unsafe public MockLibuv()
            : base(onlyForTesting: true)
        {
            OnWrite = (socket, buffers, triggerCompleted) =>
            {
                triggerCompleted(0);
                return(0);
            };

            _uv_write = UvWrite;

            _uv_async_send = postHandle =>
            {
                PostCount++;
                _loopWh.Set();

                return(0);
            };

            _uv_async_init = (loop, postHandle, callback) =>
            {
                _postHandle = postHandle;
                _onPost     = callback;

                return(0);
            };

            _uv_run = (loopHandle, mode) =>
            {
                while (!_stopLoop)
                {
                    _loopWh.Wait();
                    _loopWh.Reset();
                    _onPost(_postHandle.InternalGetHandle());
                }

                return(0);
            };

            _uv_ref   = handle => { };
            _uv_unref = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_stop = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_req_size    = reqType => IntPtr.Size;
            _uv_loop_size   = () => IntPtr.Size;
            _uv_handle_size = handleType => IntPtr.Size;
            _uv_loop_init   = loop => 0;
            _uv_tcp_init    = (loopHandle, tcpHandle) => 0;
            _uv_close       = (handle, callback) => callback(handle);
            _uv_loop_close  = handle => 0;
            _uv_walk        = (loop, callback, ignore) => 0;
            _uv_err_name    = errno => IntPtr.Zero;
            _uv_strerror    = errno => IntPtr.Zero;
            _uv_read_start  = UvReadStart;
            _uv_read_stop   = handle => 0;
        }