Skip to content

Use a condition variable to control busy threads in dispatch_apply test #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions tests/dispatch_apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ static volatile int32_t busy_threads_started, busy_threads_finished;
/*
* Keep a thread busy, spinning on the CPU.
*/
#if TARGET_OS_EMBEDDED
// iPhone 4
#define ITERS_PER_SECOND 50000000UL
#elif __s390x__
#define ITERS_PER_SECOND 15000000000UL
#else
// On a 2.7 4-core i5 iMac12,2, one thread of this loop runs at ROUGHLY:
#define ITERS_PER_SECOND 1000000000UL
#endif
static volatile int all_done = 0;

/* Fiddling with j in the middle and hitting this global will hopefully keep
* the optimizer from cutting the whole thing out as dead code.
Expand All @@ -54,14 +46,16 @@ static volatile unsigned int busythread_useless;
void busythread(void *ignored)
{
(void)ignored;
uint64_t i = 0, j = 0;
/* prevent i and j been optimized out */
volatile uint64_t i = 0, j = 0;

OSAtomicIncrement32(&busy_threads_started);

for(i = 0; i < 2*ITERS_PER_SECOND; i++)
while(!all_done)
{
if(i == 500000) { j -= busythread_useless; }
j += i;
i += 1;
}

OSAtomicIncrement32(&busy_threads_finished);
Expand Down Expand Up @@ -109,6 +103,9 @@ void test_apply_contended(dispatch_queue_t dq)
test_long("contended: count", count, final);
test_long("contended: threads finished before apply", after, 0);

/* Release busy threads by setting all_done to 1 */
all_done = 1;

dispatch_group_wait(grp, DISPATCH_TIME_FOREVER);
dispatch_release(grp);

Expand Down