Documentation ordinal fixes.
[wine.git] / loader / task.c
blob09213ddda7083bdd16d4ccba7b720e1793ca6720
1 /*
2 * Task functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <unistd.h>
12 #include "wine/port.h"
13 #include "wine/winbase16.h"
14 #include "ntddk.h"
15 #include "callback.h"
16 #include "drive.h"
17 #include "file.h"
18 #include "global.h"
19 #include "instance.h"
20 #include "miscemu.h"
21 #include "module.h"
22 #include "queue.h"
23 #include "selectors.h"
24 #include "stackframe.h"
25 #include "task.h"
26 #include "thread.h"
27 #include "toolhelp.h"
28 #include "winnt.h"
29 #include "syslevel.h"
30 #include "winsock.h"
31 #include "debugtools.h"
32 #include "services.h"
33 #include "server.h"
36 DEFAULT_DEBUG_CHANNEL(task);
37 DECLARE_DEBUG_CHANNEL(relay);
38 DECLARE_DEBUG_CHANNEL(toolhelp);
40 /* Min. number of thunks allocated when creating a new segment */
41 #define MIN_THUNKS 32
44 static THHOOK DefaultThhook;
45 THHOOK *pThhook = &DefaultThhook;
47 #define hCurrentTask (pThhook->CurTDB)
48 #define hFirstTask (pThhook->HeadTDB)
49 #define hLockedTask (pThhook->LockTDB)
51 static UINT16 nTaskCount = 0;
53 static HTASK initial_task;
55 /***********************************************************************
56 * TASK_InstallTHHook
58 void TASK_InstallTHHook( THHOOK *pNewThhook )
60 THHOOK *pOldThhook = pThhook;
62 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
64 *pThhook = *pOldThhook;
67 /***********************************************************************
68 * TASK_GetNextTask
70 HTASK16 TASK_GetNextTask( HTASK16 hTask )
72 TDB* pTask = (TDB*)GlobalLock16(hTask);
74 if (pTask->hNext) return pTask->hNext;
75 return (hFirstTask != hTask) ? hFirstTask : 0;
78 /***********************************************************************
79 * TASK_LinkTask
81 static void TASK_LinkTask( HTASK16 hTask )
83 HTASK16 *prevTask;
84 TDB *pTask;
86 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
87 prevTask = &hFirstTask;
88 while (*prevTask)
90 TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
91 if (prevTaskPtr->priority >= pTask->priority) break;
92 prevTask = &prevTaskPtr->hNext;
94 pTask->hNext = *prevTask;
95 *prevTask = hTask;
96 nTaskCount++;
100 /***********************************************************************
101 * TASK_UnlinkTask
103 static void TASK_UnlinkTask( HTASK16 hTask )
105 HTASK16 *prevTask;
106 TDB *pTask;
108 prevTask = &hFirstTask;
109 while (*prevTask && (*prevTask != hTask))
111 pTask = (TDB *)GlobalLock16( *prevTask );
112 prevTask = &pTask->hNext;
114 if (*prevTask)
116 pTask = (TDB *)GlobalLock16( *prevTask );
117 *prevTask = pTask->hNext;
118 pTask->hNext = 0;
119 nTaskCount--;
124 /***********************************************************************
125 * TASK_CreateThunks
127 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
128 * and containing 'count' entries.
130 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
132 int i;
133 WORD free;
134 THUNKS *pThunk;
136 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
137 pThunk->next = 0;
138 pThunk->magic = THUNK_MAGIC;
139 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
140 free = pThunk->free;
141 for (i = 0; i < count-1; i++)
143 free += 8; /* Offset of next thunk */
144 pThunk->thunks[4*i] = free;
146 pThunk->thunks[4*i] = 0; /* Last thunk */
150 /***********************************************************************
151 * TASK_AllocThunk
153 * Allocate a thunk for MakeProcInstance().
155 static SEGPTR TASK_AllocThunk( HTASK16 hTask )
157 TDB *pTask;
158 THUNKS *pThunk;
159 WORD sel, base;
161 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
162 sel = pTask->hCSAlias;
163 pThunk = &pTask->thunks;
164 base = (int)pThunk - (int)pTask;
165 while (!pThunk->free)
167 sel = pThunk->next;
168 if (!sel) /* Allocate a new segment */
170 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
171 pTask->hPDB, WINE_LDT_FLAGS_CODE );
172 if (!sel) return (SEGPTR)0;
173 TASK_CreateThunks( sel, 0, MIN_THUNKS );
174 pThunk->next = sel;
176 pThunk = (THUNKS *)GlobalLock16( sel );
177 base = 0;
179 base += pThunk->free;
180 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
181 return MAKESEGPTR( sel, base );
185 /***********************************************************************
186 * TASK_FreeThunk
188 * Free a MakeProcInstance() thunk.
190 static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
192 TDB *pTask;
193 THUNKS *pThunk;
194 WORD sel, base;
196 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
197 sel = pTask->hCSAlias;
198 pThunk = &pTask->thunks;
199 base = (int)pThunk - (int)pTask;
200 while (sel && (sel != HIWORD(thunk)))
202 sel = pThunk->next;
203 pThunk = (THUNKS *)GlobalLock16( sel );
204 base = 0;
206 if (!sel) return FALSE;
207 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
208 pThunk->free = LOWORD(thunk) - base;
209 return TRUE;
213 /***********************************************************************
214 * TASK_Create
216 * NOTE: This routine might be called by a Win32 thread. Thus, we need
217 * to be careful to protect global data structures. We do this
218 * by entering the Win16Lock while linking the task into the
219 * global task list.
221 BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, TEB *teb, LPCSTR cmdline, BYTE len )
223 HTASK16 hTask;
224 TDB *pTask;
225 char name[10];
227 /* Allocate the task structure */
229 hTask = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
230 if (!hTask) return FALSE;
231 pTask = (TDB *)GlobalLock16( hTask );
232 FarSetOwner16( hTask, pModule->self );
234 /* Fill the task structure */
236 pTask->hSelf = hTask;
238 if (teb->tibflags & TEBF_WIN32)
240 pTask->flags |= TDBF_WIN32;
241 pTask->hInstance = pModule->self;
242 pTask->hPrevInstance = 0;
243 /* NOTE: for 16-bit tasks, the instance handles are updated later on
244 in NE_InitProcess */
247 pTask->version = pModule->expected_version;
248 pTask->hModule = pModule->self;
249 pTask->hParent = GetCurrentTask();
250 pTask->magic = TDB_MAGIC;
251 pTask->nCmdShow = cmdShow;
252 pTask->teb = teb;
253 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
254 strcpy( pTask->curdir, "\\" );
255 lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
256 sizeof(pTask->curdir) - 1 );
258 /* Create the thunks block */
260 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
262 /* Copy the module name */
264 GetModuleName16( pModule->self, name, sizeof(name) );
265 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
267 /* Allocate a selector for the PDB */
269 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
270 pModule->self, WINE_LDT_FLAGS_DATA );
272 /* Fill the PDB */
274 pTask->pdb.int20 = 0x20cd;
275 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
276 PUT_UA_DWORD(&pTask->pdb.dispatcher[1],
277 (DWORD)GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" ));
278 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
279 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
280 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
281 pTask->pdb.fileHandlesPtr =
282 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
283 pTask->pdb.hFileHandles = 0;
284 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
285 /* FIXME: should we make a copy of the environment? */
286 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
287 pTask->pdb.nbFiles = 20;
289 /* Fill the command line */
291 if (!cmdline)
293 cmdline = GetCommandLineA();
294 /* remove the first word (program name) */
295 if (*cmdline == '"')
296 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
297 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
298 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
299 len = strlen(cmdline);
301 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
302 pTask->pdb.cmdLine[0] = len;
303 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
304 /* pTask->pdb.cmdLine[len+1] = 0; */
306 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name, len, cmdline, hTask );
308 /* Get the compatibility flags */
310 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
312 /* Allocate a code segment alias for the TDB */
314 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
315 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
317 /* Default DTA overwrites command line */
319 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
321 /* Create scheduler event for 16-bit tasks */
323 if ( !(pTask->flags & TDBF_WIN32) )
324 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
326 /* Enter task handle into thread and process */
328 teb->htask16 = hTask;
329 if (!initial_task) initial_task = hTask;
331 /* Add the task to the linked list */
333 _EnterWin16Lock();
334 TASK_LinkTask( hTask );
335 _LeaveWin16Lock();
337 return TRUE;
340 /***********************************************************************
341 * TASK_DeleteTask
343 static void TASK_DeleteTask( HTASK16 hTask )
345 TDB *pTask;
346 HGLOBAL16 hPDB;
348 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
349 hPDB = pTask->hPDB;
351 pTask->magic = 0xdead; /* invalidate signature */
353 /* Free the selector aliases */
355 GLOBAL_FreeBlock( pTask->hCSAlias );
356 GLOBAL_FreeBlock( pTask->hPDB );
358 /* Free the task module */
360 FreeModule16( pTask->hModule );
362 /* Free the task structure itself */
364 GlobalFree16( hTask );
366 /* Free all memory used by this task (including the 32-bit stack, */
367 /* the environment block and the thunk segments). */
369 GlobalFreeAll16( hPDB );
372 /***********************************************************************
373 * TASK_KillTask
375 void TASK_ExitTask(void)
377 TDB *pTask;
378 DWORD lockCount;
380 /* Enter the Win16Lock to protect global data structures */
381 _EnterWin16Lock();
383 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
384 if ( !pTask )
386 _LeaveWin16Lock();
387 return;
390 TRACE("Killing task %04x\n", pTask->hSelf );
392 /* Perform USER cleanup */
394 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
395 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
396 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
397 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
399 /* Remove the task from the list to be sure we never switch back to it */
400 TASK_UnlinkTask( pTask->hSelf );
402 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
404 TRACE("this is the last task, exiting\n" );
405 ExitKernel16();
408 if( nTaskCount )
410 TDB* p = (TDB *)GlobalLock16( hFirstTask );
411 while( p )
413 if( p->hYieldTo == pTask->hSelf ) p->hYieldTo = 0;
414 p = (TDB *)GlobalLock16( p->hNext );
418 pTask->nEvents = 0;
420 if ( hLockedTask == pTask->hSelf )
421 hLockedTask = 0;
423 TASK_DeleteTask( pTask->hSelf );
425 /* ... schedule another one ... */
426 TASK_Reschedule();
428 /* ... and completely release the Win16Lock, just in case. */
429 ReleaseThunkLock( &lockCount );
432 /***********************************************************************
433 * TASK_Reschedule
435 * This is where all the magic of task-switching happens!
437 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
438 * This means that each 16-bit task runs until it voluntarily yields
439 * control, at which point the scheduler gets active and selects the
440 * next task to run.
442 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
443 * by the standard scheduler of the underlying OS. As many 16-bit apps
444 * *rely* on the behaviour of the Windows scheduler, however, we have
445 * to simulate that behaviour.
447 * This is achieved as follows: every 16-bit task is at time (except
448 * during task creation and deletion) in one of two states: either it
449 * is the one currently running, then the global variable hCurrentTask
450 * contains its task handle, or it is not currently running, then it
451 * is blocked on a special scheduler event, a global handle which
452 * is stored in the task struct.
454 * When the current task yields control, this routine gets called. Its
455 * purpose is to determine the next task to be active, signal the
456 * scheduler event of that task, and then put the current task to sleep
457 * waiting for *its* scheduler event to get signalled again.
459 * This routine can get called in a few other special situations as well:
461 * - On creation of a 16-bit task, the Unix process executing the task
462 * calls TASK_Reschedule once it has completed its initialization.
463 * At this point, the task needs to be blocked until its scheduler
464 * event is signalled the first time (this will be done by the parent
465 * process to get the task up and running).
467 * - When the task currently running terminates itself, this routine gets
468 * called and has to schedule another task, *without* blocking the
469 * terminating task.
471 * - When a 32-bit thread posts an event for a 16-bit task, it might be
472 * the case that *no* 16-bit task is currently running. In this case
473 * the task that has now an event pending is to be scheduled.
476 void TASK_Reschedule(void)
478 TDB *pOldTask = NULL, *pNewTask = NULL;
479 HTASK16 hOldTask = 0, hNewTask = 0;
480 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
481 DWORD lockCount;
483 _EnterWin16Lock();
485 /* Check what we need to do */
486 hOldTask = GetCurrentTask();
487 pOldTask = (TDB *)GlobalLock16( hOldTask );
488 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
489 hCurrentTask, hOldTask, (long) getpid() );
491 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
493 /* We are called by an active (non-deleted) 16-bit task */
495 /* If we don't even have a current task, or else the current
496 task has yielded, we'll need to schedule a new task and
497 (possibly) put the calling task to sleep. Otherwise, we
498 only block the caller. */
500 if ( !hCurrentTask || hCurrentTask == hOldTask )
501 mode = MODE_YIELD;
502 else
503 mode = MODE_SLEEP;
505 else
507 /* We are called by a deleted 16-bit task or a 32-bit thread */
509 /* The only situation where we need to do something is if we
510 now do not have a current task. Then, we'll need to wake up
511 some task that has events pending. */
513 if ( !hCurrentTask || hCurrentTask == hOldTask )
514 mode = MODE_WAKEUP;
515 else
517 /* nothing to do */
518 _LeaveWin16Lock();
519 return;
523 /* Find a task to yield to: check for DirectedYield() */
524 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
526 hNewTask = pOldTask->hYieldTo;
527 pNewTask = (TDB *)GlobalLock16( hNewTask );
528 if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
529 pOldTask->hYieldTo = 0;
532 /* Find a task to yield to: check for pending events */
533 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
535 hNewTask = hFirstTask;
536 while (hNewTask)
538 pNewTask = (TDB *)GlobalLock16( hNewTask );
540 TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
542 if (pNewTask->nEvents) break;
543 hNewTask = pNewTask->hNext;
545 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
548 /* If we are still the task with highest priority, just return ... */
549 if ( mode == MODE_YIELD && hNewTask && hNewTask == hCurrentTask )
551 TRACE("returning to the current task (%04x)\n", hCurrentTask );
552 _LeaveWin16Lock();
554 /* Allow Win32 threads to thunk down even while a Win16 task is
555 in a tight PeekMessage() or Yield() loop ... */
556 ReleaseThunkLock( &lockCount );
557 RestoreThunkLock( lockCount );
558 return;
561 /* If no task to yield to found, suspend 16-bit scheduler ... */
562 if ( mode == MODE_YIELD && !hNewTask )
564 TRACE("No currently active task\n");
565 hCurrentTask = 0;
568 /* If we found a task to wake up, do it ... */
569 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
571 TRACE("Switching to task %04x (%.8s)\n",
572 hNewTask, pNewTask->module_name );
574 pNewTask->priority++;
575 TASK_UnlinkTask( hNewTask );
576 TASK_LinkTask( hNewTask );
577 pNewTask->priority--;
579 hCurrentTask = hNewTask;
580 NtSetEvent( pNewTask->hEvent, NULL );
582 /* This is set just in case some app reads it ... */
583 pNewTask->ss_sp = pNewTask->teb->cur_stack;
586 /* If we need to put the current task to sleep, do it ... */
587 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
589 NtResetEvent( pOldTask->hEvent, NULL );
591 ReleaseThunkLock( &lockCount );
592 SYSLEVEL_CheckNotLevel( 1 );
593 WaitForSingleObject( pOldTask->hEvent, INFINITE );
594 RestoreThunkLock( lockCount );
597 _LeaveWin16Lock();
600 /***********************************************************************
601 * InitTask (KERNEL.91)
603 * Called by the application startup code.
605 void WINAPI InitTask16( CONTEXT86 *context )
607 TDB *pTask;
608 INSTANCEDATA *pinstance;
609 SEGPTR ptr;
611 context->Eax = 0;
612 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
614 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
615 as some apps, notably Visual Basic apps, *modify* the heap/stack
616 size of the instance data segment before calling InitTask() */
618 /* Initialize the INSTANCEDATA structure */
619 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
620 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack ) + sizeof( STACK16FRAME );
621 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
622 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
623 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
625 /* Initialize the local heap */
626 if (LOWORD(context->Ecx))
627 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
629 /* Initialize implicitly loaded DLLs */
630 NE_InitializeDLLs( pTask->hModule );
631 NE_DllProcessAttach( pTask->hModule );
633 /* Registers on return are:
634 * ax 1 if OK, 0 on error
635 * cx stack limit in bytes
636 * dx cmdShow parameter
637 * si instance handle of the previous instance
638 * di instance handle of the new task
639 * es:bx pointer to command line inside PSP
641 * 0 (=%bp) is pushed on the stack
643 ptr = stack16_push( sizeof(WORD) );
644 *(WORD *)MapSL(ptr) = 0;
645 context->Esp -= 2;
647 context->Eax = 1;
649 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
650 else
652 LPBYTE p = &pTask->pdb.cmdLine[1];
653 while ((*p == ' ') || (*p == '\t')) p++;
654 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
656 context->Ecx = pinstance->stacktop;
657 context->Edx = pTask->nCmdShow;
658 context->Esi = (DWORD)pTask->hPrevInstance;
659 context->Edi = (DWORD)pTask->hInstance;
660 context->SegEs = (WORD)pTask->hPDB;
664 /***********************************************************************
665 * WaitEvent (KERNEL.30)
667 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
669 TDB *pTask;
671 if (!hTask) hTask = GetCurrentTask();
672 pTask = (TDB *)GlobalLock16( hTask );
674 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
676 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
677 return TRUE;
680 if (pTask->nEvents > 0)
682 pTask->nEvents--;
683 return FALSE;
685 TASK_Reschedule();
687 /* When we get back here, we have an event */
689 if (pTask->nEvents > 0) pTask->nEvents--;
690 return TRUE;
694 /***********************************************************************
695 * PostEvent (KERNEL.31)
697 void WINAPI PostEvent16( HTASK16 hTask )
699 TDB *pTask;
701 if (!hTask) hTask = GetCurrentTask();
702 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
704 if ( !THREAD_IsWin16( pTask->teb ) )
706 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
707 return;
710 pTask->nEvents++;
712 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
713 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
714 TASK_Reschedule();
718 /***********************************************************************
719 * SetPriority (KERNEL.32)
721 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
723 TDB *pTask;
724 INT16 newpriority;
726 if (!hTask) hTask = GetCurrentTask();
727 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
728 newpriority = pTask->priority + delta;
729 if (newpriority < -32) newpriority = -32;
730 else if (newpriority > 15) newpriority = 15;
732 pTask->priority = newpriority + 1;
733 TASK_UnlinkTask( hTask );
734 TASK_LinkTask( hTask );
735 pTask->priority--;
739 /***********************************************************************
740 * LockCurrentTask (KERNEL.33)
742 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
744 if (bLock) hLockedTask = GetCurrentTask();
745 else hLockedTask = 0;
746 return hLockedTask;
750 /***********************************************************************
751 * IsTaskLocked (KERNEL.122)
753 HTASK16 WINAPI IsTaskLocked16(void)
755 return hLockedTask;
759 /***********************************************************************
760 * OldYield (KERNEL.117)
762 void WINAPI OldYield16(void)
764 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
766 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
768 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
769 return;
772 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
773 TASK_Reschedule();
774 if (pCurTask) pCurTask->nEvents--;
777 /***********************************************************************
778 * WIN32_OldYield16 (KERNEL.447)
780 void WINAPI WIN32_OldYield16(void)
782 DWORD count;
784 ReleaseThunkLock(&count);
785 RestoreThunkLock(count);
788 /***********************************************************************
789 * DirectedYield (KERNEL.150)
791 void WINAPI DirectedYield16( HTASK16 hTask )
793 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
795 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
797 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
798 return;
801 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
803 pCurTask->hYieldTo = hTask;
804 OldYield16();
806 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
809 /***********************************************************************
810 * Yield16 (KERNEL.29)
812 void WINAPI Yield16(void)
814 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
816 if (pCurTask) pCurTask->hYieldTo = 0;
817 if (pCurTask && pCurTask->hQueue && Callout.UserYield16) Callout.UserYield16();
818 else OldYield16();
821 /***********************************************************************
822 * KERNEL_490 (KERNEL.490)
824 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
826 if ( !someTask ) return 0;
828 FIXME("(%04x): stub\n", someTask );
829 return 0;
832 /***********************************************************************
833 * MakeProcInstance16 (KERNEL.51)
835 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
837 BYTE *thunk,*lfunc;
838 SEGPTR thunkaddr;
839 WORD hInstanceSelector;
841 hInstanceSelector = GlobalHandleToSel16(hInstance);
843 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
845 if (!HIWORD(func)) {
846 /* Win95 actually protects via SEH, but this is better for debugging */
847 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
848 return (FARPROC16)0;
851 if (hInstance)
853 if ( (!(hInstance & 4)) ||
854 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
856 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
857 hInstance);
858 return 0;
862 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
863 && (hInstance != 0)
864 && (hInstance != 0xffff) )
866 /* calling MPI with a foreign DSEG is invalid ! */
867 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
868 hInstance,CURRENT_DS);
871 /* Always use the DSEG that MPI was entered with.
872 * We used to set hInstance to GetTaskDS16(), but this should be wrong
873 * as CURRENT_DS provides the DSEG value we need.
874 * ("calling" DS, *not* "task" DS !) */
875 hInstanceSelector = CURRENT_DS;
876 hInstance = GlobalHandle16(hInstanceSelector);
878 /* no thunking for DLLs */
879 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
880 return func;
882 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
883 if (!thunkaddr) return (FARPROC16)0;
884 thunk = MapSL( thunkaddr );
885 lfunc = MapSL( (SEGPTR)func );
887 TRACE("(%08lx,%04x): got thunk %08lx\n",
888 (DWORD)func, hInstance, (DWORD)thunkaddr );
889 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
890 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
892 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
895 *thunk++ = 0xb8; /* movw instance, %ax */
896 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
897 *thunk++ = (BYTE)(hInstanceSelector >> 8);
898 *thunk++ = 0xea; /* ljmp func */
899 *(DWORD *)thunk = (DWORD)func;
900 return (FARPROC16)thunkaddr;
901 /* CX reg indicates if thunkaddr != NULL, implement if needed */
905 /***********************************************************************
906 * FreeProcInstance16 (KERNEL.52)
908 void WINAPI FreeProcInstance16( FARPROC16 func )
910 TRACE("(%08lx)\n", (DWORD)func );
911 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
914 /**********************************************************************
915 * TASK_GetCodeSegment
917 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
918 * and logical segment number of a given code segment.
920 * 'proc' either *is* already a pair of module handle and segment number,
921 * in which case there's nothing to do. Otherwise, it is a pointer to
922 * a function, and we need to retrieve the code segment. If the pointer
923 * happens to point to a thunk, we'll retrieve info about the code segment
924 * where the function pointed to by the thunk resides, not the thunk itself.
926 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
927 * the function the snoop code will return to ...
930 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
931 SEGTABLEENTRY **ppSeg, int *pSegNr )
933 NE_MODULE *pModule = NULL;
934 SEGTABLEENTRY *pSeg = NULL;
935 int segNr=0;
937 /* Try pair of module handle / segment number */
938 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
939 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
941 segNr = LOWORD( proc );
942 if ( segNr && segNr <= pModule->seg_count )
943 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
946 /* Try thunk or function */
947 else
949 BYTE *thunk = MapSL( (SEGPTR)proc );
950 WORD selector;
952 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
953 selector = thunk[6] + (thunk[7] << 8);
954 else
955 selector = HIWORD( proc );
957 pModule = NE_GetPtr( GlobalHandle16( selector ) );
958 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
960 if ( pModule )
961 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
962 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
963 break;
965 if ( pModule && segNr > pModule->seg_count )
966 pSeg = NULL;
969 /* Abort if segment not found */
971 if ( !pModule || !pSeg )
972 return FALSE;
974 /* Return segment data */
976 if ( ppModule ) *ppModule = pModule;
977 if ( ppSeg ) *ppSeg = pSeg;
978 if ( pSegNr ) *pSegNr = segNr;
980 return TRUE;
983 /**********************************************************************
984 * GetCodeHandle (KERNEL.93)
986 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
988 SEGTABLEENTRY *pSeg;
990 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
991 return (HANDLE16)0;
993 return pSeg->hSeg;
996 /**********************************************************************
997 * GetCodeInfo (KERNEL.104)
999 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1001 NE_MODULE *pModule;
1002 SEGTABLEENTRY *pSeg;
1003 int segNr;
1005 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1006 return FALSE;
1008 /* Fill in segment information */
1010 segInfo->offSegment = pSeg->filepos;
1011 segInfo->cbSegment = pSeg->size;
1012 segInfo->flags = pSeg->flags;
1013 segInfo->cbAlloc = pSeg->minsize;
1014 segInfo->h = pSeg->hSeg;
1015 segInfo->alignShift = pModule->alignment;
1017 if ( segNr == pModule->dgroup )
1018 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
1020 /* Return module handle in %es */
1022 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1024 return TRUE;
1028 /**********************************************************************
1029 * DefineHandleTable16 (KERNEL.94)
1031 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1033 FIXME("(%04x): stub ?\n", wOffset);
1034 return TRUE;
1038 /***********************************************************************
1039 * SetTaskQueue (KERNEL.34)
1041 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1043 HQUEUE16 hPrev;
1044 TDB *pTask;
1046 if (!hTask) hTask = GetCurrentTask();
1047 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1049 hPrev = pTask->hQueue;
1050 pTask->hQueue = hQueue;
1052 return hPrev;
1056 /***********************************************************************
1057 * GetTaskQueue (KERNEL.35)
1059 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1061 TDB *pTask;
1063 if (!hTask) hTask = GetCurrentTask();
1064 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1065 return pTask->hQueue;
1068 /***********************************************************************
1069 * SetThreadQueue (KERNEL.463)
1071 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1073 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1074 HQUEUE16 oldQueue = teb? teb->queue : 0;
1076 if ( teb )
1078 teb->queue = hQueue;
1080 if ( GetTaskQueue16( teb->htask16 ) == oldQueue )
1081 SetTaskQueue16( teb->htask16, hQueue );
1084 return oldQueue;
1087 /***********************************************************************
1088 * GetThreadQueue (KERNEL.464)
1090 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1092 TEB *teb = NULL;
1093 if ( !thread )
1094 teb = NtCurrentTeb();
1095 else if ( HIWORD(thread) )
1096 teb = THREAD_IdToTEB( thread );
1097 else if ( IsTask16( (HTASK16)thread ) )
1098 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1100 return (HQUEUE16)(teb? teb->queue : 0);
1103 /***********************************************************************
1104 * SetFastQueue (KERNEL.624)
1106 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1108 TEB *teb = NULL;
1109 if ( !thread )
1110 teb = NtCurrentTeb();
1111 else if ( HIWORD(thread) )
1112 teb = THREAD_IdToTEB( thread );
1113 else if ( IsTask16( (HTASK16)thread ) )
1114 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1116 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1119 /***********************************************************************
1120 * GetFastQueue (KERNEL.625)
1122 HANDLE WINAPI GetFastQueue16( void )
1124 TEB *teb = NtCurrentTeb();
1125 if (!teb) return 0;
1127 if (!teb->queue && Callout.InitThreadInput16)
1128 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
1130 if (!teb->queue)
1131 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1133 return (HANDLE)teb->queue;
1136 /***********************************************************************
1137 * SwitchStackTo (KERNEL.108)
1139 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1141 TDB *pTask;
1142 STACK16FRAME *oldFrame, *newFrame;
1143 INSTANCEDATA *pData;
1144 UINT16 copySize;
1146 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1147 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1148 TRACE("old=%04x:%04x new=%04x:%04x\n",
1149 SELECTOROF( pTask->teb->cur_stack ),
1150 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1152 /* Save the old stack */
1154 oldFrame = THREAD_STACK16( pTask->teb );
1155 /* pop frame + args and push bp */
1156 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1157 + 2 * sizeof(WORD);
1158 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1159 pData->stacktop = top;
1160 pData->stackmin = ptr;
1161 pData->stackbottom = ptr;
1163 /* Switch to the new stack */
1165 /* Note: we need to take the 3 arguments into account; otherwise,
1166 * the stack will underflow upon return from this function.
1168 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1169 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1170 pTask->teb->cur_stack = MAKESEGPTR( seg, ptr - copySize );
1171 newFrame = THREAD_STACK16( pTask->teb );
1173 /* Copy the stack frame and the local variables to the new stack */
1175 memmove( newFrame, oldFrame, copySize );
1176 newFrame->bp = ptr;
1177 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1181 /***********************************************************************
1182 * SwitchStackBack (KERNEL.109)
1184 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1186 STACK16FRAME *oldFrame, *newFrame;
1187 INSTANCEDATA *pData;
1189 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1190 return;
1191 if (!pData->old_ss_sp)
1193 WARN("No previous SwitchStackTo\n" );
1194 return;
1196 TRACE("restoring stack %04x:%04x\n",
1197 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1199 oldFrame = CURRENT_STACK16;
1201 /* Pop bp from the previous stack */
1203 BP_reg(context) = *(WORD *)MapSL(pData->old_ss_sp);
1204 pData->old_ss_sp += sizeof(WORD);
1206 /* Switch back to the old stack */
1208 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1209 context->SegSs = SELECTOROF(pData->old_ss_sp);
1210 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1211 pData->old_ss_sp = 0;
1213 /* Build a stack frame for the return */
1215 newFrame = CURRENT_STACK16;
1216 newFrame->frame32 = oldFrame->frame32;
1217 newFrame->module_cs = oldFrame->module_cs;
1218 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1219 newFrame->entry_ip = oldFrame->entry_ip;
1223 /***********************************************************************
1224 * GetTaskQueueDS16 (KERNEL.118)
1226 void WINAPI GetTaskQueueDS16(void)
1228 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1232 /***********************************************************************
1233 * GetTaskQueueES16 (KERNEL.119)
1235 void WINAPI GetTaskQueueES16(void)
1237 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1241 /***********************************************************************
1242 * GetCurrentTask (KERNEL.36)
1244 HTASK16 WINAPI GetCurrentTask(void)
1246 return NtCurrentTeb()->htask16;
1249 DWORD WINAPI WIN16_GetCurrentTask(void)
1251 /* This is the version used by relay code; the first task is */
1252 /* returned in the high word of the result */
1253 return MAKELONG( GetCurrentTask(), hFirstTask );
1257 /***********************************************************************
1258 * GetCurrentPDB (KERNEL.37)
1260 * UNDOC: returns PSP of KERNEL in high word
1262 DWORD WINAPI GetCurrentPDB16(void)
1264 TDB *pTask;
1266 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1267 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1271 /***********************************************************************
1272 * GetCurPID16 (KERNEL.157)
1274 DWORD WINAPI GetCurPID16( DWORD unused )
1276 return 0;
1280 /***********************************************************************
1281 * GetInstanceData (KERNEL.54)
1283 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1285 char *ptr = (char *)GlobalLock16( instance );
1286 if (!ptr || !len) return 0;
1287 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1288 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1289 return len;
1293 /***********************************************************************
1294 * GetExeVersion (KERNEL.105)
1296 WORD WINAPI GetExeVersion16(void)
1298 TDB *pTask;
1300 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1301 return pTask->version;
1305 /***********************************************************************
1306 * SetErrorMode16 (KERNEL.107)
1308 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1310 TDB *pTask;
1311 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1312 pTask->error_mode = mode;
1313 return SetErrorMode( mode );
1317 /***********************************************************************
1318 * GetNumTasks (KERNEL.152)
1320 UINT16 WINAPI GetNumTasks16(void)
1322 return nTaskCount;
1326 /***********************************************************************
1327 * GetTaskDS (KERNEL.155)
1329 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1330 * I don't think we need to bother with this.
1332 HINSTANCE16 WINAPI GetTaskDS16(void)
1334 TDB *pTask;
1336 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1337 return GlobalHandleToSel16(pTask->hInstance);
1340 /***********************************************************************
1341 * GetDummyModuleHandleDS (KERNEL.602)
1343 WORD WINAPI GetDummyModuleHandleDS16(void)
1345 TDB *pTask;
1346 WORD selector;
1348 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1349 if (!(pTask->flags & TDBF_WIN32)) return 0;
1350 selector = GlobalHandleToSel16( pTask->hModule );
1351 CURRENT_DS = selector;
1352 return selector;
1355 /***********************************************************************
1356 * IsTask (KERNEL.320)
1357 * IsTask16 (KERNEL32.@)
1359 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1361 TDB *pTask;
1363 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1364 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1365 return (pTask->magic == TDB_MAGIC);
1369 /***********************************************************************
1370 * IsWinOldApTask16 (KERNEL.158)
1372 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1374 /* should return bit 0 of byte 0x48 in PSP */
1375 return FALSE;
1378 /***********************************************************************
1379 * SetTaskSignalProc (KERNEL.38)
1381 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1383 TDB *pTask;
1384 FARPROC16 oldProc;
1386 if (!hTask) hTask = GetCurrentTask();
1387 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1388 oldProc = pTask->userhandler;
1389 pTask->userhandler = proc;
1390 return oldProc;
1393 /***********************************************************************
1394 * TASK_CallTaskSignalProc
1396 /* ### start build ### */
1397 extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1398 /* ### stop build ### */
1399 void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1401 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1402 if ( !pTask || !pTask->userhandler ) return;
1404 TASK_CallTo16_word_wwwww( pTask->userhandler,
1405 hTaskOrModule, uCode, 0,
1406 pTask->hInstance, pTask->hQueue );
1409 /***********************************************************************
1410 * SetSigHandler (KERNEL.140)
1412 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1413 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1415 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1416 newhandler,oldhandler,oldmode,newmode,flag );
1418 if (flag != 1) return 0;
1419 if (!newmode) newhandler = NULL; /* Default handler */
1420 if (newmode != 4)
1422 TDB *pTask;
1424 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1425 if (oldmode) *oldmode = pTask->signal_flags;
1426 pTask->signal_flags = newmode;
1427 if (oldhandler) *oldhandler = pTask->sighandler;
1428 pTask->sighandler = newhandler;
1430 return 0;
1434 /***********************************************************************
1435 * GlobalNotify (KERNEL.154)
1437 * Note that GlobalNotify does _not_ return the old NotifyProc
1438 * -- contrary to LocalNotify !!
1440 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1442 TDB *pTask;
1444 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1445 pTask->discardhandler = proc;
1449 /***********************************************************************
1450 * GetExePtr (KERNEL.133)
1452 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1454 char *ptr;
1455 HANDLE16 owner;
1457 /* Check for module handle */
1459 if (!(ptr = GlobalLock16( handle ))) return 0;
1460 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1462 /* Search for this handle inside all tasks */
1464 *hTask = hFirstTask;
1465 while (*hTask)
1467 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1468 if ((*hTask == handle) ||
1469 (pTask->hInstance == handle) ||
1470 (pTask->hQueue == handle) ||
1471 (pTask->hPDB == handle)) return pTask->hModule;
1472 *hTask = pTask->hNext;
1475 /* Check the owner for module handle */
1477 owner = FarGetOwner16( handle );
1478 if (!(ptr = GlobalLock16( owner ))) return 0;
1479 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1481 /* Search for the owner inside all tasks */
1483 *hTask = hFirstTask;
1484 while (*hTask)
1486 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1487 if ((*hTask == owner) ||
1488 (pTask->hInstance == owner) ||
1489 (pTask->hQueue == owner) ||
1490 (pTask->hPDB == owner)) return pTask->hModule;
1491 *hTask = pTask->hNext;
1494 return 0;
1497 /**********************************************************************/
1499 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1501 HTASK16 hTask = 0;
1502 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1503 STACK16FRAME *frame = CURRENT_STACK16;
1504 frame->ecx = hModule;
1505 if (hTask) frame->es = hTask;
1506 return hModule;
1509 /**********************************************************************/
1511 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1513 HTASK16 hTask = 0;
1514 return GetExePtrHelper( handle, &hTask );
1518 /***********************************************************************
1519 * TaskFirst (TOOLHELP.63)
1521 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1523 lpte->hNext = hFirstTask;
1524 return TaskNext16( lpte );
1528 /***********************************************************************
1529 * TaskNext (TOOLHELP.64)
1531 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1533 TDB *pTask;
1534 INSTANCEDATA *pInstData;
1536 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1537 if (!lpte->hNext) return FALSE;
1539 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1540 while (1) {
1541 pTask = (TDB *)GlobalLock16( lpte->hNext );
1542 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1543 if (pTask->hInstance)
1544 break;
1545 lpte->hNext = pTask->hNext;
1547 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1548 lpte->hTask = lpte->hNext;
1549 lpte->hTaskParent = pTask->hParent;
1550 lpte->hInst = pTask->hInstance;
1551 lpte->hModule = pTask->hModule;
1552 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1553 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1554 lpte->wStackTop = pInstData->stacktop;
1555 lpte->wStackMinimum = pInstData->stackmin;
1556 lpte->wStackBottom = pInstData->stackbottom;
1557 lpte->wcEvents = pTask->nEvents;
1558 lpte->hQueue = pTask->hQueue;
1559 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1560 lpte->wPSPOffset = 0x100; /*??*/
1561 lpte->hNext = pTask->hNext;
1562 return TRUE;
1566 /***********************************************************************
1567 * TaskFindHandle (TOOLHELP.65)
1569 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1571 lpte->hNext = hTask;
1572 return TaskNext16( lpte );
1576 /**************************************************************************
1577 * FatalAppExit16 (KERNEL.137)
1579 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1581 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1583 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1585 if (Callout.MessageBoxA)
1586 Callout.MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1587 else
1588 ERR( "%s\n", debugstr_a(str) );
1590 ExitThread(0xff);
1594 /***********************************************************************
1595 * TerminateApp16 (TOOLHELP.77)
1597 * See "Undocumented Windows".
1599 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1601 if (hTask && hTask != GetCurrentTask())
1603 FIXME("cannot terminate task %x\n", hTask);
1604 return;
1607 if (wFlags & NO_UAE_BOX)
1609 UINT16 old_mode;
1610 old_mode = SetErrorMode16(0);
1611 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1613 FatalAppExit16( 0, NULL );
1615 /* hmm, we're still alive ?? */
1617 /* check undocumented flag */
1618 if (!(wFlags & 0x8000))
1619 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1621 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1622 but let's just call ExitThread */
1623 ExitThread(0xff);
1627 /***********************************************************************
1628 * GetAppCompatFlags16 (KERNEL.354)
1630 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1632 return GetAppCompatFlags( hTask );
1636 /***********************************************************************
1637 * GetAppCompatFlags (USER32.206)
1639 DWORD WINAPI GetAppCompatFlags( HTASK hTask )
1641 TDB *pTask;
1643 if (!hTask) hTask = GetCurrentTask();
1644 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1645 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1646 return pTask->compat_flags;