github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/runtime/cgo/gcc_android_arm.c (about)

     1  // Copyright 2014 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 (0x23581321U)
    12  
    13  // PTHREAD_KEYS_MAX has been added to sys/limits.h at head in bionic:
    14  // https://android.googlesource.com/platform/bionic/+/master/libc/include/sys/limits.h
    15  // TODO(crawshaw): remove this definition when a new NDK is released.
    16  #define PTHREAD_KEYS_MAX 128
    17  
    18  // inittls allocates a thread-local storage slot for g.
    19  //
    20  // It finds the first available slot using pthread_key_create and uses
    21  // it as the offset value for runtime.tlsg.
    22  static void
    23  inittls(void **tlsg, void **tlsbase)
    24  {
    25  	pthread_key_t k;
    26  	int i, err;
    27  
    28  	err = pthread_key_create(&k, nil);
    29  	if(err != 0) {
    30  		fatalf("pthread_key_create failed: %d", err);
    31  	}
    32  	pthread_setspecific(k, (void*)magic1);
    33  	for (i=0; i<PTHREAD_KEYS_MAX; i++) {
    34  		if (*(tlsbase+i) == (void*)magic1) {
    35  			*tlsg = (void*)(i*sizeof(void *));
    36  			pthread_setspecific(k, 0);
    37  			return;
    38  		}
    39  	}
    40  	fatalf("could not find pthread key");
    41  }
    42  
    43  void (*x_cgo_inittls)(void **tlsg, void **tlsbase) = inittls;