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 /* For NT, LWP is implemented on top of fibers. The design attempts to
11 * follow the current LWP implementation so that we are not using 2 LWP
12 * models. The parts of the Unix LWP model that are not actually used in
13 * AFS have not been implemented at all. With the exception of preemption,
14 * adding those parts to this NT base would be a matter of adding routines.
16 * required lib is kernel32.lib, header is winbase.h
19 #include <afs/param.h>
23 #include <afs/afsutil.h>
27 static struct lwp_ctl *lwp_init;
28 #define LWPANCHOR (*lwp_init)
41 #define Debug(level, msg)\
42 if (lwp_debug && lwp_debug >= level) {\
43 printf("***LWP (0x%x): ", lwp_cpptr);\
49 #define Debug(level, msg)
53 /* Forward declarations */
54 static void Dispatcher(void);
55 static void Exit_LWP(void);
56 static void Abort_LWP(char *msg);
57 static VOID WINAPI Enter_LWP(PVOID fiberData);
58 static void Initialize_PCB(PROCESS pcb, int priority, int stacksize,
59 int (*funP)(), void *argP, char *name);
61 static void Dispose_of_Dead_PCB();
62 static void Free_PCB();
63 static int Internal_Signal();
64 static void purge_dead_pcbs(void);
66 static void lwp_remove(PROCESS p, struct QUEUE *q);
67 static void insert(PROCESS p, struct QUEUE *q);
68 static void move(PROCESS p, struct QUEUE *from, struct QUEUE *to);
70 /* biggest LWP stack created so far - used by IOMGR */
71 int lwp_MaxStackSeen = 0;
74 static void Dump_One_Process(PROCESS pid);
75 static void Dump_Processes(void);
80 #define MAX_PRIORITIES (LWP_MAX_PRIORITY+1)
82 /* Invariant for runnable queues: The head of each queue points to the
83 * currently running process if it is in that queue, or it points to the
84 * next process in that queue that should run.
89 } runnable[MAX_PRIORITIES], blocked;
92 #define for_all_elts(var, q, body)\
94 register PROCESS var, _NEXT_;\
96 for (_I_=q.count, var = q.head; _I_>0; _I_--, var=_NEXT_) {\
97 _NEXT_ = var -> next;\
104 LPVOID ConvertThreadToFiber(PROCESS x)
108 LPVOID CreateFiber(DWORD x ,LPVOID y,PROCESS z)
113 VOID SwitchToFiber(LPVOID p)
117 VOID DeleteFiber(LPVOID p)
123 int lwp_MinStackSize = 0;
125 /* LWP_InitializeProcessSupport - setup base support for fibers.
128 * priority - priority of main thread.
131 * pid - return this process
133 * LWP_SUCCESS (else aborts)
137 int LWP_InitializeProcessSupport(int priority, PROCESS *pid)
143 Debug(0, ("Entered LWP_InitializeProcessSupport"))
144 if (lwp_init != NULL) return LWP_SUCCESS;
146 if (priority >= MAX_PRIORITIES) return LWP_EBADPRI;
148 pcb = (PROCESS)malloc(sizeof(*pcb));
150 Abort_LWP("Insufficient Storage to Initialize LWP PCB");
151 (void) memset((void*)pcb, 0, sizeof(*pcb));
152 pcb->fiber = ConvertThreadToFiber(pcb);
154 Abort_LWP("Cannot convert main thread to LWP fiber");
156 lwp_init = (struct lwp_ctl *) malloc(sizeof(struct lwp_ctl));
157 if (lwp_init == NULL)
158 Abort_LWP("Insufficient Storage to Initialize LWP CTL");
159 (void) memset((void*)lwp_init, 0, sizeof(struct lwp_ctl));
161 for (i=0; i<MAX_PRIORITIES; i++) {
162 runnable[i].head = NULL;
163 runnable[i].count = 0;
168 LWPANCHOR.processcnt = 1;
169 LWPANCHOR.outerpid = pcb;
170 LWPANCHOR.outersp = NULL;
173 Initialize_PCB(pcb, priority, 0, NULL, NULL,
174 "Main Process [created by LWP]");
177 Debug(10, ("Init: Insert 0x%x into runnable at priority %d\n", pcb, priority))
178 insert(pcb, &runnable[priority]);
180 if ( ( value = getenv("AFS_LWP_STACK_SIZE")) == NULL )
181 lwp_MinStackSize = AFS_LWP_MINSTACKSIZE;
183 lwp_MinStackSize = (AFS_LWP_MINSTACKSIZE>atoi(value)?
184 AFS_LWP_MINSTACKSIZE : atoi(value));
191 /* LWP_CreateProcess - create a new fiber and start executing it.
194 * funP - start function
195 * stacksize - size of
196 * priority - LWP priority
197 * argP - initial parameter for start function
201 * pid - handle of created LWP
204 * LWP_EINIT - error in intialization.
207 int LWP_CreateProcess(int (*funP)(), int stacksize, int priority, void *argP,
208 char *name, PROCESS *pid)
214 pcb = (PROCESS)malloc(sizeof(*pcb));
217 (void) memset((void*)pcb, 0, sizeof(*pcb));
220 * on some systems (e.g. hpux), a minimum usable stack size has
223 if (stacksize < lwp_MinStackSize) {
224 stacksize = lwp_MinStackSize;
226 /* more stack size computations; keep track of for IOMGR */
227 if (lwp_MaxStackSeen < stacksize)
228 lwp_MaxStackSeen = stacksize;
230 pcb->fiber = CreateFiber(stacksize, Enter_LWP, pcb);
231 if (pcb->fiber == NULL) {
235 Debug(0, ("Create: pcb=0x%x, funP=0x%x, argP=0x%x\n", pcb, funP, argP))
236 /* Fiber is now created, so fill in PCB */
237 Initialize_PCB(pcb, priority, stacksize, funP, argP, name);
238 Debug(10, ("Create: Insert 0x%x into runnable at priority %d\n", pcb, priority))
239 insert(pcb, &runnable[priority]);
241 LWPANCHOR.processcnt++;
243 /* And hand off execution. */
244 SwitchToFiber(pcb->fiber);
251 int LWP_DestroyProcess(PROCESS pid)
253 Debug(0, ("Entered Destroy_Process"))
258 if (lwp_cpptr != pid) {
259 Dispose_of_Dead_PCB(pid);
261 pid->status = DESTROYED;
262 move(pid, &runnable[pid->priority], &blocked);
273 (tp=lwp_cpptr) -> status = QWAITING;
274 lwp_remove(tp, &runnable[tp->priority]);
279 int LWP_QSignal(PROCESS pid)
281 if (pid->status == QWAITING) {
283 Debug(10, ("QSignal: Insert 0x%x into runnable at priority %d\n", pid, pid->priority))
284 insert(pid, &runnable[pid->priority]);
287 else return LWP_ENOWAIT;
290 int LWP_CurrentProcess(PROCESS *pid)
292 Debug(0, ("Entered Current_Process"))
300 PROCESS LWP_ThreadId()
302 Debug(0, ("Entered ThreadId"))
309 int LWP_DispatchProcess(void) /* explicit voluntary preemption */
311 Debug(2, ("Entered Dispatch_Process"))
321 static void Dump_Processes(void)
325 for (i=0; i<MAX_PRIORITIES; i++)
326 for_all_elts(x, runnable[i], {
327 printf("[Priority %d]\n", i);
330 for_all_elts(x, blocked, { Dump_One_Process(x); })
332 printf("***LWP: LWP support not initialized\n");
337 int LWP_GetProcessPriority(pid, priority) /* returns process priority */
341 Debug(0, ("Entered Get_Process_Priority"))
343 *priority = pid->priority;
349 static int Internal_Signal(char *event)
351 int rc = LWP_ENOWAIT;
354 Debug(0, ("Entered Internal_Signal [event id 0x%x]", event))
355 if (!lwp_init) return LWP_EINIT;
356 if (event == NULL) return LWP_EBADEVENT;
357 for_all_elts(temp, blocked, {
358 if (temp->status == WAITING)
359 for (i=0; i < temp->eventcnt; i++) {
360 if (temp -> eventlist[i] == event) {
361 temp -> eventlist[i] = NULL;
363 Debug(0, ("Signal satisfied for PCB 0x%x", temp))
364 if (--temp->waitcnt == 0) {
365 temp -> status = READY;
366 temp -> wakevent = i+1;
367 move(temp, &blocked, &runnable[temp->priority]);
376 /* signal the occurence of an event */
377 int LWP_INTERNALSIGNAL(void *event, int yield)
379 Debug(2, ("Entered LWP_SignalProcess"))
382 rc = Internal_Signal(event);
390 int LWP_TerminateProcessSupport() /* terminate all LWP support */
394 Debug(0, ("Entered Terminate_Process_Support"))
395 if (lwp_init == NULL) return LWP_EINIT;
396 if (lwp_cpptr != LWPANCHOR.outerpid)
397 Abort_LWP("Terminate_Process_Support invoked from wrong process!");
398 /* Is this safe??? @@@ */
399 for (i=0; i<MAX_PRIORITIES; i++)
400 for_all_elts(cur, runnable[i], { Free_PCB(cur); })
401 for_all_elts(cur, blocked, { Free_PCB(cur); })
407 int LWP_MwaitProcess(int wcount, void **evlist)
411 Debug(0, ("Entered Mwait_Process [waitcnt = %d]", wcount))
412 if (evlist == NULL) {
414 return LWP_EBADCOUNT;
417 for (ecount = 0; evlist[ecount] != NULL; ecount++) ;
421 return LWP_EBADCOUNT;
427 if (wcount>ecount || wcount<0) {
429 return LWP_EBADCOUNT;
431 if (ecount > lwp_cpptr->eventlistsize) {
433 void **save_eventlist = lwp_cpptr->eventlist;
434 lwp_cpptr->eventlist = (char **)realloc(lwp_cpptr->eventlist,
435 ecount*sizeof(char *));
436 if (lwp_cpptr->eventlist == NULL) {
437 lwp_cpptr->eventlist = save_eventlist;
441 lwp_cpptr->eventlistsize = ecount;
443 for (i=0; i<ecount; i++) lwp_cpptr -> eventlist[i] = evlist[i];
445 lwp_cpptr->status = WAITING;
447 move(lwp_cpptr, &runnable[lwp_cpptr->priority], &blocked);
450 lwp_cpptr->wakevent = 0;
451 lwp_cpptr->waitcnt = wcount;
452 lwp_cpptr->eventcnt = ecount;
459 int LWP_WaitProcess(void *event) /* wait on a single event */
463 Debug(2, ("Entered Wait_Process"))
464 if (event == NULL) return LWP_EBADEVENT;
467 return LWP_MwaitProcess(1, tempev);
471 /* Internal Support Routines */
472 static void Initialize_PCB(PROCESS pcb, int priority, int stacksize,
473 int (*funP)(), void *argP, char *name)
477 Debug(4, ("Entered Initialize_PCB"))
479 while (((pcb->name[i] = name[i]) != '\0') && (i < 31)) i++;
480 pcb->name[31] = '\0';
481 pcb->priority = priority;
482 pcb->stacksize = stacksize;
486 pcb->eventlist = (void**)malloc(EVINITSIZE*sizeof(void*));
487 pcb->eventlistsize = pcb->eventlist ? EVINITSIZE : 0;
491 pcb->next = pcb->prev = (PROCESS)NULL;
492 pcb->iomgrRequest = (struct IoRequest*)NULL;
493 pcb->index = lwp_nextindex ++;
497 VOID WINAPI Enter_LWP(PVOID fiberData)
499 PROCESS pcb = (PROCESS)fiberData;
501 /* next lines are new..... */
504 Debug(2, ("Enter_LWP: pcb=0x%x, funP=0x%x, argP=0x%x\n", pcb, pcb->funP, pcb->argP))
506 (*pcb->funP)(pcb->argP);
508 LWP_DestroyProcess(pcb);
512 static void Abort_LWP(char *msg)
514 Debug(0, ("Entered Abort_LWP"))
515 printf("***LWP: %s\n",msg);
517 printf("***LWP: Abort --- dumping PCBs ...\n");
524 static void Dump_One_Process(PROCESS pid)
528 printf("***LWP: Process Control Block at 0x%x\n", pid);
529 printf("***LWP: Name: %s\n", pid->name);
530 if (pid->funP != NULL)
531 printf("***LWP: Initial entry point: 0x%x\n", pid->funP);
532 switch (pid->status) {
533 case READY: printf("READY"); break;
534 case WAITING: printf("WAITING"); break;
535 case DESTROYED: printf("DESTROYED"); break;
536 default: printf("unknown");
539 printf("***LWP: Priority: %d \tInitial parameter: 0x%x\n",
540 pid->priority, pid->argP);
541 if (pid->stacksize != 0) {
542 printf("***LWP: Stacksize: %d\n", pid->stacksize);
544 if (pid->eventcnt > 0) {
545 printf("***LWP: Number of events outstanding: %d\n", pid->waitcnt);
546 printf("***LWP: Event id list:");
547 for (i=0;i<pid->eventcnt;i++)
548 printf(" 0x%x", pid->eventlist[i]);
552 printf("***LWP: Number of last wakeup event: %d\n", pid->wakevent);
557 static void purge_dead_pcbs(void)
559 for_all_elts(cur, blocked, { if (cur->status == DESTROYED) Dispose_of_Dead_PCB(cur); })
562 int LWP_TraceProcesses = 0;
564 static void Dispatcher(void)
568 static int dispatch_count = 0;
570 if (LWP_TraceProcesses > 0) {
571 for (i=0; i<MAX_PRIORITIES; i++) {
572 printf("[Priority %d, runnable (%d):", i, runnable[i].count);
573 for_all_elts(p, runnable[i], {
574 printf(" \"%s\"", p->name);
578 printf("[Blocked (%d):", blocked.count);
579 for_all_elts(p, blocked, {
580 printf(" \"%s\"", p->name);
585 /* Move head of current runnable queue forward if current LWP is still
588 if (lwp_cpptr != NULL && lwp_cpptr == runnable[lwp_cpptr->priority].head)
589 runnable[lwp_cpptr->priority].head =
590 runnable[lwp_cpptr->priority].head -> next;
591 /* Find highest priority with runnable processes. */
592 for (i=MAX_PRIORITIES-1; i>=0; i--)
593 if (runnable[i].head != NULL) break;
595 if (i < 0) Abort_LWP("No READY processes");
598 if (LWP_TraceProcesses > 0)
599 printf("Dispatch %d [PCB at 0x%x] \"%s\"\n", ++dispatch_count,
600 runnable[i].head, runnable[i].head->name);
603 lwp_cpptr = runnable[i].head;
604 SwitchToFiber(lwp_cpptr->fiber);
612 static void Exit_LWP(void)
618 static void Delete_PCB(PROCESS pid)
620 Debug(4, ("Entered Delete_PCB"))
621 lwp_remove(pid, (pid->status==WAITING || pid->status==DESTROYED
623 : &runnable[pid->priority]));
624 LWPANCHOR.processcnt--;
628 static void Free_PCB(PROCESS pid)
630 Debug(4, ("Entered Free_PCB"))
631 if (pid->fiber != NULL) {
632 DeleteFiber(pid->fiber);
634 if (pid->eventlist != NULL) free((void*)pid->eventlist);
638 static void Dispose_of_Dead_PCB(PROCESS cur)
640 Debug(4, ("Entered Dispose_of_Dead_PCB"))
645 /* Queue manipulation. */
646 static void lwp_remove(PROCESS p, struct QUEUE *q)
648 /* Special test for only element on queue */
652 /* Not only element, do normal remove */
653 p -> next -> prev = p -> prev;
654 p -> prev -> next = p -> next;
656 /* See if head pointing to this element */
657 if (q->head == p) q -> head = p -> next;
659 p -> next = p -> prev = NULL;
662 static void insert(PROCESS p, struct QUEUE *q)
664 if (q->head == NULL) { /* Queue is empty */
666 p -> next = p -> prev = p;
667 } else { /* Regular insert */
668 p -> prev = q -> head -> prev;
669 q -> head -> prev -> next = p;
670 q -> head -> prev = p;
671 p -> next = q -> head;
676 static void move(PROCESS p, struct QUEUE *from, struct QUEUE *to)
681 #endif /* AFS_NT40_ENV */