in testing/ostest/cancel.c [424:849]
void cancel_test(void)
{
#if !defined(CONFIG_DISABLE_MQUEUE) && defined(CONFIG_CANCELLATION_POINTS)
struct mq_attr attr;
mqd_t mqcancel;
#endif
pthread_t waiter;
void *result;
int status;
sem_init(&sem_thread_started, 0, 0);
/* Test 1: Normal Cancel **************************************************/
/* Start the waiter thread */
printf("cancel_test: Test 1a: Normal Cancellation\n");
printf("cancel_test: Starting thread\n");
start_thread(sem_waiter, &waiter, 1);
/* Then cancel it. It should be in the usleep now -- wait bit to
* make sure.
*/
sem_wait(&sem_thread_started);
/* Make sure sem_waiter run into pthread_cond_wait */
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
printf("cancel_test: Canceling thread\n");
status = pthread_cancel(waiter);
if (status != 0)
{
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n",
status);
ASSERT(false);
}
/* Then join to the thread to pick up the result (if we don't do
* we will have a memory leak!)
*/
printf("cancel_test: Joining\n");
status = pthread_join(waiter, &result);
if (status != 0)
{
printf("cancel_test: ERROR pthread_join failed, status=%d\n",
status);
ASSERT(false);
}
else
{
printf("cancel_test: waiter exited with result=%p\n", result);
if (result != PTHREAD_CANCELED)
{
printf("cancel_test: ERROR expected result=%p\n",
PTHREAD_CANCELED);
ASSERT(false);
}
else
{
printf("cancel_test:"
" PASS thread terminated with PTHREAD_CANCELED\n");
}
}
/* Test 2: Asynchronous Cancel ********************************************/
printf("cancel_test: Test 2: Asynchronous Cancellation\n");
#ifdef CONFIG_CANCELLATION_POINTS
/* If cancellation points were enabled, then the first test was done
* in deferred mode. Do it again it asynchronous mode.
*
* This test does not really test asynchronous cancellation (which is
* inherently dangerous), but does exercides pthread_setcanceltype().
*/
/* Start the waiter thread */
printf("cancel_test: Starting thread\n");
restart_thread(asynch_waiter, &waiter, 1);
/* Then cancel it. It should be in the pthread_cond_wait now -- wait
* bit to make sure.
*/
usleep(100 * 1000);
printf("cancel_test: Canceling thread\n");
status = pthread_cancel(waiter);
if (status != 0)
{
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n",
status);
ASSERT(false);
}
/* Then join to the thread to pick up the result (if we don't do
* we will have a memory leak!)
*/
printf("cancel_test: Joining\n");
status = pthread_join(waiter, &result);
if (status != 0)
{
printf("cancel_test: ERROR pthread_join failed, status=%d\n",
status);
ASSERT(false);
}
else
{
printf("cancel_test: waiter exited with result=%p\n", result);
if (result != PTHREAD_CANCELED)
{
printf("cancel_test: ERROR expected result=%p\n",
PTHREAD_CANCELED);
ASSERT(false);
}
else
{
printf("cancel_test: "
"PASS thread terminated with PTHREAD_CANCELED\n");
}
}
#else
printf("... Skipped\n");
#endif
/* Test 3: Cancel Detached Thread *****************************************/
printf("cancel_test: Test 3: Cancellation of detached thread\n");
printf("cancel_test: Re-starting thread\n");
restart_thread(sem_waiter, &waiter, 1);
/* Detach the thread */
status = pthread_detach(waiter);
if (status != 0)
{
printf("cancel_test: ERROR pthread_detach, status=%d\n", status);
ASSERT(false);
}
/* Then cancel it. It should be in the pthread_cond_wait now -- wait a
* bit to be certain.
*/
sem_wait(&sem_thread_started);
/* Make sure sem_waiter run into pthread_cond_wait */
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
printf("cancel_test: Canceling thread\n");
status = pthread_cancel(waiter);
if (status != 0)
{
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n",
status);
ASSERT(false);
}
#ifdef CONFIG_CANCELLATION_POINTS
/* If we are doing deferred cancellation, then we will have to wait a bit
* of the following pthread_join() may succeed because it happens before
* before the cancellation.
*/
usleep(100 * 1000);
#endif
/* Join should now fail */
printf("cancel_test: Joining\n");
status = pthread_join(waiter, &result);
if (status == 0)
{
printf("cancel_test: ERROR pthread_join succeeded\n");
ASSERT(false);
}
else if (status != ESRCH)
{
printf("cancel_test:"
" ERROR pthread_join failed but with wrong status=%d\n",
status);
ASSERT(false);
}
else
{
printf("cancel_test: PASS pthread_join failed with status=ESRCH\n");
}
/* Test 4: Non-cancelable threads *****************************************/
/* This test currently depends on signals. It doesn't have to and
* could be re-designed so that it does not depend on signals.
*/
printf("cancel_test: Test 5: Non-cancelable threads\n");
printf("cancel_test: Re-starting thread (non-cancelable)\n");
restart_thread(sem_waiter, &waiter, 0);
/* Give the thread a chance to run an to set the non-cancelable state.
* This is the dependency on signals:
*/
usleep(200 * 1000);
/* Then cancel it. It should be in the pthread_cond_wait now. The
* behavior here is non-standard: when the thread is at a cancellation
* point, it should be cancelable, even when cancellation is disable.
*
* The cancellation should succeed, because the cancellation is pending.
*/
sem_wait(&sem_thread_started);
/* Make sure sem_waiter run into pthread_cond_wait */
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
printf("cancel_test: Canceling thread\n");
status = pthread_cancel(waiter);
if (status != 0)
{
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n",
status);
ASSERT(false);
}
/* Signal the thread. It should wake up and restore the cancelable state.
* When the cancelable state is re-enabled, the thread should be canceled.
*/
status = pthread_mutex_lock(&mutex);
if (status != 0)
{
printf("cancel_test: ERROR pthread_mutex_lock failed, status=%d\n",
status);
ASSERT(false);
}
status = pthread_cond_signal(&cond);
if (status != 0)
{
printf("cancel_test: ERROR pthread_cond_signal failed, status=%d\n",
status);
ASSERT(false);
}
status = pthread_mutex_unlock(&mutex);
if (status != 0)
{
printf("cancel_test: ERROR pthread_mutex_unlock failed, status=%d\n",
status);
ASSERT(false);
}
/* Then join to the thread to pick up the result (if we don't do
* we will have a memory leak!)
*/
printf("cancel_test: Joining\n");
status = pthread_join(waiter, &result);
if (status != 0)
{
printf("cancel_test: ERROR pthread_join failed, status=%d\n", status);
ASSERT(false);
}
else
{
printf("cancel_test: waiter exited with result=%p\n", result);
if (result != PTHREAD_CANCELED)
{
printf("cancel_test: ERROR expected result=%p\n",
PTHREAD_CANCELED);
ASSERT(false);
}
else
{
printf("cancel_test: "
"PASS thread terminated with PTHREAD_CANCELED\n");
}
}
printf("cancel_test: Test 6: Cancel message queue wait\n");
printf("cancel_test: Starting thread (cancelable)\n");
sem_destroy(&sem_thread_started);
#if !defined(CONFIG_DISABLE_MQUEUE) && defined(CONFIG_CANCELLATION_POINTS)
/* Create the message queue */
/* Fill in attributes for message queue */
attr.mq_maxmsg = 20;
attr.mq_msgsize = CONFIG_MQ_MAXMSGSIZE;
attr.mq_flags = 0;
mqcancel = mq_open("mqcancel", O_WRONLY | O_CREAT, 0666, &attr);
if (mqcancel == (mqd_t)-1)
{
printf("sender_thread: ERROR mq_open failed\n");
ASSERT(false);
pthread_exit((pthread_addr_t)1);
}
/* Start the mqueue_waiter thread */
restart_thread(mqueue_waiter, &waiter, 0);
/* Then cancel it. It should be in the mq_read now -- wait bit to
* make sure.
*/
usleep(75 * 1000);
printf("cancel_test: Canceling thread\n");
status = pthread_cancel(waiter);
if (status != 0)
{
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n",
status);
ASSERT(false);
}
/* Then join to the thread to pick up the result (if we don't do
* we will have a memory leak!)
*/
printf("cancel_test: Joining\n");
status = pthread_join(waiter, &result);
if (status != 0)
{
printf("cancel_test: ERROR pthread_join failed, status=%d\n",
status);
ASSERT(false);
}
else
{
printf("cancel_test: waiter exited with result=%p\n", result);
if (result != PTHREAD_CANCELED)
{
printf("cancel_test: ERROR expected result=%p\n",
PTHREAD_CANCELED);
ASSERT(false);
}
else
{
printf("cancel_test: "
"PASS thread terminated with PTHREAD_CANCELED\n");
}
}
/* Close the message queue */
if (mq_close(mqcancel) < 0)
{
printf("sender_thread: ERROR mq_close failed\n");
ASSERT(false);
}
#else
printf("Skipped\n");
#endif
printf("cancel_test: Test 7: Cancel signal wait\n");
printf("cancel_test: Starting thread (cancelable)\n");
#ifdef CONFIG_CANCELLATION_POINTS
/* Start the sig_waiter thread */
restart_thread(sig_waiter, &waiter, 0);
/* Then cancel it. It should be waiting for a signal now -- wait bit to
* make sure.
*/
usleep(75 * 1000);
printf("cancel_test: Canceling thread\n");
status = pthread_cancel(waiter);
if (status != 0)
{
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n",
status);
ASSERT(false);
}
/* Then join to the thread to pick up the result (if we don't do
* we will have a memory leak!)
*/
printf("cancel_test: Joining\n");
status = pthread_join(waiter, &result);
if (status != 0)
{
printf("cancel_test: ERROR pthread_join failed, status=%d\n",
status);
ASSERT(false);
}
else
{
printf("cancel_test: waiter exited with result=%p\n", result);
if (result != PTHREAD_CANCELED)
{
printf("cancel_test: ERROR expected result=%p\n",
PTHREAD_CANCELED);
ASSERT(false);
}
else
{
printf("cancel_test: "
"PASS thread terminated with PTHREAD_CANCELED\n");
}
}
#else
printf("Skipped\n");
#endif
}