fix race conditions in slowticker... thanks to raldrich for finding one of them
[clinton/Smoothieware.git] / src / libs / SlowTicker.h
index 2e8204f..133dd78 100644 (file)
@@ -33,19 +33,25 @@ class SlowTicker : public Module{
         void set_frequency( int frequency );
         void tick();
         // For some reason this can't go in the .cpp, see :  http://mbed.org/forum/mbed/topic/2774/?page=1#comment-14221
+        // TODO replace this with std::function()
         template<typename T> Hook* attach( uint32_t frequency, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
             Hook* hook = new Hook();
             hook->interval = int(floor((SystemCoreClock/4)/frequency));
             hook->attach(optr, fptr);
             hook->countdown = hook->interval;
+
+            // to avoid race conditions we must stop the interupts before updating this non thread safe vector
+            __disable_irq();
             if( frequency > this->max_frequency ){
                 this->max_frequency = frequency;
                 this->set_frequency(frequency);
             }
             this->hooks.push_back(hook);
+            __enable_irq();
             return hook;
         }
 
+    private:
         bool flag_1s();
 
         vector<Hook*> hooks;