Ignore:
Timestamp:
Aug 9, 2000, 8:59:03 PM (25 years ago)
Author:
sandervl
Message:

TLS changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/wintls.cpp

    r3615 r3975  
    1 /* $Id: wintls.cpp,v 1.10 2000-05-27 11:30:36 sandervl Exp $ */
     1/* $Id: wintls.cpp,v 1.11 2000-08-09 18:59:03 sandervl Exp $ */
    22/*
    33 * Win32 TLS API functions
    44 *
    5  * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
    6  *
     5 * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
     6 *
     7 * TODO: correct errors set for Tls* apis? (verify in NT)
    78 *
    89 * Project Odin Software License can be found in LICENSE.TXT
     
    5455void Win32ImageBase::tlsAttachThread()  //setup TLS structures for new thread
    5556{
    56  EXCEPTION_FRAME exceptFrame;
     57 EXCEPTION_FRAME      exceptFrame;
    5758 PIMAGE_TLS_CALLBACK *pCallback;
    58  TEB   *winteb;
    59  char  *tibmem;
     59 LPVOID               tibmem;
    6060
    6161   if(!tlsAddress)
     
    7575   dprintf(("tlsCallbackAddr  %x", tlsCallBackAddr));
    7676   dprintf(("*tlsCallbackAddr %x", (tlsCallBackAddr) ? *tlsCallBackAddr : 0));
    77    tibmem = (char *)VirtualAlloc(0, tlsTotalSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
     77   tibmem = VirtualAlloc(0, tlsTotalSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
    7878   if(tibmem == NULL) {
    7979        dprintf(("tlsAttachThread: tibmem == NULL!!!!"));
     
    8484   memcpy(tibmem, tlsAddress, tlsInitSize);
    8585
    86    winteb = (TEB *)*TIBFlatPtr;
    87    winteb->tls_ptr[tlsIndex] = tibmem;
     86   TlsSetValue(tlsIndex, tibmem);
    8887   *tlsIndexAddr = tlsIndex;
    8988
     
    105104void Win32ImageBase::tlsDetachThread()  //destroy TLS structures
    106105{
    107  EXCEPTION_FRAME exceptFrame;
     106 EXCEPTION_FRAME      exceptFrame;
    108107 PIMAGE_TLS_CALLBACK *pCallback;
    109  TEB   *winteb;
     108 LPVOID tlsmem;
    110109
    111110   if(!tlsAddress)
     
    125124        }
    126125   }
    127    winteb = (TEB *)*TIBFlatPtr;
    128    VirtualFree(winteb->tls_ptr[tlsIndex], tlsTotalSize, MEM_RELEASE);
    129    winteb->tls_ptr[tlsIndex] = 0;
     126   tlsmem = TlsGetValue(tlsIndex);
     127   if(tlsmem) {
     128        VirtualFree(tlsmem, tlsTotalSize, MEM_RELEASE);
     129   }
     130   else {
     131        dprintf(("ERROR: tlsDetachThread: tlsmem == NULL!!!"));
     132   }
     133   TlsFree(tlsIndex);
    130134}
    131135//******************************************************************************
     
    136140DWORD WIN32API TlsAlloc()
    137141{
    138  DWORD index;
    139 
     142 DWORD index = -1;
     143
     144#if 1
     145 THDB *thdb;
     146 PDB  *pdb;
     147 DWORD mask, tibidx;
     148 int   i;
     149
     150  thdb = GetThreadTHDB();
     151  pdb  = PROCESS_Current();
     152
     153  EnterCriticalSection(&pdb->crit_section);
     154  tibidx = 0;
     155  if(pdb->tls_bits[0] == 0xFFFFFFFF) {
     156        if(pdb->tls_bits[1] == 0xFFFFFFFF) {
     157                LeaveCriticalSection(&pdb->crit_section);
     158                SetLastError(ERROR_NO_MORE_ITEMS);  //TODO: correct error?
     159                return -1;
     160        }
     161        tibidx = 1;
     162  }
     163  for(i=0;i<32;i++) {
     164        mask = (1 << i);
     165        if((pdb->tls_bits[tibidx] & mask) == 0) {
     166                pdb->tls_bits[tibidx] |= mask;
     167                index = (tibidx*32) + i;
     168                break;
     169        }
     170  }
     171  LeaveCriticalSection(&pdb->crit_section);
     172  thdb->tls_array[index] = 0;
     173#else
    140174  index = O32_TlsAlloc();
     175#endif
    141176  dprintf(("KERNEL32: TlsAlloc returned %d", index));
    142177  return index;
     
    147182{
    148183  dprintf(("KERNEL32: TlsFree %d", index));
     184#if 1
     185 THDB  *thdb;
     186 PDB   *pdb;
     187 int    tlsidx;
     188 DWORD  mask;
     189
     190  if(index >= TLS_MINIMUM_AVAILABLE)
     191  {
     192        SetLastError(ERROR_INVALID_PARAMETER);
     193        return NULL;
     194  }
     195
     196  thdb = GetThreadTHDB();
     197  pdb  = PROCESS_Current();
     198
     199  EnterCriticalSection(&pdb->crit_section);
     200  tlsidx = 0;
     201  if(index > 32) {
     202        tlsidx++;
     203  }
     204  mask = (1 << index);
     205  if(pdb->tls_bits[tlsidx] & mask) {
     206        LeaveCriticalSection(&pdb->crit_section);
     207        pdb->tls_bits[tlsidx] &= ~mask;
     208        thdb->tls_array[index] = 0;
     209        SetLastError(ERROR_SUCCESS);
     210        return TRUE;
     211  }
     212  LeaveCriticalSection(&pdb->crit_section);
     213  SetLastError(ERROR_INVALID_PARAMETER); //TODO: correct error? (does NT even change the last error?)
     214  return FALSE;
     215#else
    149216  return(O32_TlsFree(index));
     217#endif
    150218}
    151219//******************************************************************************
     
    155223 LPVOID rc;
    156224
     225  if(index >= TLS_MINIMUM_AVAILABLE)
     226  {
     227        SetLastError(ERROR_INVALID_PARAMETER);
     228        return NULL;
     229  }
     230  SetLastError(ERROR_SUCCESS);
     231
     232#if 1
     233 THDB  *thdb;
     234
     235  thdb = GetThreadTHDB();
     236  rc = thdb->tls_array[index];
     237#else
    157238  rc = O32_TlsGetValue(index);
    158   dprintf2(("KERNEL32:  TlsGetValue %d returned %X\n", index, rc));
     239#endif
     240  dprintf2(("KERNEL32: TlsGetValue %d returned %X\n", index, rc));
    159241  return(rc);
    160242}
     
    163245BOOL WIN32API TlsSetValue(DWORD index, LPVOID val)
    164246{
    165   dprintf2(("KERNEL32:  TlsSetValue\n"));
     247  dprintf2(("KERNEL32: TlsSetValue %d %x", index, val));
     248  if(index >= TLS_MINIMUM_AVAILABLE)
     249  {
     250        SetLastError(ERROR_INVALID_PARAMETER);
     251        return FALSE;
     252  }
     253  SetLastError(ERROR_SUCCESS);
     254#if 1
     255  THDB *thdb;
     256
     257  thdb = GetThreadTHDB();
     258  thdb->tls_array[index] = val;
     259  return TRUE;
     260#else
    166261  return(O32_TlsSetValue(index, val));
    167 }
    168 //******************************************************************************
    169 //******************************************************************************
     262#endif
     263}
     264//******************************************************************************
     265//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.