thread.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma src "/sys/src/libthread"
  2. #pragma lib "libthread.a"
  3. #pragma varargck argpos chanprint 2
  4. typedef struct Alt Alt;
  5. typedef struct Channel Channel;
  6. typedef struct Ref Ref;
  7. /* Channel structure. S is the size of the buffer. For unbuffered channels
  8. * s is zero. v is an array of s values. If s is zero, v is unused.
  9. * f and n represent the state of the queue pointed to by v.
  10. */
  11. enum {
  12. Nqwds = 2,
  13. Nqshift = 5, // 2log #of bits in long
  14. Nqmask = - 1,
  15. Nqbits = (1 << Nqshift) * 2,
  16. };
  17. struct Channel {
  18. int s; // Size of the channel (may be zero)
  19. uint f; // Extraction point (insertion pt: (f + n) % s)
  20. uint n; // Number of values in the channel
  21. int e; // Element size
  22. int freed; // Set when channel is being deleted
  23. volatile Alt **qentry; // Receivers/senders waiting (malloc)
  24. volatile int nentry; // # of entries malloc-ed
  25. uchar v[1]; // Array of s values in the channel
  26. };
  27. /* Channel operations for alt: */
  28. typedef enum {
  29. CHANEND,
  30. CHANSND,
  31. CHANRCV,
  32. CHANNOP,
  33. CHANNOBLK,
  34. } ChanOp;
  35. struct Alt {
  36. Channel *c; /* channel */
  37. void *v; /* pointer to value */
  38. ChanOp op; /* operation */
  39. /* the next variables are used internally to alt
  40. * they need not be initialized
  41. */
  42. Channel **tag; /* pointer to rendez-vous tag */
  43. int entryno; /* entry number */
  44. };
  45. struct Ref {
  46. long ref;
  47. };
  48. int alt(Alt alts[]);
  49. Channel* chancreate(int elemsize, int bufsize);
  50. int chaninit(Channel *c, int elemsize, int elemcnt);
  51. void chanfree(Channel *c);
  52. int chanprint(Channel *, char *, ...);
  53. long decref(Ref *r); /* returns 0 iff value is now zero */
  54. void incref(Ref *r);
  55. int nbrecv(Channel *c, void *v);
  56. void* nbrecvp(Channel *c);
  57. ulong nbrecvul(Channel *c);
  58. int nbsend(Channel *c, void *v);
  59. int nbsendp(Channel *c, void *v);
  60. int nbsendul(Channel *c, ulong v);
  61. int proccreate(void (*f)(void *arg), void *arg, uint stacksize);
  62. int procrfork(void (*f)(void *arg), void *arg, uint stacksize, int flag);
  63. void** procdata(void);
  64. void procexec(Channel *, char *, char *[]);
  65. void procexecl(Channel *, char *, ...);
  66. int recv(Channel *c, void *v);
  67. void* recvp(Channel *c);
  68. ulong recvul(Channel *c);
  69. int send(Channel *c, void *v);
  70. int sendp(Channel *c, void *v);
  71. int sendul(Channel *c, ulong v);
  72. int threadcreate(void (*f)(void *arg), void *arg, uint stacksize);
  73. void** threaddata(void);
  74. void threadexits(char *);
  75. void threadexitsall(char *);
  76. int threadgetgrp(void); /* return thread group of current thread */
  77. char* threadgetname(void);
  78. void threadint(int); /* interrupt thread */
  79. void threadintgrp(int); /* interrupt threads in grp */
  80. void threadkill(int); /* kill thread */
  81. void threadkillgrp(int); /* kill threads in group */
  82. void threadmain(int argc, char *argv[]);
  83. void threadnonotes(void);
  84. int threadnotify(int (*f)(void*, char*), int in);
  85. int threadid(void);
  86. int threadpid(int);
  87. int threadsetgrp(int); /* set thread group, return old */
  88. void threadsetname(char *fmt, ...);
  89. Channel* threadwaitchan(void);
  90. int tprivalloc(void);
  91. void tprivfree(int);
  92. void **tprivaddr(int);
  93. void yield(void);
  94. extern int mainstacksize;
  95. /* slave I/O processes */
  96. typedef struct Ioproc Ioproc;
  97. #pragma incomplete Ioproc
  98. Ioproc* ioproc(void);
  99. void closeioproc(Ioproc*);
  100. void iointerrupt(Ioproc*);
  101. int ioclose(Ioproc*, int);
  102. int iodial(Ioproc*, char*, char*, char*, int*);
  103. int ioopen(Ioproc*, char*, int);
  104. long ioread(Ioproc*, int, void*, long);
  105. long ioreadn(Ioproc*, int, void*, long);
  106. long iowrite(Ioproc*, int, void*, long);
  107. int iosleep(Ioproc*, long);
  108. long iocall(Ioproc*, long (*)(va_list*), ...);
  109. void ioret(Ioproc*, int);