Beispiel #1
0
TEST(TestMultipleSharedSection, General)
{
    CSharedSection sec;

    CEvent event;
    std::atomic<long> mutex(0L);

    locker<CSharedLock> l1(sec,&mutex, &event);

    {
        CSharedLock lock(sec);
        thread waitThread1(l1);

        EXPECT_TRUE(waitForThread(mutex,1,10000));
        SleepMillis(10);

        EXPECT_TRUE(l1.haslock);

        event.Set();

        EXPECT_TRUE(waitThread1.timed_join(MILLIS(10000)));
    }

    locker<CSharedLock> l2(sec,&mutex,&event);
    locker<CSharedLock> l3(sec,&mutex,&event);
    locker<CSharedLock> l4(sec,&mutex,&event);
    locker<CSharedLock> l5(sec,&mutex,&event);
    {
        CExclusiveLock lock(sec);
        thread waitThread1(l2);
        thread waitThread2(l3);
        thread waitThread3(l4);
        thread waitThread4(l5);

        EXPECT_TRUE(waitForThread(mutex,4,10000));
        SleepMillis(10);

        EXPECT_TRUE(!l2.haslock);
        EXPECT_TRUE(!l3.haslock);
        EXPECT_TRUE(!l4.haslock);
        EXPECT_TRUE(!l5.haslock);

        lock.Leave();

        EXPECT_TRUE(waitForWaiters(event,4,10000));

        EXPECT_TRUE(l2.haslock);
        EXPECT_TRUE(l3.haslock);
        EXPECT_TRUE(l4.haslock);
        EXPECT_TRUE(l5.haslock);

        event.Set();

        EXPECT_TRUE(waitThread1.timed_join(MILLIS(10000)));
        EXPECT_TRUE(waitThread2.timed_join(MILLIS(10000)));
        EXPECT_TRUE(waitThread3.timed_join(MILLIS(10000)));
        EXPECT_TRUE(waitThread4.timed_join(MILLIS(10000)));
    }
}
Beispiel #2
0
TEST(TestSharedSection, GetSharedLockWhileTryingExclusiveLock)
{
    std::atomic<long> mutex(0L);
    CEvent event;

    CSharedSection sec;

    CSharedLock l1(sec); // get a shared lock

    locker<CExclusiveLock> l2(sec,&mutex);
    thread waitThread1(l2); // try to get an exclusive lock

    EXPECT_TRUE(waitForThread(mutex,1,10000));
    SleepMillis(10);  // still need to give it a chance to move ahead

    EXPECT_TRUE(!l2.haslock);  // this thread is waiting ...
    EXPECT_TRUE(!l2.obtainedlock);  // this thread is waiting ...

    // now try and get a SharedLock
    locker<CSharedLock> l3(sec,&mutex,&event);
    thread waitThread3(l3); // try to get a shared lock
    EXPECT_TRUE(waitForThread(mutex,2,10000));
    SleepMillis(10);
    EXPECT_TRUE(l3.haslock);

    event.Set();
    EXPECT_TRUE(waitThread3.timed_join(MILLIS(10000)));

    // l3 should have released.
    EXPECT_TRUE(!l3.haslock);

    // but the exclusive lock should still not have happened
    EXPECT_TRUE(!l2.haslock);  // this thread is waiting ...
    EXPECT_TRUE(!l2.obtainedlock);  // this thread is waiting ...

    // let it go
    l1.Leave(); // the last shared lock leaves.

    EXPECT_TRUE(waitThread1.timed_join(MILLIS(10000)));

    EXPECT_TRUE(l2.obtainedlock);  // the exclusive lock was captured
    EXPECT_TRUE(!l2.haslock);  // ... but it doesn't have it anymore
}
Beispiel #3
0
TEST(TestSharedSection, TwoCase)
{
    CSharedSection sec;

    CEvent event;
    std::atomic<long> mutex(0L);

    locker<CSharedLock> l1(sec,&mutex,&event);

    {
        CSharedLock lock(sec);
        thread waitThread1(l1);

        EXPECT_TRUE(waitForWaiters(event,1,10000));
        EXPECT_TRUE(l1.haslock);

        event.Set();

        EXPECT_TRUE(waitThread1.timed_join(MILLIS(10000)));
    }

    locker<CSharedLock> l2(sec,&mutex,&event);
    {
        CExclusiveLock lock(sec); // get exclusive lock
        thread waitThread2(l2); // thread should block

        EXPECT_TRUE(waitForThread(mutex,1,10000));
        SleepMillis(10);

        EXPECT_TRUE(!l2.haslock);

        lock.Leave();

        EXPECT_TRUE(waitForWaiters(event,1,10000));
        SleepMillis(10);
        EXPECT_TRUE(l2.haslock);

        event.Set();

        EXPECT_TRUE(waitThread2.timed_join(MILLIS(10000)));
    }
}
/* fake loss measures */
int fake_loss_measure_data()
{
  struct timeval tv; /* measuring time */
  int elapsed; /* elapsed time */
  int speed;
  
  if (counter == 0)
  {
    gettimeofday(&tv0, 0);
  }
  
  diag_os_millisleep(250);

  /* get elapsed time */
  gettimeofday(&tv, 0);
  elapsed = MILLIS(tv) - MILLIS(tv0);
  elapsed += 11000;
  
  if      (elapsed < 11983) speed = 89*10000/36;
  else if (elapsed < 12723) speed = 88*10000/36;
  else if (elapsed < 13473) speed = 87*10000/36;
  else if (elapsed < 14713) speed = 85*10000/36;
  else if (elapsed < 15713) speed = 84*10000/36;
  else if (elapsed < 16213) speed = 83*10000/36;
  else if (elapsed < 16963) speed = 82*10000/36;
  else if (elapsed < 17463) speed = 81*10000/36;
  else if (elapsed < 18463) speed = 80*10000/36;
  else if (elapsed < 19463) speed = 79*10000/36;
  else if (elapsed < 20213) speed = 78*10000/36;
  else if (elapsed < 20963) speed = 77*10000/36;
  else if (elapsed < 22463) speed = 76*10000/36;
  else if (elapsed < 22963) speed = 75*10000/36;
  else if (elapsed < 23713) speed = 74*10000/36;
  else if (elapsed < 25213) speed = 73*10000/36;
  else if (elapsed < 26453) speed = 72*10000/36;
  else if (elapsed < 27203) speed = 71*10000/36;
  else if (elapsed < 27953) speed = 70*10000/36;
  else if (elapsed < 29193) speed = 69*10000/36;
  else if (elapsed < 29943) speed = 68*10000/36;
  else if (elapsed < 30693) speed = 67*10000/36;
  else if (elapsed < 32183) speed = 66*10000/36;
  else if (elapsed < 32933) speed = 65*10000/36;
  else if (elapsed < 33683) speed = 64*10000/36;
  else if (elapsed < 34683) speed = 63*10000/36;
  else if (elapsed < 35433) speed = 62*10000/36;
  else if (elapsed < 36433) speed = 61*10000/36;
  else if (elapsed < 37183) speed = 60*10000/36;
  else if (elapsed < 38433) speed = 59*10000/36;
  else if (elapsed < 39433) speed = 58*10000/36;
  else if (elapsed < 40183) speed = 57*10000/36;
  else if (elapsed < 40683) speed = 56*10000/36;
  else if (elapsed < 42183) speed = 55*10000/36;
  else if (elapsed < 43183) speed = 54*10000/36;
  else if (elapsed < 44683) speed = 53*10000/36;
  else if (elapsed < 45933) speed = 52*10000/36;
  else if (elapsed < 47183) speed = 51*10000/36;
  else if (elapsed < 48183) speed = 50*10000/36;
  else if (elapsed < 49433) speed = 49*10000/36;
  else                      speed = 48*10000/36;
  
  counter++;
  
  return (speed);
}