Skip to content

Commit a1813ae

Browse files
misc/cgo/testcarchive: avoid possible pthread_create race
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 <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent f0e2d32 commit a1813ae

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

misc/cgo/testcarchive/main4.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ static void init() {
4444

4545
// Test raising SIGIO on a C thread with an alternate signal stack
4646
// when there is a Go signal handler for SIGIO.
47-
static void* thread1(void* arg) {
48-
pthread_t* ptid = (pthread_t*)(arg);
47+
static void* thread1(void* arg __attribute__ ((unused))) {
4948
stack_t ss;
5049
int i;
5150
stack_t nss;
@@ -65,7 +64,7 @@ static void* thread1(void* arg) {
6564
// Send ourselves a SIGIO. This will be caught by the Go
6665
// signal handler which should forward to the C signal
6766
// handler.
68-
i = pthread_kill(*ptid, SIGIO);
67+
i = pthread_kill(pthread_self(), SIGIO);
6968
if (i != 0) {
7069
fprintf(stderr, "pthread_kill: %s\n", strerror(i));
7170
exit(EXIT_FAILURE);
@@ -101,11 +100,11 @@ static void* thread1(void* arg) {
101100

102101
// Test calling a Go function to raise SIGIO on a C thread with an
103102
// alternate signal stack when there is a Go signal handler for SIGIO.
104-
static void* thread2(void* arg) {
105-
pthread_t* ptid = (pthread_t*)(arg);
103+
static void* thread2(void* arg __attribute__ ((unused))) {
106104
stack_t ss;
107105
int i;
108106
int oldcount;
107+
pthread_t tid;
109108
stack_t nss;
110109

111110
// Set up an alternate signal stack for this thread.
@@ -124,7 +123,8 @@ static void* thread2(void* arg) {
124123

125124
// Call a Go function that will call a C function to send us a
126125
// SIGIO.
127-
GoRaiseSIGIO(ptid);
126+
tid = pthread_self();
127+
GoRaiseSIGIO(&tid);
128128

129129
// Wait until the signal has been delivered.
130130
i = 0;
@@ -161,7 +161,7 @@ int main(int argc, char **argv) {
161161
// Tell the Go library to start looking for SIGIO.
162162
GoCatchSIGIO();
163163

164-
i = pthread_create(&tid, NULL, thread1, (void*)(&tid));
164+
i = pthread_create(&tid, NULL, thread1, NULL);
165165
if (i != 0) {
166166
fprintf(stderr, "pthread_create: %s\n", strerror(i));
167167
exit(EXIT_FAILURE);
@@ -173,7 +173,7 @@ int main(int argc, char **argv) {
173173
exit(EXIT_FAILURE);
174174
}
175175

176-
i = pthread_create(&tid, NULL, thread2, (void*)(&tid));
176+
i = pthread_create(&tid, NULL, thread2, NULL);
177177
if (i != 0) {
178178
fprintf(stderr, "pthread_create: %s\n", strerror(i));
179179
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)