]> git.wh0rd.org Git - ICEs.git/blob - 194076/nptl_test.c
initial import
[ICEs.git] / 194076 / nptl_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 pthread_attr_t  __thread_attr;
6 pthread_t       __thread_id;
7
8 pthread_mutex_t     __posix_mutex;
9 pthread_mutexattr_t __posix_mutexattr;
10
11 /// entry point for the spawned thread
12 void* __pthread_launcher(void* p) {
13     // let the thread be killable under any circumstances
14     // (without this function call, this test always succeeds !)
15     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
16
17     printf("2nd thread is running\n");
18     fflush(stdout);
19
20     // this will block this 2nd thread, since we already
21     // locked this mutex by the main thread
22     pthread_mutex_lock(&__posix_mutex);
23
24     // here we would access shared resources, etc.
25
26     // just pro forma, the remainder of this function is actually not of
27     // interest, since this thread is always terminated at its mutex lock call
28     pthread_mutex_unlock(&__posix_mutex);
29     return NULL;
30 }
31
32 int main() {
33     // initialize mutex and thread attribute
34     pthread_mutexattr_init(&__posix_mutexattr);
35     pthread_mutex_init(&__posix_mutex, &__posix_mutexattr);
36     pthread_attr_init(&__thread_attr);
37
38     // already lock the mutex by the main thread ...
39     pthread_mutex_lock(&__posix_mutex);
40
41     int res;
42
43     // create and run a 2nd thread
44     res = pthread_create(&__thread_id, &__thread_attr, __pthread_launcher, NULL);
45     if (res) {
46         printf("thread creation failed\n");
47         fflush(stdout);
48         exit(-1);
49     }
50
51     // give the other thread a chance to spawn
52     usleep(400000);
53
54     // kill the other thread
55     pthread_cancel(__thread_id);
56     pthread_detach(__thread_id);
57
58     // give the other thread a chance to finish its execution
59     usleep(400000);
60
61     // cleanup
62     // (just pro forma, doesnt actually matter for this test case)
63     pthread_attr_destroy(&__thread_attr);
64     pthread_mutex_destroy(&__posix_mutex);
65     pthread_mutexattr_destroy(&__posix_mutexattr);
66
67     printf("TEST PASSED! :-)\n");
68
69     return res;
70 }