callback_c.c 1.3 KB

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