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 *
15 \*******************************************************************/
17 #include <afsconfig.h>
18 #include <afs/param.h>
26 /* allocate externs here */
31 #include <sys/errno.h>
36 int setlim(int limcon, uchar_t hard, int limit);
39 extern char *getenv();
50 #if !defined(USE_PTHREADS) && !defined(USE_SOLARIS_THREADS)
53 extern void *malloc(int size);
54 extern void *realloc(void *ptr, int size);
56 #if defined(AFS_OSF_ENV) || defined(AFS_S390_LINUX20_ENV)
57 extern int PRE_Block; /* from preempt.c */
59 extern char PRE_Block; /* from preempt.c */
70 #define MAXINT (~(1<<((sizeof(int)*8)-1)))
73 #if defined(__hp9000s800) || defined(AFS_PARISC_LINUX24_ENV)
84 #elif defined(AFS_DARWIN_ENV)
85 #define STACK_ALIGN 16
94 #define Debug(level, msg) do { \
95 if (lwp_debug && lwp_debug >= level) { \
96 printf("***LWP (0x%x): ", lwp_cpptr); \
102 #define Debug(level, msg) do { \
107 static int Dispatcher();
108 static int Create_Process_Part2();
109 static int Exit_LWP();
110 static afs_int32 Initialize_Stack();
111 static int Stack_Used();
112 char (*RC_to_ASCII());
114 static void Abort_LWP();
115 static void Overflow_Complain();
116 static void Initialize_PCB();
117 static void Dispose_of_Dead_PCB();
118 static void Free_PCB();
119 static int Internal_Signal();
120 static purge_dead_pcbs();
122 #define MAX_PRIORITIES (LWP_MAX_PRIORITY+1)
127 } runnable[MAX_PRIORITIES], blocked, qwaiting;
128 /* Invariant for runnable queues: The head of each queue points to the currently running process if it is in that queue, or it points to the next process in that queue that should run. */
130 /* Offset of stack field within pcb -- used by stack checking stuff */
133 /* special user-tweakable option for AIX */
134 int lwp_MaxStackSize = 32768;
136 /* biggest LWP stack created so far */
137 int lwp_MaxStackSeen = 0;
139 /* Stack checking action */
140 int lwp_overflowAction = LWP_SOABORT;
142 /* Controls stack size counting. */
143 int lwp_stackUseEnabled = TRUE; /* pay the price */
147 /* Minimum stack size */
148 int lwp_MinStackSize = 0;
151 lwp_remove(register PROCESS p, register struct QUEUE *q)
153 /* Special test for only element on queue */
157 /* Not only element, do normal remove */
158 p->next->prev = p->prev;
159 p->prev->next = p->next;
161 /* See if head pointing to this element */
165 p->next = p->prev = NULL;
170 insert(register PROCESS p, register struct QUEUE *q)
172 if (q->head == NULL) { /* Queue is empty */
174 p->next = p->prev = p;
175 } else { /* Regular insert */
176 p->prev = q->head->prev;
177 q->head->prev->next = p;
186 move(PROCESS p, struct QUEUE *from, struct QUEUE *to)
196 #define for_all_elts(var, q, body)\
198 register PROCESS var, _NEXT_;\
200 for (_I_=q.count, var = q.head; _I_>0; _I_--, var=_NEXT_) {\
201 _NEXT_ = var -> next;\
207 /*****************************************************************************\
209 * Following section documents the Assembler interfaces used by LWP code *
211 \*****************************************************************************/
214 savecontext(int (*ep)(), struct lwp_context *savearea, char *sp);
216 Stub for Assembler routine that will
217 save the current SP value in the passed
218 context savearea and call the function
219 whose entry point is in ep. If the sp
220 parameter is NULL, the current stack is
221 used, otherwise sp becomes the new stack
224 returnto(struct lwp_context *savearea);
226 Stub for Assembler routine that will
227 restore context from a passed savearea
228 and return to the restored C frame.
232 /* Macro to force a re-schedule. Strange name is historical */
233 #define Set_LWP_RC() savecontext(Dispatcher, &lwp_cpptr->context, NULL)
235 static struct lwp_ctl *lwp_init = 0;
241 (tp = lwp_cpptr)->status = QWAITING;
242 move(tp, &runnable[tp->priority], &qwaiting);
248 LWP_QSignal(register PROCESS pid)
250 if (pid->status == QWAITING) {
252 move(pid, &qwaiting, &runnable[pid->priority]);
260 reserveFromStack(register afs_int32 size)
269 LWP_CreateProcess(int (*ep) (), int stacksize, int priority, void *parm,
270 char *name, PROCESS * pid)
274 static char *stackptr = 0;
280 #if defined(AFS_LWP_MINSTACKSIZE)
282 * on some systems (e.g. hpux), a minimum usable stack size has
285 if (stacksize < lwp_MinStackSize) {
286 stacksize = lwp_MinStackSize;
288 #endif /* defined(AFS_LWP_MINSTACKSIZE) */
289 /* more stack size computations; keep track of for IOMGR */
290 if (lwp_MaxStackSeen < stacksize)
291 lwp_MaxStackSeen = stacksize;
293 Debug(0, ("Entered LWP_CreateProcess"));
294 /* Throw away all dead process control blocks */
297 temp = (PROCESS) malloc(sizeof(struct lwp_pcb));
302 if (stacksize < MINSTACK)
303 #ifdef AFS_DARWIN_ENV
305 #else /* !AFS_DARWIN_ENV */
307 #endif /* !AFS_DARWIN_ENV */
310 STACK_ALIGN * ((stacksize + STACK_ALIGN - 1) / STACK_ALIGN);
314 * The following signal action for AIX is necessary so that in case of a
315 * crash (i.e. core is generated) we can include the user's data section
316 * in the core dump. Unfortunately, by default, only a partial core is
317 * generated which, in many cases, isn't too useful.
319 * We also do it here in case the main program forgets to do it.
321 struct sigaction nsa;
322 extern uid_t geteuid();
324 sigemptyset(&nsa.sa_mask);
325 nsa.sa_handler = SIG_DFL;
326 nsa.sa_flags = SA_FULLDUMP;
327 sigaction(SIGABRT, &nsa, NULL);
328 sigaction(SIGSEGV, &nsa, NULL);
331 * First we need to increase the default resource limits,
332 * if necessary, so that we can guarantee that we have the
333 * resources to create the core file, but we can't always
334 * do it as an ordinary user.
337 /* vos dump causes problems */
338 /* setlim(RLIMIT_FSIZE, 0, 1048575); * 1 Gig */
339 setlim(RLIMIT_STACK, 0, 65536); /* 65 Meg */
340 setlim(RLIMIT_CORE, 0, 131072); /* 131 Meg */
343 * Now reserve in one scoop all the stack space that will be used
344 * by the particular application's main (i.e. non-lwp) body. This
345 * is plenty space for any of our applications.
347 stackptr = reserveFromStack(lwp_MaxStackSize);
349 stackptr -= stacksize;
350 stackmemory = stackptr;
352 #ifdef AFS_DARWIN_ENV
353 if ((stackmemory = (char *)malloc(stacksize + STACK_ALIGN - 1)) == NULL)
354 #else /* !AFS_DARWIN_ENV */
355 if ((stackmemory = (char *)malloc(stacksize + 7)) == NULL)
356 #endif /* !AFS_DARWIN_ENV */
361 /* Round stack pointer to byte boundary */
362 #ifdef AFS_DARWIN_ENV
363 stackptr = (char *)(STACK_ALIGN * (((long)stackmemory + STACK_ALIGN - 1) / STACK_ALIGN));
364 #else /* !AFS_DARWIN_ENV */
365 stackptr = (char *)(8 * (((long)stackmemory + 7) / 8));
366 #endif /* !AFS_DARWIN_ENV */
368 if (priority < 0 || priority >= MAX_PRIORITIES) {
372 Initialize_Stack(stackptr, stacksize);
373 Initialize_PCB(temp, priority, stackmemory, stacksize, ep, parm, name);
374 insert(temp, &runnable[priority]);
377 Abort_LWP("PRE_Block not 0");
379 /* Gross hack: beware! */
382 #if defined(AFS_PARISC_LINUX24_ENV)
383 savecontext(Create_Process_Part2, &temp2->context,
384 stackptr + MINFRAME);
387 savecontext(Create_Process_Part2, &temp2->context,
388 stackptr + MINFRAME);
390 #if defined(AFS_SGI62_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
391 #ifdef sys_x86_darwin_80
392 savecontext(Create_Process_Part2, &temp2->context, stackptr + stacksize - 16 - sizeof(void *)); /* 16 = 2 * jmp_buf_type */
393 #else /* !sys_x86_darwin_80 */
394 /* Need to have the sp on an 8-byte boundary for storing doubles. */
395 savecontext(Create_Process_Part2, &temp2->context, stackptr + stacksize - 16); /* 16 = 2 * jmp_buf_type */
396 #endif /* !sys_x86_darwin_80 */
398 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_SPARC_LINUX20_ENV)
399 savecontext(Create_Process_Part2, &temp2->context, stackptr + stacksize - 0x40); /* lomgjmp does something
402 #if defined(AFS_S390_LINUX20_ENV)
403 savecontext(Create_Process_Part2, &temp2->context,
404 stackptr + stacksize - MINFRAME);
405 #else /* !AFS_S390_LINUX20_ENV */
406 savecontext(Create_Process_Part2, &temp2->context,
407 stackptr + stacksize - sizeof(void *));
408 #endif /* AFS_S390_LINUX20_ENV */
409 #endif /* AFS_SPARC64_LINUX20_ENV || AFS_SPARC_LINUX20_ENV */
410 #endif /* AFS_SGI62_ENV */
413 /* End of gross hack */
424 LWP_CreateProcess2(int (*ep) (), int stacksize, int priority, void *parm,
425 char *name, PROCESS * pid)
430 #if defined(AFS_LWP_MINSTACKSIZE)
432 * on some systems (e.g. hpux), a minimum usable stack size has
435 if (stacksize < lwp_MinStackSize) {
436 stacksize = lwp_MinStackSize;
438 #endif /* defined(AFS_LWP_MINSTACKSIZE) */
439 /* more stack size computations; keep track of for IOMGR */
440 if (lwp_MaxStackSeen < stacksize)
441 lwp_MaxStackSeen = stacksize;
443 Debug(0, ("Entered LWP_CreateProcess"));
444 /* Throw away all dead process control blocks */
447 temp = (PROCESS) malloc(sizeof(struct lwp_pcb));
452 if (stacksize < MINSTACK)
456 STACK_ALIGN * ((stacksize + STACK_ALIGN - 1) / STACK_ALIGN);
457 if ((stackptr = (char *)malloc(stacksize)) == NULL) {
461 if (priority < 0 || priority >= MAX_PRIORITIES) {
465 Initialize_Stack(stackptr, stacksize);
466 Initialize_PCB(temp, priority, stackptr, stacksize, ep, parm, name);
467 insert(temp, &runnable[priority]);
470 Abort_LWP("PRE_Block not 0");
472 /* Gross hack: beware! */
475 savecontext(Create_Process_Part2, &temp2->context,
476 stackptr + stacksize - sizeof(void *));
477 /* End of gross hack */
488 LWP_CurrentProcess(PROCESS * pid)
489 { /* returns pid of current process */
490 Debug(0, ("Entered Current_Process"));
501 Debug(0, ("Entered ThreadId"));
508 #define LWPANCHOR (*lwp_init)
511 LWP_DestroyProcess(PROCESS pid)
512 { /* destroy a lightweight process */
515 Debug(0, ("Entered Destroy_Process"));
517 if (lwp_cpptr != pid) {
518 Dispose_of_Dead_PCB(pid);
521 pid->status = DESTROYED;
522 move(pid, &runnable[pid->priority], &blocked);
524 #if defined(__hp9000s800) || defined(AFS_PARISC_LINUX24_ENV)
525 savecontext(Dispatcher, &(temp->context),
526 &(LWPANCHOR.dsptchstack[MINFRAME]));
527 #elif defined(AFS_SGI62_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
528 savecontext(Dispatcher, &(temp->context),
530 dsptchstack[(sizeof LWPANCHOR.dsptchstack) - 8]));
531 #elif defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_SPARC_LINUX20_ENV)
532 savecontext(Dispatcher, &(temp->context),
534 dsptchstack[(sizeof LWPANCHOR.dsptchstack) -
536 #elif defined(AFS_S390_LINUX20_ENV)
537 savecontext(Dispatcher, &(temp->context),
539 dsptchstack[(sizeof LWPANCHOR.dsptchstack) -
542 savecontext(Dispatcher, &(temp->context),
544 dsptchstack[(sizeof LWPANCHOR.dsptchstack) -
554 LWP_DispatchProcess(void)
555 { /* explicit voluntary preemption */
556 Debug(2, ("Entered Dispatch_Process"));
570 for (i = 0; i < MAX_PRIORITIES; i++)
571 for_all_elts(x, runnable[i], {
572 printf("[Priority %d]\n", i);
576 for_all_elts(x, blocked, {
577 Dump_One_Process(x);}
579 for_all_elts(x, qwaiting, {
580 Dump_One_Process(x);}
583 printf("***LWP: LWP support not initialized\n");
589 LWP_GetProcessPriority(PROCESS pid, int *priority)
590 { /* returns process priority */
591 Debug(0, ("Entered Get_Process_Priority"));
593 *priority = pid->priority;
600 LWP_InitializeProcessSupport(int priority, PROCESS * pid)
603 struct lwp_pcb dummy;
607 Debug(0, ("Entered LWP_InitializeProcessSupport"));
608 if (lwp_init != NULL)
611 /* Set up offset for stack checking -- do this as soon as possible */
612 stack_offset = (char *)&dummy.stack - (char *)&dummy;
614 if (priority >= MAX_PRIORITIES)
616 for (i = 0; i < MAX_PRIORITIES; i++) {
617 runnable[i].head = NULL;
618 runnable[i].count = 0;
622 qwaiting.head = NULL;
624 lwp_init = (struct lwp_ctl *)malloc(sizeof(struct lwp_ctl));
625 temp = (PROCESS) malloc(sizeof(struct lwp_pcb));
626 if (lwp_init == NULL || temp == NULL)
627 Abort_LWP("Insufficient Storage to Initialize LWP Support");
628 LWPANCHOR.processcnt = 1;
629 LWPANCHOR.outerpid = temp;
630 LWPANCHOR.outersp = NULL;
631 Initialize_PCB(temp, priority, NULL, 0, NULL, NULL,
632 "Main Process [created by LWP]");
633 insert(temp, &runnable[priority]);
634 savecontext(Dispatcher, &temp->context, NULL);
635 LWPANCHOR.outersp = temp->context.topstack;
639 /* get minimum stack size from the environment. this allows the administrator
640 * to change the lwp stack dynamically without getting a new binary version.
642 if ((value = getenv("AFS_LWP_STACK_SIZE")) == NULL)
643 lwp_MinStackSize = AFS_LWP_MINSTACKSIZE;
646 (AFS_LWP_MINSTACKSIZE >
647 atoi(value) ? AFS_LWP_MINSTACKSIZE : atoi(value));
653 LWP_INTERNALSIGNAL(char *event, int yield)
654 { /* signal the occurence of an event */
655 Debug(2, ("Entered LWP_SignalProcess"));
658 rc = Internal_Signal(event);
667 LWP_TerminateProcessSupport(void)
668 { /* terminate all LWP support */
671 Debug(0, ("Entered Terminate_Process_Support"));
672 if (lwp_init == NULL)
674 if (lwp_cpptr != LWPANCHOR.outerpid)
675 Abort_LWP("Terminate_Process_Support invoked from wrong process!");
676 for (i = 0; i < MAX_PRIORITIES; i++)
677 for_all_elts(cur, runnable[i], {
680 for_all_elts(cur, blocked, {
683 for_all_elts(cur, qwaiting, {
692 LWP_WaitProcess(char *event)
693 { /* wait on a single event */
696 Debug(2, ("Entered Wait_Process"));
698 return LWP_EBADEVENT;
701 return LWP_MwaitProcess(1, tempev);
705 LWP_MwaitProcess(int wcount, char *evlist[])
706 { /* wait on m of n events */
707 register int ecount, i;
710 Debug(0, ("Entered Mwait_Process [waitcnt = %d]", wcount));
712 if (evlist == NULL) {
714 return LWP_EBADCOUNT;
717 for (ecount = 0; evlist[ecount] != NULL; ecount++);
721 return LWP_EBADCOUNT;
726 if (wcount > ecount || wcount < 0) {
728 return LWP_EBADCOUNT;
730 if (ecount > lwp_cpptr->eventlistsize) {
732 lwp_cpptr->eventlist =
733 (char **)realloc(lwp_cpptr->eventlist,
734 ecount * sizeof(char *));
735 lwp_cpptr->eventlistsize = ecount;
737 for (i = 0; i < ecount; i++)
738 lwp_cpptr->eventlist[i] = evlist[i];
740 lwp_cpptr->status = WAITING;
742 move(lwp_cpptr, &runnable[lwp_cpptr->priority], &blocked);
745 lwp_cpptr->wakevent = 0;
746 lwp_cpptr->waitcnt = wcount;
747 lwp_cpptr->eventcnt = ecount;
758 LWP_StackUsed(PROCESS pid, int *maxa, int *used)
760 *maxa = pid->stacksize;
761 *used = Stack_Used(pid->stack, *maxa);
768 * The following functions are strictly
769 * INTERNAL to the LWP support package.
775 struct lwp_context tempcontext;
777 Debug(0, ("Entered Abort_LWP"));
778 printf("***LWP: %s\n", msg);
779 printf("***LWP: Abort --- dumping PCBs ...\n");
783 if (LWPANCHOR.outersp == NULL)
786 savecontext(Exit_LWP, &tempcontext, LWPANCHOR.outersp);
791 Create_Process_Part2(void)
792 { /* creates a context for the new process */
795 Debug(2, ("Entered Create_Process_Part2"));
796 temp = lwp_cpptr; /* Get current process id */
797 savecontext(Dispatcher, &temp->context, NULL);
798 (*temp->ep) (temp->parm);
799 LWP_DestroyProcess(temp);
804 Delete_PCB(register PROCESS pid)
805 { /* remove a PCB from the process list */
806 Debug(4, ("Entered Delete_PCB"));
808 (pid->blockflag || pid->status == WAITING
810 DESTROYED ? &blocked :
811 (pid->status == QWAITING) ? &qwaiting :
812 &runnable[pid->priority]));
813 LWPANCHOR.processcnt--;
819 Dump_One_Process(PROCESS pid)
823 printf("***LWP: Process Control Block at 0x%x\n", pid);
824 printf("***LWP: Name: %s\n", pid->name);
826 printf("***LWP: Initial entry point: 0x%x\n", pid->ep);
828 printf("BLOCKED and ");
829 switch (pid->status) {
846 printf("***LWP: Priority: %d \tInitial parameter: 0x%x\n", pid->priority,
848 if (pid->stacksize != 0) {
849 printf("***LWP: Stacksize: %d \tStack base address: 0x%x\n",
850 pid->stacksize, pid->stack);
851 printf("***LWP: HWM stack usage: ");
852 printf("%d\n", Stack_Used(pid->stack, pid->stacksize));
854 printf("***LWP: Current Stack Pointer: 0x%x\n", pid->context.topstack);
855 if (pid->eventcnt > 0) {
856 printf("***LWP: Number of events outstanding: %d\n", pid->waitcnt);
857 printf("***LWP: Event id list:");
858 for (i = 0; i < pid->eventcnt; i++)
859 printf(" 0x%x", pid->eventlist[i]);
862 if (pid->wakevent > 0)
863 printf("***LWP: Number of last wakeup event: %d\n", pid->wakevent);
869 purge_dead_pcbs(void)
871 for_all_elts(cur, blocked, {
872 if (cur->status == DESTROYED) Dispose_of_Dead_PCB(cur);}
877 int LWP_TraceProcesses = 0;
881 { /* Lightweight process dispatcher */
884 static int dispatch_count = 0;
886 if (LWP_TraceProcesses > 0) {
887 for (i = 0; i < MAX_PRIORITIES; i++) {
888 printf("[Priority %d, runnable (%d):", i, runnable[i].count);
889 for_all_elts(p, runnable[i], {
890 printf(" \"%s\"", p->name);
895 printf("[Blocked (%d):", blocked.count);
896 for_all_elts(p, blocked, {
897 printf(" \"%s\"", p->name);
901 printf("[Qwaiting (%d):", qwaiting.count);
902 for_all_elts(p, qwaiting, {
903 printf(" \"%s\"", p->name);
910 /* Check for stack overflowif this lwp has a stack. Check for
911 * the guard word at the front of the stack being damaged and
912 * for the stack pointer being below the front of the stack.
913 * WARNING! This code assumes that stacks grow downward. */
914 #if defined(__hp9000s800) || defined(AFS_PARISC_LINUX24_ENV)
915 /* Fix this (stackcheck at other end of stack?) */
916 if (lwp_cpptr != NULL && lwp_cpptr->stack != NULL
917 && (lwp_cpptr->stackcheck !=
918 *(afs_int32 *) ((lwp_cpptr->stack) + lwp_cpptr->stacksize - 4)
919 || lwp_cpptr->context.topstack >
920 lwp_cpptr->stack + lwp_cpptr->stacksize - 4)) {
922 if (lwp_cpptr && lwp_cpptr->stack
923 && (lwp_cpptr->stackcheck != *(int *)(lwp_cpptr->stack)
924 || lwp_cpptr->context.topstack < lwp_cpptr->stack
925 || lwp_cpptr->context.topstack >
926 (lwp_cpptr->stack + lwp_cpptr->stacksize))) {
928 printf("stackcheck = %u: stack = %u \n", lwp_cpptr->stackcheck,
929 *(int *)lwp_cpptr->stack);
930 printf("topstack = 0x%x: stackptr = 0x%x: stacksize = 0x%x\n",
931 lwp_cpptr->context.topstack, lwp_cpptr->stack,
932 lwp_cpptr->stacksize);
934 switch (lwp_overflowAction) {
943 lwp_overflowAction = LWP_SOQUIET;
948 /* Move head of current runnable queue forward if current LWP is still in it. */
949 if (lwp_cpptr != NULL && lwp_cpptr == runnable[lwp_cpptr->priority].head)
950 runnable[lwp_cpptr->priority].head =
951 runnable[lwp_cpptr->priority].head->next;
952 /* Find highest priority with runnable processes. */
953 for (i = MAX_PRIORITIES - 1; i >= 0; i--)
954 if (runnable[i].head != NULL)
958 Abort_LWP("No READY processes");
961 if (LWP_TraceProcesses > 0)
962 printf("Dispatch %d [PCB at 0x%x] \"%s\"\n", ++dispatch_count,
963 runnable[i].head, runnable[i].head->name);
966 Abort_LWP("PRE_Block not 1");
967 lwp_cpptr = runnable[i].head;
969 returnto(&lwp_cpptr->context);
972 /* Complain of a stack overflow to stderr without using stdio. */
974 Overflow_Complain(void)
978 char *msg1 = " LWP: stack overflow in process ";
981 currenttime = time(0);
982 timeStamp = ctime(¤ttime);
984 write(2, timeStamp, strlen(timeStamp));
986 write(2, msg1, strlen(msg1));
987 write(2, lwp_cpptr->name, strlen(lwp_cpptr->name));
988 write(2, msg2, strlen(msg2));
992 Dispose_of_Dead_PCB(PROCESS cur)
994 Debug(4, ("Entered Dispose_of_Dead_PCB"));
998 Internal_Signal(cur);
1009 Free_PCB(PROCESS pid)
1011 Debug(4, ("Entered Free_PCB"));
1012 if (pid->stack != NULL) {
1014 ("HWM stack usage: %d, [PCB at 0x%x]",
1015 Stack_Used(pid->stack, pid->stacksize), pid));
1016 #ifndef AFS_AIX32_ENV
1020 if (pid->eventlist != NULL)
1021 free(pid->eventlist);
1026 Initialize_PCB(PROCESS temp, int priority, char *stack, int stacksize,
1027 int (*ep) (), void *parm, char *name)
1031 Debug(4, ("Entered Initialize_PCB"));
1033 while (((temp->name[i] = name[i]) != '\0') && (i < 31))
1035 temp->name[31] = '\0';
1036 temp->status = READY;
1037 temp->eventlist = (char **)malloc(EVINITSIZE * sizeof(char *));
1038 temp->eventlistsize = EVINITSIZE;
1042 temp->blockflag = 0;
1043 temp->iomgrRequest = 0;
1044 temp->priority = priority;
1045 temp->index = lwp_nextindex++;
1046 temp->stack = stack;
1047 temp->stacksize = stacksize;
1048 #if defined(__hp9000s800) || defined(AFS_PARISC_LINUX24_ENV)
1049 if (temp->stack != NULL)
1050 temp->stackcheck = *(int *)((temp->stack) + stacksize - 4);
1052 if (temp->stack != NULL)
1053 temp->stackcheck = *(int *)(temp->stack);
1057 temp->misc = NULL; /* currently unused */
1060 temp->lwp_rused = 0;
1061 temp->level = 1; /* non-preemptable */
1065 Internal_Signal(register char *event)
1067 int rc = LWP_ENOWAIT;
1070 Debug(0, ("Entered Internal_Signal [event id 0x%x]", event));
1074 return LWP_EBADEVENT;
1075 for_all_elts(temp, blocked, {
1076 if (temp->status == WAITING)
1077 for (i = 0; i < temp->eventcnt; i++) {
1078 if (temp->eventlist[i] == event) {
1079 temp->eventlist[i] = NULL; rc = LWP_SUCCESS;
1080 Debug(0, ("Signal satisfied for PCB 0x%x", temp));
1081 if (--temp->waitcnt == 0) {
1082 temp->status = READY; temp->wakevent = i + 1;
1083 move(temp, &blocked, &runnable[temp->priority]); break;}
1091 /* This can be any unlikely pattern except 0x00010203 or the reverse. */
1092 #define STACKMAGIC 0xBADBADBA
1094 Initialize_Stack(char *stackptr, int stacksize)
1098 Debug(4, ("Entered Initialize_Stack"));
1099 if (lwp_stackUseEnabled)
1100 for (i = 0; i < stacksize; i++)
1101 stackptr[i] = i & 0xff;
1103 #if defined(__hp9000s800) || defined(AFS_PARISC_LINUX24_ENV)
1104 *(afs_int32 *) (stackptr + stacksize - 4) = STACKMAGIC;
1106 *(afs_int32 *) stackptr = STACKMAGIC;
1112 Stack_Used(register char *stackptr, int stacksize)
1116 #if defined(__hp9000s800) || defined(AFS_PARISC_LINUX24_ENV)
1117 if (*(afs_int32 *) (stackptr + stacksize - 4) == STACKMAGIC)
1120 for (i = stacksize - 1; i >= 0; i--)
1121 if ((unsigned char)stackptr[i] != (i & 0xff))
1126 if (*(afs_int32 *) stackptr == STACKMAGIC)
1129 for (i = 0; i < stacksize; i++)
1130 if ((unsigned char)stackptr[i] != (i & 0xff))
1131 return (stacksize - i);
1139 LWP_NewRock(int Tag, char *Value)
1140 /* Finds a free rock and sets its value to Value.
1142 * LWP_SUCCESS Rock did not exist and a new one was used
1143 * LWP_EBADROCK Rock already exists.
1144 * LWP_ENOROCKS All rocks are in use.
1146 * From the above semantics, you can only set a rock value once. This is specifically
1147 * to prevent multiple users of the LWP package from accidentally using the same Tag
1148 * value and clobbering others. You can always use one level of indirection to obtain
1149 * a rock whose contents can change.
1153 register struct rock *ra; /* rock array */
1155 ra = lwp_cpptr->lwp_rlist;
1157 for (i = 0; i < lwp_cpptr->lwp_rused; i++)
1158 if (ra[i].tag == Tag)
1159 return (LWP_EBADROCK);
1161 if (lwp_cpptr->lwp_rused < MAXROCKS) {
1162 ra[lwp_cpptr->lwp_rused].tag = Tag;
1163 ra[lwp_cpptr->lwp_rused].value = Value;
1164 lwp_cpptr->lwp_rused++;
1165 return (LWP_SUCCESS);
1167 return (LWP_ENOROCKS);
1172 LWP_GetRock(int Tag, char **Value)
1173 /* Obtains the pointer Value associated with the rock Tag of this LWP.
1175 * LWP_SUCCESS if specified rock exists and Value has been filled
1176 * LWP_EBADROCK rock specified does not exist
1180 register struct rock *ra;
1182 ra = lwp_cpptr->lwp_rlist;
1184 for (i = 0; i < lwp_cpptr->lwp_rused; i++)
1185 if (ra[i].tag == Tag) {
1186 *Value = ra[i].value;
1187 return (LWP_SUCCESS);
1189 return (LWP_EBADROCK);
1193 #ifdef AFS_AIX32_ENV
1195 setlim(int limcon, uchar_t hard, int limit)
1199 (void)getrlimit(limcon, &rlim);
1201 limit = limit * 1024;
1203 rlim.rlim_max = limit;
1204 else if (limit == RLIM_INFINITY && geteuid() != 0)
1205 rlim.rlim_cur = rlim.rlim_max;
1207 rlim.rlim_cur = limit;
1209 /* Must use ulimit() due to Posix constraints */
1210 if (limcon == RLIMIT_FSIZE) {
1213 ((hard ? rlim.rlim_max : rlim.rlim_cur) / 512)) < 0) {
1214 printf("Can't %s%s limit\n",
1215 limit == RLIM_INFINITY ? "remove" : "set",
1216 hard ? " hard" : "");
1220 if (setrlimit(limcon, &rlim) < 0) {
1222 printf("Can't %s%s limit\n",
1223 limit == RLIM_INFINITY ? "remove" : "set",
1224 hard ? " hard" : "");
1234 * Print the specific limit out
1237 plim(char *name, afs_int32 lc, uchar_t hard)
1242 printf("%s \t", name);
1243 (void)getrlimit(lc, &rlim);
1244 lim = hard ? rlim.rlim_max : rlim.rlim_cur;
1245 if (lim == RLIM_INFINITY)
1246 printf("unlimited");
1247 printf("%d %s", lim / 1024, "kbytes");
1255 LWP_NoYieldSignal(char *event)
1257 return (LWP_INTERNALSIGNAL(event, 0));
1261 LWP_SignalProcess(char *event)
1263 return (LWP_INTERNALSIGNAL(event, 1));
1268 #ifdef USE_SOLARIS_THREADS
1271 #include "pthread.h"
1276 pthread_mutex_t lwp_mutex; /* Mutex to ensure mutual exclusion of all LWP threads */
1278 PROCESS lwp_process_list; /* List of LWP initiated threads */
1280 pthread_key_t lwp_process_key; /* Key associating lwp pid with thread */
1282 #define CHECK check(__LINE__);
1284 typedef struct event {
1285 struct event *next; /* next in hash chain */
1286 char *event; /* lwp event: an address */
1287 int refcount; /* Is it in use? */
1288 pthread_cond_t cond; /* Currently associated condition variable */
1289 int seq; /* Sequence number: this is incremented
1290 * by wakeup calls; wait will not return until
1294 #define HASHSIZE 127
1295 event_t *hashtable[HASHSIZE]; /* Hash table for events */
1296 #define hash(event) ((unsigned long) (event) % HASHSIZE);
1298 #if CMA_DEBUG || DEBUGF
1300 lwp_process_string(void)
1302 static char id[200];
1304 LWP_CurrentProcess(&p);
1305 sprintf(id, "PID %x <%s>", p, p->name);
1311 lwp_unimplemented(char *interface)
1314 "cmalwp: %s is not currently implemented: program aborted\n",
1320 lwpabort(char *interface)
1322 fprintf(stderr, "cmalwp: %s failed unexpectedly\n", interface);
1329 lwp_unimplemented("LWP_QWait");
1333 LWP_QSignal(int pid)
1335 lwp_unimplemented("LWP_QSignal");
1338 /* Allocate and initialize an LWP process handle. The associated pthread handle
1339 * must be added by the caller, and the structure threaded onto the LWP active
1340 * process list by lwp_thread_process */
1342 lwp_alloc_process(char *name, pthread_startroutine_t ep, pthread_addr_t arg)
1345 assert(lp = (PROCESS) malloc(sizeof(*lp)));
1346 memset((char *)lp, 0, sizeof(*lp));
1350 sprintf(temp, "unnamed_process_%04d", ++procnum);
1351 assert(name = (char *)malloc(strlen(temp) + 1));
1360 /* Thread the LWP process descriptor *lp onto the lwp active process list
1361 * and associate a back pointer to the process descriptor from the associated
1364 lwp_thread_process(PROCESS lp)
1366 lp->next = lwp_process_list;
1367 lwp_process_list = lp;
1368 assert(!pthread_setspecific(lwp_process_key, (pthread_addr_t) lp));
1371 /* The top-level routine used as entry point to explicitly created LWP
1372 * processes. This completes a few details of process creation left
1373 * out by LWP_CreateProcess and calls the user-specified entry point */
1375 lwp_top_level(pthread_addr_t argp)
1377 PROCESS lp = (PROCESS) argp;
1379 assert(!pthread_mutex_lock(&lwp_mutex));
1380 lwp_thread_process(lp);
1382 assert(!pthread_mutex_unlock(&lwp_mutex));
1383 /* Should cleanup state */
1387 LWP_CreateProcess(pthread_startroutine_t ep, int stacksize, int priority,
1388 void *parm, char *name, PROCESS * pid)
1391 pthread_attr_t attr;
1395 #ifndef LWP_NO_PRIORITIES
1396 if (!cmalwp_pri_inrange(priority))
1399 assert(!pthread_attr_create(&attr));
1400 assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
1402 assert(!pthread_attr_setstacksize(&attr, stacksize));
1404 (void)pthread_attr_setinheritsched(&attr, PTHREAD_DEFAULT_SCHED);
1405 (void)pthread_attr_setsched(&attr, SCHED_FIFO);
1406 #ifndef LWP_NO_PRIORITIES
1407 (void)pthread_attr_setprio(&attr, cmalwp_lwppri_to_cmapri(priority));
1410 lp = lwp_alloc_process(name, (pthread_startroutine_t) ep,
1411 (pthread_addr_t) parm);
1413 /* allow new thread to run if higher priority */
1414 assert(!pthread_mutex_unlock(&lwp_mutex));
1415 /* process is only added to active list after first time it runs (it adds itself) */
1417 pthread_create(&lp->handle, attr,
1418 (pthread_startroutine_t) lwp_top_level,
1419 (pthread_addr_t) lp);
1420 assert(!pthread_attr_delete(&attr));
1421 assert(!pthread_mutex_lock(&lwp_mutex));
1431 LWP_ActiveProcess(void)
1432 { /* returns pid of current process */
1434 assert(!pthread_getspecific(lwp_process_key, (pthread_addr_t *) & pid));
1439 LWP_CurrentProcess(PROCESS * pid)
1440 { /* get pid of current process */
1441 assert(!pthread_getspecific(lwp_process_key, (pthread_addr_t *) pid));
1446 LWP_DestroyProcess(PROCESS pid)
1447 { /* destroy a lightweight process */
1448 lwp_unimplemented("LWP_DestroyProcess");
1452 LWP_DispatchProcess(void)
1453 { /* explicit voluntary preemption */
1454 assert(!pthread_mutex_unlock(&lwp_mutex));
1456 assert(!pthread_mutex_lock(&lwp_mutex));
1461 lwp_process_key_destructor(void)
1466 LWP_InitializeProcessSupport(int priority, PROCESS * pid)
1468 static int initialized = 0;
1478 #ifndef LWP_NO_PRIORITIES
1479 if (priority < 0 || priority > LWP_MAX_PRIORITY)
1483 /* Create pthread key to associate LWP process descriptor with each
1484 * LWP-created thread */
1485 assert(!pthread_keycreate(&lwp_process_key, (pthread_destructor_t)
1486 lwp_process_key_destructor));
1488 lp = lwp_alloc_process("main process", main, 0);
1489 lp->handle = pthread_self();
1490 lwp_thread_process(lp);
1491 #ifndef LWP_NO_PRIORITIES
1492 (void)pthread_setscheduler(pthread_self(), SCHED_FIFO,
1493 cmalwp_lwppri_to_cmapri(priority));
1496 assert(pthread_mutex_init(&lwp_mutex, MUTEX_FAST_NP) == 0);
1497 assert(pthread_mutex_lock(&lwp_mutex) == 0);
1504 LWP_TerminateProcessSupport(void)
1505 { /* terminate all LWP support */
1506 lwp_unimplemented("LWP_TerminateProcessSupport");
1509 /* Get and initialize event structure corresponding to lwp event (i.e. address) */
1511 getevent(char *event)
1513 event_t *evp, *newp;
1516 hashcode = hash(event);
1517 evp = hashtable[hashcode];
1520 if (evp->event == event) {
1524 if (evp->refcount == 0)
1529 newp = (event_t *) malloc(sizeof(event_t));
1531 newp->next = hashtable[hashcode];
1532 hashtable[hashcode] = newp;
1533 assert(!pthread_cond_init(&newp->cond, ((pthread_condattr_t) 0)));
1536 newp->event = event;
1541 /* Release the specified event */
1542 #define relevent(evp) ((evp)->refcount--)
1545 LWP_WaitProcess(char *event)
1546 { /* wait on a single event */
1549 debugf(("%s: wait process (%x)\n", lwp_process_string(), event));
1551 return LWP_EBADEVENT;
1552 ev = getevent(event);
1554 while (seq == ev->seq) {
1555 assert(pthread_cond_wait(&ev->cond, &lwp_mutex) == 0);
1557 debugf(("%s: Woken up (%x)\n", lwp_process_string(), event));
1563 LWP_MwaitProcess(int wcount, char *evlist[])
1564 { /* wait on m of n events */
1565 lwp_unimplemented("LWP_MWaitProcess");
1569 LWP_NoYieldSignal(char *event)
1572 debugf(("%s: no yield signal (%x)\n", lwp_process_string(), event));
1574 return LWP_EBADEVENT;
1575 ev = getevent(event);
1576 if (ev->refcount > 1) {
1578 assert(pthread_cond_broadcast(&ev->cond) == 0);
1585 LWP_SignalProcess(char *event)
1588 debugf(("%s: signal process (%x)\n", lwp_process_string(), event));
1590 return LWP_EBADEVENT;
1591 ev = getevent(event);
1592 if (ev->refcount > 1) {
1594 assert(!pthread_mutex_unlock(&lwp_mutex));
1595 assert(!pthread_cond_broadcast(&ev->cond));
1597 assert(!pthread_mutex_lock(&lwp_mutex));
1604 LWP_StackUsed(PROCESS pid, int *maxa, int *used)
1606 lwp_unimplemented("LWP_StackUsed");
1610 LWP_NewRock(int Tag, char *Value)
1612 lwp_unimplemented("LWP_NewRock");
1616 LWP_GetRock(int Tag, char **Value)
1618 lwp_unimplemented("LWP_GetRock");
1622 LWP_GetProcessPriority(PROCESS pid, int *priority)
1623 { /* returns process priority */
1624 lwp_unimplemented("LWP_GetProcessPriority");
1627 #endif /* USE_PTHREADS */