thread.h 3.6 KB

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