GateKeeper.cpp
changeset 284 83ad8d765099
parent 279 79c90923f384
child 299 3f5f25280e32
     1.1 --- a/GateKeeper.cpp	Sat Apr 28 18:45:16 2018 +0200
     1.2 +++ b/GateKeeper.cpp	Sat Apr 28 19:55:17 2018 +0200
     1.3 @@ -109,6 +109,9 @@
     1.4  }
     1.5  
     1.6  namespace pEp {
     1.7 +    std::mutex GateKeeper::update_wait_mtx;
     1.8 +    std::condition_variable GateKeeper::update_wait_var;
     1.9 +    bool GateKeeper::update_wait_forced = false;
    1.10  
    1.11      const LPCTSTR GateKeeper::plugin_reg_path = _T("Software\\Microsoft\\Office\\Outlook\\Addins\\pEp");
    1.12      const LPCTSTR GateKeeper::plugin_reg_value_name = _T("LoadBehavior");
    1.13 @@ -116,7 +119,7 @@
    1.14  
    1.15      const time_t GateKeeper::cycle = 7200;   // 7200 sec is 2 h
    1.16      const time_t GateKeeper::fraction = 10;  // first update is at 10% of cycle
    1.17 -    const DWORD GateKeeper::waiting = 10000; // 10000 ms is 10 sec
    1.18 +    const chrono::seconds GateKeeper::waiting = 10s; //  10 sec
    1.19  
    1.20      GateKeeper::GateKeeper(CpEpCOMServerAdapterModule * self)
    1.21          : _self(self), now(time(NULL)), next(now /*+ time_diff()*/), hkUpdater(NULL),
    1.22 @@ -174,13 +177,28 @@
    1.23              now = time(NULL);
    1.24              assert(now != -1);
    1.25  
    1.26 -            if (now > next) {
    1.27 +            bool force_check;
    1.28 +            // We need to sleep, but we should be interruptible by the update_now() method.
    1.29 +            {
    1.30 +                std::unique_lock<std::mutex> guard(GateKeeper::update_wait_mtx);
    1.31 +                GateKeeper::update_wait_var.wait_for(guard, waiting);
    1.32 +                force_check = GateKeeper::update_wait_forced;
    1.33 +                GateKeeper::update_wait_forced = false;
    1.34 +            }
    1.35 +
    1.36 +            if (force_check || now > next) {
    1.37                  next = now + GateKeeper::cycle;
    1.38                  keep_updated();
    1.39              }
    1.40 +        }
    1.41 +    }
    1.42  
    1.43 -            Sleep(waiting);
    1.44 -        }
    1.45 +    void GateKeeper::update_now() 
    1.46 +    {
    1.47 +        // Signal the GateKeeper thread that we need to check for updates now.
    1.48 +        std::unique_lock<std::mutex> guard(GateKeeper::update_wait_mtx);
    1.49 +        GateKeeper::update_wait_forced = true;
    1.50 +        GateKeeper::update_wait_var.notify_all();
    1.51      }
    1.52  
    1.53      void GateKeeper::keep_plugin()