2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /*******************************************************************\
12 * Information Technology Center *
13 * Carnegie-Mellon University *
16 \*******************************************************************/
18 #ifndef __LWP_INCLUDE_
19 #define __LWP_INCLUDE_ 1
21 #if !defined(KERNEL) && !defined(_KMEMUSER)
22 #include <afs/param.h>
24 /* External function declarations. */
26 #ifndef _MFC_VER /*skip if doing Microsoft foundation class */
29 #elif defined(AFS_LINUX20_ENV)
34 # include <unistd.h> /* select() prototype */
35 # include <sys/types.h> /* fd_set on older platforms */
36 # include <sys/time.h> /* struct timeval, select() prototype */
38 # include <sys/select.h> /* fd_set on newer platforms */
43 extern int FT_GetTimeOfDay(struct timeval *tv, struct timezone *tz);
44 extern int FT_Init(int printErrors, int notReally);
45 extern int FT_AGetTimeOfDay(struct timeval *tv, struct timezone *tz);
46 extern unsigned int FT_ApproxTime(void);
48 #if !defined(AFS_PTHREAD_ENV)
49 # if defined(USE_UCONTEXT) && defined(HAVE_UCONTEXT_H)
50 # include <ucontext.h>
56 #define LWP_EBADPID -1
57 #define LWP_EBLOCKED -2
59 #define LWP_EMAXPROC -4
60 #define LWP_ENOBLOCK -5
62 #define LWP_ENOPROCESS -7
63 #define LWP_ENOWAIT -8
64 #define LWP_EBADCOUNT -9
65 #define LWP_EBADEVENT -10
66 #define LWP_EBADPRI -11
67 #define LWP_NO_STACK -12
68 /* These two are for the signal mechanism. */
69 #define LWP_EBADSIG -13 /* bad signal number */
70 #define LWP_ESYSTEM -14 /* system call failed */
71 /* These are for the rock mechanism */
72 #define LWP_ENOROCKS -15 /* all rocks are in use */
73 #define LWP_EBADROCK -16 /* the specified rock does not exist */
76 #define lwp_abort() abort()
78 /* Maximum priority permissible (minimum is always 0) */
79 #define LWP_MAX_PRIORITY 4 /* changed from 1 */
81 /* Usual priority used by user LWPs */
82 #define LWP_NORMAL_PRIORITY (LWP_MAX_PRIORITY-2)
84 /* Initial size of eventlist in a PCB; grows dynamically */
87 typedef struct lwp_pcb *PROCESS;
91 typedef struct lwp_pcb {
92 char name[32]; /* name of LWP */
94 int (*funP) (); /* function to execute on this LWP */
95 void *argP; /* argument for function */
96 int priority; /* LWP priority */
97 int stacksize; /* Just for reference. */
98 /* the following are used for scheduling */
106 struct lwp_pcb *next, *prev;
107 struct IoRequest *iomgrRequest;
108 int index; /* new number (++) for each process created. */
112 struct lwp_context { /* saved context for dispatcher */
113 char *topstack; /* ptr to top of process stack */
114 #if defined(USE_UCONTEXT) && defined(HAVE_UCONTEXT_H)
117 #else /* !HAVE_UCONTEXT_H */
118 # if defined(sparc) && !defined(__linux__)
120 int globals[7 + 1 + 32 + 2 + 32 + 2]; /* g1-g7, y reg, f0-f31, fsr, fq, c0-c31, csr, cq. */
122 int globals[8]; /* g1-g7 and y registers. */
125 jmp_buf setjmp_buffer;
126 #endif /* HAVE_UCONTEXT_H */
129 struct rock { /* to hide things associated with this LWP under */
130 int tag; /* unique identifier for this rock */
131 char *value; /* pointer to some arbitrary data structure */
134 #define MAXROCKS 4 /* max no. of rocks per LWP */
136 struct lwp_pcb { /* process control block */
137 char name[32]; /* ASCII name */
138 int rc; /* most recent return code */
139 char status; /* status flags */
140 char blockflag; /* if (blockflag), process blocked */
141 char eventlistsize; /* size of eventlist array */
142 char padding; /* force 32-bit alignment */
143 void **eventlist; /* ptr to array of eventids */
144 int eventcnt; /* no. of events currently in eventlist array */
145 int wakevent; /* index of eventid causing wakeup */
146 int waitcnt; /* min number of events awaited */
147 int priority; /* dispatching priority */
148 struct lwp_pcb *misc; /* for LWP internal use only */
149 char *stack; /* ptr to process stack */
150 int stacksize; /* size of stack */
151 int stackcheck; /* first word of stack for overflow checking */
152 void *(*ep)(void *); /* initial entry point */
153 char *parm; /* initial parm for process */
155 context; /* saved context for next dispatch */
156 int lwp_rused; /* no of rocks presently in use */
157 struct rock lwp_rlist[MAXROCKS]; /* set of rocks to hide things under */
158 struct lwp_pcb *next, *prev; /* ptrs to next and previous pcb */
159 int level; /* nesting level of critical sections */
160 struct IoRequest *iomgrRequest; /* request we're waiting for */
161 int index; /* LWP index: should be small index; actually is
162 * incremented on each lwp_create_process */
164 #endif /* AFS_NT40_ENV */
166 extern int lwp_nextindex; /* Next lwp index to assign */
170 #define LWP_ActiveProcess (lwp_cpptr+0)
171 #define LWP_Index() (LWP_ActiveProcess->index)
172 #define LWP_HighestIndex() (lwp_nextindex - 1)
173 #ifndef AFS_SUN5_ENV /* Actual functions for solaris */
174 #define LWP_SignalProcess(event) LWP_INTERNALSIGNAL(event, 1)
175 #define LWP_NoYieldSignal(event) LWP_INTERNALSIGNAL(event, 0)
180 struct lwp_pcb *lwp_cpptr; /* pointer to current process pcb */
182 struct lwp_ctl { /* LWP control structure */
183 int processcnt; /* number of lightweight processes */
184 char *outersp; /* outermost stack pointer */
185 struct lwp_pcb *outerpid; /* process carved by Initialize */
186 struct lwp_pcb *first, last; /* ptrs to first and last pcbs */
188 double dsptchstack[200]; /* stack for dispatcher use only */
189 /* force 8 byte alignment */
191 char dsptchstack[800]; /* stack for dispatcher use only */
198 char lwp_debug; /* ON = show LWP debugging trace */
201 * Under hpux, any stack size smaller than 16K seems prone to
204 * On Solaris 2.5, gethostbyname() can use up to 21920 bytes of stack
205 * space. Note: when measuring this, it is important to check the
206 * amount of stack space it uses for hosts that are known as well as
207 * for hosts that are unknown; the stack usage can differ between these
208 * cases, and also between machines apparently running the same OS
212 * On ia64 where the ucontext is used, it can be an extra 48K
213 * Need to account for this. There might be two of these on the
214 * stack too. This needs to be checked.
216 #if defined(USE_UCONTEXT) && defined(HAVE_UCONTEXT_H)
217 #define AFS_LWP_MINSTACKSIZE (288 * 1024)
218 #elif defined(AFS_LINUX22_ENV)
219 #define AFS_LWP_MINSTACKSIZE (192 * 1024)
221 #define AFS_LWP_MINSTACKSIZE (48 * 1024)
224 /* Action to take on stack overflow. */
225 #define LWP_SOQUIET 1 /* do nothing */
226 #define LWP_SOABORT 2 /* abort the program */
227 #define LWP_SOMESSAGE 3 /* print a message and be quiet */
228 extern int lwp_overflowAction;
230 /* Tells if stack size counting is enabled. */
231 extern int lwp_stackUseEnabled;
232 extern int lwp_MaxStackSeen;
234 #ifndef AFS_AIX32_ENV
235 #define LWP_CreateProcess2(a, b, c, d, e, f) \
236 LWP_CreateProcess((a), (b), (c), (d), (e), (f))
240 extern fd_set *IOMGR_AllocFDSet(void);
241 extern int IOMGR_Select(int nfds, fd_set * rfds, fd_set * wfds, fd_set * efds,
242 struct timeval *tvp);
243 extern int IOMGR_Poll(void);
244 extern void IOMGR_Sleep(int seconds);
245 extern int IOMGR_Cancel(PROCESS pid);
246 extern int IOMGR_Initialize(void);
247 extern void IOMGR_FreeFDSet(fd_set * fds);
248 extern int IOMGR_SoftSig(void *(*aproc) (void *), void *arock);
252 extern int LWP_InitializeProcessSupport(int priority, PROCESS * pid);
253 extern int LWP_CreateProcess(int (*funP) (), int stacksize, int priority,
254 void *argP, char *name, PROCESS * pid);
255 extern int LWP_DestroyProcess(PROCESS pid);
256 extern int LWP_DispatchProcess(void);
257 extern int LWP_WaitProcess(void *event);
258 extern int LWP_INTERNALSIGNAL(void *event, int yield);
259 extern int LWP_QWait(void);
260 extern int LWP_QSignal(PROCESS pid);
262 extern int LWP_CurrentProcess(PROCESS * pid);
263 extern int LWP_INTERNALSIGNAL(void *event, int yield);
264 extern int LWP_InitializeProcessSupport(int priority, PROCESS * pid);
265 extern int LWP_CreateProcess(void *(*ep)(void *), int stacksize, int priority,
266 void *parm, char *name, PROCESS * pid);
267 extern int LWP_DestroyProcess(PROCESS pid);
268 extern int LWP_DispatchProcess(void);
269 extern int LWP_WaitProcess(void *event);
270 extern PROCESS LWP_ThreadId(void);
271 extern int LWP_QWait(void);
272 extern int LWP_QSignal(PROCESS pid);
275 extern afs_int32 savecontext(void (*ep)(void),
276 struct lwp_context *savearea, char *sp);
277 extern void returnto(struct lwp_context *savearea);
279 #ifdef AFS_LINUX24_ENV
280 /* max time we are allowed to spend in a select call on Linux to avoid
281 lost signal issues */
282 #define IOMGR_MAXWAITTIME 60 /* seconds */
284 /* max time we are allowed to spend in a select call on NT */
285 #define IOMGR_MAXWAITTIME 5 /* seconds */
288 /* max time we spend on a select in a Win95 DOS box */
289 #define IOMGR_WIN95WAITTIME 5000 /* microseconds */
291 #endif /* !AFS_PTHREAD_ENV */
293 extern int LWP_WaitForKeystroke(int seconds); /* -1 => forever */
294 extern int LWP_GetResponseKey(int seconds, char *key);
295 extern int LWP_GetLine(char *linebuf, int len);
297 #endif /* !KERNEL && !_KMEMUSER */
298 #endif /* __LWP_INCLUDE_ */