Ignore:
Timestamp:
Jan 27, 2011, 8:56:05 PM (10 years ago)
Author:
rme
Message:

Checkpoint of work in progress.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/shrink-tcr/lisp-kernel/thread_manager.c

    r14328 r14606  
    7474  char _contextbuf[sizeof(CONTEXT)+__alignof(CONTEXT)];
    7575  CONTEXT  *pcontext;
    76   HANDLE hthread = (HANDLE)(target->osid);
     76  HANDLE hthread = (HANDLE)(TCR_AUX(target)->osid);
    7777  pc where;
    78   area *cs = target->cs_area, *ts = target->cs_area;
     78  area *ts = target->ts_area;
    7979  DWORD rc;
    8080  BOOL io_pending;
     
    173173raise_thread_interrupt(TCR *target)
    174174{
    175   pthread_t thread = (pthread_t)target->osid;
     175  pthread_t thread = (pthread_t)TCR_AUX(target)->osid;
    176176#ifdef DARWIN_not_yet
    177177  if (use_mach_exception_handling) {
     
    534534
    535535  if (tcr) {
    536     current = tcr->osid;
     536    current = TCR_AUX(tcr)->osid;
    537537  }
    538538  if (current == 0) {
     
    545545                    DUPLICATE_SAME_ACCESS);
    546546    if (tcr) {
    547       tcr->osid = current;
     547      TCR_AUX(tcr)->osid = current;
    548548    }
    549549  }
     
    589589   
    590590    tcr = new_tcr(initial_stack_size, MIN_TSTACK_SIZE);
    591     tcr->suspend_count = 1;
     591    TCR_AUX(tcr)->suspend_count = 1;
    592592    tcr->vs_area->active -= node_size;
    593593    *(--tcr->save_vsp) = lisp_nil;
     
    597597    SET_TCR_FLAG(tcr,TCR_FLAG_BIT_PENDING_SUSPEND);
    598598  } else {
    599     tcr->suspend_context = context;
    600     SEM_RAISE(tcr->suspend);
    601     SEM_WAIT_FOREVER(tcr->resume);
    602     tcr->suspend_context = NULL;
     599    TCR_AUX(tcr)->suspend_context = context;
     600    SEM_RAISE(TCR_AUX(tcr)->suspend);
     601    SEM_WAIT_FOREVER(TCR_AUX(tcr)->resume);
     602    TCR_AUX(tcr)->suspend_context = NULL;
    603603  }
    604604#ifdef DARWIN_GS_HACK
     
    776776  TCR *next, *prev;
    777777
    778   next = tcr->next;
    779   prev = tcr->prev;
    780 
    781   prev->next = next;
    782   next->prev = prev;
    783   tcr->prev = tcr->next = NULL;
     778  next = TCR_AUX(tcr)->next;
     779  prev = TCR_AUX(tcr)->prev;
     780
     781  TCR_AUX(prev)->next = next;
     782  TCR_AUX(next)->prev = prev;
     783  TCR_AUX(tcr)->prev = TCR_AUX(tcr)->next = NULL;
    784784#ifdef X8664
    785785  tcr->linear = NULL;
     
    794794  LOCK(lisp_global(TCR_AREA_LOCK),new);
    795795  head = (TCR *)ptr_from_lispobj(lisp_global(INITIAL_TCR));
    796   tail = head->prev;
    797   tail->next = new;
    798   head->prev = new;
    799   new->prev = tail;
    800   new->next = head;
     796  tail = TCR_AUX(head)->prev;
     797  TCR_AUX(tail)->next = new;
     798  TCR_AUX(head)->prev = new;
     799  TCR_AUX(new)->prev = tail;
     800  TCR_AUX(new)->next = head;
    801801  UNLOCK(lisp_global(TCR_AREA_LOCK),new);
    802802}
     
    806806allocate_tcr()
    807807{
    808   void *p = calloc(1,sizeof(TCR)+15);
    809   TCR *tcr = (TCR *)((((natural)p)+15)&~15);
    810 
    811   tcr->allocated = p;
     808  void *p = calloc(1,sizeof(struct tcr_aux));
     809  char *teb = (char *)NtCurrentTeb();
     810  TCR *tcr = (TCR *)(teb + TCR_BIAS);
     811
     812  if (p == NULL)
     813    allocation_failure(true, sizeof(struct tcr_aux));
     814
     815  if ((intptr_t)p & 03) {
     816    fprintf(dbgout, "%p not aligned\n", p);
     817    exit(1);
     818  }
     819  memset(tcr, 0, sizeof(TCR));
     820  tcr->aux = p;
    812821  return tcr;
    813822}
     
    10631072setup_tcr_extra_segment(TCR *tcr)
    10641073{
    1065   int i, status;
    1066   DWORD nret;
    1067   win32_ldt_info info;
    1068   LDT_ENTRY *entry = &(info.entry);
    1069   DWORD *words = (DWORD *)entry, tcraddr = (DWORD)tcr;
    1070 
    1071 
    1072   WaitForSingleObject(ldt_lock,INFINITE);
    1073 
    1074   for (i = 0; i < 8192; i++) {
    1075     if (!ref_bit(ldt_entries_in_use,i)) {
    1076       info.offset = i << 3;
    1077       info.size = sizeof(LDT_ENTRY);
    1078       words[0] = 0;
    1079       words[1] = 0;
    1080       status = NtQueryInformationProcess(GetCurrentProcess(),10,&info,sizeof(info),&nret);
    1081       if (status == 0) {
    1082         if ((info.size == 0) ||
    1083             ((words[0] == 0) && (words[1] == 0))) {
    1084           break;
    1085         }
    1086       }
    1087     }
    1088   }
    1089   if (i == 8192) {
    1090     ReleaseMutex(ldt_lock);
    1091     fprintf(dbgout, "All 8192 ldt entries in use ?\n");
    1092     _exit(1);
    1093   }
    1094   set_bit(ldt_entries_in_use,i);
    1095   words[0] = 0;
    1096   words[1] = 0;
    1097   entry->LimitLow = sizeof(TCR);
    1098   entry->BaseLow = tcraddr & 0xffff;
    1099   entry->HighWord.Bits.BaseMid = (tcraddr >> 16) & 0xff;
    1100   entry->HighWord.Bits.BaseHi = (tcraddr >> 24);
    1101   entry->HighWord.Bits.Pres = 1;
    1102   entry->HighWord.Bits.Default_Big = 1;
    1103   entry->HighWord.Bits.Type = 16 | 2; /* read-write data */
    1104   entry->HighWord.Bits.Dpl = 3; /* for use by the great unwashed */
    1105   info.size = sizeof(LDT_ENTRY);
    1106   status = NtSetInformationProcess(GetCurrentProcess(),10,&info,sizeof(info));
    1107   if (status != 0) {
    1108     ReleaseMutex(ldt_lock);
    1109     FBug(NULL, "can't set LDT entry %d, status = 0x%x", i, status);
    1110   }
    1111 #if 1
    1112   /* Sanity check */
    1113   info.offset = i << 3;
    1114   info.size = sizeof(LDT_ENTRY);
    1115   words[0] = 0;
    1116   words[0] = 0;
    1117   NtQueryInformationProcess(GetCurrentProcess(),10,&info,sizeof(info),&nret);
    1118   if (((entry->BaseLow)|((entry->HighWord.Bits.BaseMid)<<16)|((entry->HighWord.Bits.BaseHi)<<24)) != tcraddr) {
    1119     Bug(NULL, "you blew it: bad address in ldt entry\n");
    1120   }
    1121 #endif
    1122   tcr->ldt_selector = (i << 3) | 7;
    1123   ReleaseMutex(ldt_lock);
    11241074}
    11251075
     
    11271077free_tcr_extra_segment(TCR *tcr)
    11281078{
    1129   win32_ldt_info info;
    1130   LDT_ENTRY *entry = &(info.entry);
    1131   DWORD *words = (DWORD *)entry;
    1132   int idx = tcr->ldt_selector >> 3;
    1133 
    1134 
    1135   info.offset = idx << 3;
    1136   info.size = sizeof(LDT_ENTRY);
    1137 
    1138   words[0] = 0;
    1139   words[1] = 0;
    1140 
    1141   WaitForSingleObject(ldt_lock,INFINITE);
    1142   NtSetInformationProcess(GetCurrentProcess(),10,&info,sizeof(info));
    1143   clr_bit(ldt_entries_in_use,idx);
    1144   ReleaseMutex(ldt_lock);
    1145 
    1146   tcr->ldt_selector = 0;
    11471079}
    11481080
     
    13351267  tcr->single_float_convert.tag = subtag_single_float;
    13361268#endif
    1337   tcr->suspend = new_semaphore(0);
    1338   tcr->resume = new_semaphore(0);
    1339   tcr->reset_completion = new_semaphore(0);
    1340   tcr->activate = new_semaphore(0);
     1269  TCR_AUX(tcr)->suspend = new_semaphore(0);
     1270  TCR_AUX(tcr)->resume = new_semaphore(0);
     1271  TCR_AUX(tcr)->reset_completion = new_semaphore(0);
     1272  TCR_AUX(tcr)->activate = new_semaphore(0);
    13411273  LOCK(lisp_global(TCR_AREA_LOCK),tcr);
    13421274  a = allocate_vstack_holding_area_lock(vstack_size);
     
    13861318  tcr->shutdown_count = PTHREAD_DESTRUCTOR_ITERATIONS;
    13871319#else
    1388   tcr->shutdown_count = 1;
     1320  TCR_AUX(tcr)->shutdown_count = 1;
    13891321#endif
    13901322  return tcr;
     
    14021334  }
    14031335
    1404   if (--(tcr->shutdown_count) == 0) {
     1336  if (--(TCR_AUX(tcr)->shutdown_count) == 0) {
    14051337    if (tcr->flags & (1<<TCR_FLAG_BIT_FOREIGN)) {
    14061338      LispObj callback_macptr = nrs_FOREIGN_THREAD_CONTROL.vcell,
     
    14211353    tcr->ts_area = NULL;
    14221354#endif
    1423     cs = tcr->cs_area;
    1424     tcr->cs_area = NULL;
     1355    cs = TCR_AUX(tcr)->cs_area;
     1356    TCR_AUX(tcr)->cs_area = NULL;
    14251357    if (vs) {
    14261358      condemn_area_holding_area_lock(vs);
     
    14501382#endif
    14511383#endif
    1452     destroy_semaphore(&tcr->suspend);
    1453     destroy_semaphore(&tcr->resume);
    1454     destroy_semaphore(&tcr->reset_completion);
    1455     destroy_semaphore(&tcr->activate);
     1384    destroy_semaphore(&TCR_AUX(tcr)->suspend);
     1385    destroy_semaphore(&TCR_AUX(tcr)->resume);
     1386    destroy_semaphore(&TCR_AUX(tcr)->reset_completion);
     1387    destroy_semaphore(&TCR_AUX(tcr)->activate);
    14561388    tcr->tlb_limit = 0;
    14571389    free(tcr->tlb_pointer);
    14581390    tcr->tlb_pointer = NULL;
    14591391#ifdef WINDOWS
    1460     if (tcr->osid != 0) {
    1461       CloseHandle((HANDLE)(tcr->osid));
    1462     }
    1463 #endif
    1464     tcr->osid = 0;
     1392    if (TCR_AUX(tcr)->osid != 0) {
     1393      CloseHandle((HANDLE)(TCR_AUX(tcr)->osid));
     1394    }
     1395#endif
     1396    TCR_AUX(tcr)->osid = 0;
    14651397    tcr->interrupt_pending = 0;
    1466     tcr->termination_semaphore = NULL;
     1398    TCR_AUX(tcr)->termination_semaphore = NULL;
    14671399#ifdef HAVE_TLS
    14681400    dequeue_tcr(tcr);
     
    14721404#endif
    14731405#ifdef WIN32
    1474     CloseHandle((HANDLE)tcr->io_datum);
    1475     tcr->io_datum = NULL;
    1476     free(tcr->native_thread_info);
    1477     tcr->native_thread_info = NULL;
     1406    CloseHandle((HANDLE)TCR_AUX(tcr)->io_datum);
     1407    TCR_AUX(tcr)->io_datum = NULL;
     1408    free(TCR_AUX(tcr)->native_thread_info);
     1409    TCR_AUX(tcr)->native_thread_info = NULL;
    14781410#endif
    14791411    UNLOCK(lisp_global(TCR_AREA_LOCK),current);
     
    14991431  }
    15001432#endif
    1501   a = tcr->cs_area;
     1433  a = TCR_AUX(tcr)->cs_area;
    15021434  if (a) {
    15031435    a->active = a->high;
    15041436  }
    15051437  tcr->valence = TCR_STATE_FOREIGN;
    1506   tcr->shutdown_count = 1;
     1438  TCR_AUX(tcr)->shutdown_count = 1;
    15071439  shutdown_thread_tcr(tcr);
    15081440  tsd_set(lisp_global(TCR_KEY), NULL);
     
    15411473  area *a, *register_cstack_holding_area_lock(BytePtr, natural);
    15421474
    1543   tcr->osid = current_thread_osid();
    1544   tcr->native_thread_id = current_native_thread_id();
     1475  TCR_AUX(tcr)->osid = current_thread_osid();
     1476  TCR_AUX(tcr)->native_thread_id = current_native_thread_id();
    15451477  LOCK(lisp_global(TCR_AREA_LOCK),tcr);
    15461478  a = register_cstack_holding_area_lock((BytePtr)stack_base, stack_size);
    15471479  UNLOCK(lisp_global(TCR_AREA_LOCK),tcr);
    1548   tcr->cs_area = a;
     1480  TCR_AUX(tcr)->cs_area = a;
    15491481  a->owner = tcr;
    15501482#ifdef ARM
     
    15521484#endif
    15531485  if (!(tcr->flags & (1<<TCR_FLAG_BIT_FOREIGN))) {
    1554     tcr->cs_limit = (LispObj)ptr_to_lispobj(a->softlimit);
     1486    TCR_AUX(tcr)->cs_limit = (LispObj)ptr_to_lispobj(a->softlimit);
    15551487  }
    15561488#ifdef LINUX
     
    15611493#endif
    15621494#endif
    1563   tcr->errno_loc = &errno;
     1495  TCR_AUX(tcr)->errno_loc = &errno;
    15641496  tsd_set(lisp_global(TCR_KEY), TCR_TO_TSD(tcr));
    15651497#ifdef DARWIN
     
    15731505#endif
    15741506#ifdef WINDOWS
    1575   tcr->io_datum = (VOID *)CreateEvent(NULL, true, false, NULL);
    1576   tcr->native_thread_info = malloc(sizeof(CONTEXT));
    1577 #endif
    1578   tcr->log2_allocation_quantum = unbox_fixnum(lisp_global(DEFAULT_ALLOCATION_QUANTUM));
     1507  TCR_AUX(tcr)->io_datum = (VOID *)CreateEvent(NULL, true, false, NULL);
     1508  TCR_AUX(tcr)->native_thread_info = malloc(sizeof(CONTEXT));
     1509#endif
     1510  TCR_AUX(tcr)->log2_allocation_quantum = unbox_fixnum(lisp_global(DEFAULT_ALLOCATION_QUANTUM));
    15791511}
    15801512
     
    17031635  SEM_RAISE(activation->created);
    17041636  do {
    1705     SEM_RAISE(tcr->reset_completion);
    1706     SEM_WAIT_FOREVER(tcr->activate);
     1637    SEM_RAISE(TCR_AUX(tcr)->reset_completion);
     1638    SEM_WAIT_FOREVER(TCR_AUX(tcr)->activate);
    17071639    /* Now go run some lisp code */
    17081640    start_lisp(TCR_TO_TSD(tcr),0);
     
    17571689  start_vsp = tcr->save_vsp;
    17581690  do {
    1759     SEM_RAISE(tcr->reset_completion);
     1691    SEM_RAISE(TCR_AUX(tcr)->reset_completion);
    17601692    suspend_current_cooperative_thread();
    17611693     
     
    18031735      return true;
    18041736    }
    1805     p = p->next;
     1737    p = TCR_AUX(p)->next;
    18061738  } while (p != head);
    18071739  return false;
     
    19061838#ifdef HAVE_TLS
    19071839  TCR *current = current_tcr;
     1840#elif defined(WIN_32)
     1841  TCR *current = (TCR *)((char *)NtCurrentTeb() + TCR_BIAS);
    19081842#else
    19091843  void *tsd = (void *)tsd_get(lisp_global(TCR_KEY));
     
    19491883      current->vs_area->active -= node_size;
    19501884    }
    1951     current->shutdown_count = 1;
     1885    TCR_AUX(current)->shutdown_count = 1;
    19521886    ((void (*)())ptr_from_lispobj(callback_ptr))(0);
    19531887
     
    20081942suspend_tcr(TCR *tcr)
    20091943{
    2010   int suspend_count = atomic_incf(&(tcr->suspend_count));
     1944  int suspend_count = atomic_incf(&(TCR_AUX(tcr)->suspend_count));
    20111945  DWORD rc;
    20121946  if (suspend_count == 1) {
    2013     CONTEXT  *pcontext = (CONTEXT *)tcr->native_thread_info;
    2014     HANDLE hthread = (HANDLE)(tcr->osid);
     1947    CONTEXT  *pcontext = (CONTEXT *)TCR_AUX(tcr)->native_thread_info;
     1948    HANDLE hthread = (HANDLE)(TCR_AUX(tcr)->osid);
    20151949    pc where;
    2016     area *cs = tcr->cs_area;
     1950    area *cs = TCR_AUX(tcr)->cs_area;
    20171951    LispObj foreign_rsp;
    20181952
     
    20651999          SET_TCR_FLAG(tcr,TCR_FLAG_BIT_PENDING_SUSPEND);
    20662000          ResumeThread(hthread);
    2067           SEM_WAIT_FOREVER(tcr->suspend);
     2001          SEM_WAIT_FOREVER(TCR_AUX(tcr)->suspend);
    20682002          SuspendThread(hthread);
    20692003          /* The thread is either waiting for its resume semaphore to
    20702004             be signaled or is about to wait.  Signal it now, while
    20712005             the thread's suspended. */
    2072           SEM_RAISE(tcr->resume);
     2006          SEM_RAISE(TCR_AUX(tcr)->resume);
    20732007          pcontext->ContextFlags = CONTEXT_ALL;
    20742008          GetThreadContext(hthread, pcontext);
     
    20872021#endif
    20882022    }
    2089     tcr->suspend_context = pcontext;
     2023    TCR_AUX(tcr)->suspend_context = pcontext;
    20902024    return true;
    20912025  }
     
    21462080  LOCK(lisp_global(TCR_AREA_LOCK),current);
    21472081  {
    2148     LispObj osid = tcr->osid;
     2082    LispObj osid = TCR_AUX(tcr)->osid;
    21492083   
    21502084    if (osid) {
     
    21542088         forcing the thread to run quit_handler().  For now,
    21552089         mark the TCR as dead and kill the Windows thread. */
    2156       tcr->osid = 0;
     2090      /* xxx TerminateThread() bad */
     2091      TCR_AUX(tcr)->osid = 0;
    21572092      if (!TerminateThread((HANDLE)osid, 0)) {
    21582093        CloseHandle((HANDLE)osid);
     
    21922127resume_tcr(TCR *tcr)
    21932128{
    2194   int suspend_count = atomic_decf(&(tcr->suspend_count)), err;
     2129  int suspend_count = atomic_decf(&(TCR_AUX(tcr)->suspend_count)), err;
    21952130  DWORD rc;
    21962131  if (suspend_count == 0) {
    2197     CONTEXT *context = tcr->suspend_context;
    2198     HANDLE hthread = (HANDLE)(tcr->osid);
     2132    CONTEXT *context = TCR_AUX(tcr)->suspend_context;
     2133    HANDLE hthread = (HANDLE)(TCR_AUX(tcr)->osid);
    21992134
    22002135    if (context) {
    22012136      context->ContextFlags = CONTEXT_ALL;
    2202       tcr->suspend_context = NULL;
     2137      TCR_AUX(tcr)->suspend_context = NULL;
    22032138      SetThreadContext(hthread,context);
    22042139      rc = ResumeThread(hthread);
     
    22502185{
    22512186#ifndef HAVE_TLS
    2252   tcr->next = freed_tcrs;
     2187  TCR_AUX(tcr)->next = freed_tcrs;
    22532188  freed_tcrs = tcr;
    22542189#endif
     
    22782213#endif
    22792214
    2280   a = tcr->cs_area;
     2215  a = TCR_AUX(tcr)->cs_area;
    22812216  if (a) {
    22822217    a->active = a->high;
     
    22902225
    22912226  for (current = freed_tcrs; current; current = next) {
    2292     next = current->next;
     2227    next = TCR_AUX(current)->next;
    22932228#ifndef HAVE_TLS
    22942229#ifdef WIN32
    2295     free(current->allocated);
     2230    free(current->aux);
    22962231#else
    22972232    free(current);
     
    23102245
    23112246  LOCK(lisp_global(TCR_AREA_LOCK), current);
    2312   for (other = current->next; other != current; other = other->next) {
    2313     if ((other->osid != 0)) {
     2247  for (other = TCR_AUX(current)->next; other != current; other = TCR_AUX(other)->next) {
     2248    if ((TCR_AUX(other)->osid != 0)) {
    23142249      suspend_tcr(other);
    2315       if (other->osid == 0) {
     2250      if (TCR_AUX(other)->osid == 0) {
    23162251        dead_tcr_count++;
    23172252      }
     
    23232258  do {
    23242259    all_acked = true;
    2325     for (other = current->next; other != current; other = other->next) {
    2326       if ((other->osid != 0)) {
     2260    for (other = TCR_AUX(current)->next; other != current; other = TCR_AUX(other)->next) {
     2261      if ((TCR_AUX(other)->osid != 0)) {
    23272262        if (!tcr_suspend_ack(other)) {
    23282263          all_acked = false;
     
    23362271  /* All other threads are suspended; can safely delete dead tcrs now */
    23372272  if (dead_tcr_count) {
    2338     for (other = current->next; other != current; other = next) {
    2339       next = other->next;
    2340       if ((other->osid == 0))  {
     2273    for (other = TCR_AUX(current)->next; other != current; other = next) {
     2274      next = TCR_AUX(other)->next;
     2275      if ((TCR_AUX(other)->osid == 0))  {
    23412276        normalize_dead_tcr_areas(other);
    23422277        dequeue_tcr(other);
     
    23572292{
    23582293  TCR *current = get_tcr(true), *other;
    2359   for (other = current->next; other != current; other = other->next) {
    2360     if ((other->osid != 0)) {
     2294  for (other = TCR_AUX(current)->next; other != current; other = TCR_AUX(other)->next) {
     2295    if ((TCR_AUX(other)->osid != 0)) {
    23612296      resume_tcr(other);
    23622297    }
Note: See TracChangeset for help on using the changeset viewer.