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;\
103 int lwp_MinStackSize = 0;
104 /* LWP_InitializeProcessSupport - setup base support for fibers.
107 * priority - priority of main thread.
110 * pid - return this process
112 * LWP_SUCCESS (else aborts)
115 int LWP_InitializeProcessSupport(int priority, PROCESS *pid)
121 Debug(0, ("Entered LWP_InitializeProcessSupport"))
122 if (lwp_init != NULL) return LWP_SUCCESS;
124 if (priority >= MAX_PRIORITIES) return LWP_EBADPRI;
126 pcb = (PROCESS)malloc(sizeof(*pcb));
128 Abort_LWP("Insufficient Storage to Initialize LWP PCB");
129 (void) memset((void*)pcb, 0, sizeof(*pcb));
130 pcb->fiber = ConvertThreadToFiber(pcb);
132 Abort_LWP("Cannot convert main thread to LWP fiber");
134 lwp_init = (struct lwp_ctl *) malloc(sizeof(struct lwp_ctl));
135 if (lwp_init == NULL)
136 Abort_LWP("Insufficient Storage to Initialize LWP CTL");
137 (void) memset((void*)lwp_init, 0, sizeof(struct lwp_ctl));
139 for (i=0; i<MAX_PRIORITIES; i++) {
140 runnable[i].head = NULL;
141 runnable[i].count = 0;
146 LWPANCHOR.processcnt = 1;
147 LWPANCHOR.outerpid = pcb;
148 LWPANCHOR.outersp = NULL;
151 Initialize_PCB(pcb, priority, 0, NULL, NULL,
152 "Main Process [created by LWP]");
155 Debug(10, ("Init: Insert 0x%x into runnable at priority %d\n", pcb, priority))
156 insert(pcb, &runnable[priority]);
158 if ( ( value = getenv("AFS_LWP_STACK_SIZE")) == NULL )
159 lwp_MinStackSize = AFS_LWP_MINSTACKSIZE;
161 lwp_MinStackSize = (AFS_LWP_MINSTACKSIZE>atoi(value)?
162 AFS_LWP_MINSTACKSIZE : atoi(value));
169 /* LWP_CreateProcess - create a new fiber and start executing it.
172 * funP - start function
173 * stacksize - size of
174 * priority - LWP priority
175 * argP - initial parameter for start function
179 * pid - handle of created LWP
182 * LWP_EINIT - error in intialization.
185 int LWP_CreateProcess(int (*funP)(), int stacksize, int priority, void *argP,
186 char *name, PROCESS *pid)
192 pcb = (PROCESS)malloc(sizeof(*pcb));
195 (void) memset((void*)pcb, 0, sizeof(*pcb));
198 * on some systems (e.g. hpux), a minimum usable stack size has
201 if (stacksize < lwp_MinStackSize) {
202 stacksize = lwp_MinStackSize;
204 /* more stack size computations; keep track of for IOMGR */
205 if (lwp_MaxStackSeen < stacksize)
206 lwp_MaxStackSeen = stacksize;
208 pcb->fiber = CreateFiber(stacksize, Enter_LWP, pcb);
209 if (pcb->fiber == NULL) {
213 Debug(0, ("Create: pcb=0x%x, funP=0x%x, argP=0x%x\n", pcb, funP, argP))
214 /* Fiber is now created, so fill in PCB */
215 Initialize_PCB(pcb, priority, stacksize, funP, argP, name);
216 Debug(10, ("Create: Insert 0x%x into runnable at priority %d\n", pcb, priority))
217 insert(pcb, &runnable[priority]);
219 LWPANCHOR.processcnt++;
221 /* And hand off execution. */
222 SwitchToFiber(pcb->fiber);
229 int LWP_DestroyProcess(PROCESS pid)
231 Debug(0, ("Entered Destroy_Process"))
236 if (lwp_cpptr != pid) {
237 Dispose_of_Dead_PCB(pid);
239 pid->status = DESTROYED;
240 move(pid, &runnable[pid->priority], &blocked);
251 (tp=lwp_cpptr) -> status = QWAITING;
252 lwp_remove(tp, &runnable[tp->priority]);
257 int LWP_QSignal(PROCESS pid)
259 if (pid->status == QWAITING) {
261 Debug(10, ("QSignal: Insert 0x%x into runnable at priority %d\n", pid, pid->priority))
262 insert(pid, &runnable[pid->priority]);
265 else return LWP_ENOWAIT;
268 int LWP_CurrentProcess(PROCESS *pid)
270 Debug(0, ("Entered Current_Process"))
278 PROCESS LWP_ThreadId()
280 Debug(0, ("Entered ThreadId"))
287 int LWP_DispatchProcess(void) /* explicit voluntary preemption */
289 Debug(2, ("Entered Dispatch_Process"))
299 static void Dump_Processes(void)
303 for (i=0; i<MAX_PRIORITIES; i++)
304 for_all_elts(x, runnable[i], {
305 printf("[Priority %d]\n", i);
308 for_all_elts(x, blocked, { Dump_One_Process(x); })
310 printf("***LWP: LWP support not initialized\n");
315 int LWP_GetProcessPriority(pid, priority) /* returns process priority */
319 Debug(0, ("Entered Get_Process_Priority"))
321 *priority = pid->priority;
327 static int Internal_Signal(char *event)
329 int rc = LWP_ENOWAIT;
332 Debug(0, ("Entered Internal_Signal [event id 0x%x]", event))
333 if (!lwp_init) return LWP_EINIT;
334 if (event == NULL) return LWP_EBADEVENT;
335 for_all_elts(temp, blocked, {
336 if (temp->status == WAITING)
337 for (i=0; i < temp->eventcnt; i++) {
338 if (temp -> eventlist[i] == event) {
339 temp -> eventlist[i] = NULL;
341 Debug(0, ("Signal satisfied for PCB 0x%x", temp))
342 if (--temp->waitcnt == 0) {
343 temp -> status = READY;
344 temp -> wakevent = i+1;
345 move(temp, &blocked, &runnable[temp->priority]);
354 /* signal the occurence of an event */
355 int LWP_INTERNALSIGNAL(void *event, int yield)
357 Debug(2, ("Entered LWP_SignalProcess"))
360 rc = Internal_Signal(event);
368 int LWP_TerminateProcessSupport() /* terminate all LWP support */
372 Debug(0, ("Entered Terminate_Process_Support"))
373 if (lwp_init == NULL) return LWP_EINIT;
374 if (lwp_cpptr != LWPANCHOR.outerpid)
375 Abort_LWP("Terminate_Process_Support invoked from wrong process!");
376 /* Is this safe??? @@@ */
377 for (i=0; i<MAX_PRIORITIES; i++)
378 for_all_elts(cur, runnable[i], { Free_PCB(cur); })
379 for_all_elts(cur, blocked, { Free_PCB(cur); })
385 int LWP_MwaitProcess(int wcount, void **evlist)
389 Debug(0, ("Entered Mwait_Process [waitcnt = %d]", wcount))
390 if (evlist == NULL) {
392 return LWP_EBADCOUNT;
395 for (ecount = 0; evlist[ecount] != NULL; ecount++) ;
399 return LWP_EBADCOUNT;
405 if (wcount>ecount || wcount<0) {
407 return LWP_EBADCOUNT;
409 if (ecount > lwp_cpptr->eventlistsize) {
411 void **save_eventlist = lwp_cpptr->eventlist;
412 lwp_cpptr->eventlist = (char **)realloc(lwp_cpptr->eventlist,
413 ecount*sizeof(char *));
414 if (lwp_cpptr->eventlist == NULL) {
415 lwp_cpptr->eventlist = save_eventlist;
419 lwp_cpptr->eventlistsize = ecount;
421 for (i=0; i<ecount; i++) lwp_cpptr -> eventlist[i] = evlist[i];
423 lwp_cpptr->status = WAITING;
425 move(lwp_cpptr, &runnable[lwp_cpptr->priority], &blocked);
428 lwp_cpptr->wakevent = 0;
429 lwp_cpptr->waitcnt = wcount;
430 lwp_cpptr->eventcnt = ecount;
437 int LWP_WaitProcess(void *event) /* wait on a single event */
441 Debug(2, ("Entered Wait_Process"))
442 if (event == NULL) return LWP_EBADEVENT;
445 return LWP_MwaitProcess(1, tempev);
449 /* Internal Support Routines */
450 static void Initialize_PCB(PROCESS pcb, int priority, int stacksize,
451 int (*funP)(), void *argP, char *name)
455 Debug(4, ("Entered Initialize_PCB"))
457 while (((pcb->name[i] = name[i]) != '\0') && (i < 31)) i++;
458 pcb->name[31] = '\0';
459 pcb->priority = priority;
460 pcb->stacksize = stacksize;
464 pcb->eventlist = (void**)malloc(EVINITSIZE*sizeof(void*));
465 pcb->eventlistsize = pcb->eventlist ? EVINITSIZE : 0;
469 pcb->next = pcb->prev = (PROCESS)NULL;
470 pcb->iomgrRequest = (struct IoRequest*)NULL;
471 pcb->index = lwp_nextindex ++;
475 VOID WINAPI Enter_LWP(PVOID fiberData)
477 PROCESS pcb = (PROCESS)fiberData;
479 /* next lines are new..... */
482 Debug(2, ("Enter_LWP: pcb=0x%x, funP=0x%x, argP=0x%x\n", pcb, pcb->funP, pcb->argP))
484 (*pcb->funP)(pcb->argP);
486 LWP_DestroyProcess(pcb);
490 static void Abort_LWP(char *msg)
492 Debug(0, ("Entered Abort_LWP"))
493 printf("***LWP: %s\n",msg);
495 printf("***LWP: Abort --- dumping PCBs ...\n");
502 static void Dump_One_Process(PROCESS pid)
506 printf("***LWP: Process Control Block at 0x%x\n", pid);
507 printf("***LWP: Name: %s\n", pid->name);
508 if (pid->funP != NULL)
509 printf("***LWP: Initial entry point: 0x%x\n", pid->funP);
510 switch (pid->status) {
511 case READY: printf("READY"); break;
512 case WAITING: printf("WAITING"); break;
513 case DESTROYED: printf("DESTROYED"); break;
514 default: printf("unknown");
517 printf("***LWP: Priority: %d \tInitial parameter: 0x%x\n",
518 pid->priority, pid->argP);
519 if (pid->stacksize != 0) {
520 printf("***LWP: Stacksize: %d\n", pid->stacksize);
522 if (pid->eventcnt > 0) {
523 printf("***LWP: Number of events outstanding: %d\n", pid->waitcnt);
524 printf("***LWP: Event id list:");
525 for (i=0;i<pid->eventcnt;i++)
526 printf(" 0x%x", pid->eventlist[i]);
530 printf("***LWP: Number of last wakeup event: %d\n", pid->wakevent);
535 static void purge_dead_pcbs(void)
537 for_all_elts(cur, blocked, { if (cur->status == DESTROYED) Dispose_of_Dead_PCB(cur); })
540 int LWP_TraceProcesses = 0;
542 static void Dispatcher(void)
546 static int dispatch_count = 0;
548 if (LWP_TraceProcesses > 0) {
549 for (i=0; i<MAX_PRIORITIES; i++) {
550 printf("[Priority %d, runnable (%d):", i, runnable[i].count);
551 for_all_elts(p, runnable[i], {
552 printf(" \"%s\"", p->name);
556 printf("[Blocked (%d):", blocked.count);
557 for_all_elts(p, blocked, {
558 printf(" \"%s\"", p->name);
563 /* Move head of current runnable queue forward if current LWP is still
566 if (lwp_cpptr != NULL && lwp_cpptr == runnable[lwp_cpptr->priority].head)
567 runnable[lwp_cpptr->priority].head =
568 runnable[lwp_cpptr->priority].head -> next;
569 /* Find highest priority with runnable processes. */
570 for (i=MAX_PRIORITIES-1; i>=0; i--)
571 if (runnable[i].head != NULL) break;
573 if (i < 0) Abort_LWP("No READY processes");
576 if (LWP_TraceProcesses > 0)
577 printf("Dispatch %d [PCB at 0x%x] \"%s\"\n", ++dispatch_count,
578 runnable[i].head, runnable[i].head->name);
581 lwp_cpptr = runnable[i].head;
582 SwitchToFiber(lwp_cpptr->fiber);
590 static void Exit_LWP(void)
596 static void Delete_PCB(PROCESS pid)
598 Debug(4, ("Entered Delete_PCB"))
599 lwp_remove(pid, (pid->status==WAITING || pid->status==DESTROYED
601 : &runnable[pid->priority]));
602 LWPANCHOR.processcnt--;
605 static void Free_PCB(PROCESS pid)
607 Debug(4, ("Entered Free_PCB"))
608 if (pid->fiber != NULL) {
609 DeleteFiber(pid->fiber);
611 if (pid->eventlist != NULL) free((void*)pid->eventlist);
615 static void Dispose_of_Dead_PCB(PROCESS cur)
617 Debug(4, ("Entered Dispose_of_Dead_PCB"))
622 /* Queue manipulation. */
623 static void lwp_remove(PROCESS p, struct QUEUE *q)
625 /* Special test for only element on queue */
629 /* Not only element, do normal remove */
630 p -> next -> prev = p -> prev;
631 p -> prev -> next = p -> next;
633 /* See if head pointing to this element */
634 if (q->head == p) q -> head = p -> next;
636 p -> next = p -> prev = NULL;
639 static void insert(PROCESS p, struct QUEUE *q)
641 if (q->head == NULL) { /* Queue is empty */
643 p -> next = p -> prev = p;
644 } else { /* Regular insert */
645 p -> prev = q -> head -> prev;
646 q -> head -> prev -> next = p;
647 q -> head -> prev = p;
648 p -> next = q -> head;
653 static void move(PROCESS p, struct QUEUE *from, struct QUEUE *to)
658 #endif /* AFS_NT40_ENV */