File: tls.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (111 lines) | stat: -rw-r--r-- 1,846 bytes parent folder | download | duplicates (8)
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
#include <config.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>

#ifdef HAVE_TLS

#define COUNT 10

static int race;
static __thread int local;
__thread int global;
extern __thread int static_extern;
extern __thread int so_extern;

/* deliberate failure */
static int *test_race(void)
{
	return &race;
}

static int *test_local(void)
{
	return &local;
}

static int *test_global(void)
{
	return &global;
}

static int *test_static_extern(void)
{
	return &static_extern;
}

static int *test_so_extern(void)
{
	return &so_extern;
}

static const struct timespec awhile = { 0, 200000000 };

typedef int *(*func_t)(void);
struct testcase {
	const char *name;
	func_t func;
        char pad[2 * (8 - sizeof(void*))];
};
static void *tls_ptr(void *p)
{
	struct testcase *test = (struct testcase *)p;
	int *ip = (*test->func)();
	int here = 0;
	int i;

	for(i = 0; i < COUNT; i++) {
		int a = (*ip)++;
		int b = here++;
		if (a != b)
			printf("tls_ptr: case \"%s\" has mismatch: *ip=%d here=%d\n",
			       test->name, a, b);
		nanosleep(&awhile, 0);
	}

	return 0;
}

int *test_so_extern(void);
int *test_so_local(void);
int *test_so_global(void);

static const struct testcase tests[] = {
#define T(t)	{ #t, test_##t }
	T(race),
	T(local),
	T(global),
	T(static_extern),
	T(so_extern),
	T(so_local),
	T(so_global),
#undef T
};

#define NTESTS	(sizeof(tests)/sizeof(*tests))

int main()
{
	pthread_t threads[NTESTS*2];
	int curthread = 0;
	static 
	int i;

	for(i = 0; i < NTESTS; i++) {
		pthread_create(&threads[curthread++], NULL, tls_ptr, (void *)&tests[i]);
		pthread_create(&threads[curthread++], NULL, tls_ptr, (void *)&tests[i]);
	}

	for(i = 0; i < curthread; i++)
		pthread_join(threads[i], NULL);

	return 0;
}
#else
int main()
{
	printf("FAILED: no compiler support for __thread\n");
	return 1;
}
#endif