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 <afsconfig.h>
20 #include <afs/param.h>
26 #include <afs/afsutil.h>
30 static struct lwp_ctl *lwp_init;
31 #define LWPANCHOR (*lwp_init)
44 #define Debug(level, msg)\
45 if (lwp_debug && lwp_debug >= level) {\
46 printf("***LWP (0x%p): ", lwp_cpptr);\
52 #define Debug(level, msg)
56 /* Forward declarations */
57 static void Dispatcher(void);
58 static void Exit_LWP(void);
59 static void Abort_LWP(char *msg);
60 static VOID WINAPI Enter_LWP(PVOID fiberData);
61 static void Initialize_PCB(PROCESS pcb, int priority, int stacksize,
62 int (*funP)(), void *argP, char *name);
64 static void Dispose_of_Dead_PCB();
65 static void Free_PCB();
66 static int Internal_Signal();
67 static void purge_dead_pcbs(void);
69 static void lwp_remove(PROCESS p, struct QUEUE *q);
70 static void insert(PROCESS p, struct QUEUE *q);
71 static void move(PROCESS p, struct QUEUE *from, struct QUEUE *to);
73 /* biggest LWP stack created so far - used by IOMGR */
74 int lwp_MaxStackSeen = 0;
77 static void Dump_One_Process(PROCESS pid);
78 static void Dump_Processes(void);
83 #define MAX_PRIORITIES (LWP_MAX_PRIORITY+1)
85 /* Invariant for runnable queues: The head of each queue points to the
86 * currently running process if it is in that queue, or it points to the
87 * next process in that queue that should run.
92 } runnable[MAX_PRIORITIES], blocked;
95 #define for_all_elts(var, q, body)\
99 for (_I_=q.count, var = q.head; _I_>0; _I_--, var=_NEXT_) {\
100 _NEXT_ = var -> next;\
107 LPVOID ConvertThreadToFiber(PROCESS x)
111 LPVOID CreateFiber(DWORD x ,LPVOID y,PROCESS z)
116 VOID SwitchToFiber(LPVOID p)
120 VOID DeleteFiber(LPVOID p)
126 int lwp_MinStackSize = 0;
128 /* LWP_InitializeProcessSupport - setup base support for fibers.
131 * priority - priority of main thread.
134 * pid - return this process
136 * LWP_SUCCESS (else aborts)
140 int LWP_InitializeProcessSupport(int priority, PROCESS *pid)
146 Debug(0, ("Entered LWP_InitializeProcessSupport"))
147 if (lwp_init != NULL) return LWP_SUCCESS;
149 if (priority >= MAX_PRIORITIES) return LWP_EBADPRI;
151 pcb = (PROCESS)malloc(sizeof(*pcb));
153 Abort_LWP("Insufficient Storage to Initialize LWP PCB");
154 (void) memset((void*)pcb, 0, sizeof(*pcb));
155 pcb->fiber = ConvertThreadToFiber(pcb);
157 Abort_LWP("Cannot convert main thread to LWP fiber");
159 lwp_init = (struct lwp_ctl *) malloc(sizeof(struct lwp_ctl));
160 if (lwp_init == NULL)
161 Abort_LWP("Insufficient Storage to Initialize LWP CTL");
162 (void) memset((void*)lwp_init, 0, sizeof(struct lwp_ctl));
164 for (i=0; i<MAX_PRIORITIES; i++) {
165 runnable[i].head = NULL;
166 runnable[i].count = 0;
171 LWPANCHOR.processcnt = 1;
172 LWPANCHOR.outerpid = pcb;
173 LWPANCHOR.outersp = NULL;
176 Initialize_PCB(pcb, priority, 0, NULL, NULL,
177 "Main Process [created by LWP]");
180 Debug(10, ("Init: Insert 0x%p into runnable at priority %d\n", pcb, priority))
181 insert(pcb, &runnable[priority]);
183 if ( ( value = getenv("AFS_LWP_STACK_SIZE")) == NULL )
184 lwp_MinStackSize = AFS_LWP_MINSTACKSIZE;
186 lwp_MinStackSize = (AFS_LWP_MINSTACKSIZE>atoi(value)?
187 AFS_LWP_MINSTACKSIZE : atoi(value));
194 /* LWP_CreateProcess - create a new fiber and start executing it.
197 * funP - start function
198 * stacksize - size of
199 * priority - LWP priority
200 * argP - initial parameter for start function
204 * pid - handle of created LWP
207 * LWP_EINIT - error in intialization.
210 int LWP_CreateProcess(int (*funP)(), int stacksize, int priority, void *argP,
211 char *name, PROCESS *pid)
217 pcb = (PROCESS)malloc(sizeof(*pcb));
220 (void) memset((void*)pcb, 0, sizeof(*pcb));
223 * on some systems (e.g. hpux), a minimum usable stack size has
226 if (stacksize < lwp_MinStackSize) {
227 stacksize = lwp_MinStackSize;
229 /* more stack size computations; keep track of for IOMGR */
230 if (lwp_MaxStackSeen < stacksize)
231 lwp_MaxStackSeen = stacksize;
233 pcb->fiber = CreateFiber(stacksize, Enter_LWP, pcb);
234 if (pcb->fiber == NULL) {
238 Debug(0, ("Create: pcb=0x%p, funP=0x%p, argP=0x%p\n", pcb, funP, argP))
239 /* Fiber is now created, so fill in PCB */
240 Initialize_PCB(pcb, priority, stacksize, funP, argP, name);
241 Debug(10, ("Create: Insert 0x%p into runnable at priority %d\n", pcb, priority))
242 insert(pcb, &runnable[priority]);
244 LWPANCHOR.processcnt++;
246 /* And hand off execution. */
247 SwitchToFiber(pcb->fiber);
254 int LWP_DestroyProcess(PROCESS pid)
256 Debug(0, ("Entered Destroy_Process"))
261 if (lwp_cpptr != pid) {
262 Dispose_of_Dead_PCB(pid);
264 pid->status = DESTROYED;
265 move(pid, &runnable[pid->priority], &blocked);
276 (tp=lwp_cpptr) -> status = QWAITING;
277 lwp_remove(tp, &runnable[tp->priority]);
282 int LWP_QSignal(PROCESS pid)
284 if (pid->status == QWAITING) {
286 Debug(10, ("QSignal: Insert 0x%p into runnable at priority %d\n", pid, pid->priority))
287 insert(pid, &runnable[pid->priority]);
290 else return LWP_ENOWAIT;
293 int LWP_CurrentProcess(PROCESS *pid)
295 Debug(0, ("Entered Current_Process"))
303 PROCESS LWP_ThreadId()
305 Debug(0, ("Entered ThreadId"))
312 int LWP_DispatchProcess(void) /* explicit voluntary preemption */
314 Debug(2, ("Entered Dispatch_Process"))
324 static void Dump_Processes(void)
328 for (i=0; i<MAX_PRIORITIES; i++)
329 for_all_elts(x, runnable[i], {
330 printf("[Priority %d]\n", i);
333 for_all_elts(x, blocked, { Dump_One_Process(x); })
335 printf("***LWP: LWP support not initialized\n");
340 int LWP_GetProcessPriority(pid, priority) /* returns process priority */
344 Debug(0, ("Entered Get_Process_Priority"))
346 *priority = pid->priority;
352 static int Internal_Signal(char *event)
354 int rc = LWP_ENOWAIT;
357 Debug(0, ("Entered Internal_Signal [event id 0x%p]", event))
358 if (!lwp_init) return LWP_EINIT;
359 if (event == NULL) return LWP_EBADEVENT;
360 for_all_elts(temp, blocked, {
361 if (temp->status == WAITING)
362 for (i=0; i < temp->eventcnt; i++) {
363 if (temp -> eventlist[i] == event) {
364 temp -> eventlist[i] = NULL;
366 Debug(0, ("Signal satisfied for PCB 0x%p", temp))
367 if (--temp->waitcnt == 0) {
368 temp -> status = READY;
369 temp -> wakevent = i+1;
370 move(temp, &blocked, &runnable[temp->priority]);
379 /* signal the occurence of an event */
380 int LWP_INTERNALSIGNAL(void *event, int yield)
382 Debug(2, ("Entered LWP_SignalProcess"))
385 rc = Internal_Signal(event);
393 int LWP_TerminateProcessSupport() /* terminate all LWP support */
397 Debug(0, ("Entered Terminate_Process_Support"))
398 if (lwp_init == NULL) return LWP_EINIT;
399 if (lwp_cpptr != LWPANCHOR.outerpid)
400 Abort_LWP("Terminate_Process_Support invoked from wrong process!");
401 /* Is this safe??? @@@ */
402 for (i=0; i<MAX_PRIORITIES; i++)
403 for_all_elts(cur, runnable[i], { Free_PCB(cur); })
404 for_all_elts(cur, blocked, { Free_PCB(cur); })
410 int LWP_MwaitProcess(int wcount, void **evlist)
414 Debug(0, ("Entered Mwait_Process [waitcnt = %d]", wcount))
415 if (evlist == NULL) {
417 return LWP_EBADCOUNT;
420 for (ecount = 0; evlist[ecount] != NULL; ecount++) ;
424 return LWP_EBADCOUNT;
430 if (wcount>ecount || wcount<0) {
432 return LWP_EBADCOUNT;
434 if (ecount > lwp_cpptr->eventlistsize) {
436 void **save_eventlist = lwp_cpptr->eventlist;
437 lwp_cpptr->eventlist = (char **)realloc(lwp_cpptr->eventlist,
438 ecount*sizeof(char *));
439 if (lwp_cpptr->eventlist == NULL) {
440 lwp_cpptr->eventlist = save_eventlist;
444 lwp_cpptr->eventlistsize = ecount;
446 for (i=0; i<ecount; i++) lwp_cpptr -> eventlist[i] = evlist[i];
448 lwp_cpptr->status = WAITING;
450 move(lwp_cpptr, &runnable[lwp_cpptr->priority], &blocked);
453 lwp_cpptr->wakevent = 0;
454 lwp_cpptr->waitcnt = wcount;
455 lwp_cpptr->eventcnt = ecount;
462 int LWP_WaitProcess(void *event) /* wait on a single event */
466 Debug(2, ("Entered Wait_Process"))
467 if (event == NULL) return LWP_EBADEVENT;
470 return LWP_MwaitProcess(1, tempev);
474 /* Internal Support Routines */
475 static void Initialize_PCB(PROCESS pcb, int priority, int stacksize,
476 int (*funP)(), void *argP, char *name)
480 Debug(4, ("Entered Initialize_PCB"))
482 while (((pcb->name[i] = name[i]) != '\0') && (i < 31)) i++;
483 pcb->name[31] = '\0';
484 pcb->priority = priority;
485 pcb->stacksize = stacksize;
489 pcb->eventlist = (void**)malloc(EVINITSIZE*sizeof(void*));
490 pcb->eventlistsize = pcb->eventlist ? EVINITSIZE : 0;
494 pcb->next = pcb->prev = (PROCESS)NULL;
495 pcb->iomgrRequest = (struct IoRequest*)NULL;
496 pcb->index = lwp_nextindex ++;
500 static VOID WINAPI Enter_LWP(PVOID fiberData)
502 PROCESS pcb = (PROCESS)fiberData;
504 /* next lines are new..... */
507 Debug(2, ("Enter_LWP: pcb=0x%p, funP=0x%p, argP=0x%p\n", pcb, pcb->funP, pcb->argP))
509 (*pcb->funP)(pcb->argP);
511 LWP_DestroyProcess(pcb);
515 static void Abort_LWP(char *msg)
517 Debug(0, ("Entered Abort_LWP"))
518 printf("***LWP: %s\n",msg);
520 printf("***LWP: Abort --- dumping PCBs ...\n");
527 static void Dump_One_Process(PROCESS pid)
531 printf("***LWP: Process Control Block at 0x%p\n", pid);
532 printf("***LWP: Name: %s\n", pid->name);
533 if (pid->funP != NULL)
534 printf("***LWP: Initial entry point: 0x%p\n", pid->funP);
535 switch (pid->status) {
536 case READY: printf("READY"); break;
537 case WAITING: printf("WAITING"); break;
538 case DESTROYED: printf("DESTROYED"); break;
539 default: printf("unknown");
542 printf("***LWP: Priority: %d \tInitial parameter: 0x%p\n",
543 pid->priority, pid->argP);
544 if (pid->stacksize != 0) {
545 printf("***LWP: Stacksize: %d\n", pid->stacksize);
547 if (pid->eventcnt > 0) {
548 printf("***LWP: Number of events outstanding: %d\n", pid->waitcnt);
549 printf("***LWP: Event id list:");
550 for (i=0;i<pid->eventcnt;i++)
551 printf(" 0x%p", pid->eventlist[i]);
555 printf("***LWP: Number of last wakeup event: %d\n", pid->wakevent);
560 static void purge_dead_pcbs(void)
562 for_all_elts(cur, blocked, { if (cur->status == DESTROYED) Dispose_of_Dead_PCB(cur); })
565 int LWP_TraceProcesses = 0;
567 static void Dispatcher(void)
571 static int dispatch_count = 0;
573 if (LWP_TraceProcesses > 0) {
574 for (i=0; i<MAX_PRIORITIES; i++) {
575 printf("[Priority %d, runnable (%d):", i, runnable[i].count);
576 for_all_elts(p, runnable[i], {
577 printf(" \"%s\"", p->name);
581 printf("[Blocked (%d):", blocked.count);
582 for_all_elts(p, blocked, {
583 printf(" \"%s\"", p->name);
588 /* Move head of current runnable queue forward if current LWP is still
591 if (lwp_cpptr != NULL && lwp_cpptr == runnable[lwp_cpptr->priority].head)
592 runnable[lwp_cpptr->priority].head =
593 runnable[lwp_cpptr->priority].head -> next;
594 /* Find highest priority with runnable processes. */
595 for (i=MAX_PRIORITIES-1; i>=0; i--)
596 if (runnable[i].head != NULL) break;
598 if (i < 0) Abort_LWP("No READY processes");
601 if (LWP_TraceProcesses > 0)
602 printf("Dispatch %d [PCB at 0x%p] \"%s\"\n", ++dispatch_count,
603 runnable[i].head, runnable[i].head->name);
606 lwp_cpptr = runnable[i].head;
607 SwitchToFiber(lwp_cpptr->fiber);
615 static void Exit_LWP(void)
621 static void Delete_PCB(PROCESS pid)
623 Debug(4, ("Entered Delete_PCB"))
624 lwp_remove(pid, (pid->status==WAITING || pid->status==DESTROYED
626 : &runnable[pid->priority]));
627 LWPANCHOR.processcnt--;
631 static void Free_PCB(PROCESS pid)
633 Debug(4, ("Entered Free_PCB"))
634 if (pid->fiber != NULL) {
635 DeleteFiber(pid->fiber);
637 if (pid->eventlist != NULL) free((void*)pid->eventlist);
641 static void Dispose_of_Dead_PCB(PROCESS cur)
643 Debug(4, ("Entered Dispose_of_Dead_PCB"))
648 /* Queue manipulation. */
649 static void lwp_remove(PROCESS p, struct QUEUE *q)
651 /* Special test for only element on queue */
655 /* Not only element, do normal remove */
656 p -> next -> prev = p -> prev;
657 p -> prev -> next = p -> next;
659 /* See if head pointing to this element */
660 if (q->head == p) q -> head = p -> next;
662 p -> next = p -> prev = NULL;
665 static void insert(PROCESS p, struct QUEUE *q)
667 if (q->head == NULL) { /* Queue is empty */
669 p -> next = p -> prev = p;
670 } else { /* Regular insert */
671 p -> prev = q -> head -> prev;
672 q -> head -> prev -> next = p;
673 q -> head -> prev = p;
674 p -> next = q -> head;
679 static void move(PROCESS p, struct QUEUE *from, struct QUEUE *to)
684 #endif /* AFS_NT40_ENV */