Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Feb 18th, 2013 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // usp_threadpooltimer.h
  2.  
  3.  
  4. #pragma once
  5. #include "usp_config.h"
  6.  
  7. #include "usp_windowsh.h"
  8. #include "usp_callwrap.h"
  9. #include "usp_rwspinlock.h"
  10.  
  11. class ThreadpoolTimerVista
  12. {
  13. };
  14.  
  15. class ThreadPoolTimerXP
  16. {
  17.   HANDLE timerQueueTimer;
  18.   typedef std::function<void(BOOL)> TargetFunction;
  19.   TargetFunction target;
  20.   RWSpinLock lock;
  21.  
  22. public:
  23.   ThreadPoolTimerXP()
  24.     : timerQueueTimer(0)
  25.   {
  26.   }
  27.  
  28.   ~ThreadPoolTimerXP()
  29.   {
  30.     StopTimer();
  31.   }
  32.  
  33.   template<typename T>
  34.   void SetCallback(T *targetObject, void (T::*member)(BOOLEAN))
  35.   {
  36.     target = TargetFunction(
  37.     [=](BOOLEAN fired)
  38.     {
  39.       (targetObject->*member)(fired);
  40.     });
  41.   }
  42.  
  43.   static void NTAPI TimerCallback(void *parameter, BOOLEAN timerOrWaitFired)
  44.   {
  45.     auto self = reinterpret_cast<ThreadPoolTimerXP*>(parameter);
  46.  
  47.     // Attempt to acquire a shared lock
  48.     auto hold(self->lock.TryHoldShared());
  49.  
  50.     // If it was acquired, invoke the callback, else drop the callback
  51.     // because we must be destructing this object
  52.     if (hold.IsHeld())
  53.       self->target(timerOrWaitFired);
  54.   }
  55.  
  56.   BOOL SetTimer(DWORD dueTime, DWORD period)
  57.   {
  58.     if (timerQueueTimer)
  59.     {
  60.       // Change existing timer
  61.       auto ok = ChangeTimerQueueTimer(NULL, timerQueueTimer,
  62.           dueTime, period);
  63.       return ok;
  64.     }
  65.     else
  66.     {
  67.       // Create timer
  68.       auto ok = CreateTimerQueueTimer(&timerQueueTimer, NULL,
  69.           TimerCallback, this,
  70.           dueTime, period, WT_EXECUTEDEFAULT);
  71.       return ok;
  72.     }
  73.   }
  74.  
  75.   void StopTimer()
  76.   {
  77.     auto curTimer = Interlocked::Xchg(&timerQueueTimer, (HANDLE)0);
  78.     if (curTimer)
  79.       DeleteTimerQueueTimer(NULL, curTimer, INVALID_HANDLE_VALUE);
  80.   }
  81. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top