Text file src/runtime/cgo/gcc_darwin_amd64.c

     1  // Copyright 2009 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  #include "libcgo_unix.h"
    10  
    11  static void* threadentry(void*);
    12  static void (*setg_gcc)(void*);
    13  
    14  void
    15  x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    16  {
    17  	setg_gcc = setg;
    18  	_cgo_set_stacklo(g, NULL);
    19  }
    20  
    21  
    22  void
    23  _cgo_sys_thread_start(ThreadStart *ts)
    24  {
    25  	pthread_attr_t attr;
    26  	sigset_t ign, oset;
    27  	pthread_t p;
    28  	size_t size;
    29  	int err;
    30  
    31  	sigfillset(&ign);
    32  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    33  
    34  	size = pthread_get_stacksize_np(pthread_self());
    35  	pthread_attr_init(&attr);
    36  	pthread_attr_setstacksize(&attr, size);
    37  	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    38  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    39  	ts->g->stackhi = size;
    40  	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    41  
    42  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    43  
    44  	if (err != 0) {
    45  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    46  		abort();
    47  	}
    48  }
    49  
    50  extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
    51  static void*
    52  threadentry(void *v)
    53  {
    54  	ThreadStart ts;
    55  
    56  	ts = *(ThreadStart*)v;
    57  	free(v);
    58  
    59  	crosscall1(ts.fn, setg_gcc, (void*)ts.g);
    60  	return nil;
    61  }
    62  

View as plain text