File: test_thread_specifier.c

package info (click to toggle)
freshplayerplugin 0.3.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 5,324 kB
  • ctags: 13,257
  • sloc: ansic: 45,542; cpp: 26,176; yacc: 1,707; lex: 910; python: 176; sh: 87; makefile: 21
file content (67 lines) | stat: -rw-r--r-- 1,287 bytes parent folder | download | duplicates (2)
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
// testing that "__thread" specifier works

#undef NDEBUG
#include <assert.h>
#include <pthread.h>
#include <stdio.h>


static pthread_barrier_t b1, b2;
static pthread_t         t;
static __thread int      v;

static int *vaddr1;
static int *vaddr2;

static
void *
thread_func(void *param)
{
    vaddr2 = &v;
    v = 2;    // setting this shouldn't change "v" value in main thread

    pthread_barrier_wait(&b1);
    pthread_barrier_wait(&b2);
    return NULL;
}

int
main(void)
{
    int ret;

    ret = pthread_barrier_init(&b1, NULL, 2);
    assert(ret == 0);
    ret = pthread_barrier_init(&b2, NULL, 2);
    assert(ret == 0);

    vaddr1 = &v;
    v = 1;  // assign thread local variable a value

    ret = pthread_create(&t, NULL, thread_func, NULL);
    assert(ret == 0);

    // wait for thread to set "v"
    ret = pthread_barrier_wait(&b1);
    assert(ret == 0);

    // ensure this "v" is not affected by other thread
    assert(v == 1);

    // allow other thread to continue
    pthread_barrier_wait(&b2);
    pthread_join(t, NULL);

    ret = pthread_barrier_destroy(&b1);
    assert(ret == 0);
    ret = pthread_barrier_destroy(&b2);
    assert(ret == 0);

    printf("vaddr1 = %p\n", vaddr1);
    printf("vaddr2 = %p\n", vaddr2);

    printf("pass\n");
    return 0;
}