blob: 8921b7306c6b148f13611ecaca6da4b47dcbfb98 [file] [log] [blame]
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07001// Copyright 2011 The Go Authors. All rights reserved.
Russ Coxf9ca3b52011-03-07 10:37:42 -05002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Ian Lance Taylor17360ac2015-11-19 17:03:01 -08005#include <string.h>
Russ Coxf9ca3b52011-03-07 10:37:42 -05006#include <sys/types.h>
Shenghou Maaf1dd562013-05-18 02:55:44 +08007#include <unistd.h>
Russ Coxf9ca3b52011-03-07 10:37:42 -05008#include "_cgo_export.h"
9
10void
11callback(void *f)
12{
Dmitriy Vyukovfbfed492011-11-09 23:11:48 +030013 // use some stack space
14 volatile char data[64*1024];
15
16 data[0] = 0;
Russ Coxf9ca3b52011-03-07 10:37:42 -050017 goCallback(f);
Dmitriy Vyukovfbfed492011-11-09 23:11:48 +030018 data[sizeof(data)-1] = 0;
Russ Coxf9ca3b52011-03-07 10:37:42 -050019}
Russ Coxc3f43192012-03-06 23:27:30 -050020
21void
22callGoFoo(void)
23{
24 extern void goFoo(void);
25 goFoo();
26}
27
28void
29IntoC(void)
30{
31 BackIntoGo();
32}
33
Shenghou Maaf1dd562013-05-18 02:55:44 +080034#ifdef WIN32
35#include <windows.h>
36long long
37mysleep(int seconds) {
38 long long st = GetTickCount();
Alex Brainman03f3bfc2016-03-02 16:58:40 +110039 Sleep(1000 * seconds);
Shenghou Maaf1dd562013-05-18 02:55:44 +080040 return st;
41}
42#else
43#include <sys/time.h>
44long long
45mysleep(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
55long long
Russ Coxc3f43192012-03-06 23:27:30 -050056twoSleep(int n)
57{
58 BackgroundSleep(n);
Shenghou Maaf1dd562013-05-18 02:55:44 +080059 return mysleep(n);
Russ Coxc3f43192012-03-06 23:27:30 -050060}
Dmitriy Vyukov9fe4a9e2013-07-22 21:53:20 +040061
62void
63callGoStackCheck(void)
64{
65 extern void goStackCheck(void);
66 goStackCheck();
67}
Keith Randall1b6807b2014-09-25 07:59:01 -070068
69int
70returnAfterGrow(void)
71{
72 extern int goReturnVal(void);
73 goReturnVal();
74 return 123456;
75}
76
77int
78returnAfterGrowFromGo(void)
79{
80 extern int goReturnVal(void);
81 return goReturnVal();
82}
83
Ian Lance Taylor17360ac2015-11-19 17:03:01 -080084void
85callGoWithString(void)
86{
87 extern void goWithString(GoString);
88 const char *str = "string passed from C to Go";
89 goWithString((GoString){str, strlen(str)});
90}