| 
 | 1 | +using UnitsNet;  | 
 | 2 | +using WPIHal.Handles;  | 
 | 3 | +using WPIHal.Natives;  | 
 | 4 | +using WPIUtil;  | 
 | 5 | + | 
 | 6 | +namespace WPILib;  | 
 | 7 | + | 
 | 8 | +public class Notifier : IDisposable  | 
 | 9 | +{  | 
 | 10 | +    private Thread m_thread;  | 
 | 11 | + | 
 | 12 | +    private readonly object m_processLock = new();  | 
 | 13 | + | 
 | 14 | +    private int m_notifier;  | 
 | 15 | + | 
 | 16 | +    private Action m_callback;  | 
 | 17 | + | 
 | 18 | +    private Duration m_expirationTime;  | 
 | 19 | + | 
 | 20 | +    private Duration m_period;  | 
 | 21 | + | 
 | 22 | +    private bool m_periodic;  | 
 | 23 | + | 
 | 24 | +    public void Dispose()  | 
 | 25 | +    {  | 
 | 26 | +        GC.SuppressFinalize(this);  | 
 | 27 | +        HalNotifierHandle handle = new(Interlocked.Exchange(ref m_notifier, 0));  | 
 | 28 | +        if (handle.Handle == 0)  | 
 | 29 | +        {  | 
 | 30 | +            return;  | 
 | 31 | +        }  | 
 | 32 | + | 
 | 33 | +        HalNotifier.StopNotifier(handle);  | 
 | 34 | +        if (m_thread.IsAlive == true)  | 
 | 35 | +        {  | 
 | 36 | +            m_thread.Interrupt();  | 
 | 37 | +            m_thread.Join();  | 
 | 38 | +        }  | 
 | 39 | +        HalNotifier.CleanNotifier(handle);  | 
 | 40 | +    }  | 
 | 41 | + | 
 | 42 | +    private void UpdateAlarm(long triggerTime)  | 
 | 43 | +    {  | 
 | 44 | +        HalNotifierHandle handle = new(Interlocked.CompareExchange(ref m_notifier, 0, 0));  | 
 | 45 | +        if (handle.Handle == 0)  | 
 | 46 | +        {  | 
 | 47 | +            return;  | 
 | 48 | +        }  | 
 | 49 | + | 
 | 50 | +        HalNotifier.UpdateNotifierAlarm(handle, (uint)triggerTime);  | 
 | 51 | +    }  | 
 | 52 | + | 
 | 53 | +    private void UpdateAlarm()  | 
 | 54 | +    {  | 
 | 55 | +        UpdateAlarm((long)m_expirationTime.Microseconds);  | 
 | 56 | +    }  | 
 | 57 | + | 
 | 58 | +    public Notifier(Action callback, string name = "Notifier")  | 
 | 59 | +    {  | 
 | 60 | +        m_callback = WpiGuard.RequireNotNull(callback, nameof(callback));  | 
 | 61 | + | 
 | 62 | +        var notifier = HalNotifier.InitializeNotifier();  | 
 | 63 | + | 
 | 64 | +        m_notifier = notifier.Handle;  | 
 | 65 | +        HalNotifier.SetNotifierName(notifier, name);  | 
 | 66 | + | 
 | 67 | +        m_thread = new(() =>  | 
 | 68 | +        {  | 
 | 69 | +            while (true)  | 
 | 70 | +            {  | 
 | 71 | +                HalNotifierHandle handle = new(Interlocked.CompareExchange(ref m_notifier, 0, 0));  | 
 | 72 | +                if (handle.Handle == 0)  | 
 | 73 | +                {  | 
 | 74 | +                    break;  | 
 | 75 | +                }  | 
 | 76 | +                ulong curTime = HalNotifier.WaitForNotifierAlarm(handle);  | 
 | 77 | +                if (curTime == 0)  | 
 | 78 | +                {  | 
 | 79 | +                    break;  | 
 | 80 | +                }  | 
 | 81 | + | 
 | 82 | +                Action? threadHandler;  | 
 | 83 | +                lock (m_processLock)  | 
 | 84 | +                {  | 
 | 85 | +                    threadHandler = m_callback;  | 
 | 86 | +                    if (m_periodic)  | 
 | 87 | +                    {  | 
 | 88 | +                        m_expirationTime += m_period;  | 
 | 89 | +                        UpdateAlarm();  | 
 | 90 | +                    }  | 
 | 91 | +                    else  | 
 | 92 | +                    {  | 
 | 93 | +                        UpdateAlarm(-1);  | 
 | 94 | +                    }  | 
 | 95 | +                }  | 
 | 96 | + | 
 | 97 | +                threadHandler?.Invoke();  | 
 | 98 | +            }  | 
 | 99 | +        })  | 
 | 100 | +        {  | 
 | 101 | +            Name = name,  | 
 | 102 | +            IsBackground = true,  | 
 | 103 | +        };  | 
 | 104 | +        m_thread.Start();  | 
 | 105 | +    }  | 
 | 106 | + | 
 | 107 | +    public void SetCallback(Action callback)  | 
 | 108 | +    {  | 
 | 109 | +        lock (m_processLock)  | 
 | 110 | +        {  | 
 | 111 | +            m_callback = WpiGuard.RequireNotNull(callback, nameof(callback));  | 
 | 112 | +        }  | 
 | 113 | +    }  | 
 | 114 | + | 
 | 115 | +    public void StartSingle(Duration delay)  | 
 | 116 | +    {  | 
 | 117 | +        lock (m_processLock)  | 
 | 118 | +        {  | 
 | 119 | +            m_periodic = false;  | 
 | 120 | +            m_period = delay;  | 
 | 121 | +            m_expirationTime = Timer.FPGATimestamp + delay;  | 
 | 122 | +            UpdateAlarm();  | 
 | 123 | +        }  | 
 | 124 | +    }  | 
 | 125 | + | 
 | 126 | +    public void StartPeriodic(Duration period)  | 
 | 127 | +    {  | 
 | 128 | +        lock (m_processLock)  | 
 | 129 | +        {  | 
 | 130 | +            m_periodic = true;  | 
 | 131 | +            m_period = period;  | 
 | 132 | +            m_expirationTime = Timer.FPGATimestamp + period;  | 
 | 133 | +            UpdateAlarm();  | 
 | 134 | +        }  | 
 | 135 | +    }  | 
 | 136 | + | 
 | 137 | +    public void Stop()  | 
 | 138 | +    {  | 
 | 139 | +        lock (m_processLock)  | 
 | 140 | +        {  | 
 | 141 | +            m_periodic = false;  | 
 | 142 | +            HalNotifier.CancelNotifierAlarm(new(Interlocked.CompareExchange(ref m_notifier, 0, 0)));  | 
 | 143 | +        }  | 
 | 144 | +    }  | 
 | 145 | + | 
 | 146 | +    public static bool SetHalThreadPriority(bool realTime, int priority)  | 
 | 147 | +    {  | 
 | 148 | +        return HalNotifier.SetNotifierThreadPriority(realTime, priority);  | 
 | 149 | +    }  | 
 | 150 | +}  | 
0 commit comments