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) && !defined(AFS_PTHREAD_ENV)
22 #include <afs/param.h>
23 #if defined(USE_UCONTEXT) && defined(HAVE_UCONTEXT_H)
30 #define LWP_EBADPID -1
31 #define LWP_EBLOCKED -2
33 #define LWP_EMAXPROC -4
34 #define LWP_ENOBLOCK -5
36 #define LWP_ENOPROCESS -7
37 #define LWP_ENOWAIT -8
38 #define LWP_EBADCOUNT -9
39 #define LWP_EBADEVENT -10
40 #define LWP_EBADPRI -11
41 #define LWP_NO_STACK -12
42 /* These two are for the signal mechanism. */
43 #define LWP_EBADSIG -13 /* bad signal number */
44 #define LWP_ESYSTEM -14 /* system call failed */
45 /* These are for the rock mechanism */
46 #define LWP_ENOROCKS -15 /* all rocks are in use */
47 #define LWP_EBADROCK -16 /* the specified rock does not exist */
49 #if defined(USE_PTHREADS) || defined(USE_SOLARIS_THREADS)
50 #ifdef USE_SOLARIS_THREADS
52 typedef int pthread_t;
53 typedef void *pthread_addr_t;
54 typedef void *pthread_condattr_t;
55 typedef void (*pthread_destructor_t) (void *);
56 typedef pthread_addr_t(*pthread_startroutine_t) (pthread_addr_t);
57 #define pthread_mutex_lock mutex_lock
58 #define pthread_mutex_unlock mutex_unlock
59 #define pthread_getspecific thr_getspecific
60 #define pthread_setspecific thr_setspecific
61 #define pthread_yield thr_yield
62 /*typedef mutex_t pthread_mutex_t;*/
63 typedef thread_key_t pthread_key_t;
64 typedef cond_t PTHREAD_COND, *pthread_cond_t;
66 #define PTHREAD_DEFAULT_SCHED 1
68 #define MUTEX_FAST_NP 0
69 #define PTHREAD_DEFAULT_STACK 65536 /* 64 K */
70 #define PRI_OTHER_MIN 1
71 #define PRI_OTHER_MAX 127
72 #define PRI_OTHER_MID ((PRI_OTHER_MIN + PRI_OTHER_MAX)/2)
73 #define DESTRUCTOR_TAB_CHUNK 20
74 #define maskAllSignals(o) thr_sigsetmask(SIG_BLOCK,&pthread_allSignals,&o)
75 #define restoreAllSignals(o) thr_sigsetmask(SIG_SETMASK,&o,NULL)
77 typedef struct PTHREAD_MUTEX {
82 } PTHREAD_MUTEX, *pthread_mutex_t;
85 typedef struct PTHREAD_ATTR {
88 } PTHREAD_ATTR, *pthread_attr_t;
91 void (*destructor) (void *);
96 int nentries; /* size allocated (in terms of number of slots) */
97 int next_free; /* next free slot */
99 } pthread_destructor_tab_t;
100 define DTAB_SIZE(size) (sizeof(pthread_destructor_tab_t) +
101 (size) * sizeof(dest_slot_t))
107 #define LWP_MAX_PRIORITY 0
108 #define LWP_NORMAL_PRIORITY 0
109 #define LWP_NO_PRIORITIES
111 * We define PROCESS as a pointer to this struct, rather than simply as
112 * a pthread_t since some applications test for a process handle being
113 * non-zero. This can't be done on a pthread_t.
115 typedef struct lwp_process {
116 pthread_t handle; /* The pthreads handle */
117 struct lwp_process *next; /* Next LWP process */
118 char *name; /* LWP name of the process */
119 pthread_startroutine_t ep; /* Process entry point */
120 pthread_addr_t arg; /* Initial parameter */
123 struct rock { /* to hide things associated with this LWP under */
124 int tag; /* unique identifier for this rock */
125 char *value; /* pointer to some arbitrary data structure */
128 #define MAXROCKS 4 /* max no. of rocks per LWP */
133 /*#define CMA_DEBUG 1*/
137 #define LWP_CHECKSTUFF(msg) lwp_checkstuff(msg)
139 #define LWP_CHECKSTUFF(msg)
143 #define debugf(m) printf m
148 #define IOMGR_Poll() LWP_DispatchProcess()
151 * These two macros can be used to enter/exit the LWP context in a CMA
152 * program. They simply acquire/release the global LWP mutex .
154 extern pthread_mutex_t lwp_mutex;
155 #define LWP_EXIT_LWP_CONTEXT() pthread_mutex_unlock(&lwp_mutex)
156 #define LWP_ENTER_LWP_CONTEXT() pthread_mutex_lock(&lwp_mutex)
159 #define lwp_abort() abort()
161 /* Maximum priority permissible (minimum is always 0) */
162 #define LWP_MAX_PRIORITY 4 /* changed from 1 */
164 /* Usual priority used by user LWPs */
165 #define LWP_NORMAL_PRIORITY (LWP_MAX_PRIORITY-2)
167 /* Initial size of eventlist in a PCB; grows dynamically */
170 typedef struct lwp_pcb *PROCESS;
174 typedef struct lwp_pcb {
175 char name[32]; /* name of LWP */
177 int (*funP) (); /* function to execute on this LWP */
178 void *argP; /* argument for function */
179 int priority; /* LWP priority */
180 int stacksize; /* Just for reference. */
181 /* the following are used for scheduling */
189 struct lwp_pcb *next, *prev;
190 struct IoRequest *iomgrRequest;
191 int index; /* new number (++) for each process created. */
195 struct lwp_context { /* saved context for dispatcher */
196 char *topstack; /* ptr to top of process stack */
197 #if defined(USE_UCONTEXT) && defined(HAVE_UCONTEXT_H)
200 #else /* !HAVE_UCONTEXT_H */
201 # if defined(sparc) && !defined(__linux__)
203 int globals[7 + 1 + 32 + 2 + 32 + 2]; /* g1-g7, y reg, f0-f31, fsr, fq, c0-c31, csr, cq. */
205 int globals[8]; /* g1-g7 and y registers. */
208 jmp_buf setjmp_buffer;
209 #endif /* HAVE_UCONTEXT_H */
212 struct rock { /* to hide things associated with this LWP under */
213 int tag; /* unique identifier for this rock */
214 char *value; /* pointer to some arbitrary data structure */
217 #define MAXROCKS 4 /* max no. of rocks per LWP */
219 struct lwp_pcb { /* process control block */
220 char name[32]; /* ASCII name */
221 int rc; /* most recent return code */
222 char status; /* status flags */
223 char blockflag; /* if (blockflag), process blocked */
224 char eventlistsize; /* size of eventlist array */
225 char padding; /* force 32-bit alignment */
226 char **eventlist; /* ptr to array of eventids */
227 int eventcnt; /* no. of events currently in eventlist array */
228 int wakevent; /* index of eventid causing wakeup */
229 int waitcnt; /* min number of events awaited */
230 int priority; /* dispatching priority */
231 struct lwp_pcb *misc; /* for LWP internal use only */
232 char *stack; /* ptr to process stack */
233 int stacksize; /* size of stack */
234 int stackcheck; /* first word of stack for overflow checking */
235 int (*ep) (); /* initial entry point */
236 char *parm; /* initial parm for process */
238 context; /* saved context for next dispatch */
239 int lwp_rused; /* no of rocks presently in use */
240 struct rock lwp_rlist[MAXROCKS]; /* set of rocks to hide things under */
241 struct lwp_pcb *next, *prev; /* ptrs to next and previous pcb */
242 int level; /* nesting level of critical sections */
243 struct IoRequest *iomgrRequest; /* request we're waiting for */
244 int index; /* LWP index: should be small index; actually is
245 * incremented on each lwp_create_process */
247 #endif /* AFS_NT40_ENV */
249 extern int lwp_nextindex; /* Next lwp index to assign */
253 #define LWP_ActiveProcess (lwp_cpptr+0)
254 #define LWP_Index() (LWP_ActiveProcess->index)
255 #define LWP_HighestIndex() (lwp_nextindex - 1)
256 #ifndef AFS_SUN5_ENV /* Actual functions for solaris */
257 #define LWP_SignalProcess(event) LWP_INTERNALSIGNAL(event, 1)
258 #define LWP_NoYieldSignal(event) LWP_INTERNALSIGNAL(event, 0)
263 struct lwp_pcb *lwp_cpptr; /* pointer to current process pcb */
265 struct lwp_ctl { /* LWP control structure */
266 int processcnt; /* number of lightweight processes */
267 char *outersp; /* outermost stack pointer */
268 struct lwp_pcb *outerpid; /* process carved by Initialize */
269 struct lwp_pcb *first, last; /* ptrs to first and last pcbs */
271 double dsptchstack[200]; /* stack for dispatcher use only */
272 /* force 8 byte alignment */
274 char dsptchstack[800]; /* stack for dispatcher use only */
281 char lwp_debug; /* ON = show LWP debugging trace */
284 * Under hpux, any stack size smaller than 16K seems prone to
287 * On Solaris 2.5, gethostbyname() can use up to 21920 bytes of stack
288 * space. Note: when measuring this, it is important to check the
289 * amount of stack space it uses for hosts that are known as well as
290 * for hosts that are unknown; the stack usage can differ between these
291 * cases, and also between machines apparently running the same OS
295 * On ia64 where the ucontext is used, it can be an extra 48K
296 * Need to account for this. There might be two of these on the
297 * stack too. This needs to be checked.
299 #if defined(USE_UCONTEXT) && defined(HAVE_UCONTEXT_H)
300 #define AFS_LWP_MINSTACKSIZE (288 * 1024)
301 #elif defined(AFS_LINUX22_ENV)
302 #define AFS_LWP_MINSTACKSIZE (192 * 1024)
304 #define AFS_LWP_MINSTACKSIZE (48 * 1024)
307 /* Action to take on stack overflow. */
308 #define LWP_SOQUIET 1 /* do nothing */
309 #define LWP_SOABORT 2 /* abort the program */
310 #define LWP_SOMESSAGE 3 /* print a message and be quiet */
311 extern int lwp_overflowAction;
313 /* Tells if stack size counting is enabled. */
314 extern int lwp_stackUseEnabled;
315 extern int lwp_MaxStackSeen;
317 #ifndef AFS_AIX32_ENV
318 #define LWP_CreateProcess2(a, b, c, d, e, f) \
319 LWP_CreateProcess((a), (b), (c), (d), (e), (f))
322 /* External function declarations. */
324 #ifndef _MFC_VER /*skip if doing Microsoft foundation class */
325 #include <winsock2.h>
327 #elif defined(AFS_LINUX20_ENV)
330 #include <sys/time.h>
332 # include <unistd.h> /* select() prototype */
333 # include <sys/types.h> /* fd_set on older platforms */
334 # include <sys/time.h> /* struct timeval, select() prototype */
336 # include <sys/select.h> /* fd_set on newer platforms */
340 #endif /* USE_PTHREADS */
343 extern fd_set *IOMGR_AllocFDSet(void);
344 extern int IOMGR_Select(int nfds, fd_set * rfds, fd_set * wfds, fd_set * efds,
345 struct timeval *tvp);
346 extern int IOMGR_Poll(void);
347 extern void IOMGR_Sleep(int seconds);
348 extern int IOMGR_Cancel(PROCESS pid);
349 extern int IOMGR_Initialize(void);
350 extern void IOMGR_FreeFDSet(fd_set * fds);
351 extern int IOMGR_SoftSig(int (*aproc) (), char *arock);
355 extern int FT_GetTimeOfDay(struct timeval *tv, struct timezone *tz);
356 extern int FT_Init(int printErrors, int notReally);
357 extern int FT_AGetTimeOfDay(struct timeval *tv, struct timezone *tz);
358 extern unsigned int FT_ApproxTime(void);
361 extern int LWP_WaitForKeystroke(int seconds); /* -1 => forever */
362 extern int LWP_GetResponseKey(int seconds, char *key);
363 extern int LWP_GetLine(char *linebuf, int len);
366 extern int LWP_InitializeProcessSupport(int priority, PROCESS * pid);
367 extern int LWP_CreateProcess(int (*funP) (), int stacksize, int priority,
368 void *argP, char *name, PROCESS * pid);
369 extern int LWP_DestroyProcess(PROCESS pid);
370 extern int LWP_DispatchProcess(void);
371 extern int LWP_WaitProcess(void *event);
372 extern int LWP_INTERNALSIGNAL(void *event, int yield);
373 extern int LWP_QWait(void);
374 extern int LWP_QSignal(PROCESS pid);
376 extern int LWP_CurrentProcess(PROCESS * pid);
377 extern int LWP_INTERNALSIGNAL(char *event, int yield);
378 extern int LWP_InitializeProcessSupport(int priority, PROCESS * pid);
379 extern int LWP_CreateProcess(int (*ep) (), int stacksize, int priority,
380 void *parm, char *name, PROCESS * pid);
381 extern int LWP_DestroyProcess(PROCESS pid);
382 extern int LWP_WaitProcess(char *event);
383 extern PROCESS LWP_ThreadId(void);
386 #ifdef AFS_LINUX24_ENV
387 /* max time we are allowed to spend in a select call on Linux to avoid
388 lost signal issues */
389 #define IOMGR_MAXWAITTIME 60 /* seconds */
391 /* max time we are allowed to spend in a select call on NT */
392 #define IOMGR_MAXWAITTIME 5 /* seconds */
395 /* max time we spend on a select in a Win95 DOS box */
396 #define IOMGR_WIN95WAITTIME 5000 /* microseconds */
398 #endif /* __LWP_INCLUDE_ */
400 #endif /* !KERNEL && !_KMEMUSER */