github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/misc/cgo/test/callback_c.c (about)

     1  // Copyright 2011 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 <unistd.h>
     7  #include "_cgo_export.h"
     8  
     9  void
    10  callback(void *f)
    11  {
    12  	// use some stack space
    13  	volatile char data[64*1024];
    14  
    15  	data[0] = 0;
    16  	goCallback(f);
    17          data[sizeof(data)-1] = 0;
    18  }
    19  
    20  void
    21  callGoFoo(void)
    22  {
    23  	extern void goFoo(void);
    24  	goFoo();
    25  }
    26  
    27  void
    28  IntoC(void)
    29  {
    30  	BackIntoGo();
    31  }
    32  
    33  #ifdef WIN32
    34  #include <windows.h>
    35  long long
    36  mysleep(int seconds) {
    37  	long long st = GetTickCount();
    38  	sleep(seconds);
    39  	return st;
    40  }
    41  #else
    42  #include <sys/time.h>
    43  long long
    44  mysleep(int seconds) {
    45  	long long st;
    46  	struct timeval tv;
    47  	gettimeofday(&tv, NULL);
    48  	st = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    49  	sleep(seconds);
    50  	return st;
    51  }
    52  #endif
    53  
    54  long long
    55  twoSleep(int n)
    56  {
    57  	BackgroundSleep(n);
    58  	return mysleep(n);
    59  }
    60  
    61  void
    62  callGoStackCheck(void)
    63  {
    64  	extern void goStackCheck(void);
    65  	goStackCheck();
    66  }
    67  
    68  int
    69  returnAfterGrow(void)
    70  {
    71  	extern int goReturnVal(void);
    72  	goReturnVal();
    73  	return 123456;
    74  }
    75  
    76  int
    77  returnAfterGrowFromGo(void)
    78  {
    79  	extern int goReturnVal(void);
    80  	return goReturnVal();
    81  }
    82