thread.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma src "/sys/src/libthread"
  2. #pragma lib "libthread.a"
  3. #pragma varargck argpos chanprint 2
  4. typedef struct Proc Proc;
  5. typedef struct Thread Thread;
  6. typedef struct Tqueue Tqueue;
  7. typedef struct Alt Alt;
  8. typedef struct Channel Channel;
  9. typedef struct Ref Ref;
  10. /* Channel structure. S is the size of the buffer. For unbuffered channels
  11. * s is zero. v is an array of s values. If s is zero, v is unused.
  12. * f and n represent the state of the queue pointed to by v.
  13. */
  14. enum {
  15. Nqwds = 2,
  16. Nqshift = 5, // 2log #of bits in long
  17. Nqmask = - 1,
  18. Nqbits = (1 << Nqshift) * 2,
  19. };
  20. struct Channel {
  21. int s; // Size of the channel (may be zero)
  22. uint f; // Extraction point (insertion pt: (f + n) % s)
  23. uint n; // Number of values in the channel
  24. int e; // Element size
  25. int freed; // Set when channel is being deleted
  26. volatile Alt **qentry; // Receivers/senders waiting (malloc)
  27. volatile int nentry; // # of entries malloc-ed
  28. uchar v[1]; // Array of s values in the channel
  29. };
  30. /* Channel operations for alt: */
  31. typedef enum {
  32. CHANEND,
  33. CHANSND,
  34. CHANRCV,
  35. CHANNOP,
  36. CHANNOBLK,
  37. } ChanOp;
  38. struct Alt {
  39. Channel *c; /* channel */
  40. void *v; /* pointer to value */
  41. ChanOp op; /* operation */
  42. /* the next variables are used internally to alt
  43. * they need not be initialized
  44. */
  45. Channel **tag; /* pointer to rendez-vous tag */
  46. int entryno; /* entry number */
  47. };
  48. struct Ref {
  49. long ref;
  50. };
  51. int alt(Alt alts[]);
  52. Channel* chancreate(int elemsize, int bufsize);
  53. int chaninit(Channel *c, int elemsize, int elemcnt);
  54. void chanfree(Channel *c);
  55. int chanprint(Channel *, char *, ...);
  56. long decref(Ref *r); /* returns 0 iff value is now zero */
  57. void incref(Ref *r);
  58. int nbrecv(Channel *c, void *v);
  59. void* nbrecvp(Channel *c);
  60. ulong nbrecvul(Channel *c);
  61. int nbsend(Channel *c, void *v);
  62. int nbsendp(Channel *c, void *v);
  63. int nbsendul(Channel *c, ulong v);
  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 *name);
  92. Channel* threadwaitchan(void);
  93. void yield(void);
  94. extern int mainstacksize;
  95. /* slave I/O processes */
  96. typedef struct Ioproc Ioproc;
  97. Ioproc* ioproc(void);
  98. void closeioproc(Ioproc*);
  99. void iointerrupt(Ioproc*);
  100. int ioclose(Ioproc*, int);
  101. int iodial(Ioproc*, char*, char*, char*, int*);
  102. int ioopen(Ioproc*, char*, int);
  103. long ioread(Ioproc*, int, void*, long);
  104. long ioreadn(Ioproc*, int, void*, long);
  105. long iowrite(Ioproc*, int, void*, long);
  106. int iosleep(Ioproc*, long);
  107. long iocall(Ioproc*, long (*)(va_list*), ...);
  108. void ioret(Ioproc*, int);