...
Run Format

Text file src/runtime/cgo/gcc_android_386.c

Documentation: runtime/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 <string.h> /* for strerror */
     6	#include <pthread.h>
     7	#include <signal.h>
     8	#include "libcgo.h"
     9	
    10	static void* threadentry(void*);
    11	static pthread_key_t k1;
    12	
    13	#define magic1 (0x23581321U)
    14	
    15	static void
    16	inittls(void)
    17	{
    18		uint32 x;
    19		pthread_key_t tofree[128], k;
    20		int i, ntofree;
    21	
    22		/*
    23		 * Same logic, code as gcc_android_amd64.c:/inittls.
    24		 * Note that this is a temporary hack that should be fixed soon.
    25		 *
    26		 * TODO: fix this.
    27		 *
    28		 * The linker and runtime hard-code this constant offset
    29		 * from %gs where we expect to find g. Disgusting.
    30		 *
    31		 * Known to src/cmd/link/internal/ld/sym.go:/0xf8
    32		 * and to src/runtime/sys_linux_386.s:/0xf8 or /GOOS_android.
    33		 * TODO(hyangah): check 0xb0 works with API23+
    34		 *
    35		 * As disgusting as on the darwin/386, darwin/amd64.
    36		 */
    37		ntofree = 0;
    38		for(;;) {
    39			if(pthread_key_create(&k, nil) < 0) {
    40				fprintf(stderr, "runtime/cgo: pthread_key_create failed\n");
    41				abort();
    42			}
    43			pthread_setspecific(k, (void*)magic1);
    44			asm volatile("movl %%gs:0xf8, %0" : "=r"(x));
    45			pthread_setspecific(k, 0);
    46			if (x == magic1) {
    47				k1 = k;
    48				break;
    49			}
    50			if(ntofree >= nelem(tofree)) {
    51				fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n");
    52				fprintf(stderr, "\ttried");
    53				for(i=0; i<ntofree; i++)
    54					fprintf(stderr, " %#x", (unsigned)tofree[i]);
    55				fprintf(stderr, "\n");
    56				abort();
    57			}
    58			tofree[ntofree++] = k;
    59		}
    60		// TODO: output to stderr is not useful for apps.
    61		// Can we fall back to Android's log library?
    62	
    63		/*
    64		 * We got the key we wanted.  Free the others.
    65		 */
    66		for(i=0; i<ntofree; i++) {
    67			pthread_key_delete(tofree[i]);
    68		}
    69	}
    70	
    71	
    72	static void*
    73	threadentry(void *v)
    74	{
    75		ThreadStart ts;
    76	
    77		ts = *(ThreadStart*)v;
    78		free(v);
    79	
    80		pthread_setspecific(k1, (void*)ts.g);
    81	
    82		crosscall_386(ts.fn);
    83		return nil;
    84	}
    85	
    86	void (*x_cgo_inittls)(void) = inittls;
    87	void* (*x_cgo_threadentry)(void*) = threadentry;

View as plain text