Ignore:
Timestamp:
Mar 6, 2003, 11:22:27 AM (22 years ago)
Author:
sandervl
Message:

KSO: logging changes; fast PID & TID retrieval; added functions to set/clear the odin environment (fs, exception handler)

File:
1 edited

Legend:

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

    r9748 r9910  
    1 /* $Id: thread.cpp,v 1.50 2003-02-04 11:29:03 sandervl Exp $ */
     1/* $Id: thread.cpp,v 1.51 2003-03-06 10:22:27 sandervl Exp $ */
    22
    33/*
     
    3535#include <codepage.h>
    3636
     37#include <FastInfoBlocks.h>
     38
    3739#define DBG_LOCALLOG    DBG_thread
    3840#include "dbglocal.h"
     
    4850    // check cached identifier
    4951    TEB *teb = GetThreadTEB();
    50     if(teb != NULL && teb->o.odin.threadId != 0xFFFFFFFF) 
     52    if(teb != NULL && teb->o.odin.threadId != 0xFFFFFFFF)
    5153    {
    5254        // this is set in InitializeTIB() already.
    5355        return teb->o.odin.threadId;
    5456    }
    55  
     57
    5658////  dprintf(("GetCurrentThreadId\n"));
    5759    return MAKE_THREADID(O32_GetCurrentProcessId(), O32_GetCurrentThreadId());
     
    6668    if(teb == 0) {
    6769        DebugInt3();
    68         SetLastError(ERROR_INVALID_HANDLE); //todo 
     70        SetLastError(ERROR_INVALID_HANDLE); //todo
    6971        return 0;
    7072    }
     
    111113#ifdef DEBUG
    112114  TEB *teb;
    113  
     115
    114116  // embedded dbg_IncThreadCallDepth
    115117  teb = GetThreadTEB();
    116118  if(teb == NULL)
    117119    return;
    118    
     120
    119121  // add caller name to call stack trace
    120122  int iIndex = teb->o.odin.dbgCallDepth;
    121  
     123
    122124  // allocate callstack on demand
    123125  if (teb->o.odin.arrstrCallStack == NULL)
    124126    teb->o.odin.arrstrCallStack = (PVOID*)malloc( sizeof(LPSTR) * MAX_CALLSTACK_SIZE);
    125  
     127
    126128  // insert entry
    127129  if (iIndex < MAX_CALLSTACK_SIZE)
     
    151153#ifdef DEBUG
    152154  TEB *teb;
    153  
     155
    154156  // embedded dbg_DecThreadCallDepth
    155157  teb = GetThreadTEB();
    156158  if(teb == NULL)
    157159    return;
    158  
     160
    159161  --(teb->o.odin.dbgCallDepth);
    160  
     162
    161163  // add caller name to call stack trace
    162164  int iIndex = teb->o.odin.dbgCallDepth;
    163  
     165
    164166  // insert entry
    165167  if (teb->o.odin.arrstrCallStack)
     
    175177  // retrieve last caller name from stack
    176178  TEB *teb;
    177  
     179
    178180  // embedded dbg_DecThreadCallDepth
    179181  teb = GetThreadTEB();
     
    188190  }
    189191#endif
    190  
     192
    191193  return NULL;
    192194}
     
    323325    OS2SetExceptionHandler((void *)&exceptFrame);
    324326    winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
    325    
     327
    326328    //Determine if thread callback is inside a PE dll; if true, then force
    327329    //switch to win32 TIB (FS selector)
     
    367369//******************************************************************************
    368370//******************************************************************************
     371
     372/**
     373 * Enter odin context with this thread.
     374 *
     375 * Is called when an OS/2 process is calling into an Odin32 DLL.
     376 * This may be called also in a nested fashion and supports that.
     377 * The conterpart of ODIN_ThreadLeaveOdinContext().
     378 *
     379 * @returns The old FS selector.
     380 * @returns 0 if TEB creation failed.
     381 * @param   pExceptionRegRec    OS/2 Exception Registration Record (2 ULONGs)
     382 *                              must be located on the callers stack.
     383 * @param   fForceFSSwitch      If set we will force switching to Odin32 FS selector.
     384 *                              If clear it depends on defaults.
     385 */
     386USHORT WIN32API ODIN_ThreadEnterOdinContext(void *pExceptionRegRec, BOOL fForceFSSwitch)
     387{
     388    USHORT  selFSOld = 0;
     389
     390    /*
     391     * Get TEB pointer, create it if necessary.
     392     * @todo    Check if this really is the thread which the TEB was created
     393     *          for. If not create the TEB.
     394     */
     395    TEB *pTeb = GetThreadTEB();
     396    if (!pTeb)
     397    {
     398        BOOL fMainThread = fibGetTid() == 1;
     399        HANDLE hThreadMain = HMCreateThread(NULL, 0, 0, 0, 0, 0, fMainThread);
     400        pTeb = CreateTEB(hThreadMain, fibGetTid());
     401        if (!pTeb || InitializeThread(pTeb, fMainThread) == FALSE)
     402        {
     403            dprintf(("ODIN_ThreadEnterOdinContext: Failed to create TEB!"));
     404        }
     405    }
     406
     407    /*
     408     * Install the Odin32 exception handler.
     409     * Note: The Win32 exception structure referenced by FS:[0] is the same in OS/2
     410     */
     411    if (pExceptionRegRec)
     412        OS2SetExceptionHandler(pExceptionRegRec);
     413    if (    pTeb
     414        &&  !pTeb->o.odin.exceptFrame)  /* if allready present, we'll keep the first one. */
     415        pTeb->o.odin.exceptFrame = (ULONG)pExceptionRegRec;
     416
     417    /*
     418     * Switch Selector if TIB was created.
     419     */
     420    if (pTeb)
     421        selFSOld = SetWin32TIB(fForceFSSwitch ? TIB_SWITCH_FORCE_WIN32 : TIB_SWITCH_DEFAULT);
     422
     423    return selFSOld;
     424}
     425
     426
     427/**
     428 * Leave odin context with this thread.
     429 *
     430 * Is called when an OS/2 process is returning from an Odin32 DLL.
     431 * This may be called also in a nested fashion and supports that.
     432 * The conterpart of ODIN_ThreadEnterOdinContext().
     433 *
     434 * @returns The old FS selector.
     435 * @returns 0 if TEB creation failed.
     436 * @param   pExceptionRegRec    OS/2 Exception Registration Record (2 ULONGs)
     437 *                              must be located on the callers stack.
     438 * @param   fForceFSSwitch      If set we will force switching to Odin32 FS selector.
     439 *                              If clear it depends on defaults.
     440 */
     441void   WIN32API ODIN_ThreadLeaveOdinContext(void *pExceptionRegRec, USHORT selFSOld)
     442{
     443    /*
     444     * Install the Odin32 exception handler.
     445     * Note: The Win32 exception structure referenced by FS:[0] is the same in OS/2
     446     */
     447    if (pExceptionRegRec)
     448        OS2UnsetExceptionHandler(pExceptionRegRec);
     449    TEB *pTeb = GetThreadTEB();
     450    if (    pTeb
     451        &&  pTeb->o.odin.exceptFrame == (ULONG)pExceptionRegRec)
     452        pTeb->o.odin.exceptFrame = 0;
     453
     454    /*
     455     * Switch Back FS Selector.
     456     */
     457    if (selFSOld)
     458        SetFS(selFSOld);
     459}
     460
     461
     462/**
     463 * Leave odin context to call back into OS/2 code.
     464 *
     465 * Is called when and Odin32 Dll/Exe calls back into the OS/2 code.
     466 * The conterpart of ODIN_ThreadEnterOdinContextNested().
     467 *
     468 * @returns The old FS selector.
     469 * @returns 0 on failure.
     470 * @param   pExceptionRegRec    New OS/2 exception handler frame which are to be registered
     471 *                              before the Odin handler in the chain.
     472 *                              Must be located on the callers stack.
     473 * @param   fRemoveOdinExcpt    Remove the odin exception handler.
     474 */
     475USHORT WIN32API ODIN_ThreadLeaveOdinContextNested(void *pExceptionRegRec, BOOL fRemoveOdinExcpt)
     476{
     477    /*
     478     * Set OS/2 FS Selector.
     479     */
     480    USHORT  selFSOld = RestoreOS2FS();
     481
     482    /*
     483     * Remove the Odin exception handler (if requested).
     484     */
     485    if (fRemoveOdinExcpt)
     486    {
     487        TEB *pTeb = GetThreadTEB();
     488        if (pTeb)
     489            OS2UnsetExceptionHandler((void*)pTeb->o.odin.exceptFrame);
     490        /* else: no TEB created propbably no exception handler to remove. */
     491    }
     492
     493    /*
     494     * Change exception handler if required.
     495     */
     496    if (pExceptionRegRec)
     497    {
     498        extern unsigned long _System DosSetExceptionHandler(void *);
     499        DosSetExceptionHandler(pExceptionRegRec);
     500    }
     501
     502    return selFSOld;
     503}
     504
     505
     506/**
     507 * Re-enter Odin context after being back in OS/2 code.
     508 *
     509 * Is called when returning to Odin from OS/2 code.
     510 * The conterpart of ODIN_ThreadLeaveOdinContextNested().
     511 *
     512 * @param   pExceptionRegRec    The exception registration record for the OS/2
     513 *                              exception handler used with Nested Leave. NULL
     514 *                              if not used.
     515 * @param   fRestoreOdinExcpt   Restore the Odin exception handler.
     516 *                              This flag must not be set unless fRemoveOdinExcpt
     517 *                              was set when leaving the Odin context!
     518 * @param   selFSOld            The Odin FS selector returned by the Nested Leave api.
     519 *
     520 */
     521void   WIN32API ODIN_ThreadEnterOdinContextNested(void *pExceptionRegRec, BOOL fRestoreOdinExcpt, USHORT selFSOld)
     522{
     523    /*
     524     * Remove the exception handler registered in ODIN_ThreadLeaveOdinContextNested
     525     */
     526    if (pExceptionRegRec)
     527        OS2UnsetExceptionHandler(pExceptionRegRec);
     528
     529    /*
     530     * Restore Odin exception handler (if requested).
     531     */
     532    if (fRestoreOdinExcpt)
     533    {
     534        TEB *pTeb = GetThreadTEB();
     535        if (pTeb)
     536            OS2SetExceptionHandler((void*)pTeb->o.odin.exceptFrame);
     537    }
     538
     539    /*
     540     * Switch Back FS Selector.
     541     */
     542    if (selFSOld)
     543        SetFS(selFSOld);
     544}
     545
     546
Note: See TracChangeset for help on using the changeset viewer.