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

  CEvent event;
  volatile long mutex = 0;

  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)));
  }
}
Example #2
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)));
    }
}