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 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Nov 9, 2018
1 parent d11a87e commit 3b9719b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 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,17 @@ 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)
{
if (s_instance == null)
{
s_instance = new Gpio​Controller();
}
}
}

return s_instance;
}

Expand Down

0 comments on commit 3b9719b

Please sign in to comment.