Skip to content

Commit

Permalink
Adjust to changed directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrawehr committed Apr 27, 2024
1 parent 1f4b066 commit cd8331f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 593 deletions.
107 changes: 0 additions & 107 deletions src/System.Device.Gpio.Tests/LibGpiodDriverTests.cs

This file was deleted.

34 changes: 33 additions & 1 deletion src/System.Device.Gpio.Tests/LibGpiodV1DriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,36 @@ static void PinChanged(object sender, PinValueChangedEventArgs args)
gc.UnregisterCallbackForPinValueChangedEvent(InputPin, PinChanged);
}
}
}

/// <summary>
/// Ensure leaking instances of the driver doesn't cause a segfault
/// Regression test for https://github.com/dotnet/iot/issues/1849
/// </summary>
[Fact]
public void LeakingDriverDoesNotCrash()
{
GpioController controller1 = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver());
controller1.OpenPin(10, PinMode.Output);
GpioController controller2 = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver());
controller2.OpenPin(11, PinMode.Output);
GpioController controller3 = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver());
controller3.OpenPin(12, PinMode.Output);
GpioController controller4 = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver());
controller4.OpenPin(13, PinMode.Output);
GpioController controller5 = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver());
controller5.OpenPin(14, PinMode.Output);

for (int i = 0; i < 10; i++)
{
GC.Collect();
GpioController controller6 = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver());
controller6.OpenPin(15, PinMode.Output);
controller6.ClosePin(15);
controller6.Dispose();
GC.Collect();
Thread.Sleep(20);
}

GC.WaitForPendingFinalizers();
}
}
63 changes: 0 additions & 63 deletions src/System.Device.Gpio/Interop/Unix/SafeLineHandle.cs

This file was deleted.

46 changes: 34 additions & 12 deletions src/System.Device.Gpio/Interop/Unix/libgpiod/V1/SafeLineHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,53 @@
namespace System.Device.Gpio.Libgpiod.V1;

/// <summary>
/// Pointer to a pin.
/// Pointer to a pin (Not a real SafeLineHandle, because we need to align its finalization with the owning object)
/// </summary>
internal class SafeLineHandle : SafeHandle
internal sealed class SafeLineHandle : IDisposable
{
public PinMode PinMode { get; set; }

private IntPtr _handle;
public SafeLineHandle()
: base(IntPtr.Zero, true)
{
_handle = IntPtr.Zero;
}

protected override bool ReleaseHandle()
public SafeLineHandle(IntPtr handle)
{
// Contrary to intuition, this does not invalidate the handle (see comment on declaration)
LibgpiodV1.gpiod_line_release(handle);
return true;
_handle = handle;
PinMode = PinMode.Input;
}

public PinMode PinMode { get; set; }

public IntPtr Handle
{
get
{
return _handle;
}
set
{
_handle = value;
}
}

/// <summary>
/// Release the lock on the line handle. <see cref="LibgpiodV1.gpiod_line_release"/>
/// Release the lock on the line handle. <see cref="Interop.libgpiod.gpiod_line_release"/>
/// </summary>
public void ReleaseLock()
{
ReleaseHandle();
// Contrary to intuition, this does not invalidate the handle (see comment on declaration)
Interop.libgpiod.gpiod_line_release(_handle);
}

public override bool IsInvalid => handle == IntPtr.Zero || handle == LibgpiodV1.InvalidHandleValue;
public bool IsInvalid => _handle == IntPtr.Zero || _handle == Interop.libgpiod.InvalidHandleValue;

public void Dispose()
{
if (_handle != IntPtr.Zero)
{
Interop.libgpiod.gpiod_line_release(_handle);
_handle = IntPtr.Zero;
}
}
}
Loading

0 comments on commit cd8331f

Please sign in to comment.