2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2018-12-12 23:37:38 +01:00
|
|
|
* testing/ostest/cancel.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2017-01-24 16:15:42 +01:00
|
|
|
* Copyright (C) 2007-2009, 2017 Gregory Nutt. All rights reserved.
|
2012-09-13 16:14:18 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2017-01-24 16:15:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-12-08 16:28:49 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2017-10-12 18:44:06 +02:00
|
|
|
#include <fcntl.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <pthread.h>
|
2017-10-12 18:44:06 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <mqueue.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <errno.h>
|
2016-12-08 16:28:49 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#include "ostest.h"
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Preprocessor definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define SIG_WAITCANCEL 27
|
|
|
|
|
2017-01-24 16:15:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
static pthread_mutex_t mutex;
|
|
|
|
static pthread_cond_t cond;
|
|
|
|
|
2017-01-24 16:15:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-12-08 16:28:49 +01:00
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
2017-10-12 18:44:06 +02:00
|
|
|
static void sem_cleaner(FAR void *arg)
|
2016-12-08 16:28:49 +01:00
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_cleaner #%u\n", (unsigned int)((uintptr_t)arg));
|
2016-12-08 16:28:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
static FAR void *sem_waiter(FAR void *parameter)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2016-12-08 16:28:49 +01:00
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Register some clean-up handlers */
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_PTHREAD_CLEANUP_STACKSIZE ; i++)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
pthread_cleanup_push(sem_cleaner, (FAR void *)((uintptr_t)(i+1)));
|
2016-12-08 16:28:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Take the mutex */
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: Taking mutex\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
status = pthread_mutex_lock(&mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: ERROR pthread_mutex_lock failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: Starting wait for condition\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Are we a non-cancelable thread? Yes, set the non-cancelable state */
|
|
|
|
|
|
|
|
if (!parameter)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: Setting non-cancelable\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: ERROR pthread_setcancelstate failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Then wait -- we will never awaken from this normally. We will wake
|
2017-01-24 16:01:37 +01:00
|
|
|
* either by signal or cancellation.
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
status = pthread_cond_wait(&cond, &mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: ERROR pthread_cond_wait failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 16:01:37 +01:00
|
|
|
if (!parameter)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2017-01-24 16:01:37 +01:00
|
|
|
/* Release the mutex */
|
2018-08-13 15:47:26 +02:00
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: Releasing mutex\n");
|
2017-01-24 16:01:37 +01:00
|
|
|
status = pthread_mutex_unlock(&mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: ERROR pthread_mutex_unlock failed, status=%d\n", status);
|
2017-01-24 16:01:37 +01:00
|
|
|
}
|
2018-08-13 15:47:26 +02:00
|
|
|
|
2017-01-24 16:01:37 +01:00
|
|
|
/* Set the cancelable state */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: Setting cancelable\n");
|
2017-01-24 16:01:37 +01:00
|
|
|
status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: ERROR pthread_setcancelstate failed, status=%d\n", status);
|
2017-01-24 16:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: ERROR pthread_cond_wait returned after being cancelled!\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2017-10-10 22:01:26 +02:00
|
|
|
/* Why is this here? Because pthread_setcancelstate() is not a
|
2017-10-11 18:32:59 +02:00
|
|
|
* cancellation point and printf() may or may not be a cancellation point
|
|
|
|
* (OpenGroup.org allows printf to optionally be a cancellation point).
|
|
|
|
* If printf() is not a cancellation point, then the thread will never
|
2020-02-23 05:51:44 +01:00
|
|
|
* be cancelled in the deferred cancellation mode and will instead exit
|
2017-10-11 18:32:59 +02:00
|
|
|
* with no error. The pthread_testcancel() is needed to correctly test
|
|
|
|
* that cancellation has been re-enabled.
|
2017-10-10 22:01:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
pthread_testcancel();
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("sem_waiter: Exit with status 0x12345678\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
pthread_exit((pthread_addr_t)0x12345678);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MQUEUE) && defined(CONFIG_CANCELLATION_POINTS)
|
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
static void mqueue_cleaner(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR mqd_t *mqcancel = (FAR mqd_t *)arg;
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
printf("mqueue_cleaner... closing message queue\n");
|
2017-10-12 18:44:06 +02:00
|
|
|
if (mq_close(*mqcancel) < 0)
|
|
|
|
{
|
|
|
|
printf("mqueue_cleaner: ERROR mq_close failed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static FAR void *mqueue_waiter(FAR void *parameter)
|
|
|
|
{
|
|
|
|
mqd_t mqcancel;
|
|
|
|
struct mq_attr attr;
|
|
|
|
char msgbuffer[CONFIG_MQ_MAXMSGSIZE];
|
|
|
|
size_t nbytes;
|
|
|
|
|
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
/* Register clean-up handler */
|
|
|
|
|
|
|
|
pthread_cleanup_push(mqueue_cleaner, (FAR void *)&mqcancel);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Open the message queue for reading */
|
|
|
|
|
|
|
|
attr.mq_maxmsg = 20;
|
|
|
|
attr.mq_msgsize = CONFIG_MQ_MAXMSGSIZE;
|
|
|
|
attr.mq_flags = 0;
|
|
|
|
|
|
|
|
mqcancel = mq_open("mqcancel", O_RDONLY|O_CREAT, 0666, &attr);
|
|
|
|
if (mqcancel < 0)
|
|
|
|
{
|
|
|
|
printf("mqueue_waiter: ERROR mq_open failed\n");
|
|
|
|
pthread_exit((pthread_addr_t)0xdeadbeef);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait for the message that will never come */
|
|
|
|
|
|
|
|
printf("mqueue_waiter: Waiting to receive a message...\n");
|
|
|
|
nbytes = mq_receive(mqcancel, msgbuffer, CONFIG_MQ_MAXMSGSIZE, 0);
|
|
|
|
|
|
|
|
pthread_testcancel();
|
|
|
|
printf("mqueue_waiter: Awakened with %d\n", (int)nbytes);
|
|
|
|
|
|
|
|
if (mq_close(mqcancel) < 0)
|
|
|
|
{
|
|
|
|
printf("mqueue_waiter: ERROR mq_close failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("mqueue_waiter: Exit with status 0x12345678\n");
|
|
|
|
pthread_exit((pthread_addr_t)0x12345678);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
2017-10-12 18:44:06 +02:00
|
|
|
static FAR void *sig_waiter(FAR void *parameter)
|
|
|
|
{
|
|
|
|
struct siginfo info;
|
|
|
|
sigset_t set;
|
|
|
|
int ret;
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Wait for a signal that will never be delivered */
|
2017-10-12 18:44:06 +02:00
|
|
|
|
|
|
|
printf("sig_waiter: Waiting to receive signal %d ...\n", SIG_WAITCANCEL);
|
|
|
|
|
|
|
|
ret = sigwaitinfo(&set, &info);
|
|
|
|
|
|
|
|
pthread_testcancel();
|
|
|
|
printf("sig_waiter: Awakened with %d\n", ret);
|
|
|
|
printf("sig_waiter: Exit with status 0x12345678\n");
|
|
|
|
pthread_exit((pthread_addr_t)0x12345678);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-11 14:51:22 +01:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
static FAR void *asynch_waiter(FAR void *parameter)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Register some clean-up handlers */
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_PTHREAD_CLEANUP_STACKSIZE ; i++)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
pthread_cleanup_push(sem_cleaner, (FAR void *)((uintptr_t)(i+1)));
|
2016-12-11 14:51:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set the non-cancelable state */
|
|
|
|
|
|
|
|
printf("asynch_waiter: Setting non-cancelable\n");
|
|
|
|
status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("asynch_waiter: ERROR pthread_setcancelstate failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the asynchronous cancellation type */
|
|
|
|
|
|
|
|
printf("asynch_waiter: Setting synchronous cancellation type\n");
|
|
|
|
status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("asynch_waiter: ERROR pthread_setcanceltype failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then wait a bit. We should be canceled aynchronously while waiting, but the
|
2020-02-23 05:51:44 +01:00
|
|
|
* cancellation should pend because we are non-cancellable.
|
2016-12-11 14:51:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
usleep(250*1000);
|
|
|
|
|
|
|
|
/* We should be canceled when restore the cancelable state. */
|
|
|
|
|
|
|
|
printf("asynch_waiter: Restoring cancelable state\n");
|
|
|
|
|
|
|
|
printf("asynch_waiter: Setting cancelable\n");
|
|
|
|
status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
|
|
|
|
|
|
|
/* We should not get here!!! */
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
printf("asynch_waiter: ERROR pthread_setcancelstate failed, status=%d\n",
|
|
|
|
status);
|
2016-12-11 14:51:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the cancelable state */
|
|
|
|
|
|
|
|
printf("asynch_waiter: Exit with status 0x12345678\n");
|
|
|
|
pthread_exit((pthread_addr_t)0x12345678);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
static void start_thread(FAR void *(*entry)(FAR void *), pthread_t *waiter,
|
|
|
|
int cancelable)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Initialize the mutex */
|
|
|
|
|
|
|
|
printf("start_thread: Initializing mutex\n");
|
|
|
|
status = pthread_mutex_init(&mutex, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("start_thread: ERROR pthread_mutex_init failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the condition variable */
|
|
|
|
|
|
|
|
printf("start_thread: Initializing cond\n");
|
|
|
|
status = pthread_cond_init(&cond, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("start_thread: ERROR pthread_cond_init failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up attributes */
|
|
|
|
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("start_thread: pthread_attr_init failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_attr_setstacksize(&attr, STACKSIZE);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("start_thread: pthread_attr_setstacksize failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the waiter thread */
|
|
|
|
|
|
|
|
printf("start_thread: Starting thread\n");
|
2017-10-12 18:44:06 +02:00
|
|
|
status = pthread_create(waiter, &attr, entry,
|
|
|
|
(pthread_addr_t)((uintptr_t)cancelable));
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("start_thread: ERROR pthread_create failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the waiter thread gets a chance to run */
|
|
|
|
|
|
|
|
printf("start_thread: Yielding\n");
|
|
|
|
pthread_yield();
|
|
|
|
}
|
|
|
|
|
2017-10-12 18:44:06 +02:00
|
|
|
static void restart_thread(FAR void *(*entry)(FAR void *), pthread_t *waiter,
|
|
|
|
int cancelable)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Destroy the condition variable */
|
|
|
|
|
|
|
|
printf("restart_thread: Destroying cond\n");
|
|
|
|
status = pthread_cond_destroy(&cond);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("restart_thread: ERROR pthread_cond_destroy failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
2017-01-24 16:01:37 +01:00
|
|
|
/* Destroy the mutex. Note that this relies on non-portable NuttX assumption,
|
|
|
|
* that it is possible to destroy a locked mutex owned by a cancelled thread.
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("restart_thread: Destroying mutex\n");
|
2015-06-16 16:32:20 +02:00
|
|
|
status = pthread_mutex_destroy(&mutex);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("restart_thread: ERROR pthread_mutex_destroy failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then restart the thread */
|
|
|
|
|
|
|
|
printf("restart_thread: Re-starting thread\n");
|
2016-12-11 14:51:22 +01:00
|
|
|
start_thread(entry, waiter, cancelable);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 16:15:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
void cancel_test(void)
|
|
|
|
{
|
2017-10-12 18:44:06 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MQUEUE) && defined(CONFIG_CANCELLATION_POINTS)
|
|
|
|
struct mq_attr attr;
|
|
|
|
mqd_t mqcancel;
|
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
pthread_t waiter;
|
|
|
|
void *result;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Test 1: Normal Cancel *********************************************/
|
|
|
|
/* Start the waiter thread */
|
|
|
|
|
2016-12-11 14:51:22 +01:00
|
|
|
printf("cancel_test: Test 1a: Normal Cancellation\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("cancel_test: Starting thread\n");
|
2017-10-12 18:44:06 +02:00
|
|
|
start_thread(sem_waiter, &waiter, 1);
|
2016-12-11 14:51:22 +01:00
|
|
|
|
|
|
|
/* Then cancel it. It should be in the usleep 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("cancel_test: PASS thread terminated with PTHREAD_CANCELED\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 16:01:37 +01:00
|
|
|
/* Test 2: Asynchronous Cancel ***************************************/
|
2016-12-11 14:51:22 +01:00
|
|
|
|
|
|
|
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);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2016-12-10 23:41:16 +01:00
|
|
|
/* Then cancel it. It should be in the pthread_cond_wait now -- wait
|
|
|
|
* bit to make sure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usleep(100*1000);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("cancel_test: Canceling thread\n");
|
|
|
|
status = pthread_cancel(waiter);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("cancel_test: PASS thread terminated with PTHREAD_CANCELED\n");
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 14:51:22 +01:00
|
|
|
#else
|
|
|
|
printf("... Skipped\n");
|
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2016-12-11 14:51:22 +01:00
|
|
|
/* Test 3: Cancel Detached Thread ************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2016-12-11 14:51:22 +01:00
|
|
|
printf("cancel_test: Test 3: Cancellation of detached thread\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("cancel_test: Re-starting thread\n");
|
2017-10-12 18:44:06 +02:00
|
|
|
restart_thread(sem_waiter, &waiter, 1);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Detach the thread */
|
|
|
|
|
|
|
|
status = pthread_detach(waiter);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("cancel_test: ERROR pthread_detach, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
2016-12-10 23:41:16 +01:00
|
|
|
/* Then cancel it. It should be in the pthread_cond_wait now -- wait a
|
|
|
|
* bit to be certain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usleep(100*1000);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("cancel_test: Canceling thread\n");
|
|
|
|
status = pthread_cancel(waiter);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
2016-12-10 23:41:16 +01:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* If we are doing deferred cancellation, then we will have to wait a bit
|
2020-02-23 05:51:44 +01:00
|
|
|
* of the following pthread_join() may succeed because it happens before
|
2016-12-10 23:41:16 +01:00
|
|
|
* before the cancellation.
|
|
|
|
*/
|
|
|
|
|
2016-12-11 14:51:22 +01:00
|
|
|
usleep(100*1000);
|
2016-12-10 23:41:16 +01:00
|
|
|
#endif
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* 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");
|
|
|
|
}
|
|
|
|
else if (status != ESRCH)
|
|
|
|
{
|
|
|
|
printf("cancel_test: ERROR pthread_join failed but with wrong status=%d\n", status);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("cancel_test: PASS pthread_join failed with status=ESRCH\n");
|
|
|
|
}
|
|
|
|
|
2016-12-11 14:51:22 +01:00
|
|
|
/* Test 4: Non-cancelable threads ************************************/
|
2013-04-18 19:37:38 +02:00
|
|
|
/* This test currently depends on signals. It doesn't have to and
|
|
|
|
* could be re-designed so that it does not depend on signals.
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2016-12-11 14:51:22 +01:00
|
|
|
printf("cancel_test: Test 5: Non-cancelable threads\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("cancel_test: Re-starting thread (non-cancelable)\n");
|
2017-10-12 18:44:06 +02:00
|
|
|
restart_thread(sem_waiter, &waiter, 0);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2013-04-18 19:37:38 +02:00
|
|
|
/* Give the thread a chance to run an to set the non-cancelable state.
|
|
|
|
* This is the dependency on signals:
|
|
|
|
*/
|
|
|
|
|
|
|
|
usleep(200*1000);
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Then cancel it. It should be in the pthread_cond_wait now. The
|
2020-02-23 05:51:44 +01:00
|
|
|
* behavior here is non-standard: when the thread is at a cancellation
|
|
|
|
* point, it should be cancelable, even when cancellation is disable.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2020-02-23 05:51:44 +01:00
|
|
|
* The cancellation should succeed, because the cancellation is pending.
|
2011-03-20 19:18:19 +01:00
|
|
|
*/
|
|
|
|
|
2016-12-10 23:41:16 +01:00
|
|
|
usleep(100*1000);
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("cancel_test: Canceling thread\n");
|
|
|
|
status = pthread_cancel(waiter);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("cancel_test: ERROR pthread_cancel failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_cond_signal(&cond);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("cancel_test: ERROR pthread_cond_signal failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_mutex_unlock(&mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("cancel_test: ERROR pthread_mutex_unlock failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("cancel_test: PASS thread terminated with PTHREAD_CANCELED\n");
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 18:44:06 +02:00
|
|
|
|
|
|
|
printf("cancel_test: Test 6: Cancel message queue wait\n");
|
|
|
|
printf("cancel_test: Starting thread (cancelable)\n");
|
|
|
|
|
|
|
|
#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");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
printf("Skipped\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("cancel_test: Test 7: Cancel signal wait\n");
|
|
|
|
printf("cancel_test: Starting thread (cancelable)\n");
|
|
|
|
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
2017-10-12 18:44:06 +02:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("cancel_test: PASS thread terminated with PTHREAD_CANCELED\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
printf("Skipped\n");
|
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|