thread.h 4.1 KB

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