unit-tests: Fix cancel_onoff test
If it takes a while to start one of the threads, another thread might already have passed the usleep() call previously used and re-enabled cancelability so that the loop that checked for it would never terminate.
This commit is contained in:
@@ -1118,17 +1118,22 @@ START_TEST(test_cancel)
|
|||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
static void *cancel_onoff_run(void *data)
|
typedef struct {
|
||||||
|
semaphore_t *sem;
|
||||||
|
bool cancellable;
|
||||||
|
} cancel_onoff_data_t;
|
||||||
|
|
||||||
|
static void *cancel_onoff_run(void *data_in)
|
||||||
{
|
{
|
||||||
bool *cancellable = (bool*)data;
|
cancel_onoff_data_t *data = (cancel_onoff_data_t*)data_in;
|
||||||
|
|
||||||
thread_cancelability(FALSE);
|
thread_cancelability(FALSE);
|
||||||
*cancellable = FALSE;
|
data->cancellable = FALSE;
|
||||||
|
|
||||||
/* we should not get cancelled here */
|
/* we should not get cancelled here */
|
||||||
usleep(50000);
|
data->sem->wait(data->sem);
|
||||||
|
|
||||||
*cancellable = TRUE;
|
data->cancellable = TRUE;
|
||||||
thread_cancelability(TRUE);
|
thread_cancelability(TRUE);
|
||||||
|
|
||||||
/* but here */
|
/* but here */
|
||||||
@@ -1142,28 +1147,37 @@ static void *cancel_onoff_run(void *data)
|
|||||||
START_TEST(test_cancel_onoff)
|
START_TEST(test_cancel_onoff)
|
||||||
{
|
{
|
||||||
thread_t *threads[THREADS];
|
thread_t *threads[THREADS];
|
||||||
bool cancellable[THREADS];
|
cancel_onoff_data_t data[THREADS];
|
||||||
|
semaphore_t *sem;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
sem = semaphore_create(0);
|
||||||
for (i = 0; i < THREADS; i++)
|
for (i = 0; i < THREADS; i++)
|
||||||
{
|
{
|
||||||
cancellable[i] = TRUE;
|
data[i].sem = sem;
|
||||||
threads[i] = thread_create(cancel_onoff_run, &cancellable[i]);
|
data[i].cancellable = TRUE;
|
||||||
}
|
threads[i] = thread_create(cancel_onoff_run, &data[i]);
|
||||||
for (i = 0; i < THREADS; i++)
|
|
||||||
{
|
|
||||||
/* wait until thread has cleared its cancelability */
|
/* wait until thread has cleared its cancelability */
|
||||||
while (cancellable[i])
|
while (data[i].cancellable)
|
||||||
{
|
{
|
||||||
sched_yield();
|
sched_yield();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
{
|
||||||
threads[i]->cancel(threads[i]);
|
threads[i]->cancel(threads[i]);
|
||||||
}
|
}
|
||||||
|
/* let all threads continue */
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
{
|
||||||
|
sem->post(sem);
|
||||||
|
}
|
||||||
for (i = 0; i < THREADS; i++)
|
for (i = 0; i < THREADS; i++)
|
||||||
{
|
{
|
||||||
threads[i]->join(threads[i]);
|
threads[i]->join(threads[i]);
|
||||||
ck_assert(cancellable[i]);
|
ck_assert(data[i].cancellable);
|
||||||
}
|
}
|
||||||
|
sem->destroy(sem);
|
||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user