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