Xenomai API  2.6.5

Thread cancellation. More...

Collaboration diagram for Thread cancellation.:

Functions

int pthread_cancel (pthread_t thread)
 Cancel a thread. More...
 
void pthread_cleanup_push (cleanup_routine_t *routine, void *arg)
 Register a cleanup handler to be executed at the time of cancellation. More...
 
void pthread_cleanup_pop (int execute)
 Unregister the last registered cleanup handler. More...
 
int pthread_setcanceltype (int type, int *oldtype_ptr)
 Set cancelability type of the current thread. More...
 
int pthread_setcancelstate (int state, int *oldstate_ptr)
 Set cancelability state of the current thread. More...
 
void pthread_testcancel (void)
 Test if a cancellation request is pending. More...
 

Detailed Description

Thread cancellation.

Cancellation is the mechanism by which a thread can terminate the execution of a Xenomai POSIX skin thread (created with pthread_create()). More precisely, a thread can send a cancellation request to a Xenomai POSIX skin thread and depending on its cancelability type (see pthread_setcanceltype()) and state (see pthread_setcancelstate()), the target thread can then either ignore the request, honor it immediately, or defer it till it reaches a cancellation point. When threads are first created by pthread_create(), they always defer cancellation requests.

When a thread eventually honors a cancellation request, it behaves as if pthread_exit(PTHREAD_CANCELED) was called. All cleanup handlers are executed in reverse order, finalization functions for thread-specific data are called, and finally the thread stops executing. If the canceled thread was joinable, the return value PTHREAD_CANCELED is provided to whichever thread calls pthread_join() on it. See pthread_exit() for more information.

Cancellation points are the points where the thread checks for pending cancellation requests and performs them. The POSIX threads functions pthread_join(), pthread_cond_wait(), pthread_cond_timedwait(), pthread_testcancel(), sem_wait(), sem_timedwait(), sigwait(), sigwaitinfo() and sigtimedwait() are cancellation points.

See also
Specification.

Function Documentation

int pthread_cancel ( pthread_t  thread)

Cancel a thread.

This service sends a cancellation request to the thread thread and returns immediately. Depending on the target thread cancelability state (see pthread_setcancelstate()) and type (see pthread_setcanceltype()), its termination is either immediate, deferred or ignored.

When the cancellation request is handled and before the thread is terminated, the cancellation cleanup handlers (registered with the pthread_cleanup_push() service) are called, then the thread-specific data destructor functions (registered with pthread_key_create()).

Returns
0 on success;
an error number if:
  • ESRCH, the thread thread was not found.
See also
Specification.

References xnpod_schedule(), and xnpod_unblock_thread().

Referenced by rt_task_delete().

void pthread_cleanup_pop ( int  execute)

Unregister the last registered cleanup handler.

If the calling thread is a Xenomai POSIX skin thread (i.e. created with pthread_create()), this service unregisters the last routine which was registered with pthread_cleanup_push() and call it if execute is not null.

If the caller context is invalid (not a Xenomai POSIX skin thread), this service has no effect.

This service may be called at any place, but for maximal portability, should only called in the same lexical scope as the matching call to pthread_cleanup_push().

Parameters
executeif non zero, the last registered cleanup handler should be executed before it is unregistered.
Valid contexts:
  • Xenomai POSIX skin kernel-space thread,
  • Xenomai POSIX skin user-space thread (switches to primary mode).
See also
Specification.

Referenced by rt_cond_wait(), and rt_cond_wait_until().

void pthread_cleanup_push ( cleanup_routine_t *  routine,
void *  arg 
)

Register a cleanup handler to be executed at the time of cancellation.

This service registers the given routine to be executed a the time of cancellation of the calling thread, if this thread is a Xenomai POSIX skin thread (i.e. created with the pthread_create() service). If the caller context is invalid (not a Xenomai POSIX skin thread), this service has no effect.

If allocation from the system heap fails (because the system heap size is to small), this service fails silently.

The routines registered with this service get called in LIFO order when the calling thread calls pthread_exit() or is canceled, or when it calls the pthread_cleanup_pop() service with a non null argument.

Parameters
routinethe cleanup routine to be registered;
argthe argument associated with this routine.
Valid contexts:
  • Xenomai POSIX skin kernel-space thread,
  • Xenomai POSIX skin user-space thread (switches to primary mode).
See also
Specification.

Referenced by rt_cond_wait(), and rt_cond_wait_until().

int pthread_setcancelstate ( int  state,
int *  oldstate_ptr 
)

Set cancelability state of the current thread.

This service atomically set the cancelability state of the calling thread and returns its previous value at the address oldstate_ptr, if the calling thread is a Xenomai POSIX skin thread (i.e. created with the pthread_create service).

The cancelability state of a POSIX thread may be:

  • PTHREAD_CANCEL_ENABLE, meaning that cancellation requests will be handled if received;
  • PTHREAD_CANCEL_DISABLE, meaning that cancellation requests will not be handled if received.
Parameters
statenew cancelability state of the calling thread;
oldstate_ptraddress where the old cancelability state will be stored on success.
Returns
0 on success;
an error number if:
  • EINVAL, state is not a valid cancelability state;
  • EPERM, the caller context is invalid.
Valid contexts:
  • Xenomai POSIX skin kernel-space thread,
  • Xenomai POSIX skin user-space thread (switches to primary mode).
See also
Specification.
int pthread_setcanceltype ( int  type,
int *  oldtype_ptr 
)

Set cancelability type of the current thread.

This service atomically sets the cancelability type of the calling thread, and return its previous value at the address oldtype_ptr, if this thread is a Xenomai POSIX skin thread (i.e. was created with the pthread_create() service).

The cancelability type of a POSIX thread may be:

  • PTHREAD_CANCEL_DEFERRED, meaning that cancellation requests are only handled in services which are cancellation points;
  • PTHREAD_CANCEL_ASYNCHRONOUS, meaning that cancellation requests are handled as soon as they are sent.
Parameters
typenew cancelability type of the calling thread;
oldtype_ptraddress where the old cancelability type will be stored on success.
Returns
0 on success;
an error number if:
  • EINVAL, type is not a valid cancelability type;
  • EPERM, the caller context is invalid.
Valid contexts:
  • Xenomai POSIX skin kernel-space thread,
  • Xenomai POSIX skin user-space thread (switches to primary mode).
See also
Specification.

Referenced by pthread_intr_wait_np(), pthread_wait_np(), rt_cond_wait(), rt_cond_wait_until(), rt_intr_wait(), rt_queue_read(), rt_queue_read_until(), rt_queue_receive(), rt_queue_receive_until(), rt_queue_send(), rt_queue_write(), rt_sem_p(), rt_sem_p_until(), rt_task_send(), rt_task_shadow(), rt_task_sleep(), rt_task_sleep_until(), and rt_task_wait_period().

void pthread_testcancel ( void  )

Test if a cancellation request is pending.

This function creates a cancellation point if the calling thread is a Xenomai POSIX skin thread (i.e. created with the pthread_create() service).

This function is a cancellation point. It has no effect if cancellation is disabled.

Valid contexts:
  • Xenomai POSIX skin kernel-space thread,
  • Xenomai POSIX skin user-space thread (switches to primary mode).
See also
Specification.

Referenced by rt_cond_wait(), and rt_cond_wait_until().