chan.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. typedef struct WaitQ WaitQ;
  5. typedef struct SudoG SudoG;
  6. typedef struct Select Select;
  7. typedef struct Scase Scase;
  8. typedef struct __go_type_descriptor Type;
  9. typedef struct __go_channel_type ChanType;
  10. struct SudoG
  11. {
  12. G* g;
  13. uint32* selectdone;
  14. SudoG* link;
  15. int64 releasetime;
  16. byte* elem; // data element
  17. };
  18. struct WaitQ
  19. {
  20. SudoG* first;
  21. SudoG* last;
  22. };
  23. // The garbage collector is assuming that Hchan can only contain pointers into the stack
  24. // and cannot contain pointers into the heap.
  25. struct Hchan
  26. {
  27. uintgo qcount; // total data in the q
  28. uintgo dataqsiz; // size of the circular q
  29. uint16 elemsize;
  30. uint16 pad; // ensures proper alignment of the buffer that follows Hchan in memory
  31. bool closed;
  32. const Type* elemtype; // element type
  33. uintgo sendx; // send index
  34. uintgo recvx; // receive index
  35. WaitQ recvq; // list of recv waiters
  36. WaitQ sendq; // list of send waiters
  37. Lock;
  38. };
  39. // Buffer follows Hchan immediately in memory.
  40. // chanbuf(c, i) is pointer to the i'th slot in the buffer.
  41. #define chanbuf(c, i) ((byte*)((c)+1)+(uintptr)(c)->elemsize*(i))
  42. enum
  43. {
  44. debug = 0,
  45. // Scase.kind
  46. CaseRecv,
  47. CaseSend,
  48. CaseDefault,
  49. };
  50. struct Scase
  51. {
  52. SudoG sg; // must be first member (cast to Scase)
  53. Hchan* chan; // chan
  54. uint16 kind;
  55. uint16 index; // index to return
  56. bool* receivedp; // pointer to received bool (recv2)
  57. };
  58. struct Select
  59. {
  60. uint16 tcase; // total count of scase[]
  61. uint16 ncase; // currently filled scase[]
  62. uint16* pollorder; // case poll order
  63. Hchan** lockorder; // channel lock order
  64. Scase scase[1]; // one per case (in order of appearance)
  65. };