Home | History | Annotate | Download | only in cgo
      1 // Copyright 2015 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 #include <pthread.h>
      6 #include <signal.h>
      7 #include <stdio.h>
      8 #include <sys/limits.h>
      9 #include "libcgo.h"
     10 
     11 #define magic1 (0x23581321345589ULL)
     12 
     13 // inittls allocates a thread-local storage slot for g.
     14 //
     15 // It finds the first available slot using pthread_key_create and uses
     16 // it as the offset value for runtime.tlsg.
     17 static void
     18 inittls(void **tlsg, void **tlsbase)
     19 {
     20 	pthread_key_t k;
     21 	int i, err;
     22 
     23 	err = pthread_key_create(&k, nil);
     24 	if(err != 0) {
     25 		fatalf("pthread_key_create failed: %d", err);
     26 	}
     27 	pthread_setspecific(k, (void*)magic1);
     28 	for (i=0; i<PTHREAD_KEYS_MAX; i++) {
     29 		if (*(tlsbase+i) == (void*)magic1) {
     30 			*tlsg = (void*)(i*sizeof(void *));
     31 			pthread_setspecific(k, 0);
     32 			return;
     33 		}
     34 	}
     35 	fatalf("could not find pthread key");
     36 }
     37 
     38 void (*x_cgo_inittls)(void **tlsg, void **tlsbase) = inittls;
     39