1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
|
/* BLURB lgpl
Coda File System
Release 5
Copyright (c) 1987-1999 Carnegie Mellon University
Additional copyrights listed below
This code is distributed "AS IS" without warranty of any kind under
the terms of the GNU Library General Public Licence Version 2, as
shown in the file LICENSE. The technical and financial contributors to
Coda are listed in the file CREDITS.
Additional copyrights
#*/
/*
IBM COPYRIGHT NOTICE
Copyright (C) 1986
International Business Machines Corporation
All Rights Reserved
This file contains some code identical to or derived from the 1986
version of the Andrew File System ("AFS"), which is owned by the IBM
Corporation. This code is provided "AS IS" and IBM does not warrant
that it is free of infringement of any intellectual rights of any
third party. IBM disclaims liability of any kind for any damages
whatsoever resulting directly or indirectly from use of this software
or of any derivative work. Carnegie Mellon University has obtained
permission to modify, distribute and sublicense this code, which is
based on Version 2 of AFS and does not contain the features and
enhancements that are part of Version 3 of AFS. Version 3 of AFS is
commercially available and supported by Transarc Corporation,
Pittsburgh, PA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <assert.h>
/* for netbsd 1.3 and libc5 */
#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif
#include <lwp/lwp.h>
#include "lwp.private.h"
#include "lwp_ucontext.h"
#include "lwp_stacktrace.h"
#define ON 1
#define OFF 0
#define READY 2
#define WAITING 3
#define MINSTACK 32768 /* allocate at least 32KB for stacks */
#define STACKPAD 4096 /* pad any requested stack size to PAGE_SIZE */
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#define LWPANCHOR (*lwp_init)
#define MAX_PRIORITIES (LWP_MAX_PRIORITY+1)
/* simple test, we normally want to mmap our stacks whenever possible, except
* when we compile venus for Win95 */
#undef MMAP_LWP_STACKS
#ifdef HAVE_MMAP
#define MMAP_LWP_STACKS 1
#if defined(__NetBSD__) || defined(__FreeBSD__)
char *lwp_stackbase = (char *)0x45000000;
#else
char *lwp_stackbase = (char *)0x15000000;
#endif
#endif /* MMAP_LWP_STACKS */
struct QUEUE {
PROCESS head;
int count;
} runnable[MAX_PRIORITIES], blocked;
static ucontext_t reaper; /* reaper context, see comments for lwp_Reaper() */
static ucontext_t tracer; /* context for the for stack tracing thread */
#define REAPER_STACKSIZE 32768
#define TRACER_STACKSIZE 32768
/* 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. */
/* internal procedure declarations */
static void lwpremove(PROCESS p, struct QUEUE *q);
static void lwpinsert(PROCESS p, struct QUEUE *q);
static void lwpmove(PROCESS p, struct QUEUE *from, struct QUEUE *to);
static void Initialize_PCB (PROCESS temp, int priority, char *stack, int stacksize, void (*func)(void *), void *arg, char *name);
static void Abort_LWP(char *msg);
static void Exit_LWP();
static void Free_PCB(PROCESS pid);
static void Overflow_Complain (void);
static void Initialize_Stack (void *stackptr, int stacksize);
static int Stack_Used (stack_t *stack);
/*----------------------------------------*/
/* Globals identical in OLD and NEW lwps */
/*----------------------------------------*/
FILE *lwp_logfile = NULL;
int lwp_debug = 0;
int LWP_TraceProcesses = 0;
PROCESS lwp_cpptr;
int lwp_nextindex; /* Next lwp index to assign */
static struct lwp_ctl *lwp_init = NULL;
int Cont_Sws;
struct timeval last_context_switch; /* used to find out how long a lwp was running */
struct timeval cont_sw_threshold; /* how long a lwp is allowed to run */
PROCESS cont_sw_id; /* id of thread setting the last_context_switch time */
struct timeval run_wait_threshold;
/*-----------------------------------------*/
/* Globals that differ in OLD and NEW lwps */
/*-----------------------------------------*/
int lwp_overflowAction = LWP_SOABORT; /* Stack checking action */
int lwp_stackUseEnabled = 1; /* Controls stack size counting */
/* fixing up some missing funtions */
#ifndef timercmp
#define timercmp(tvp, uvp, cmp) \
((tvp)->tv_sec cmp (uvp)->tv_sec || \
(tvp)->tv_sec == (uvp)->tv_sec && (tvp)->tv_usec cmp (uvp)->tv_usec)
#endif
#ifndef timerisset
#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
#endif
#ifndef timerclear
#define timerclear(tvp) do {(tvp)->tv_sec=0; (tvp)->tv_usec=0; } while(0);
#endif
/* Iterator macro */
#define for_all_elts(var, q, body)\
{\
PROCESS var, _NEXT_;\
int _I_;\
for (_I_=q.count, var = q.head; _I_>0; _I_--, var=_NEXT_) {\
_NEXT_ = var -> next;\
body\
}\
}
/* removes PROCESS p from a QUEUE pointed at by q */
static void lwpremove(PROCESS p, struct QUEUE *q)
{
/* Special test for only element on queue */
if (q->count == 1)
q -> head = NULL;
else {
/* Not only element, do normal remove */
p -> next -> prev = p -> prev;
p -> prev -> next = p -> next;
}
/* See if head pointing to this element */
if (q->head == p) q -> head = p -> next;
q->count--;
p -> next = p -> prev = NULL;
}
static void lwpinsert(PROCESS p, struct QUEUE *q)
{
if (q->head == NULL) { /* Queue is empty */
q -> head = p;
p -> next = p -> prev = p;
} else { /* Regular insert */
p -> prev = q -> head -> prev;
q -> head -> prev -> next = p;
q -> head -> prev = p;
p -> next = q -> head;
}
q->count++;
}
/* Moves a PROCESS p from QUEUE "from" to QUEUE "to" */
static void lwpmove(PROCESS p, struct QUEUE *from, struct QUEUE *to)
{
lwpremove(p, from);
lwpinsert(p, to);
}
int LWP_TerminateProcessSupport(void) /* terminate all LWP support */
{
int i;
lwpdebug(0, "Entered Terminate_Process_Support");
if (!lwp_init)
return LWP_EINIT;
if (lwp_cpptr != LWPANCHOR.outerpid)
/* terminate support not called from same process as the init process */
Abort_LWP("Terminate_Process_Support invoked from wrong process!");
/* free all space allocated */
for (i=0; i<MAX_PRIORITIES; i++)
for_all_elts(cur, runnable[i], { Free_PCB(cur);})
for_all_elts(cur, blocked, { Free_PCB(cur);})
free((char *)lwp_init);
lwp_init = NULL;
return LWP_SUCCESS;
}
int LWP_GetRock(int Tag, char **Value)
{
/* Obtains the pointer Value associated with the rock Tag of this LWP.
Returns:
LWP_SUCCESS if specified rock exists and Value has been filled
LWP_EBADROCK rock specified does not exist
*/
int i;
struct rock *ra;
ra = lwp_cpptr->rlist;
for (i = 0; i < lwp_cpptr->rused; i++)
if (ra[i].tag == Tag) {
*Value = ra[i].value;
return(LWP_SUCCESS);
}
return(LWP_EBADROCK);
}
int LWP_NewRock(int Tag, char *Value)
{
/* Finds a free rock and sets its value to Value.
Return codes:
LWP_SUCCESS Rock did not exist and a new one was used
LWP_EBADROCK Rock already exists.
LWP_ENOROCKS All rocks are in use.
From the above semantics, you can only set a rock value once.
This is specifically to prevent multiple users of the LWP
package from accidentally using the same Tag value and
clobbering others. You can always use one level of
indirection to obtain a rock whose contents can change. */
int i;
struct rock *ra; /* rock array */
ra = lwp_cpptr->rlist;
/* check if rock has been used before */
for (i = 0; i < lwp_cpptr->rused; i++)
if (ra[i].tag == Tag) return(LWP_EBADROCK);
/* insert new rock in rock list and increment count of rocks */
if (lwp_cpptr->rused < MAXROCKS) {
ra[lwp_cpptr->rused].tag = Tag;
ra[lwp_cpptr->rused].value = Value;
lwp_cpptr->rused++;
return(LWP_SUCCESS);
}
else return(LWP_ENOROCKS);
}
int LWP_CurrentProcess(PROCESS *pid)
{
lwpdebug(0, "Entered LWP_CurrentProcess");
*pid = lwp_cpptr;
return lwp_init ? LWP_SUCCESS : LWP_EINIT;
}
PROCESS LWP_ThisProcess()
{
lwpdebug(0, "Entered LWP_ThisProcess");
return lwp_init ? lwp_cpptr : NULL;
}
void LWP_SetLog(FILE *file, int level)
{
lwp_logfile = file;
lwp_debug = level;
}
int LWP_GetProcessPriority(PROCESS pid, int *priority)
{
lwpdebug(0, "Entered Get_Process_Priority");
if (!lwp_init)
return LWP_EINIT;
*priority = pid -> priority;
return 0;
}
int LWP_WaitProcess(void *event)
{
void *tempev[2];
lwpdebug(0, "Entered Wait_Process");
if (event == NULL) return LWP_EBADEVENT;
tempev[0] = event;
tempev[1] = NULL;
return LWP_MwaitProcess(1, (char **)tempev);
}
static void Exit_LWP()
{
exit (-1);
}
char *LWP_Name()
{
return(lwp_cpptr->name);
}
int LWP_Index()
{
return(lwp_cpptr->index);
}
int LWP_HighestIndex()
{
return(lwp_nextindex-1);
}
static void CheckWorkTime(PROCESS currentThread, PROCESS nextThread)
{
struct timeval current;
struct timeval worktime;
if (!cont_sw_threshold.tv_sec && !cont_sw_threshold.tv_usec) return;
if (last_context_switch.tv_sec && cont_sw_id == currentThread)
{
gettimeofday(¤t, NULL);
worktime.tv_sec = current.tv_sec;
worktime.tv_usec = current.tv_usec;
if (worktime.tv_usec < last_context_switch.tv_usec) {
worktime.tv_usec += 1000000;
worktime.tv_sec -= 1;
}
worktime.tv_sec -= last_context_switch.tv_sec;
worktime.tv_usec -= last_context_switch.tv_usec;
if (timercmp(&worktime, &cont_sw_threshold, >)) {
struct tm *lt = localtime((const time_t *)¤t.tv_sec);
fprintf(stderr, "[ %02d:%02d:%02d ] ***LWP %s(%p) took too much cpu %d secs %6d usecs\n",
lt->tm_hour, lt->tm_min, lt->tm_sec,
currentThread->name, currentThread, (int)worktime.tv_sec, (int)worktime.tv_usec);
fflush(stderr);
}
last_context_switch.tv_sec = current.tv_sec;
last_context_switch.tv_usec = current.tv_usec;
}
else gettimeofday(&last_context_switch, NULL);
cont_sw_id = nextThread;
}
static void CheckRunWaitTime(PROCESS thread)
{
struct timeval current;
struct timeval waittime;
if (!timerisset(&run_wait_threshold)) return;
/* timer can be null during process creation. */
if (!timerisset(&thread->lastReady)) return;
gettimeofday(¤t, NULL);
waittime.tv_sec = current.tv_sec;
waittime.tv_usec = current.tv_usec;
if (waittime.tv_usec < thread->lastReady.tv_usec) {
waittime.tv_usec += 1000000;
waittime.tv_sec -= 1;
}
waittime.tv_sec -= thread->lastReady.tv_sec;
waittime.tv_usec -= thread->lastReady.tv_usec;
if (timercmp(&waittime, &run_wait_threshold, >)) {
struct tm *lt = localtime((const time_t *)¤t.tv_sec);
fprintf(stderr, "[ %02d:%02d:%02d ] ***LWP %s(%p) run-wait too long %d secs %6d usecs\n",
lt->tm_hour, lt->tm_min, lt->tm_sec,
thread->name, thread, (int)waittime.tv_sec, (int)waittime.tv_usec);
fflush(stderr);
}
timerclear(&thread->lastReady);
}
/*------------------------------------------*/
/* Routines that differ in OLD and NEW lwps */
/*------------------------------------------*/
/* A process calls this routine to wait until somebody signals it.
* LWP_QWait removes the calling process from the runnable queue
* and makes the process sleep until some other process signals via LWP_QSignal.
*/
int LWP_QWait()
{
if (--lwp_cpptr->qpending >= 0)
return LWP_SUCCESS;
lwp_cpptr->status = WAITING;
lwpmove(lwp_cpptr, &runnable[lwp_cpptr->priority], &blocked);
timerclear(&lwp_cpptr->lastReady);
LWP_DispatchProcess();
return LWP_SUCCESS;
}
/* signal the PROCESS pid - by adding it to the runnable queue */
int LWP_QSignal(PROCESS pid)
{
if (++pid->qpending != 0)
return LWP_ENOWAIT;
lwpdebug(0, "LWP_Qsignal: %s is going to QSignal %s\n",
lwp_cpptr->name, pid->name);
pid->status = READY;
lwpmove(pid, &blocked, &runnable[pid->priority]);
lwpdebug(0, "LWP_QSignal: Just inserted %s into runnable queue\n", pid->name);
gettimeofday(&pid->lastReady, NULL);
return LWP_SUCCESS;
}
int LWP_CreateProcess(void (*ep)(void *), int stacksize, int priority,
void *parm, char *name, PROCESS *pid)
{
PROCESS temp;
char *stackptr;
#ifdef MMAP_LWP_STACKS
int pagesize;
#endif
lwpdebug(0, "Entered LWP_CreateProcess");
if (!lwp_init)
return LWP_EINIT;
temp = (PROCESS) malloc (sizeof (struct lwp_pcb));
if (!temp)
return LWP_ENOMEM;
if (stacksize < MINSTACK)
stacksize = MINSTACK;
else
stacksize = (stacksize + (STACKPAD-1)) & ~(STACKPAD-1);
#ifndef MMAP_LWP_STACKS
stackptr = (char *) malloc(stacksize);
#else
#ifdef MAP_ANON
stackptr = mmap(lwp_stackbase, stacksize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
#else
{
int fd;
stackptr = MAP_FAILED;
if ((fd = open("/dev/zero", O_RDWR)) != -1) {
stackptr = mmap(lwp_stackbase, stacksize,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | (lwp_stackbase ? MAP_FIXED : 0),
fd, 0);
(void)close(fd);
}
}
#endif
if ( stackptr == MAP_FAILED ) {
perror("stack: ");
assert(0);
}
pagesize = getpagesize();
lwp_stackbase += ((stacksize/pagesize) + 2) * pagesize;
#endif
if (!stackptr)
return LWP_ENOMEM;
if (priority < 0 || priority >= MAX_PRIORITIES)
return LWP_EBADPRI;
Initialize_Stack(stackptr, stacksize);
Initialize_PCB(temp, priority, stackptr, stacksize, ep, parm, name);
lwpinsert(temp, &runnable[priority]);
gettimeofday(&temp->lastReady, NULL);
LWP_DispatchProcess();
*pid = temp;
return 0;
}
/* The reaper is used to kill the current thread and schedule another
* runnable thread. This context is called whenever a thread exits and by
* LWP_DestroyProcess if the current process is destroyed. */
static void lwp_Reaper(void)
{
/* On FreeBSD we cannot enter a second time into a context that was
* created by makecontext. So we save the current state here to get
* a safe re-entry point. */
getcontext(&reaper);
Free_PCB(lwp_cpptr);
lwp_cpptr = NULL;
LWP_DispatchProcess();
/* we never get here */
}
static void Dump_One_Process(PROCESS pid, FILE *fp)
{
stack_t *stack = &pid->stack;
int i;
fprintf(fp,"***LWP: process %s (%p)\n", pid->name, pid);
if (pid->ep)
fprintf(fp,"\tentry point: %p, parameter %p\n", pid->ep, pid->parm);
fprintf(fp,"\tpriority: %d, status: ", pid->priority);
switch (pid->status) {
case READY: fprintf(fp, "READY"); break;
case WAITING: fprintf(fp, "WAITING"); break;
default: fprintf(fp, "unknown");
}
fprintf(fp, "\n");
if (pid->eventcnt) {
fprintf(fp, "\tnumber of events outstanding: %d\n", pid->waitcnt);
fprintf(fp, "\tevent list:");
for (i = 0; i < pid->eventcnt; i++)
fprintf(fp, " %p", pid->eventlist[i]);
fprintf(fp, "\n");
}
if (pid->wakevent)
fprintf(fp, "\tlast wakeup event: %d\n", pid->wakevent);
if (stack->ss_sp) {
fprintf(fp, "\tstack base: %p, size: %d\n",
stack->ss_sp, stack->ss_size);
fprintf(fp, "\tstack usage high water mark: %d\n", Stack_Used(stack));
fprintf(fp, "\tstack pointer at last yield: %p\n", pid->topstack);
}
lwp_stacktrace(fp, pid->topstack, stack);
fprintf(fp,"==========================================\n");
}
static void lwp_Tracer(void)
{
int i;
/* See comment in lwp_Reaper() */
getcontext(&tracer);
for (i=0; i<MAX_PRIORITIES; i++)
for_all_elts(x, runnable[i], {
fprintf(lwp_logfile, "[Priority %d]\n", i);
Dump_One_Process(x, lwp_logfile);
fflush(lwp_logfile);
})
for_all_elts(x, blocked, {
fprintf(lwp_logfile, "[Blocked]\n");
Dump_One_Process(x, lwp_logfile);
fflush(lwp_logfile);
})
fprintf(lwp_logfile, "Trace done\n");
/* jump back to the thread that called us */
setcontext(&lwp_cpptr->ctx);
}
static void init_contexts(void)
{
getcontext(&reaper);
reaper.uc_stack.ss_sp = malloc(REAPER_STACKSIZE);
reaper.uc_stack.ss_size = REAPER_STACKSIZE;
makecontext(&reaper, lwp_Reaper, 0);
getcontext(&tracer);
tracer.uc_stack.ss_sp = malloc(TRACER_STACKSIZE);
tracer.uc_stack.ss_size = TRACER_STACKSIZE;
makecontext(&tracer, lwp_Tracer, 0);
}
int LWP_DestroyProcess(PROCESS pid)
{
lwpdebug(0, "Entered Destroy_Process");
if (!lwp_init)
return LWP_EINIT;
if (lwp_cpptr == pid)
swapcontext(&lwp_cpptr->ctx, &reaper);
Free_PCB(pid);
return LWP_SUCCESS;
}
static void lwp_Print_Processes(int dummy)
{
if (!lwp_init) {
fprintf(lwp_logfile, "***LWP: LWP support not initialized\n");
return;
}
lwp_cpptr->topstack = &dummy;
/* switch to the context that dumps the process stacks */
swapcontext(&lwp_cpptr->ctx, &tracer);
}
void LWP_Print_Processes(void)
{
lwp_Print_Processes(0);
}
/* Used to be externally visible as LWP_InitializeProcessSupport() */
static int InitializeProcessSupport(int priority, PROCESS *pid)
{
PROCESS temp;
int i;
lwpdebug(0, "Entered InitializeProcessSupport");
if (lwp_init)
return LWP_SUCCESS;
init_contexts();
timerclear(&last_context_switch);
cont_sw_id = NULL;
if (priority >= MAX_PRIORITIES)
return LWP_EBADPRI;
for (i=0; i<MAX_PRIORITIES; i++) {
runnable[i].head = NULL;
runnable[i].count = 0;
}
blocked.head = NULL;
blocked.count = 0;
lwp_init = (struct lwp_ctl *) malloc(sizeof(struct lwp_ctl));
temp = (PROCESS) malloc(sizeof(struct lwp_pcb));
if (lwp_init == NULL || temp == NULL)
Abort_LWP("Insufficient Storage to Initialize LWP Support");
LWPANCHOR.processcnt = 1;
LWPANCHOR.outerpid = temp;
LWPANCHOR.outersp = NULL;
Initialize_PCB(temp, priority, NULL, 0, NULL, NULL, "Main Process");
lwpinsert(temp, &runnable[priority]);
lwp_cpptr = temp;
gettimeofday(&temp->lastReady, NULL);
LWP_DispatchProcess();
LWPANCHOR.outersp = temp->topstack;
if (pid) *pid = temp;
return LWP_SUCCESS;
}
/* New initialization procedure; checks header and library versions
Use this instead of LWP_InitializeProcessSupport() First argument
should always be LWP_VERSION. */
int LWP_Init(int version, int priority, PROCESS *pid)
{
lwp_logfile = stderr;
if (version != LWP_VERSION) {
fprintf(stderr, "**** FATAL ERROR: LWP VERSION MISMATCH ****\n");
exit(-1);
}
return(InitializeProcessSupport(priority, pid));
}
/* wait on m of n events */
int LWP_MwaitProcess(int wcount, char **evlist)
{
int ecount, i;
lwpdebug(0, "Entered Mwait_Process [waitcnt = %d]", wcount);
if (evlist == NULL) {
return LWP_EBADCOUNT;
}
for (ecount = 0; evlist[ecount] != NULL; ecount++) ;
if (ecount == 0) {
return LWP_EBADCOUNT;
}
if (!lwp_init)
return LWP_EINIT;
if (wcount>ecount || wcount<0) {
return LWP_EBADCOUNT;
}
if (ecount > lwp_cpptr->eventlistsize) {
lwp_cpptr->eventlist =
(char **)realloc((char *)lwp_cpptr->eventlist,
ecount*sizeof(char *));
lwp_cpptr->eventlistsize = ecount;
}
for (i=0; i<ecount; i++)
lwp_cpptr -> eventlist[i] = evlist[i];
if (wcount > 0) {
lwp_cpptr -> status = WAITING;
lwpmove(lwp_cpptr, &runnable[lwp_cpptr->priority], &blocked);
timerclear(&lwp_cpptr->lastReady);
}
lwp_cpptr -> wakevent = 0;
lwp_cpptr -> waitcnt = wcount;
lwp_cpptr -> eventcnt = ecount;
LWP_DispatchProcess();
lwp_cpptr -> eventcnt = 0;
return LWP_SUCCESS;
}
static void Abort_LWP(char *msg)
{
lwpdebug(0, "Entered Abort_LWP");
printf("***LWP Abort: %s\n", msg);
Exit_LWP();
}
static void Free_PCB(PROCESS pid)
{
stack_t *stack = &pid->stack;
lwpdebug(0, "Entered Free_PCB");
IOMGR_Cancel(pid);
lwpremove(pid, (pid->status==WAITING ?
&blocked :
&runnable[pid->priority]));
LWPANCHOR.processcnt--;
if (pid->name)
free(pid->name);
if (stack->ss_sp) {
lwpdebug(0, "HWM stack usage: %d, [PCB at %p]", Stack_Used(stack), pid);
#ifdef MMAP_LWP_STACKS
munmap(stack->ss_sp, stack->ss_size);
#else
free(stack->ss_sp);
#endif
}
if (pid->eventlist != NULL) free((char *)pid->eventlist);
free((char *)pid);
}
/* explicit voluntary preemption */
static int lwp_DispatchProcess(int dummy)
{
static int dispatch_count = 0;
PROCESS old_cpptr = lwp_cpptr;
int i;
if (!lwp_init)
return LWP_EINIT;
/* keep track of where the stack is */
if (lwp_cpptr)
lwp_cpptr->topstack = &dummy;
lwpdebug(0, "Entered LWP_DispatchProcess");
if (LWP_TraceProcesses > 0) {
for (i=0; i<MAX_PRIORITIES; i++) {
printf("[Priority %d, runnable (%d):", i, runnable[i].count);
for_all_elts(p, runnable[i], { printf(" \"%s\"", p->name); })
puts("]");
}
printf("[Blocked (%d):", blocked.count);
for_all_elts(p, blocked, { printf(" \"%s\"", p->name); })
puts("]");
}
/* Check for stack overflowif this lwp has a stack. Check for
the guard word at the front of the stack being damaged.
WARNING! This code assumes that stacks grow downward. */
if (lwp_cpptr && lwp_cpptr->stackcheck &&
(lwp_cpptr->stackcheck != *(int *)lwp_cpptr->stack.ss_sp ||
(char *)&dummy < (char *)lwp_cpptr->stack.ss_sp))
{
switch (lwp_overflowAction) {
case LWP_SOABORT:
Overflow_Complain();
abort ();
case LWP_SOMESSAGE:
default:
Overflow_Complain();
lwp_overflowAction = LWP_SOQUIET;
case LWP_SOQUIET:
break;
}
}
/* Move head of current runnable queue forward if current LWP is still in it. */
if (lwp_cpptr && lwp_cpptr == runnable[lwp_cpptr->priority].head)
runnable[lwp_cpptr->priority].head = runnable[lwp_cpptr->priority].head -> next;
/* Find highest priority with runnable processes. */
for (i=MAX_PRIORITIES-1; i>=0; i--)
if (runnable[i].head)
break;
if (i < 0)
Abort_LWP("LWP_DispatchProcess: Possible deadlock, "
"no runnable threads found\n");
if (LWP_TraceProcesses > 0)
printf("Dispatch %d [PCB at %p] \"%s\"\n",
++dispatch_count, runnable[i].head, runnable[i].head->name);
if (old_cpptr)
gettimeofday(&old_cpptr->lastReady, NULL); /* back in queue */
lwp_cpptr = runnable[i].head;
Cont_Sws++; /* number of context switches, for statistics */
/* check time to context switch */
if (timerisset(&cont_sw_threshold))
CheckWorkTime(old_cpptr, lwp_cpptr);
/* check time waiting to run */
if (timerisset(&run_wait_threshold))
CheckRunWaitTime(lwp_cpptr);
if (!old_cpptr) {
setcontext(&lwp_cpptr->ctx);
assert(0); /* we should never get here */
}
/* If another thread is now runnable, swap context */
if (lwp_cpptr != old_cpptr)
swapcontext(&old_cpptr->ctx, &lwp_cpptr->ctx);
return LWP_SUCCESS;
}
int LWP_DispatchProcess(void)
{
return lwp_DispatchProcess(0);
}
static void Initialize_PCB(PROCESS temp, int priority, char *stack,
int stacksize, void (*func)(void *), void *arg,
char *name)
{
lwpdebug(0, "Entered Initialize_PCB");
memset(temp, 0, sizeof(struct lwp_pcb));
if (name)
temp->name = strdup(name);
temp->status = READY;
temp->eventlist = (char **)malloc(EVINITSIZE*sizeof(char *));
temp->eventlistsize = EVINITSIZE;
temp->priority = priority;
temp->index = lwp_nextindex++;
temp->level = 1; /* non-preemptable */
timerclear(&temp->lastReady);
temp->ep = func;
temp->parm = arg;
if (stack) {
/* seems to be broken on x86_64 */
/* *(int *)stack = temp->stackcheck = 0xBAD57ACC; */
temp->stackcheck = 0;
getcontext(&temp->ctx);
temp->stack.ss_sp = temp->ctx.uc_stack.ss_sp = stack;
temp->stack.ss_size = temp->ctx.uc_stack.ss_size = stacksize;
temp->ctx.uc_link = &reaper; /* whenever a thread exits, we
automatically reap it and schedule
another runnable thread */
makecontext(&temp->ctx, (void (*)(void))func, 1, arg);
}
else
temp->stackcheck = 0;
lwpdebug(0, "Leaving Initialize_PCB\n");
}
static int Internal_Signal(void *event)
{
int rc = LWP_ENOWAIT;
int i;
lwpdebug(0, "Entered Internal_Signal [event id %p]", event);
if (!lwp_init) return LWP_EINIT;
if (event == NULL) return LWP_EBADEVENT;
for_all_elts(temp, blocked, { /* for all pcb's on the blocked q */
if (temp->status == WAITING)
{
for (i=0; i < temp->eventcnt; i++)
{ /* check each event in list */
if (temp -> eventlist[i] == event)
{
temp -> eventlist[i] = NULL;
rc = LWP_SUCCESS;
/* reduce waitcnt by 1 for the signal */
/* if wcount reaches 0 then make the process runnable */
if (--temp->waitcnt == 0)
{
temp -> status = READY;
temp -> wakevent = i+1;
lwpmove(temp, &blocked, &runnable[temp->priority]);
gettimeofday(&temp->lastReady, NULL);
break;
}
}
}
}
})
return rc;
}
int LWP_INTERNALSIGNAL(void *event, int yield)
{
int rc;
lwpdebug(0, "Entered LWP_SignalProcess");
if (!lwp_init)
return LWP_EINIT;
rc = Internal_Signal(event);
if (yield)
LWP_DispatchProcess();
return rc;
}
static void Initialize_Stack(void *stackptr, int stacksize)
{
/* This can be any unlikely pattern except 0x00010203 or the reverse. */
#define STACKMAGIC 0xBADBADBA
int i;
lwpdebug(0, "Entered Initialize_Stack");
if (lwp_stackUseEnabled)
for (i=0; i<stacksize; i++)
((unsigned char *)stackptr)[i] = i &0xff;
else
*(int *)stackptr = STACKMAGIC;
}
static int Stack_Used(stack_t *stack)
{
int i;
if (*(int *) stack->ss_sp == STACKMAGIC)
return 0;
for (i = 0; i < stack->ss_size; i++)
if (((unsigned char *)stack->ss_sp)[i] != (i & 0xff))
return (stack->ss_size - i);
return 0;
}
int LWP_StackUsed(PROCESS pid, int *max, int *used)
{
*max = pid->stack.ss_size;
*used = Stack_Used(&pid->stack);
if (*used == 0)
return LWP_NO_STACK;
return LWP_SUCCESS;
}
/* Complain of a stack overflow to stderr without using stdio (and use as
* little stack as possible). */
static void Overflow_Complain()
{
static const char *msg1 = "LWP: stack overflow in process ";
static const char *msg2 = "!\n";
write (2, msg1, strlen(msg1));
write (2, lwp_cpptr->name, strlen(lwp_cpptr->name));
write (2, msg2, strlen(msg2));
}
void PRE_Concurrent(int on) { }
void PRE_BeginCritical(void) { }
void PRE_EndCritical(void) { }
/* The following documents the Assembler interfaces used by old LWP:
savecontext(void (*ep)(), struct lwp_context *savearea, char *sp)
Stub for Assembler routine that will
save the current SP value in the passed
context savearea and call the function
whose entry point is in ep. If the sp
parameter is NULL, the current stack is
used, otherwise sp becomes the new stack
pointer.
returnto(struct lwp_context *savearea);
Stub for Assembler routine that will
restore context from a passed savearea
and return to the restored C frame.
*/
|