未验证 提交 4a8a4d90 编写于 作者: T Thomas Ibel 提交者: GitHub

retry when epoll_wait fails with EINTR (#1801)

* retry when epoll_wait fails with EINTR

* add error code to exception message

* retry on EINTR for first time call too
上级 c978c28d
......@@ -8,6 +8,6 @@ using System.Runtime.InteropServices;
internal partial class Interop
{
[DllImport(LibcLibrary)]
[DllImport(LibcLibrary, SetLastError = true)]
internal static extern int epoll_wait(int epfd, out epoll_event events, int maxevents, int timeout);
}
......@@ -6,6 +6,7 @@ using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
namespace System.Device.Gpio.Drivers
......@@ -15,6 +16,8 @@ namespace System.Device.Gpio.Drivers
/// </summary>
public class SysFsDriver : UnixDriver
{
private const int ERROR_CODE_EINTR = 4; // Interrupted system call
private const string GpioBasePath = "/sys/class/gpio";
private const string GpioChip = "gpiochip";
private const string GpioLabel = "/label";
......@@ -442,7 +445,15 @@ namespace System.Device.Gpio.Drivers
}
// Ignore first time because it will always return the current state.
Interop.epoll_wait(pollFileDescriptor, out _, 1, 0);
while (Interop.epoll_wait(pollFileDescriptor, out _, 1, 0) == -1)
{
var errorCode = Marshal.GetLastWin32Error();
if (errorCode != ERROR_CODE_EINTR)
{
// don't retry on unknown error
break;
}
}
}
private unsafe bool WasEventDetected(int pollFileDescriptor, int valueFileDescriptor, out int pinNumber, CancellationToken cancellationToken)
......@@ -457,7 +468,14 @@ namespace System.Device.Gpio.Drivers
int waitResult = Interop.epoll_wait(pollFileDescriptor, out epoll_event events, 1, PollingTimeout);
if (waitResult == -1)
{
throw new IOException("Error while waiting for pin interrupts.");
var errorCode = Marshal.GetLastWin32Error();
if (errorCode == ERROR_CODE_EINTR)
{
// ignore Interrupted system call error and retry
continue;
}
throw new IOException($"Error while waiting for pin interrupts. (ErrorCode={errorCode})");
}
if (waitResult > 0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册