Changeset 919

Show
Ignore:
Timestamp:
03/08/08 12:12:18 (13 years ago)
Author:
ppalmers
Message:

fix concurrency issue in cycle timer updater

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libffado/src/libieee1394/CycleTimerHelper.cpp

    r918 r919  
    3434// the high-bandwidth coefficients are used 
    3535// to speed up inital tracking 
    36 #define DLL_BANDWIDTH_HIGH (0.1
     36#define DLL_BANDWIDTH_HIGH (0.2
    3737#define DLL_OMEGA_HIGH     (2.0*DLL_PI*DLL_BANDWIDTH_HIGH) 
    3838#define DLL_COEFF_B_HIGH   (DLL_SQRT2 * DLL_OMEGA_HIGH) 
     
    380380    new_vars.usecs = (uint64_t)m_current_time_usecs; 
    381381    new_vars.rate = getRate(); 
    382      
     382 
    383383    // get the next index 
    384384    unsigned int next_idx = (m_current_shadow_idx + 1) % CTRHELPER_NB_SHADOW_VARS; 
    385      
    386     // check whether next index position is in use 
    387     while(m_shadow_usecount[next_idx] > 0) { 
    388         debugOutput(DEBUG_LEVEL_VERBOSE, 
    389                     "next shadow position still in use (pos: %d, %d uses)", 
    390                     next_idx, m_shadow_usecount[next_idx]); 
    391  
    392         // sleep for some time 
    393         m_TimeSource.SleepUsecRelative(100); 
    394     } 
    395     //debugOutputExtreme(DEBUG_LEVEL_VERBOSE, " updating %d\n", next_idx); 
    396  
    397     // the next index position is now unused, so we can update it 
     385 
     386    // update the next index position 
    398387    m_shadow_vars[next_idx] = new_vars; 
    399388 
    400389    // then we can update the current index 
    401390    m_current_shadow_idx = next_idx; 
    402     //debugOutputExtreme(DEBUG_LEVEL_VERBOSE, " updated %d\n", m_current_shadow_idx); 
    403      
    404     // reclaim is not necessary since it's a preallocated array 
    405      
     391 
    406392    return true; 
    407393} 
     
    420406    struct compute_vars my_vars; 
    421407 
    422     // get pointer and prevent it from disappearing 
    423     // by marking it as in-use 
    424     unsigned int curr_idx; 
    425     do { 
    426         // get the current shadow var set idx 
    427         curr_idx = m_current_shadow_idx; 
    428         // mark it as used 
    429         INC_ATOMIC(&m_shadow_usecount[curr_idx]); 
    430      
    431         // check whether the pointer changed between getting the idx 
    432         // and marking it as used 
    433         if (curr_idx != m_current_shadow_idx) { 
    434             debugOutput(DEBUG_LEVEL_VERBOSE, 
    435                         "pointer (%d) changed to (%d) while marking it as used\n", 
    436                         curr_idx, m_current_shadow_idx); 
    437             // undo in-use marking 
    438             DEC_ATOMIC(&m_shadow_usecount[curr_idx]); 
    439             // this requires a retry to obtain the new pointer 
    440         } 
    441     } while(curr_idx != m_current_shadow_idx); // retry if needed 
    442     //debugOutputExtreme(DEBUG_LEVEL_VERBOSE, " using %d\n",curr_idx); 
    443  
    444     // copy the contents 
     408    // get pointer and copy the contents 
     409    // no locking should be needed since we have more than one 
     410    // of these vars available, and the copy will always be finished before 
     411    // m_current_shadow_idx changes since this thread's priority should 
     412    // be higher than the one of the writer thread. Even if not, we only have to ensure 
     413    // that the used dataset is consistent. We can use an older dataset if it's consistent 
     414    // since it will also provide a fairly decent extrapolation. 
     415    unsigned int curr_idx = m_current_shadow_idx; // NOTE: this needs ordering 
    445416    my_vars = m_shadow_vars[curr_idx]; 
    446      
    447     // release the pointer 
    448     DEC_ATOMIC(&m_shadow_usecount[curr_idx]); 
    449      
     417 
    450418    int64_t time_diff = now - my_vars.usecs; 
    451419    double y_step_in_ticks = ((double)time_diff) * my_vars.rate; 
  • trunk/libffado/src/libieee1394/CycleTimerHelper.h

    r914 r919  
    134134    #define CTRHELPER_NB_SHADOW_VARS 8 
    135135    struct compute_vars m_shadow_vars[CTRHELPER_NB_SHADOW_VARS]; 
    136     SInt32 m_shadow_usecount[CTRHELPER_NB_SHADOW_VARS]; 
    137     unsigned int m_current_shadow_idx; 
     136    volatile unsigned int m_current_shadow_idx; 
    138137 
    139138    // Threading 
  • trunk/libffado/tests/test-ieee1394service.cpp

    r917 r919  
    4747 
    4848 
    49 #define NB_THREADS 10 
     49#define NB_THREADS 2 
    5050#define THREAD_RT  true 
    5151#define THREAD_PRIO 51 
     
    5959 
    6060#define DIFF_CONSIDERED_LARGE (TICKS_PER_CYCLE/2) 
    61 int PORT_TO_USE = 0
     61int PORT_TO_USE = 1
    6262 
    6363int max_diff=-99999;