thread.h 3.5 KB

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