summaryrefslogtreecommitdiff
blob: 1af709a8cab1d422ef4401e2b2d178df86f863c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>


static volatile bool destr_called;
static volatile bool except_caught;

static pthread_barrier_t b;


struct monitor
{
  // gcc is broken and would generate a warning without this dummy
  // constructor.
  monitor () { }
  ~monitor() { destr_called = true; }
};


static void *
tf (void *arg)
{
  sem_t *s = static_cast<sem_t *> (arg);

  try
    {
      monitor m;

      pthread_barrier_wait (&b);

      while (1)
      sem_wait (s);
    }
  catch (...)
    {
      except_caught = true;
      throw;
    }

  return NULL;
}


static int
do_test ()
{
  if (pthread_barrier_init (&b, NULL, 2) != 0)
    {
      puts ("barrier_init failed");
      return 1;
    }

  sem_t s;
  if (sem_init (&s, 0, 0) != 0)
    {
      puts ("sem_init failed");
      return 1;
    }

  pthread_t th;
  if (pthread_create (&th, NULL, tf, &s) != 0)
    {
      puts ("pthread_create failed");
      return 1;
    }

  pthread_barrier_wait (&b);

  /* There is unfortunately no better method to try to assure the
     child thread reached the sem_wait call and is actually waiting
     than to sleep here.  */
  sleep (1);

  if (pthread_cancel (th) != 0)
    {
      puts ("cancel failed");
      return 1;
    }

  void *res;
  if (pthread_join (th, &res) != 0)
    {
      puts ("join failed");
      return 1;
    }

  if (res != PTHREAD_CANCELED)
    {
      puts ("thread was not canceled");
      return 1;
    }

  if (! except_caught)
    {
      puts ("exception not caught");
      return 1;
    }

  if (! destr_called)
    {
      puts ("destructor not called");
      return 1;
    }

  return 0;
}

#define TEST_FUNCTION do_test ()
#define TIMEOUT 3
#include "../test-skeleton.c"