This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: 1.7.15-1: pthread_cancel and pthread_kill not working as expected


>> Testcase cancel deferred:
>> Works with 1.7.9 and 20120517 snapshot, fails (hangs) with 1.7.12-1
>> and 1.7.15-1.
> If that works in the snapshot anyway, I'm not going to look into that
> one.

It worked in the reduced testcase with sem_wait(). With read() itâs
still half-broken. See below.

>> Testcase cancel asynchronous:
>> Async cancel seems to have no effect with any tested version.
> I think I found a solution for this problem.  See the comment in the
> patch at
> http://sourceware.org/cgi-bin/cvsweb.cgi/src/winsup/cygwin/thread.cc.diff?cvsroot=src&r1=1.258&r2=1.259
> Please test the today's developer snapshot.

Asynchronous cancel seems to work as well as deferred cancel now. Thanks.

Both cancel types work with sem_wait() and pause() now, but for threads
blocked in read() theyâre still unreliable. Only one of three blocked
threads is killed in the attached updated testcases.

>> Testcase signal/kill:
>> Signals may or may not reach the correct thread with 1.7.12-1 and newer.
> Confirmed. [...] This is cgf's domain so I leave it at that for now.

Okay, Iâll hope for him to respond then.

Otto
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

pthread_t tids[3];

static void cleanup_handler(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i exiting (%p)\n", *intptr, self);
}

static void* simplethread(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i starting (%p)\n", *intptr, self);
  char buffer[1] __attribute((unused));

  pthread_cleanup_push(&cleanup_handler, intptr);

  int oldtype;
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
  fprintf(stderr, "Changing canceltype from %i to %i\n", oldtype, PTHREAD_CANCEL_ASYNCHRONOUS);

  while (1) {
    if (read(STDIN_FILENO, buffer, 1) <= 0) {
      fprintf(stderr, "Thread %i encountered an error: %s (%p)\n",
          *intptr, strerror(errno), self);
    } else {
      fprintf(stderr, "Thread %i woke up just fine\n", *intptr);
    }
  }

  pthread_cleanup_pop(1);
  return NULL;
}

int main() {
  fprintf(stderr, "Testing asynchronous pthread_cancel()\n\n");

  int i;
  int result;

  for (i=0; i<3; i++) {
    int *intptr = (int*)malloc(sizeof(int));
    *intptr = i;
    result = pthread_create(tids+i, NULL, &simplethread, intptr);
    if (result != 0) {
      fprintf(stderr, "Can't create thread: %s\n", strerror(result));
      return 1;
    }
  }

  sleep(1);
  fprintf(stderr, "\n");

  int mainint = 42;
  pthread_cleanup_push(&cleanup_handler, &mainint);

  for (i=2; i>=0; i--) {
    fprintf(stderr, "Cancelling thread %i (%p)\n", i, tids[i]);
    result = pthread_cancel(tids[i]);
    if (result != 0) {
      fprintf(stderr, "Error during pthread_cancel: %s\n", strerror(result));
    }
    sleep(1);
  }

  fprintf(stderr, "\n");
  for (i=0; i<3; i++) {
    result = pthread_kill(tids[i], 0);
    if (result == 0) {
      fprintf(stderr, "Thread %i is still there (%p)\n", i, tids[i]);
    } else {
      fprintf(stderr, "Thread %i is gone (%p)\n", i, tids[i]);
    }
  }

  pthread_cleanup_pop(0);
  return 0;
}
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

pthread_t tids[3];

static void cleanup_handler(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i exiting (%p)\n", *intptr, self);
}

static void* simplethread(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i starting (%p)\n", *intptr, self);
  char buffer[1] __attribute((unused));

  pthread_cleanup_push(&cleanup_handler, intptr);

  while (1) {
    if (read(STDIN_FILENO, buffer, 1) <= 0) {
      fprintf(stderr, "Thread %i encountered an error: %s (%p)\n",
          *intptr, strerror(errno), self);
    } else {
      fprintf(stderr, "Thread %i woke up just fine\n", *intptr);
    }
  }

  pthread_cleanup_pop(1);
  return NULL;
}

int main() {
  fprintf(stderr, "Testing deferred pthread_cancel()\n\n");

  int i;
  int result;

  for (i=0; i<3; i++) {
    int *intptr = (int*)malloc(sizeof(int));
    *intptr = i;
    result = pthread_create(tids+i, NULL, &simplethread, intptr);
    if (result != 0) {
      fprintf(stderr, "Can't create thread: %s\n", strerror(result));
      return 1;
    }
  }

  sleep(1);
  fprintf(stderr, "\n");

  int mainint = 42;
  pthread_cleanup_push(&cleanup_handler, &mainint);

  for (i=2; i>=0; i--) {
    fprintf(stderr, "Cancelling thread %i (%p)\n", i, tids[i]);
    result = pthread_cancel(tids[i]);
    if (result != 0) {
      fprintf(stderr, "Error during pthread_cancel: %s\n", strerror(result));
    }
    sleep(1);
  }

  fprintf(stderr, "\n");
  for (i=0; i<3; i++) {
    result = pthread_kill(tids[i], 0);
    if (result == 0) {
      fprintf(stderr, "Thread %i is still there (%p)\n", i, tids[i]);
    } else {
      fprintf(stderr, "Thread %i is gone (%p)\n", i, tids[i]);
    }
  }

  pthread_cleanup_pop(0);
  return 0;
}

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]