1 // commit: fb11b6b85e1e01daf17228be32d7f98b47517363 2011-02-19
2 // pthread_exit should call dtors (even in the last thread)
11 #define TEST(r, f) if (((r)=(f))) t_error(#f " failed: %s\n", strerror(r))
12 #define TESTC(c, m) ( (c) || (t_error("%s failed (" m ")\n", #c), 0) )
14 static pthread_key_t k
;
17 static void dtor(void *p
)
22 static void *start(void *arg
)
24 if (pthread_setspecific(k
, arg
))
29 static void cleanup(void)
31 TESTC(data
== 1, "dtor was not run for the last thread");
46 // test if atexit handlers are run after pthread_exit
47 // (early musl failed this test)
51 t_error("fork failed: %s\n", strerror(errno
));
57 if (waitpid(pid
, &r
, 0) != pid
) {
58 t_error("waitpid failed: %s\n", strerror(errno
));
61 if (!WIFEXITED(r
) || WEXITSTATUS(r
) != 1) {
62 t_error("atexit handler was not run after last thread exited"
63 " (exited=%d, signal=%d, status=%d, want exit status=1)\n",
64 WIFEXITED(r
), !WIFEXITED(r
)&&WIFSIGNALED(r
)?WTERMSIG(r
):0, WIFEXITED(r
)?WEXITSTATUS(r
):0);
69 // dtor should set tsd (arg and data) from 0 to 1
70 if (atexit(cleanup
)) {
71 t_error("atexit failed\n");
74 TEST(r
, pthread_key_create(&k
, dtor
));
75 TEST(r
, pthread_setspecific(k
, &data
));
76 TEST(r
, pthread_create(&td
, 0, start
, &arg
));
77 TEST(r
, pthread_join(td
, &res
));
78 TESTC(res
== 0, "pthread_setspecific failed in thread");
79 TESTC(arg
== 1, "dtor failed to run");
80 TESTC(data
== 0, "tsd in main thread is corrupted");
81 TESTC(pthread_getspecific(k
) == &data
, "tsd in main thread is corrupted");