github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/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  // 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  	// If thread local slots are laid out as we expect, our magic word will
    29  	// be located at some low offset from tlsbase. However, just in case something went
    30  	// wrong, the search is limited to sensible offsets. PTHREAD_KEYS_MAX was the
    31  	// original limit, but issue 19472 made a higher limit necessary.
    32  	for (i=0; i<384; i++) {
    33  		if (*(tlsbase+i) == (void*)magic1) {
    34  			*tlsg = (void*)(i*sizeof(void *));
    35  			pthread_setspecific(k, 0);
    36  			return;
    37  		}
    38  	}
    39  	fatalf("could not find pthread key");
    40  }
    41  
    42  void (*x_cgo_inittls)(void **tlsg, void **tlsbase) = inittls;