[9945] | 1 | /* $Id: oslibthread.cpp,v 1.1 2003-03-27 14:00:53 sandervl Exp $ */
|
---|
| 2 | /*
|
---|
| 3 | * Wrappers for OS/2 Dos* API (Thread)
|
---|
| 4 | *
|
---|
| 5 | * Copyright 1998-2002 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
| 6 | * Copyright 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
| 7 | *
|
---|
| 8 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 9 | *
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | /*******************************************************************************
|
---|
| 15 | * Header Files *
|
---|
| 16 | *******************************************************************************/
|
---|
| 17 | #define INCL_BASE
|
---|
| 18 | #define INCL_DOSEXCEPTIONS
|
---|
| 19 | #define INCL_DOSMEMMGR
|
---|
| 20 | #define INCL_DOSPROCESS
|
---|
| 21 | #define INCL_DOSFILEMGR
|
---|
| 22 | #define INCL_DOSERRORS
|
---|
| 23 | #define INCL_DOSDEVIOCTL
|
---|
| 24 | #define INCL_DOSDEVICES
|
---|
| 25 | #define INCL_NPIPES
|
---|
| 26 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
| 27 | #include <stdlib.h>
|
---|
| 28 | #include <stdio.h>
|
---|
| 29 | #include <string.h>
|
---|
| 30 | #include <ctype.h>
|
---|
| 31 | #include <win32api.h>
|
---|
| 32 | #include <winconst.h>
|
---|
| 33 | #include <dbglog.h>
|
---|
| 34 | #include "oslibdos.h"
|
---|
| 35 | #include "oslibthread.h"
|
---|
| 36 | #include "exceptions.h"
|
---|
| 37 |
|
---|
| 38 | #define DBG_LOCALLOG DBG_oslibthread
|
---|
| 39 | #include "dbglocal.h"
|
---|
| 40 |
|
---|
| 41 | //******************************************************************************
|
---|
| 42 | //******************************************************************************
|
---|
| 43 | DWORD OSLibDosSetPriority(ULONG tid, int priority)
|
---|
| 44 | {
|
---|
| 45 | DWORD ret, os2priorityclass;
|
---|
| 46 | LONG os2prioritydelta;
|
---|
| 47 | APIRET rc;
|
---|
| 48 |
|
---|
| 49 | switch(priority)
|
---|
| 50 | {
|
---|
| 51 | case THREAD_PRIORITY_IDLE_W:
|
---|
| 52 | os2priorityclass = PRTYC_IDLETIME;
|
---|
| 53 | os2prioritydelta = 0;
|
---|
| 54 | break;
|
---|
| 55 | case THREAD_PRIORITY_LOWEST_W:
|
---|
| 56 | os2priorityclass = PRTYC_REGULAR;
|
---|
| 57 | os2prioritydelta = PRTYD_MINIMUM;
|
---|
| 58 | break;
|
---|
| 59 | case THREAD_PRIORITY_BELOW_NORMAL_W:
|
---|
| 60 | os2priorityclass = PRTYC_REGULAR;
|
---|
| 61 | os2prioritydelta = -15;
|
---|
| 62 | break;
|
---|
| 63 | case THREAD_PRIORITY_NORMAL_W:
|
---|
| 64 | os2priorityclass = PRTYC_REGULAR;
|
---|
| 65 | os2prioritydelta = 0;
|
---|
| 66 | break;
|
---|
| 67 | case THREAD_PRIORITY_ABOVE_NORMAL_W:
|
---|
| 68 | os2priorityclass = PRTYC_REGULAR;
|
---|
| 69 | os2prioritydelta = 15;
|
---|
| 70 | break;
|
---|
| 71 | case THREAD_PRIORITY_HIGHEST_W:
|
---|
| 72 | os2priorityclass = PRTYC_REGULAR;
|
---|
| 73 | os2prioritydelta = PRTYD_MAXIMUM;
|
---|
| 74 | break;
|
---|
| 75 | case THREAD_PRIORITY_TIME_CRITICAL_W:
|
---|
| 76 | //NOTE: There is code relying on the fact that win32 threads can never
|
---|
| 77 | // have the maximum priority available in OS/2!!
|
---|
| 78 | // (e.g. SetThreadContext)
|
---|
| 79 | dprintf(("PRTYC_TIMECRITICAL!!"));
|
---|
| 80 | os2priorityclass = PRTYC_TIMECRITICAL;
|
---|
| 81 | os2prioritydelta = 0;
|
---|
| 82 | break;
|
---|
| 83 | default:
|
---|
| 84 | dprintf(("!WARNING!: Invalid priority!!"));
|
---|
| 85 | SetLastError(ERROR_INVALID_PARAMETER_W);
|
---|
| 86 | return ERROR_INVALID_PARAMETER_W;
|
---|
| 87 | }
|
---|
| 88 | rc = DosSetPriority(PRTYS_THREAD, os2priorityclass, os2prioritydelta, tid);
|
---|
| 89 | ret = error2WinError(rc, ERROR_INVALID_PARAMETER);
|
---|
| 90 | SetLastError(ret);
|
---|
| 91 | return ret;
|
---|
| 92 | }
|
---|
| 93 | //******************************************************************************
|
---|
| 94 | //Useful to temporarily boost the priority to max to ensure it's scheduled first
|
---|
| 95 | //Windows threads never receive such a high priority (time critical, delta 0 = max)
|
---|
| 96 | //******************************************************************************
|
---|
| 97 | void OSLibDosSetMaxPriority(ULONG tid)
|
---|
| 98 | {
|
---|
| 99 | APIRET rc;
|
---|
| 100 |
|
---|
| 101 | dprintf(("OSLibDosSetMaxPriority for %x", tid));
|
---|
| 102 | rc = DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, tid);
|
---|
| 103 | if(rc != NO_ERROR) {
|
---|
| 104 | DebugInt3();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | //******************************************************************************
|
---|
| 108 | //******************************************************************************
|
---|
| 109 | void OSLibDosExitThread(ULONG retcode)
|
---|
| 110 | {
|
---|
| 111 | DosExit(EXIT_THREAD, retcode);
|
---|
| 112 | }
|
---|
| 113 | //******************************************************************************
|
---|
| 114 | //******************************************************************************
|
---|
| 115 | BOOL OSLibQueryThreadContext(ULONG ulThreadId, ULONG teb_sel, CONTEXT *lpContext)
|
---|
| 116 | {
|
---|
| 117 | CONTEXTRECORD ctxrec;
|
---|
| 118 | APIRET rc;
|
---|
| 119 | int nrtries = 0;
|
---|
| 120 |
|
---|
| 121 | tryagain:
|
---|
| 122 | rc = DosQueryThreadContext(ulThreadId, CONTEXT_FULL, &ctxrec);
|
---|
| 123 | if(rc != NO_ERROR) {
|
---|
| 124 | dprintf(("ERROR: DosQueryThreadContext failed with rc %d!!", rc));
|
---|
[21302] | 125 | //testestest
|
---|
| 126 | if((rc == ERROR_NOT_FROZEN || rc == ERROR_INVALID_THREADID) && ++nrtries < 5) {
|
---|
[9945] | 127 | DosSleep(50);
|
---|
| 128 | goto tryagain;
|
---|
| 129 | }
|
---|
[21302] | 130 | DebugInt3();
|
---|
[9945] | 131 | SetLastError(error2WinError(rc, ERROR_INVALID_PARAMETER));
|
---|
| 132 | return FALSE;
|
---|
| 133 | }
|
---|
| 134 | if(lpContext->ContextFlags & WINCONTEXT_CONTROL) {
|
---|
| 135 | lpContext->Ebp = ctxrec.ctx_RegEbp;
|
---|
| 136 | lpContext->Eip = ctxrec.ctx_RegEip;
|
---|
| 137 | lpContext->SegCs = ctxrec.ctx_SegCs;
|
---|
| 138 | lpContext->EFlags = ctxrec.ctx_EFlags;
|
---|
| 139 | lpContext->Esp = ctxrec.ctx_RegEsp;
|
---|
| 140 | lpContext->SegSs = ctxrec.ctx_SegSs;
|
---|
| 141 | }
|
---|
| 142 | if(lpContext->ContextFlags & WINCONTEXT_INTEGER) {
|
---|
| 143 | lpContext->Edi = ctxrec.ctx_RegEdi;
|
---|
| 144 | lpContext->Esi = ctxrec.ctx_RegEsi;
|
---|
| 145 | lpContext->Ebx = ctxrec.ctx_RegEbx;
|
---|
| 146 | lpContext->Edx = ctxrec.ctx_RegEdx;
|
---|
| 147 | lpContext->Ecx = ctxrec.ctx_RegEcx;
|
---|
| 148 | lpContext->Eax = ctxrec.ctx_RegEax;
|
---|
| 149 | }
|
---|
| 150 | if(lpContext->ContextFlags & WINCONTEXT_SEGMENTS) {
|
---|
| 151 | lpContext->SegGs = ctxrec.ctx_SegGs;
|
---|
| 152 | // This resets FS to 0x150B - we DON'T want that!!
|
---|
| 153 | // lpContext->SegFs = ctxrec.ctx_SegFs;
|
---|
| 154 | lpContext->SegFs = teb_sel;
|
---|
| 155 | lpContext->SegEs = ctxrec.ctx_SegEs;
|
---|
| 156 | lpContext->SegDs = ctxrec.ctx_SegDs;
|
---|
| 157 | }
|
---|
| 158 | if(lpContext->ContextFlags & WINCONTEXT_FLOATING_POINT) {
|
---|
| 159 | //TODO: First 7 dwords the same?
|
---|
| 160 | memcpy(&lpContext->FloatSave, ctxrec.ctx_env, sizeof(ctxrec.ctx_env));
|
---|
| 161 | memcpy(&lpContext->FloatSave.RegisterArea, ctxrec.ctx_stack, sizeof(ctxrec.ctx_stack));
|
---|
| 162 | }
|
---|
| 163 | SetLastError(ERROR_SUCCESS_W);
|
---|
| 164 | return TRUE;
|
---|
| 165 | }
|
---|
| 166 | //******************************************************************************
|
---|
| 167 | //******************************************************************************
|
---|
| 168 | void OSLibDisableThreadSwitching()
|
---|
| 169 | {
|
---|
| 170 | DosEnterCritSec();
|
---|
| 171 | }
|
---|
| 172 | //******************************************************************************
|
---|
| 173 | //******************************************************************************
|
---|
| 174 | void OSLibEnableThreadSwitching()
|
---|
| 175 | {
|
---|
| 176 | DosExitCritSec();
|
---|
| 177 | }
|
---|
| 178 | //******************************************************************************
|
---|
| 179 | //******************************************************************************
|
---|