plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

thread.h (4069B)


      1 #ifndef _THREAD_H_
      2 #define _THREAD_H_ 1
      3 #if defined(__cplusplus)
      4 extern "C" {
      5 #endif
      6 
      7 AUTOLIB(thread)
      8 
      9 /*
     10  * basic procs and threads
     11  */
     12 int		proccreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
     13 int		threadcreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
     14 void		threadexits(char *);
     15 void		threadexitsall(char *);
     16 void		threadsetname(char*, ...);
     17 void		threadsetstate(char*, ...);
     18 void		threadneedbackground(void);
     19 char		*threadgetname(void);
     20 int			threadyield(void);
     21 int			threadidle(void);
     22 void		_threadready(_Thread*);
     23 void		_threadswitch(void);
     24 void		_threadsetsysproc(void);
     25 void		_threadsleep(Rendez*);
     26 _Thread	*_threadwakeup(Rendez*);
     27 #define	yield		threadyield
     28 int		threadid(void);
     29 void		_threadpin(void);
     30 void		_threadunpin(void);
     31 
     32 /*
     33  * I am tired of making this mistake.
     34  */
     35 #define	exits	do_not_call_exits_in_threaded_programs
     36 #define	_exits	do_not_call__exits_in_threaded_programs
     37 
     38 
     39 
     40 /*
     41  * signals
     42  */
     43 void		threadnotify(int(*f)(void*,char*), int);
     44 
     45 /*
     46  * daemonize
     47  *
     48 void	threaddaemonize(void);
     49  */
     50 
     51 /*
     52  * per proc and thread data
     53  */
     54 void		**procdata(void);
     55 void		**threaddata(void);
     56 
     57 /*
     58  * supplied by user instead of main.
     59  * mainstacksize is size of stack allocated to run threadmain
     60  */
     61 void		threadmain(int argc, char *argv[]);
     62 extern	int	mainstacksize;
     63 
     64 int	threadmaybackground(void);
     65 
     66 /*
     67  * channel communication
     68  */
     69 typedef struct Alt Alt;
     70 typedef struct _Altarray _Altarray;
     71 typedef struct Channel Channel;
     72 
     73 enum
     74 {
     75 	CHANEND,
     76 	CHANSND,
     77 	CHANRCV,
     78 	CHANNOP,
     79 	CHANNOBLK
     80 };
     81 
     82 struct Alt
     83 {
     84 	Channel		*c;
     85 	void		*v;
     86 	uint		op;
     87 	_Thread		*thread;
     88 };
     89 
     90 struct _Altarray
     91 {
     92 	Alt			**a;
     93 	uint		n;
     94 	uint		m;
     95 };
     96 
     97 struct Channel
     98 {
     99 	uint			bufsize;
    100 	uint			elemsize;
    101 	uchar		*buf;
    102 	uint			nbuf;
    103 	uint			off;
    104 	_Altarray	asend;
    105 	_Altarray	arecv;
    106 	char			*name;
    107 };
    108 
    109 /* [Edit .+1,./^$/ |cfn -h $PLAN9/src/libthread/channel.c] */
    110 int		chanalt(Alt *alts);
    111 Channel*	chancreate(int elemsize, int elemcnt);
    112 void		chanfree(Channel *c);
    113 int		channbrecv(Channel *c, void *v);
    114 void*		channbrecvp(Channel *c);
    115 ulong		channbrecvul(Channel *c);
    116 int		channbsend(Channel *c, void *v);
    117 int		channbsendp(Channel *c, void *v);
    118 int		channbsendul(Channel *c, ulong v);
    119 int		chanrecv(Channel *c, void *v);
    120 void*		chanrecvp(Channel *c);
    121 ulong		chanrecvul(Channel *c);
    122 int		chansend(Channel *c, void *v);
    123 int		chansendp(Channel *c, void *v);
    124 int		chansendul(Channel *c, ulong v);
    125 void		chansetname(Channel *c, char *fmt, ...);
    126 
    127 #define	alt		chanalt
    128 #define	nbrecv	channbrecv
    129 #define	nbrecvp	channbrecvp
    130 #define	nbrecvul	channbrecvul
    131 #define	nbsend	channbsend
    132 #define	nbsendp	channbsendp
    133 #define	nbsendul	channbsendul
    134 #define	recv		chanrecv
    135 #define	recvp	chanrecvp
    136 #define	recvul	chanrecvul
    137 #define	send		chansend
    138 #define	sendp	chansendp
    139 #define	sendul	chansendul
    140 
    141 /*
    142  * reference counts
    143  */
    144 typedef struct Ref	Ref;
    145 
    146 struct Ref {
    147 	Lock lock;
    148 	long ref;
    149 };
    150 
    151 long		decref(Ref *r);
    152 long		incref(Ref *r);
    153 
    154 /*
    155  * slave i/o processes
    156  */
    157 typedef struct Ioproc Ioproc;
    158 
    159 /* [Edit .+1,/^$/ |cfn -h $PLAN9/src/libthread/io*.c] */
    160 void		closeioproc(Ioproc *io);
    161 long		iocall(Ioproc *io, long (*op)(va_list*), ...);
    162 int		ioclose(Ioproc *io, int fd);
    163 int		iodial(Ioproc *io, char *addr, char *local, char *dir, int *cdfp);
    164 void		iointerrupt(Ioproc *io);
    165 int		ioopen(Ioproc *io, char *path, int mode);
    166 Ioproc*		ioproc(void);
    167 long		ioread(Ioproc *io, int fd, void *a, long n);
    168 int		ioread9pmsg(Ioproc*, int, void*, int);
    169 long		ioreadn(Ioproc *io, int fd, void *a, long n);
    170 int		iorecvfd(Ioproc *, int);
    171 int		iosendfd(Ioproc*, int, int);
    172 int		iosleep(Ioproc *io, long n);
    173 long		iowrite(Ioproc *io, int fd, void *a, long n);
    174 
    175 /*
    176  * exec external programs
    177  */
    178 void		threadexec(Channel*, int[3], char*, char *[]);
    179 void		threadexecl(Channel*, int[3], char*, ...);
    180 int		threadspawn(int[3], char*, char*[]);
    181 int		threadspawnd(int[3], char*, char*[], char*);
    182 int		threadspawnl(int[3], char*, ...);
    183 Channel*	threadwaitchan(void);
    184 
    185 /*
    186  * alternate interface to threadwaitchan - don't use both!
    187  */
    188 Waitmsg*	procwait(int pid);
    189 
    190 #if defined(__cplusplus)
    191 }
    192 #endif
    193 #endif	/* _THREADH_ */