thread.h 4.0 KB

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