]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/testcarchive: avoid possible pthread_create race
authorIan Lance Taylor <iant@golang.org>
Fri, 6 May 2016 18:32:18 +0000 (11:32 -0700)
committerIan Lance Taylor <iant@golang.org>
Fri, 6 May 2016 19:15:52 +0000 (19:15 +0000)
The old code assumed that the thread ID set by pthread_create would be
available in the newly created thread.  While that is clearly true
eventually, it is not necessarily true immediately.  Rather than try to
pass down the thread ID, just call pthread_self in the created thread.

Fixes #15576 (I hope).

Change-Id: Ic07086b00e4fd5676c04719a299c583320da64a1
Reviewed-on: https://go-review.googlesource.com/22880
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
misc/cgo/testcarchive/main4.c

index 2aaf09b7c1961ec5d16f678996aaaf17d6020e05..353f980c50d47e3336ff66ebc0770ff3b210487c 100644 (file)
@@ -44,8 +44,7 @@ static void init() {
 
 // Test raising SIGIO on a C thread with an alternate signal stack
 // when there is a Go signal handler for SIGIO.
-static void* thread1(void* arg) {
-       pthread_t* ptid = (pthread_t*)(arg);
+static void* thread1(void* arg __attribute__ ((unused))) {
        stack_t ss;
        int i;
        stack_t nss;
@@ -65,7 +64,7 @@ static void* thread1(void* arg) {
        // Send ourselves a SIGIO.  This will be caught by the Go
        // signal handler which should forward to the C signal
        // handler.
-       i = pthread_kill(*ptid, SIGIO);
+       i = pthread_kill(pthread_self(), SIGIO);
        if (i != 0) {
                fprintf(stderr, "pthread_kill: %s\n", strerror(i));
                exit(EXIT_FAILURE);
@@ -101,11 +100,11 @@ static void* thread1(void* arg) {
 
 // Test calling a Go function to raise SIGIO on a C thread with an
 // alternate signal stack when there is a Go signal handler for SIGIO.
-static void* thread2(void* arg) {
-       pthread_t* ptid = (pthread_t*)(arg);
+static void* thread2(void* arg __attribute__ ((unused))) {
        stack_t ss;
        int i;
        int oldcount;
+       pthread_t tid;
        stack_t nss;
 
        // Set up an alternate signal stack for this thread.
@@ -124,7 +123,8 @@ static void* thread2(void* arg) {
 
        // Call a Go function that will call a C function to send us a
        // SIGIO.
-       GoRaiseSIGIO(ptid);
+       tid = pthread_self();
+       GoRaiseSIGIO(&tid);
 
        // Wait until the signal has been delivered.
        i = 0;
@@ -161,7 +161,7 @@ int main(int argc, char **argv) {
        // Tell the Go library to start looking for SIGIO.
        GoCatchSIGIO();
 
-       i = pthread_create(&tid, NULL, thread1, (void*)(&tid));
+       i = pthread_create(&tid, NULL, thread1, NULL);
        if (i != 0) {
                fprintf(stderr, "pthread_create: %s\n", strerror(i));
                exit(EXIT_FAILURE);
@@ -173,7 +173,7 @@ int main(int argc, char **argv) {
                exit(EXIT_FAILURE);
        }
 
-       i = pthread_create(&tid, NULL, thread2, (void*)(&tid));
+       i = pthread_create(&tid, NULL, thread2, NULL);
        if (i != 0) {
                fprintf(stderr, "pthread_create: %s\n", strerror(i));
                exit(EXIT_FAILURE);