thread.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. uchar v[1]; /* Array of s values in the channel */
  27. };
  28. /* Channel operations for alt: */
  29. typedef enum {
  30. CHANEND,
  31. CHANSND,
  32. CHANRCV,
  33. CHANNOP,
  34. CHANNOBLK,
  35. } ChanOp;
  36. struct Alt {
  37. Channel *c; /* channel */
  38. void *v; /* pointer to value */
  39. ChanOp op; /* operation */
  40. /*
  41. * the next variables are used internally to alt
  42. * they need not be initialized
  43. */
  44. Channel **tag; /* pointer to rendez-vous tag */
  45. int entryno; /* entry number */
  46. };
  47. struct Ref {
  48. long ref;
  49. };
  50. int alt(Alt alts[]);
  51. Channel*chancreate(int elemsize, int bufsize);
  52. int chaninit(Channel *c, int elemsize, int elemcnt);
  53. void chanfree(Channel *c);
  54. int chanprint(Channel *, char *, ...);
  55. long decref(Ref *r); /* returns 0 iff value is now zero */
  56. void incref(Ref *r);
  57. int nbrecv(Channel *c, void *v);
  58. void* nbrecvp(Channel *c);
  59. ulong nbrecvul(Channel *c);
  60. int nbsend(Channel *c, void *v);
  61. int nbsendp(Channel *c, void *v);
  62. int nbsendul(Channel *c, ulong v);
  63. void needstack(int);
  64. int proccreate(void (*f)(void *arg), void *arg, uint stacksize);
  65. int procrfork(void (*f)(void *arg), void *arg, uint stacksize, int flag);
  66. void** procdata(void);
  67. void procexec(Channel *, char *, char *[]);
  68. void procexecl(Channel *, char *, ...);
  69. int recv(Channel *c, void *v);
  70. void* recvp(Channel *c);
  71. ulong recvul(Channel *c);
  72. int send(Channel *c, void *v);
  73. int sendp(Channel *c, void *v);
  74. int sendul(Channel *c, ulong v);
  75. int threadcreate(void (*f)(void *arg), void *arg, uint stacksize);
  76. void** threaddata(void);
  77. void threadexits(char *);
  78. void threadexitsall(char *);
  79. int threadgetgrp(void); /* return thread group of current thread */
  80. char* threadgetname(void);
  81. void threadint(int); /* interrupt thread */
  82. void threadintgrp(int); /* interrupt threads in grp */
  83. void threadkill(int); /* kill thread */
  84. void threadkillgrp(int); /* kill threads in group */
  85. void threadmain(int argc, char *argv[]);
  86. void threadnonotes(void);
  87. int threadnotify(int (*f)(void*, char*), int in);
  88. int threadid(void);
  89. int threadpid(int);
  90. int threadsetgrp(int); /* set thread group, return old */
  91. void threadsetname(char *fmt, ...);
  92. Channel*threadwaitchan(void);
  93. int tprivalloc(void);
  94. void tprivfree(int);
  95. void **tprivaddr(int);
  96. void yield(void);
  97. extern int mainstacksize;
  98. /* slave I/O processes */
  99. typedef struct Ioproc Ioproc;
  100. #pragma incomplete Ioproc
  101. Ioproc* ioproc(void);
  102. void closeioproc(Ioproc*);
  103. void iointerrupt(Ioproc*);
  104. int ioclose(Ioproc*, int);
  105. int iodial(Ioproc*, char*, char*, char*, int*);
  106. int ioopen(Ioproc*, char*, int);
  107. long ioread(Ioproc*, int, void*, long);
  108. long ioreadn(Ioproc*, int, void*, long);
  109. long iowrite(Ioproc*, int, void*, long);
  110. int iosleep(Ioproc*, long);
  111. long iocall(Ioproc*, long (*)(va_list*), ...);
  112. void ioret(Ioproc*, int);