Home | History | Annotate | Download | only in 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 <sys/types.h>
      6 #include <errno.h>
      7 #include <pthread.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include "libcgo.h"
     11 
     12 // The context function, used when tracing back C calls into Go.
     13 static void (*cgo_context_function)(struct context_arg*);
     14 
     15 void
     16 x_cgo_sys_thread_create(void* (*func)(void*), void* arg) {
     17 	fprintf(stderr, "x_cgo_sys_thread_create not implemented");
     18 	abort();
     19 }
     20 
     21 uintptr_t
     22 _cgo_wait_runtime_init_done() {
     23 	void (*pfn)(struct context_arg*);
     24 
     25 	// TODO(spetrovic): implement this method.
     26 
     27 	pfn = _cgo_get_context_function();
     28 	if (pfn != nil) {
     29 		struct context_arg arg;
     30 
     31 		arg.Context = 0;
     32 		(*pfn)(&arg);
     33 		return arg.Context;
     34 	}
     35 	return 0;
     36 }
     37 
     38 void
     39 x_cgo_notify_runtime_init_done(void* dummy) {
     40 	// TODO(spetrovic): implement this method.
     41 }
     42 
     43 // Sets the context function to call to record the traceback context
     44 // when calling a Go function from C code. Called from runtime.SetCgoTraceback.
     45 void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
     46 	// TODO(iant): Needs synchronization.
     47 	cgo_context_function = context;
     48 }
     49 
     50 // Gets the context function.
     51 void (*(_cgo_get_context_function(void)))(struct context_arg*) {
     52 	return cgo_context_function;
     53 }
     54 
     55 // _cgo_try_pthread_create retries sys_pthread_create if it fails with
     56 // EAGAIN.
     57 int
     58 _cgo_openbsd_try_pthread_create(int (*sys_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*),
     59 	pthread_t* thread, const pthread_attr_t* attr, void* (*pfn)(void*), void* arg) {
     60 	int tries;
     61 	int err;
     62 	struct timespec ts;
     63 
     64 	for (tries = 0; tries < 100; tries++) {
     65 		err = sys_pthread_create(thread, attr, pfn, arg);
     66 		if (err != EAGAIN) {
     67 			return err;
     68 		}
     69 		ts.tv_sec = 0;
     70 		ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds.
     71 		nanosleep(&ts, nil);
     72 	}
     73 	return EAGAIN;
     74 }
     75