PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/src/pkg/runtime/runtime.h

https://code.google.com/p/golang-on-cygwin/
C Header | 505 lines | 377 code | 31 blank | 97 comment | 0 complexity | 47b08bdeabba7b0333966a9d45463273 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright 2009 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. * basic types
  6. */
  7. typedef signed char int8;
  8. typedef unsigned char uint8;
  9. typedef signed short int16;
  10. typedef unsigned short uint16;
  11. typedef signed int int32;
  12. typedef unsigned int uint32;
  13. typedef signed long long int int64;
  14. typedef unsigned long long int uint64;
  15. typedef float float32;
  16. typedef double float64;
  17. #ifdef _64BIT
  18. typedef uint64 uintptr;
  19. #else
  20. typedef uint32 uintptr;
  21. #endif
  22. /*
  23. * get rid of C types
  24. * the / / / forces a syntax error immediately,
  25. * which will show "last name: XXunsigned".
  26. */
  27. #define unsigned XXunsigned / / /
  28. #define signed XXsigned / / /
  29. #define char XXchar / / /
  30. #define short XXshort / / /
  31. #define int XXint / / /
  32. #define long XXlong / / /
  33. #define float XXfloat / / /
  34. #define double XXdouble / / /
  35. /*
  36. * defined types
  37. */
  38. typedef uint8 bool;
  39. typedef uint8 byte;
  40. typedef struct Alg Alg;
  41. typedef struct Func Func;
  42. typedef struct G G;
  43. typedef struct Gobuf Gobuf;
  44. typedef struct Lock Lock;
  45. typedef struct M M;
  46. typedef struct Mem Mem;
  47. typedef union Note Note;
  48. typedef struct Slice Slice;
  49. typedef struct Stktop Stktop;
  50. typedef struct String String;
  51. typedef struct Usema Usema;
  52. typedef struct SigTab SigTab;
  53. typedef struct MCache MCache;
  54. typedef struct Iface Iface;
  55. typedef struct Itab Itab;
  56. typedef struct Eface Eface;
  57. typedef struct Type Type;
  58. typedef struct Defer Defer;
  59. typedef struct hash Hmap;
  60. typedef struct Hchan Hchan;
  61. /*
  62. * per-cpu declaration.
  63. * "extern register" is a special storage class implemented by 6c, 8c, etc.
  64. * on machines with lots of registers, it allocates a register that will not be
  65. * used in generated code. on the x86, it allocates a slot indexed by a
  66. * segment register.
  67. *
  68. * amd64: allocated downwards from R15
  69. * x86: allocated upwards from 0(FS)
  70. * arm: allocated upwards from R9
  71. *
  72. * every C file linked into a Go program must include runtime.h
  73. * so that the C compiler knows to avoid other uses of these registers.
  74. * the Go compilers know to avoid them.
  75. */
  76. extern register G* g;
  77. extern register M* m;
  78. /*
  79. * defined constants
  80. */
  81. enum
  82. {
  83. // G status
  84. Gidle,
  85. Grunnable,
  86. Grunning,
  87. Gsyscall,
  88. Gwaiting,
  89. Gmoribund,
  90. Gdead,
  91. };
  92. enum
  93. {
  94. true = 1,
  95. false = 0,
  96. };
  97. /*
  98. * structures
  99. */
  100. struct Lock
  101. {
  102. uint32 key;
  103. uint32 sema; // for OS X
  104. };
  105. struct Usema
  106. {
  107. uint32 u;
  108. uint32 k;
  109. };
  110. union Note
  111. {
  112. struct { // Linux
  113. Lock lock;
  114. };
  115. struct { // OS X
  116. int32 wakeup;
  117. Usema sema;
  118. };
  119. };
  120. struct String
  121. {
  122. byte* str;
  123. int32 len;
  124. };
  125. struct Iface
  126. {
  127. Itab* tab;
  128. void* data;
  129. };
  130. struct Eface
  131. {
  132. Type* type;
  133. void* data;
  134. };
  135. struct Slice
  136. { // must not move anything
  137. byte* array; // actual data
  138. uint32 len; // number of elements
  139. uint32 cap; // allocated number of elements
  140. };
  141. struct Gobuf
  142. {
  143. // The offsets of these fields are known to (hard-coded in) libmach.
  144. byte* sp;
  145. byte* pc;
  146. G* g;
  147. };
  148. struct G
  149. {
  150. byte* stackguard; // cannot move - also known to linker, libmach, libcgo
  151. byte* stackbase; // cannot move - also known to libmach, libcgo
  152. Defer* defer;
  153. Gobuf sched; // cannot move - also known to libmach
  154. byte* stack0;
  155. byte* entry; // initial function
  156. G* alllink; // on allg
  157. void* param; // passed parameter on wakeup
  158. int16 status;
  159. int32 goid;
  160. int32 selgen; // valid sudog pointer
  161. G* schedlink;
  162. bool readyonstop;
  163. M* m; // for debuggers, but offset not hard-coded
  164. M* lockedm;
  165. void (*cgofn)(void*); // for cgo/ffi
  166. void *cgoarg;
  167. };
  168. struct Mem
  169. {
  170. uint8* hunk;
  171. uint32 nhunk;
  172. uint64 nmmap;
  173. uint64 nmal;
  174. };
  175. struct M
  176. {
  177. // The offsets of these fields are known to (hard-coded in) libmach.
  178. G* g0; // goroutine with scheduling stack
  179. void (*morepc)(void);
  180. void* morefp; // frame pointer for more stack
  181. Gobuf morebuf; // gobuf arg to morestack
  182. // Fields not known to debuggers.
  183. uint32 moreframe; // size arguments to morestack
  184. uint32 moreargs;
  185. uintptr cret; // return value from C
  186. uint64 procid; // for debuggers, but offset not hard-coded
  187. G* gsignal; // signal-handling G
  188. uint32 tls[8]; // thread-local storage (for 386 extern register)
  189. Gobuf sched; // scheduling stack
  190. G* curg; // current running goroutine
  191. int32 id;
  192. int32 mallocing;
  193. int32 gcing;
  194. int32 locks;
  195. int32 waitnextg;
  196. Note havenextg;
  197. G* nextg;
  198. M* alllink; // on allm
  199. M* schedlink;
  200. Mem mem;
  201. uint32 machport; // Return address for Mach IPC (OS X)
  202. MCache *mcache;
  203. G* lockedg;
  204. };
  205. struct Stktop
  206. {
  207. // The offsets of these fields are known to (hard-coded in) libmach.
  208. uint8* stackguard;
  209. uint8* stackbase;
  210. Gobuf gobuf;
  211. uint32 args;
  212. // Frame pointer: where args start in old frame.
  213. // fp == gobuf.sp except in the case of a reflected
  214. // function call, which uses an off-stack argument frame.
  215. uint8* fp;
  216. };
  217. struct Alg
  218. {
  219. uintptr (*hash)(uint32, void*);
  220. uint32 (*equal)(uint32, void*, void*);
  221. void (*print)(uint32, void*);
  222. void (*copy)(uint32, void*, void*);
  223. };
  224. struct SigTab
  225. {
  226. int32 flags;
  227. int8 *name;
  228. };
  229. enum
  230. {
  231. SigCatch = 1<<0,
  232. SigIgnore = 1<<1,
  233. SigRestart = 1<<2,
  234. };
  235. // (will be) shared with go; edit ../cmd/6g/sys.go too.
  236. // should move out of sys.go eventually.
  237. // also eventually, the loaded symbol table should
  238. // be closer to this form.
  239. struct Func
  240. {
  241. String name;
  242. String type; // go type string
  243. String src; // src file name
  244. uint64 entry; // entry pc
  245. int64 frame; // stack frame size
  246. Slice pcln; // pc/ln tab for this func
  247. int64 pc0; // starting pc, ln for table
  248. int32 ln0;
  249. int32 args; // number of 32-bit in/out args
  250. int32 locals; // number of 32-bit locals
  251. };
  252. /*
  253. * defined macros
  254. * you need super-goru privilege
  255. * to add this list.
  256. */
  257. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  258. #define nil ((void*)0)
  259. /*
  260. * known to compiler
  261. */
  262. enum
  263. {
  264. AMEM,
  265. ANOEQ,
  266. ASTRING,
  267. AINTER,
  268. ANILINTER,
  269. AFAKE,
  270. Amax
  271. };
  272. enum {
  273. Structrnd = sizeof(uintptr)
  274. };
  275. /*
  276. * deferred subroutine calls
  277. */
  278. struct Defer
  279. {
  280. int32 siz;
  281. byte* sp;
  282. byte* fn;
  283. Defer* link;
  284. byte args[8]; // padded to actual size
  285. };
  286. /*
  287. * external data
  288. */
  289. extern Alg algarray[Amax];
  290. extern String emptystring;
  291. G* allg;
  292. M* allm;
  293. int32 goidgen;
  294. extern int32 gomaxprocs;
  295. extern int32 panicking;
  296. extern int32 maxround;
  297. extern int32 fd; // usually 1; set to 2 when panicking
  298. int8* goos;
  299. /*
  300. * common functions and data
  301. */
  302. int32 strcmp(byte*, byte*);
  303. int32 findnull(byte*);
  304. void dump(byte*, int32);
  305. int32 runetochar(byte*, int32);
  306. int32 charntorune(int32*, uint8*, int32);
  307. /*
  308. * very low level c-called
  309. */
  310. void gogo(Gobuf*, uintptr);
  311. void gogocall(Gobuf*, void(*)(void));
  312. uintptr gosave(Gobuf*);
  313. void runtimeˇlessstack(void);
  314. void goargs(void);
  315. void FLUSH(void*);
  316. void* getu(void);
  317. void throw(int8*);
  318. uint32 rnd(uint32, uint32);
  319. void prints(int8*);
  320. void printf(int8*, ...);
  321. byte* mchr(byte*, byte, byte*);
  322. void mcpy(byte*, byte*, uint32);
  323. int32 mcmp(byte*, byte*, uint32);
  324. void mmov(byte*, byte*, uint32);
  325. void* mal(uint32);
  326. uint32 cmpstring(String, String);
  327. String gostring(byte*);
  328. void initsig(void);
  329. int32 gotraceback(void);
  330. void traceback(uint8 *pc, uint8 *sp, G* gp);
  331. void tracebackothers(G*);
  332. int32 open(byte*, int32, ...);
  333. int32 write(int32, void*, int32);
  334. bool cas(uint32*, uint32, uint32);
  335. void jmpdefer(byte*, void*);
  336. void exit1(int32);
  337. void ready(G*);
  338. byte* getenv(int8*);
  339. int32 atoi(byte*);
  340. void newosproc(M *m, G *g, void *stk, void (*fn)(void));
  341. void signalstack(byte*, int32);
  342. G* malg(int32);
  343. void minit(void);
  344. Func* findfunc(uintptr);
  345. int32 funcline(Func*, uint64);
  346. void* stackalloc(uint32);
  347. void stackfree(void*);
  348. MCache* allocmcache(void);
  349. void mallocinit(void);
  350. bool ifaceeq(Iface, Iface);
  351. bool efaceeq(Eface, Eface);
  352. uintptr ifacehash(Iface);
  353. uintptr efacehash(Eface);
  354. uintptr nohash(uint32, void*);
  355. uint32 noequal(uint32, void*, void*);
  356. void* malloc(uintptr size);
  357. void* mallocgc(uintptr size);
  358. void free(void *v);
  359. void exit(int32);
  360. void breakpoint(void);
  361. void gosched(void);
  362. void goexit(void);
  363. void runcgo(void (*fn)(void*), void*);
  364. #pragma varargck argpos printf 1
  365. #pragma varargck type "d" int32
  366. #pragma varargck type "d" uint32
  367. #pragma varargck type "D" int64
  368. #pragma varargck type "D" uint64
  369. #pragma varargck type "x" int32
  370. #pragma varargck type "x" uint32
  371. #pragma varargck type "X" int64
  372. #pragma varargck type "X" uint64
  373. #pragma varargck type "p" void*
  374. #pragma varargck type "p" uintptr
  375. #pragma varargck type "s" int8*
  376. #pragma varargck type "s" uint8*
  377. #pragma varargck type "S" String
  378. // TODO(rsc): Remove. These are only temporary,
  379. // for the mark and sweep collector.
  380. void stoptheworld(void);
  381. void starttheworld(void);
  382. /*
  383. * mutual exclusion locks. in the uncontended case,
  384. * as fast as spin locks (just a few user-level instructions),
  385. * but on the contention path they sleep in the kernel.
  386. * a zeroed Lock is unlocked (no need to initialize each lock).
  387. */
  388. void lock(Lock*);
  389. void unlock(Lock*);
  390. /*
  391. * sleep and wakeup on one-time events.
  392. * before any calls to notesleep or notewakeup,
  393. * must call noteclear to initialize the Note.
  394. * then, any number of threads can call notesleep
  395. * and exactly one thread can call notewakeup (once).
  396. * once notewakeup has been called, all the notesleeps
  397. * will return. future notesleeps will return immediately.
  398. */
  399. void noteclear(Note*);
  400. void notesleep(Note*);
  401. void notewakeup(Note*);
  402. /*
  403. * Redefine methods for the benefit of gcc, which does not support
  404. * UTF-8 characters in identifiers.
  405. */
  406. #ifndef __GNUC__
  407. #define runtime_memclr runtimeˇmemclr
  408. #define runtime_getcallerpc runtimeˇgetcallerpc
  409. #define runtime_mmap runtimeˇmmap
  410. #define runtime_printslice runtimeˇprintslice
  411. #define runtime_printbool runtimeˇprintbool
  412. #define runtime_printfloat runtimeˇprintfloat
  413. #define runtime_printhex runtimeˇprinthex
  414. #define runtime_printint runtimeˇprintint
  415. #define runtime_printiface runtimeˇprintiface
  416. #define runtime_printeface runtimeˇprinteface
  417. #define runtime_printpc runtimeˇprintpc
  418. #define runtime_printpointer runtimeˇprintpointer
  419. #define runtime_printstring runtimeˇprintstring
  420. #define runtime_printuint runtimeˇprintuint
  421. #define runtime_setcallerpc runtimeˇsetcallerpc
  422. #endif
  423. /*
  424. * low level go-called
  425. */
  426. uint8* runtime_mmap(byte*, uint32, int32, int32, int32, uint32);
  427. void runtime_memclr(byte*, uint32);
  428. void runtime_setcallerpc(void*, void*);
  429. void* runtime_getcallerpc(void*);
  430. /*
  431. * runtime go-called
  432. */
  433. void runtime_printbool(bool);
  434. void runtime_printfloat(float64);
  435. void runtime_printint(int64);
  436. void runtime_printiface(Iface);
  437. void runtime_printeface(Eface);
  438. void runtime_printstring(String);
  439. void runtime_printpc(void*);
  440. void runtime_printpointer(void*);
  441. void runtime_printuint(uint64);
  442. void runtime_printhex(uint64);
  443. void runtime_printslice(Slice);
  444. /*
  445. * wrapped for go users
  446. */
  447. float64 Inf(int32 sign);
  448. float64 NaN(void);
  449. float32 float32frombits(uint32 i);
  450. uint32 float32tobits(float32 f);
  451. float64 float64frombits(uint64 i);
  452. uint64 float64tobits(float64 f);
  453. float64 frexp(float64 d, int32 *ep);
  454. bool isInf(float64 f, int32 sign);
  455. bool isNaN(float64 f);
  456. float64 ldexp(float64 d, int32 e);
  457. float64 modf(float64 d, float64 *ip);
  458. void semacquire(uint32*);
  459. void semrelease(uint32*);
  460. void mapassign(Hmap*, byte*, byte*);
  461. void mapaccess(Hmap*, byte*, byte*, bool*);
  462. struct hash_iter* mapiterinit(Hmap*);
  463. void mapiternext(struct hash_iter*);
  464. bool mapiterkey(struct hash_iter*, void*);
  465. void mapiterkeyvalue(struct hash_iter*, void*, void*);
  466. Hmap* makemap(Type*, Type*, uint32);
  467. Hchan* makechan(Type*, uint32);
  468. void chansend(Hchan*, void*, bool*);
  469. void chanrecv(Hchan*, void*, bool*);
  470. void chanclose(Hchan*);
  471. bool chanclosed(Hchan*);
  472. int32 chanlen(Hchan*);
  473. int32 chancap(Hchan*);
  474. void ifaceE2I(struct InterfaceType*, Eface, Iface*);