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