Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
split unittests and use ThreadGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinNowak committed Jun 2, 2011
1 parent ae0654d commit 10eb812
Showing 1 changed file with 51 additions and 47 deletions.
98 changes: 51 additions & 47 deletions src/core/thread.d
Expand Up @@ -3862,66 +3862,70 @@ version( unittest )
assert(fib.sum == TestFiber.expSum);
}
}
}

// Single thread running separate fibers
unittest
{
runTen();
}
// Single thread running separate fibers
unittest
{
runTen();
}

// Multiple threads running separate fibers
unittest
// Multiple threads running separate fibers
unittest
{
auto group = new ThreadGroup();
foreach(_; 0 .. 4)
{
foreach(_; 0 .. 4)
{
auto thr = new Thread(&runTen);
thr.start();
}
thread_joinAll();
group.create(&runTen);
}
group.joinAll();
}

// Multiple threads running shared fibers
unittest
{
shared bool[10] locks;
TestFiber[10] fibs;
// Multiple threads running shared fibers
unittest
{
shared bool[10] locks;
TestFiber[10] fibs;

void runShared()
{
bool cont;
do {
cont = false;
foreach(idx; 0 .. 10)
void runShared()
{
bool cont;
do {
cont = false;
foreach(idx; 0 .. 10)
{
if (cas(&locks[idx], false, true))
{
if (cas(&locks[idx], false, true))
{
if (fibs[idx].state == Fiber.State.HOLD)
{
fibs[idx].call();
cont |= fibs[idx].state != Fiber.State.TERM;
}
locks[idx] = false;
}
else
if (fibs[idx].state == Fiber.State.HOLD)
{
cont = true;
fibs[idx].call();
cont |= fibs[idx].state != Fiber.State.TERM;
}
locks[idx] = false;
}
} while (cont);
}
else
{
cont = true;
}
}
} while (cont);
}

foreach(ref fib; fibs)
fib = new TestFiber();
foreach(ref fib; fibs)
{
fib = new TestFiber();
}

foreach(_; 0 .. 4)
{
auto thr = new Thread(&runShared);
thr.start();
}
thread_joinAll();
auto group = new ThreadGroup();
foreach(_; 0 .. 4)
{
group.create(&runShared);
}
group.joinAll();

foreach(fib; fibs)
assert(fib.sum == TestFiber.expSum);
foreach(fib; fibs)
{
assert(fib.sum == TestFiber.expSum);
}
}

Expand Down

0 comments on commit 10eb812

Please sign in to comment.