xref: /plan9-contrib/sys/src/libthread/threadimpl.h (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #include "thread.h"
2 
3 typedef enum {
4 	Running,
5 	Runnable,
6 	Rendezvous,
7 } State;
8 
9 typedef enum {
10 	Callnil,
11 	Callalt,
12 	Callsnd,
13 	Callrcv,
14 } Callstate;
15 
16 enum {
17 	DOEXEC = 1,
18 	DOEXIT = 2,
19 	DOPROC = 3,
20 };
21 
22 struct Tqueue {		// Thread queue
23 	Lock	lock;
24 	Thread	*head;
25 	Thread	*tail;
26 };
27 
28 struct Thread {
29 	Lock		lock;		// protects thread data structure
30 	int		id;		// thread id
31 	int 		grp;		// thread group
32 	State		state;		// state of thread
33 	int		exiting;	// shouls it die?
34 	Callstate	call;		// which `system call' is current
35 	char		*cmdname;	// ptr to name of thread
36 	Thread		*next;		// next on queue (run, rendezvous)
37 	Thread		*nextt;		// next on list of all theads
38 	Proc		*proc;		// proc of this thread
39 	ulong		tag;		// rendez-vous tag
40 	Alt		*alt;		// pointer to alt structure
41 	ulong		value;		// rendez-vous value
42 	jmp_buf		env;		// jump buf for launching or switching threads
43 	uchar		*stk;		// top of stack (lowest address of stack)
44 	uint		stksize;	// stack size
45 };
46 
47 struct Proc {
48 	Lock	lock;
49 	int	pid;
50 
51 	jmp_buf	oldenv;			// jump buf for returning to original stack
52 
53 	int	nthreads;
54 	Tqueue	threads;		// All threads of this proc
55 	Tqueue	runnable;		// Runnable threads
56 	Thread	*curthread;		// Running thread
57 
58 	int	blocked;		// In a rendezvous
59 	uint	nextID;			// ID of most recently created thread
60 	Proc	*next;			// linked list of Procs
61 
62 	void	*arg;			// passed between shared and unshared stk
63 
64 	ulong	udata;			// User per-proc data pointer
65 };
66 
67 typedef struct Newproc {
68 	uchar	*stack;
69 	uint	stacksize;
70 	ulong	*stackptr;
71 	ulong	launcher;
72 	int		grp;
73 } Newproc;
74 
75 typedef struct Execproc {
76 	char *file;
77 	void **arg;
78 } Execproc;
79 
80 struct Pqueue {		// Proc queue
81 	Lock	lock;
82 	Proc	*head;
83 	Proc	*tail;
84 };
85 
86 extern struct Pqueue pq;	// Linked list of procs
87 extern Proc **procp;		// Pointer to pointer to proc's Proc structure
88 
89 int		tas(int*);
90 int		inc(int*, int);
91 int		cas(Lock *lock, Lock old, Lock new);
92 ulong	threadrendezvous(ulong, ulong);
93 void	*_threadmalloc(long size);
94 
95 void _xinc(long *);
96 long _xdec(long *);
97