Esempio n. 1
0
void ptw32_PrepCancel( ptw32_thread_t* tp )
{
	HANDLE threadH = tp->threadH;

	SuspendThread (threadH);

	if (WaitForSingleObject (threadH, 0) == WAIT_TIMEOUT)
	{
		tp->state = PThreadStateCanceling;
		tp->cancelState = PTHREAD_CANCEL_DISABLE;
		/*
		* If alertdrv and QueueUserAPCEx is available then the following
		* will result in a call to QueueUserAPCEx with the args given, otherwise
		* this will result in a call to ptw32_RegisterCancelation and only
		* the threadH arg will be used.
		*/
		ptw32_register_cancelation (ptw32_cancel_callback, threadH, 0);
		(void) pthread_mutex_unlock (&tp->cancelLock);
		ResumeThread (threadH);
	}
}
Esempio n. 2
0
int
pthread_cancel (pthread_t thread)
     /*
      * ------------------------------------------------------
      * DOCPUBLIC
      *      This function requests cancellation of 'thread'.
      *
      * PARAMETERS
      *      thread
      *              reference to an instance of pthread_t
      *
      *
      * DESCRIPTION
      *      This function requests cancellation of 'thread'.
      *      NOTE: cancellation is asynchronous; use pthread_join to
      *                wait for termination of 'thread' if necessary.
      *
      * RESULTS
      *              0               successfully requested cancellation,
      *              ESRCH           no thread found corresponding to 'thread',
      *              ENOMEM          implicit self thread create failed.
      * ------------------------------------------------------
      */
{
  int result;
  int cancel_self;
  pthread_t self;
  ptw32_thread_t * tp;

  result = pthread_kill (thread, 0);

  if (0 != result)
    {
      return result;
    }

  if ((self = pthread_self ()).p == NULL)
    {
      return ENOMEM;
    };

  /*
   * FIXME!!
   *
   * Can a thread cancel itself?
   *
   * The standard doesn't
   * specify an error to be returned if the target
   * thread is itself.
   *
   * If it may, then we need to ensure that a thread can't
   * deadlock itself trying to cancel itself asyncronously
   * (pthread_cancel is required to be an async-cancel
   * safe function).
   */
  cancel_self = pthread_equal (thread, self);

  tp = (ptw32_thread_t *) thread.p;

  /*
   * Lock for async-cancel safety.
   */
  (void) pthread_mutex_lock (&tp->cancelLock);

  if (tp->cancelType == PTHREAD_CANCEL_ASYNCHRONOUS
      && tp->cancelState == PTHREAD_CANCEL_ENABLE
      && tp->state < PThreadStateCanceling)
    {
      if (cancel_self)
	{
	  tp->state = PThreadStateCanceling;
	  tp->cancelState = PTHREAD_CANCEL_DISABLE;

	  (void) pthread_mutex_unlock (&tp->cancelLock);
	  ptw32_throw (PTW32_EPS_CANCEL);

	  /* Never reached */
	}
      else
	{
	  HANDLE threadH = tp->threadH;

	  SuspendThread (threadH);

	  if (WaitForSingleObject (threadH, 0) == WAIT_TIMEOUT)
	    {
	      tp->state = PThreadStateCanceling;
	      tp->cancelState = PTHREAD_CANCEL_DISABLE;
	      /*
	       * If alertdrv and QueueUserAPCEx is available then the following
	       * will result in a call to QueueUserAPCEx with the args given, otherwise
	       * this will result in a call to ptw32_RegisterCancelation and only
	       * the threadH arg will be used.
	       */
	      ptw32_register_cancelation (ptw32_cancel_callback, threadH, 0);
	      (void) pthread_mutex_unlock (&tp->cancelLock);
	      //ResumeThread (threadH);
	    }
	}
    }
  else
    {
      /*
       * Set for deferred cancellation.
       */
      if (tp->state < PThreadStateCancelPending)
	{
	  tp->state = PThreadStateCancelPending;
	  if (!SetEvent (tp->cancelEvent))
	    {
	      result = ESRCH;
	    }
	}
      else if (tp->state >= PThreadStateCanceling)
	{
	  result = ESRCH;
	}

      (void) pthread_mutex_unlock (&tp->cancelLock);
    }
  return (result);
}
int
pthread_cancel (pthread_t thread)
{
    int result;
    int cancel_self;
    pthread_t self;
    ptw32_thread_t * tp;
    ptw32_mcs_local_node_t stateLock;

    result = pthread_kill (thread, 0);

    if (0 != result)
    {
        return result;
    }

    if ((self = pthread_self ()).p == NULL)
    {
        return ENOMEM;
    };

    cancel_self = pthread_equal (thread, self);

    tp = (ptw32_thread_t *) thread.p;

    ptw32_mcs_lock_acquire (&tp->stateLock, &stateLock);

    if (tp->cancelType == PTHREAD_CANCEL_ASYNCHRONOUS
            && tp->cancelState == PTHREAD_CANCEL_ENABLE
            && tp->state < PThreadStateCanceling)
    {
        if (cancel_self)
        {
            tp->state = PThreadStateCanceling;
            tp->cancelState = PTHREAD_CANCEL_DISABLE;

            ptw32_mcs_lock_release (&stateLock);
            ptw32_throw (PTW32_EPS_CANCEL);


        }
        else
        {
            HANDLE threadH = tp->threadH;

            SuspendThread (threadH);

            if (WaitForSingleObject (threadH, 0) == WAIT_TIMEOUT)
            {
                tp->state = PThreadStateCanceling;
                tp->cancelState = PTHREAD_CANCEL_DISABLE;
                ptw32_register_cancelation ((PAPCFUNC)ptw32_cancel_callback, threadH, 0);
                ptw32_mcs_lock_release (&stateLock);
                ResumeThread (threadH);
            }
        }
    }
    else
    {
        if (tp->state < PThreadStateCancelPending)
        {
            tp->state = PThreadStateCancelPending;
            if (!SetEvent (tp->cancelEvent))
            {
                result = ESRCH;
            }
        }
        else if (tp->state >= PThreadStateCanceling)
        {
            result = ESRCH;
        }

        ptw32_mcs_lock_release (&stateLock);
    }

    return (result);
}