summaryrefslogtreecommitdiff
path: root/tests/tsd1.c
blob: aa2d3daeb5570e853cbd07cc7d9f8a3866a076cb (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
#include <pthread.h>
#include <stdio.h>

pthread_key_t key;
pthread_once_t key_once = PTHREAD_ONCE_INIT;

void
destroy_key(void * arg)
{
  /* arg is not NULL if we get to here. */
  printf("SUCCESS: %s: destroying key.\n", (char *) arg);

  free((char *) arg);

  /* Is it our responsibility to do this? */
  arg = NULL;
}

void
make_key(void)
{
  if (pthread_key_create(&key, destroy_key) != 0)
    {
      printf("Key create failed\n");
      exit(1);
    }
}

void *
mythread(void * arg)
{
  void * ptr;

  (void) pthread_once(&key_once, make_key);

  if ((ptr = pthread_getspecific(key)) != NULL)
    {
      printf("ERROR: Thread %d, Key 0x%x not initialised to NULL\n",
	     (int) arg,
	     (int) key);
      exit(1);
    }
  else
    {
      ptr = (void *) malloc(80);
      sprintf((char *) ptr, "Thread %d Key 0x%x",
	      (int) arg,
	      (int) key);
      (void) pthread_setspecific(key, ptr);
    }

  if ((ptr = pthread_getspecific(key)) == NULL)
    {
      printf("FAILED: Thread %d Key 0x%x: key value set or get failed.\n",
	     (int) arg,
	     (int) key);
      exit(1);
    }
  else
    {
      printf("SUCCESS: Thread %d Key 0x%x: key value set and get succeeded.\n",
	     (int) arg,
	     (int) key);

      printf("SUCCESS: %s: exiting thread.\n", (char *) ptr);
    }

  return 0;

  /* Exiting the thread will call the key destructor. */
}

int
main()
{
  int rc;
  pthread_t t1, t2;
  
  rc = pthread_create(&t1, NULL, mythread, (void *) 1);
  printf("pthread_create returned %d\n", rc);

  rc = pthread_create(&t2, NULL, mythread, (void *) 2);
  printf("pthread_create returned %d\n", rc);

  Sleep(2000);
  return 0;
}