Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Commit

Permalink
Add lazy initialization to GpioController
Browse files Browse the repository at this point in the history
Signed-off-by: José Simões <jose.simoes@eclo.solutions>
  • Loading branch information
josesimoes committed Nov 9, 2018
1 parent d11a87e commit 92e4a4b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion source/Windows.Devices.Gpio/GpioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ namespace Windows.Devices.Gpio
/// <remarks>To get a <see cref="Gpio​Controller"/> object, use the <see cref="GetDefault"/> method.</remarks>
public sealed class Gpio​Controller : IGpioController
{
// this is used as the lock object
// a lock is required because multiple threads can access the I2C controller
readonly static object _syncLock = new object();

// we can have only one instance of the GpioController
private static GpioController s_instance = new GpioController();
// need to do a lazy initialization of this field to make sure it exists when called elsewhere.
private static GpioController s_instance;

/// <summary>
/// Gets the number of pins on the general-purpose I/O (GPIO) controller.
Expand Down Expand Up @@ -49,6 +54,14 @@ public extern int PinCount
/// <returns>The default GPIO controller for the system, or null if the system has no GPIO controller.</returns>
public static Gpio​Controller GetDefault()
{
if (s_instance == null)
{
lock (_syncLock)
{
s_instance = new Gpio​Controller();
}
}

return s_instance;
}

Expand Down

0 comments on commit 92e4a4b

Please sign in to comment.