Solaris fixes.
[wine.git] / loader / task.c
blobf38f2b2969bf54bf6a53bdd532f97f6317bcacfe
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/winbase16.h"
13 #include "ntddk.h"
14 #include "callback.h"
15 #include "drive.h"
16 #include "file.h"
17 #include "global.h"
18 #include "instance.h"
19 #include "miscemu.h"
20 #include "module.h"
21 #include "queue.h"
22 #include "selectors.h"
23 #include "stackframe.h"
24 #include "task.h"
25 #include "thread.h"
26 #include "toolhelp.h"
27 #include "winnt.h"
28 #include "syslevel.h"
29 #include "winsock.h"
30 #include "debugtools.h"
31 #include "services.h"
32 #include "server.h"
35 DEFAULT_DEBUG_CHANNEL(task);
36 DECLARE_DEBUG_CHANNEL(relay);
37 DECLARE_DEBUG_CHANNEL(toolhelp);
39 /* Min. number of thunks allocated when creating a new segment */
40 #define MIN_THUNKS 32
43 static THHOOK DefaultThhook;
44 THHOOK *pThhook = &DefaultThhook;
46 #define hCurrentTask (pThhook->CurTDB)
47 #define hFirstTask (pThhook->HeadTDB)
48 #define hLockedTask (pThhook->LockTDB)
50 static UINT16 nTaskCount = 0;
52 static HTASK initial_task;
54 /***********************************************************************
55 * TASK_InstallTHHook
57 void TASK_InstallTHHook( THHOOK *pNewThhook )
59 THHOOK *pOldThhook = pThhook;
61 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
63 *pThhook = *pOldThhook;
66 /***********************************************************************
67 * TASK_GetNextTask
69 HTASK16 TASK_GetNextTask( HTASK16 hTask )
71 TDB* pTask = (TDB*)GlobalLock16(hTask);
73 if (pTask->hNext) return pTask->hNext;
74 return (hFirstTask != hTask) ? hFirstTask : 0;
77 /***********************************************************************
78 * TASK_LinkTask
80 static void TASK_LinkTask( HTASK16 hTask )
82 HTASK16 *prevTask;
83 TDB *pTask;
85 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
86 prevTask = &hFirstTask;
87 while (*prevTask)
89 TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
90 if (prevTaskPtr->priority >= pTask->priority) break;
91 prevTask = &prevTaskPtr->hNext;
93 pTask->hNext = *prevTask;
94 *prevTask = hTask;
95 nTaskCount++;
99 /***********************************************************************
100 * TASK_UnlinkTask
102 static void TASK_UnlinkTask( HTASK16 hTask )
104 HTASK16 *prevTask;
105 TDB *pTask;
107 prevTask = &hFirstTask;
108 while (*prevTask && (*prevTask != hTask))
110 pTask = (TDB *)GlobalLock16( *prevTask );
111 prevTask = &pTask->hNext;
113 if (*prevTask)
115 pTask = (TDB *)GlobalLock16( *prevTask );
116 *prevTask = pTask->hNext;
117 pTask->hNext = 0;
118 nTaskCount--;
123 /***********************************************************************
124 * TASK_CreateThunks
126 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
127 * and containing 'count' entries.
129 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
131 int i;
132 WORD free;
133 THUNKS *pThunk;
135 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
136 pThunk->next = 0;
137 pThunk->magic = THUNK_MAGIC;
138 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
139 free = pThunk->free;
140 for (i = 0; i < count-1; i++)
142 free += 8; /* Offset of next thunk */
143 pThunk->thunks[4*i] = free;
145 pThunk->thunks[4*i] = 0; /* Last thunk */
149 /***********************************************************************
150 * TASK_AllocThunk
152 * Allocate a thunk for MakeProcInstance().
154 static SEGPTR TASK_AllocThunk( HTASK16 hTask )
156 TDB *pTask;
157 THUNKS *pThunk;
158 WORD sel, base;
160 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
161 sel = pTask->hCSAlias;
162 pThunk = &pTask->thunks;
163 base = (int)pThunk - (int)pTask;
164 while (!pThunk->free)
166 sel = pThunk->next;
167 if (!sel) /* Allocate a new segment */
169 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
170 pTask->hPDB, WINE_LDT_FLAGS_CODE );
171 if (!sel) return (SEGPTR)0;
172 TASK_CreateThunks( sel, 0, MIN_THUNKS );
173 pThunk->next = sel;
175 pThunk = (THUNKS *)GlobalLock16( sel );
176 base = 0;
178 base += pThunk->free;
179 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
180 return MAKESEGPTR( sel, base );
184 /***********************************************************************
185 * TASK_FreeThunk
187 * Free a MakeProcInstance() thunk.
189 static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
191 TDB *pTask;
192 THUNKS *pThunk;
193 WORD sel, base;
195 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
196 sel = pTask->hCSAlias;
197 pThunk = &pTask->thunks;
198 base = (int)pThunk - (int)pTask;
199 while (sel && (sel != HIWORD(thunk)))
201 sel = pThunk->next;
202 pThunk = (THUNKS *)GlobalLock16( sel );
203 base = 0;
205 if (!sel) return FALSE;
206 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
207 pThunk->free = LOWORD(thunk) - base;
208 return TRUE;
212 /***********************************************************************
213 * TASK_Create
215 * NOTE: This routine might be called by a Win32 thread. Thus, we need
216 * to be careful to protect global data structures. We do this
217 * by entering the Win16Lock while linking the task into the
218 * global task list.
220 BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, TEB *teb, LPCSTR cmdline, BYTE len )
222 HTASK16 hTask;
223 TDB *pTask;
224 char name[10];
226 /* Allocate the task structure */
228 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
229 pModule->self, WINE_LDT_FLAGS_DATA );
230 if (!hTask) return FALSE;
231 pTask = (TDB *)GlobalLock16( hTask );
233 /* Fill the task structure */
235 pTask->hSelf = hTask;
237 if (teb->tibflags & TEBF_WIN32)
239 pTask->flags |= TDBF_WIN32;
240 pTask->hInstance = pModule->self;
241 pTask->hPrevInstance = 0;
242 /* NOTE: for 16-bit tasks, the instance handles are updated later on
243 in NE_InitProcess */
246 pTask->version = pModule->expected_version;
247 pTask->hModule = pModule->self;
248 pTask->hParent = GetCurrentTask();
249 pTask->magic = TDB_MAGIC;
250 pTask->nCmdShow = cmdShow;
251 pTask->teb = teb;
252 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
253 strcpy( pTask->curdir, "\\" );
254 lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
255 sizeof(pTask->curdir) - 1 );
257 /* Create the thunks block */
259 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
261 /* Copy the module name */
263 GetModuleName16( pModule->self, name, sizeof(name) );
264 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
266 /* Allocate a selector for the PDB */
268 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
269 pModule->self, WINE_LDT_FLAGS_DATA );
271 /* Fill the PDB */
273 pTask->pdb.int20 = 0x20cd;
274 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
275 PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)GetProcAddress16( GetModuleHandle16("KERNEL"),
276 "DOS3Call" ));
277 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
278 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
279 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
280 pTask->pdb.fileHandlesPtr =
281 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
282 pTask->pdb.hFileHandles = 0;
283 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
284 /* FIXME: should we make a copy of the environment? */
285 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
286 pTask->pdb.nbFiles = 20;
288 /* Fill the command line */
290 if (!cmdline)
292 cmdline = GetCommandLineA();
293 /* remove the first word (program name) */
294 if (*cmdline == '"')
295 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
296 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
297 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
298 len = strlen(cmdline);
300 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
301 pTask->pdb.cmdLine[0] = len;
302 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
303 /* pTask->pdb.cmdLine[len+1] = 0; */
305 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name, len, cmdline, hTask );
307 /* Get the compatibility flags */
309 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
311 /* Allocate a code segment alias for the TDB */
313 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
314 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
316 /* Default DTA overwrites command line */
318 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
320 /* Create scheduler event for 16-bit tasks */
322 if ( !(pTask->flags & TDBF_WIN32) )
323 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
325 /* Enter task handle into thread and process */
327 teb->htask16 = hTask;
328 if (!initial_task) initial_task = hTask;
330 /* Add the task to the linked list */
332 _EnterWin16Lock();
333 TASK_LinkTask( hTask );
334 _LeaveWin16Lock();
336 return TRUE;
339 /***********************************************************************
340 * TASK_DeleteTask
342 static void TASK_DeleteTask( HTASK16 hTask )
344 TDB *pTask;
345 HGLOBAL16 hPDB;
347 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
348 hPDB = pTask->hPDB;
350 pTask->magic = 0xdead; /* invalidate signature */
352 /* Free the selector aliases */
354 GLOBAL_FreeBlock( pTask->hCSAlias );
355 GLOBAL_FreeBlock( pTask->hPDB );
357 /* Free the task module */
359 FreeModule16( pTask->hModule );
361 /* Free the task structure itself */
363 GlobalFree16( hTask );
365 /* Free all memory used by this task (including the 32-bit stack, */
366 /* the environment block and the thunk segments). */
368 GlobalFreeAll16( hPDB );
371 /***********************************************************************
372 * TASK_KillTask
374 void TASK_ExitTask(void)
376 TDB *pTask;
377 DWORD lockCount;
379 /* Enter the Win16Lock to protect global data structures */
380 _EnterWin16Lock();
382 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
383 if ( !pTask )
385 _LeaveWin16Lock();
386 return;
389 TRACE("Killing task %04x\n", pTask->hSelf );
391 /* Perform USER cleanup */
393 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
394 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
395 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
396 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
398 /* Remove the task from the list to be sure we never switch back to it */
399 TASK_UnlinkTask( pTask->hSelf );
401 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
403 TRACE("this is the last task, exiting\n" );
404 ExitKernel16();
407 if( nTaskCount )
409 TDB* p = (TDB *)GlobalLock16( hFirstTask );
410 while( p )
412 if( p->hYieldTo == pTask->hSelf ) p->hYieldTo = 0;
413 p = (TDB *)GlobalLock16( p->hNext );
417 pTask->nEvents = 0;
419 if ( hLockedTask == pTask->hSelf )
420 hLockedTask = 0;
422 TASK_DeleteTask( pTask->hSelf );
424 /* ... schedule another one ... */
425 TASK_Reschedule();
427 /* ... and completely release the Win16Lock, just in case. */
428 ReleaseThunkLock( &lockCount );
431 /***********************************************************************
432 * TASK_Reschedule
434 * This is where all the magic of task-switching happens!
436 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
437 * This means that each 16-bit task runs until it voluntarily yields
438 * control, at which point the scheduler gets active and selects the
439 * next task to run.
441 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
442 * by the standard scheduler of the underlying OS. As many 16-bit apps
443 * *rely* on the behaviour of the Windows scheduler, however, we have
444 * to simulate that behaviour.
446 * This is achieved as follows: every 16-bit task is at time (except
447 * during task creation and deletion) in one of two states: either it
448 * is the one currently running, then the global variable hCurrentTask
449 * contains its task handle, or it is not currently running, then it
450 * is blocked on a special scheduler event, a global handle which
451 * is stored in the task struct.
453 * When the current task yields control, this routine gets called. Its
454 * purpose is to determine the next task to be active, signal the
455 * scheduler event of that task, and then put the current task to sleep
456 * waiting for *its* scheduler event to get signalled again.
458 * This routine can get called in a few other special situations as well:
460 * - On creation of a 16-bit task, the Unix process executing the task
461 * calls TASK_Reschedule once it has completed its initialization.
462 * At this point, the task needs to be blocked until its scheduler
463 * event is signalled the first time (this will be done by the parent
464 * process to get the task up and running).
466 * - When the task currently running terminates itself, this routine gets
467 * called and has to schedule another task, *without* blocking the
468 * terminating task.
470 * - When a 32-bit thread posts an event for a 16-bit task, it might be
471 * the case that *no* 16-bit task is currently running. In this case
472 * the task that has now an event pending is to be scheduled.
475 void TASK_Reschedule(void)
477 TDB *pOldTask = NULL, *pNewTask = NULL;
478 HTASK16 hOldTask = 0, hNewTask = 0;
479 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
480 DWORD lockCount;
482 _EnterWin16Lock();
484 /* Check what we need to do */
485 hOldTask = GetCurrentTask();
486 pOldTask = (TDB *)GlobalLock16( hOldTask );
487 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
488 hCurrentTask, hOldTask, (long) getpid() );
490 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
492 /* We are called by an active (non-deleted) 16-bit task */
494 /* If we don't even have a current task, or else the current
495 task has yielded, we'll need to schedule a new task and
496 (possibly) put the calling task to sleep. Otherwise, we
497 only block the caller. */
499 if ( !hCurrentTask || hCurrentTask == hOldTask )
500 mode = MODE_YIELD;
501 else
502 mode = MODE_SLEEP;
504 else
506 /* We are called by a deleted 16-bit task or a 32-bit thread */
508 /* The only situation where we need to do something is if we
509 now do not have a current task. Then, we'll need to wake up
510 some task that has events pending. */
512 if ( !hCurrentTask || hCurrentTask == hOldTask )
513 mode = MODE_WAKEUP;
514 else
516 /* nothing to do */
517 _LeaveWin16Lock();
518 return;
522 /* Find a task to yield to: check for DirectedYield() */
523 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
525 hNewTask = pOldTask->hYieldTo;
526 pNewTask = (TDB *)GlobalLock16( hNewTask );
527 if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
528 pOldTask->hYieldTo = 0;
531 /* Find a task to yield to: check for pending events */
532 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
534 hNewTask = hFirstTask;
535 while (hNewTask)
537 pNewTask = (TDB *)GlobalLock16( hNewTask );
539 TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
541 if (pNewTask->nEvents) break;
542 hNewTask = pNewTask->hNext;
544 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
547 /* If we are still the task with highest priority, just return ... */
548 if ( mode == MODE_YIELD && hNewTask && hNewTask == hCurrentTask )
550 TRACE("returning to the current task (%04x)\n", hCurrentTask );
551 _LeaveWin16Lock();
553 /* Allow Win32 threads to thunk down even while a Win16 task is
554 in a tight PeekMessage() or Yield() loop ... */
555 ReleaseThunkLock( &lockCount );
556 RestoreThunkLock( lockCount );
557 return;
560 /* If no task to yield to found, suspend 16-bit scheduler ... */
561 if ( mode == MODE_YIELD && !hNewTask )
563 TRACE("No currently active task\n");
564 hCurrentTask = 0;
567 /* If we found a task to wake up, do it ... */
568 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
570 TRACE("Switching to task %04x (%.8s)\n",
571 hNewTask, pNewTask->module_name );
573 pNewTask->priority++;
574 TASK_UnlinkTask( hNewTask );
575 TASK_LinkTask( hNewTask );
576 pNewTask->priority--;
578 hCurrentTask = hNewTask;
579 NtSetEvent( pNewTask->hEvent, NULL );
581 /* This is set just in case some app reads it ... */
582 pNewTask->ss_sp = pNewTask->teb->cur_stack;
585 /* If we need to put the current task to sleep, do it ... */
586 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
588 NtResetEvent( pOldTask->hEvent, NULL );
590 ReleaseThunkLock( &lockCount );
591 SYSLEVEL_CheckNotLevel( 1 );
592 WaitForSingleObject( pOldTask->hEvent, INFINITE );
593 RestoreThunkLock( lockCount );
596 _LeaveWin16Lock();
599 /***********************************************************************
600 * InitTask (KERNEL.91)
602 * Called by the application startup code.
604 void WINAPI InitTask16( CONTEXT86 *context )
606 TDB *pTask;
607 INSTANCEDATA *pinstance;
608 SEGPTR ptr;
610 context->Eax = 0;
611 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
613 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
614 as some apps, notably Visual Basic apps, *modify* the heap/stack
615 size of the instance data segment before calling InitTask() */
617 /* Initialize the INSTANCEDATA structure */
618 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
619 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack ) + sizeof( STACK16FRAME );
620 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
621 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
622 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
624 /* Initialize the local heap */
625 if (LOWORD(context->Ecx))
626 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
628 /* Initialize implicitly loaded DLLs */
629 NE_InitializeDLLs( pTask->hModule );
630 NE_DllProcessAttach( pTask->hModule );
632 /* Registers on return are:
633 * ax 1 if OK, 0 on error
634 * cx stack limit in bytes
635 * dx cmdShow parameter
636 * si instance handle of the previous instance
637 * di instance handle of the new task
638 * es:bx pointer to command line inside PSP
640 * 0 (=%bp) is pushed on the stack
642 ptr = stack16_push( sizeof(WORD) );
643 *(WORD *)MapSL(ptr) = 0;
644 context->Esp -= 2;
646 context->Eax = 1;
648 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
649 else
651 LPBYTE p = &pTask->pdb.cmdLine[1];
652 while ((*p == ' ') || (*p == '\t')) p++;
653 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
655 context->Ecx = pinstance->stacktop;
656 context->Edx = pTask->nCmdShow;
657 context->Esi = (DWORD)pTask->hPrevInstance;
658 context->Edi = (DWORD)pTask->hInstance;
659 context->SegEs = (WORD)pTask->hPDB;
663 /***********************************************************************
664 * WaitEvent (KERNEL.30)
666 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
668 TDB *pTask;
670 if (!hTask) hTask = GetCurrentTask();
671 pTask = (TDB *)GlobalLock16( hTask );
673 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
675 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
676 return TRUE;
679 if (pTask->nEvents > 0)
681 pTask->nEvents--;
682 return FALSE;
684 TASK_Reschedule();
686 /* When we get back here, we have an event */
688 if (pTask->nEvents > 0) pTask->nEvents--;
689 return TRUE;
693 /***********************************************************************
694 * PostEvent (KERNEL.31)
696 void WINAPI PostEvent16( HTASK16 hTask )
698 TDB *pTask;
700 if (!hTask) hTask = GetCurrentTask();
701 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
703 if ( !THREAD_IsWin16( pTask->teb ) )
705 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
706 return;
709 pTask->nEvents++;
711 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
712 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
713 TASK_Reschedule();
717 /***********************************************************************
718 * SetPriority (KERNEL.32)
720 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
722 TDB *pTask;
723 INT16 newpriority;
725 if (!hTask) hTask = GetCurrentTask();
726 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
727 newpriority = pTask->priority + delta;
728 if (newpriority < -32) newpriority = -32;
729 else if (newpriority > 15) newpriority = 15;
731 pTask->priority = newpriority + 1;
732 TASK_UnlinkTask( hTask );
733 TASK_LinkTask( hTask );
734 pTask->priority--;
738 /***********************************************************************
739 * LockCurrentTask (KERNEL.33)
741 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
743 if (bLock) hLockedTask = GetCurrentTask();
744 else hLockedTask = 0;
745 return hLockedTask;
749 /***********************************************************************
750 * IsTaskLocked (KERNEL.122)
752 HTASK16 WINAPI IsTaskLocked16(void)
754 return hLockedTask;
758 /***********************************************************************
759 * OldYield (KERNEL.117)
761 void WINAPI OldYield16(void)
763 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
765 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
767 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
768 return;
771 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
772 TASK_Reschedule();
773 if (pCurTask) pCurTask->nEvents--;
776 /***********************************************************************
777 * WIN32_OldYield16 (KERNEL.447)
779 void WINAPI WIN32_OldYield16(void)
781 DWORD count;
783 ReleaseThunkLock(&count);
784 RestoreThunkLock(count);
787 /***********************************************************************
788 * DirectedYield (KERNEL.150)
790 void WINAPI DirectedYield16( HTASK16 hTask )
792 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
794 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
796 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
797 return;
800 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
802 pCurTask->hYieldTo = hTask;
803 OldYield16();
805 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
808 /***********************************************************************
809 * Yield16 (KERNEL.29)
811 void WINAPI Yield16(void)
813 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
815 if (pCurTask) pCurTask->hYieldTo = 0;
816 if (pCurTask && pCurTask->hQueue && Callout.UserYield16) Callout.UserYield16();
817 else OldYield16();
820 /***********************************************************************
821 * KERNEL_490 (KERNEL.490)
823 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
825 if ( !someTask ) return 0;
827 FIXME("(%04x): stub\n", someTask );
828 return 0;
831 /***********************************************************************
832 * MakeProcInstance16 (KERNEL.51)
834 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
836 BYTE *thunk,*lfunc;
837 SEGPTR thunkaddr;
838 WORD hInstanceSelector;
840 hInstanceSelector = GlobalHandleToSel16(hInstance);
842 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
844 if (!HIWORD(func)) {
845 /* Win95 actually protects via SEH, but this is better for debugging */
846 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
847 return (FARPROC16)0;
850 if (hInstance)
852 if ( (!(hInstance & 4)) ||
853 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
855 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
856 hInstance);
857 return 0;
861 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
862 && (hInstance != 0)
863 && (hInstance != 0xffff) )
865 /* calling MPI with a foreign DSEG is invalid ! */
866 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
867 hInstance,CURRENT_DS);
870 /* Always use the DSEG that MPI was entered with.
871 * We used to set hInstance to GetTaskDS16(), but this should be wrong
872 * as CURRENT_DS provides the DSEG value we need.
873 * ("calling" DS, *not* "task" DS !) */
874 hInstanceSelector = CURRENT_DS;
875 hInstance = GlobalHandle16(hInstanceSelector);
877 /* no thunking for DLLs */
878 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
879 return func;
881 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
882 if (!thunkaddr) return (FARPROC16)0;
883 thunk = MapSL( thunkaddr );
884 lfunc = MapSL( (SEGPTR)func );
886 TRACE("(%08lx,%04x): got thunk %08lx\n",
887 (DWORD)func, hInstance, (DWORD)thunkaddr );
888 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
889 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
891 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
894 *thunk++ = 0xb8; /* movw instance, %ax */
895 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
896 *thunk++ = (BYTE)(hInstanceSelector >> 8);
897 *thunk++ = 0xea; /* ljmp func */
898 *(DWORD *)thunk = (DWORD)func;
899 return (FARPROC16)thunkaddr;
900 /* CX reg indicates if thunkaddr != NULL, implement if needed */
904 /***********************************************************************
905 * FreeProcInstance16 (KERNEL.52)
907 void WINAPI FreeProcInstance16( FARPROC16 func )
909 TRACE("(%08lx)\n", (DWORD)func );
910 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
913 /**********************************************************************
914 * TASK_GetCodeSegment
916 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
917 * and logical segment number of a given code segment.
919 * 'proc' either *is* already a pair of module handle and segment number,
920 * in which case there's nothing to do. Otherwise, it is a pointer to
921 * a function, and we need to retrieve the code segment. If the pointer
922 * happens to point to a thunk, we'll retrieve info about the code segment
923 * where the function pointed to by the thunk resides, not the thunk itself.
925 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
926 * the function the snoop code will return to ...
929 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
930 SEGTABLEENTRY **ppSeg, int *pSegNr )
932 NE_MODULE *pModule = NULL;
933 SEGTABLEENTRY *pSeg = NULL;
934 int segNr=0;
936 /* Try pair of module handle / segment number */
937 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
938 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
940 segNr = LOWORD( proc );
941 if ( segNr && segNr <= pModule->seg_count )
942 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
945 /* Try thunk or function */
946 else
948 BYTE *thunk = MapSL( (SEGPTR)proc );
949 WORD selector;
951 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
952 selector = thunk[6] + (thunk[7] << 8);
953 else
954 selector = HIWORD( proc );
956 pModule = NE_GetPtr( GlobalHandle16( selector ) );
957 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
959 if ( pModule )
960 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
961 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
962 break;
964 if ( pModule && segNr > pModule->seg_count )
965 pSeg = NULL;
968 /* Abort if segment not found */
970 if ( !pModule || !pSeg )
971 return FALSE;
973 /* Return segment data */
975 if ( ppModule ) *ppModule = pModule;
976 if ( ppSeg ) *ppSeg = pSeg;
977 if ( pSegNr ) *pSegNr = segNr;
979 return TRUE;
982 /**********************************************************************
983 * GetCodeHandle (KERNEL.93)
985 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
987 SEGTABLEENTRY *pSeg;
989 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
990 return (HANDLE16)0;
992 return pSeg->hSeg;
995 /**********************************************************************
996 * GetCodeInfo (KERNEL.104)
998 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1000 NE_MODULE *pModule;
1001 SEGTABLEENTRY *pSeg;
1002 int segNr;
1004 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1005 return FALSE;
1007 /* Fill in segment information */
1009 segInfo->offSegment = pSeg->filepos;
1010 segInfo->cbSegment = pSeg->size;
1011 segInfo->flags = pSeg->flags;
1012 segInfo->cbAlloc = pSeg->minsize;
1013 segInfo->h = pSeg->hSeg;
1014 segInfo->alignShift = pModule->alignment;
1016 if ( segNr == pModule->dgroup )
1017 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
1019 /* Return module handle in %es */
1021 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1023 return TRUE;
1027 /**********************************************************************
1028 * DefineHandleTable16 (KERNEL.94)
1030 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1032 FIXME("(%04x): stub ?\n", wOffset);
1033 return TRUE;
1037 /***********************************************************************
1038 * SetTaskQueue (KERNEL.34)
1040 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1042 HQUEUE16 hPrev;
1043 TDB *pTask;
1045 if (!hTask) hTask = GetCurrentTask();
1046 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1048 hPrev = pTask->hQueue;
1049 pTask->hQueue = hQueue;
1051 return hPrev;
1055 /***********************************************************************
1056 * GetTaskQueue (KERNEL.35)
1058 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1060 TDB *pTask;
1062 if (!hTask) hTask = GetCurrentTask();
1063 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1064 return pTask->hQueue;
1067 /***********************************************************************
1068 * SetThreadQueue (KERNEL.463)
1070 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1072 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1073 HQUEUE16 oldQueue = teb? teb->queue : 0;
1075 if ( teb )
1077 teb->queue = hQueue;
1079 if ( GetTaskQueue16( teb->htask16 ) == oldQueue )
1080 SetTaskQueue16( teb->htask16, hQueue );
1083 return oldQueue;
1086 /***********************************************************************
1087 * GetThreadQueue (KERNEL.464)
1089 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1091 TEB *teb = NULL;
1092 if ( !thread )
1093 teb = NtCurrentTeb();
1094 else if ( HIWORD(thread) )
1095 teb = THREAD_IdToTEB( thread );
1096 else if ( IsTask16( (HTASK16)thread ) )
1097 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1099 return (HQUEUE16)(teb? teb->queue : 0);
1102 /***********************************************************************
1103 * SetFastQueue (KERNEL.624)
1105 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1107 TEB *teb = NULL;
1108 if ( !thread )
1109 teb = NtCurrentTeb();
1110 else if ( HIWORD(thread) )
1111 teb = THREAD_IdToTEB( thread );
1112 else if ( IsTask16( (HTASK16)thread ) )
1113 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1115 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1118 /***********************************************************************
1119 * GetFastQueue (KERNEL.625)
1121 HANDLE WINAPI GetFastQueue16( void )
1123 TEB *teb = NtCurrentTeb();
1124 if (!teb) return 0;
1126 if (!teb->queue && Callout.InitThreadInput16)
1127 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
1129 if (!teb->queue)
1130 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1132 return (HANDLE)teb->queue;
1135 /***********************************************************************
1136 * SwitchStackTo (KERNEL.108)
1138 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1140 TDB *pTask;
1141 STACK16FRAME *oldFrame, *newFrame;
1142 INSTANCEDATA *pData;
1143 UINT16 copySize;
1145 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1146 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1147 TRACE("old=%04x:%04x new=%04x:%04x\n",
1148 SELECTOROF( pTask->teb->cur_stack ),
1149 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1151 /* Save the old stack */
1153 oldFrame = THREAD_STACK16( pTask->teb );
1154 /* pop frame + args and push bp */
1155 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1156 + 2 * sizeof(WORD);
1157 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1158 pData->stacktop = top;
1159 pData->stackmin = ptr;
1160 pData->stackbottom = ptr;
1162 /* Switch to the new stack */
1164 /* Note: we need to take the 3 arguments into account; otherwise,
1165 * the stack will underflow upon return from this function.
1167 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1168 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1169 pTask->teb->cur_stack = MAKESEGPTR( seg, ptr - copySize );
1170 newFrame = THREAD_STACK16( pTask->teb );
1172 /* Copy the stack frame and the local variables to the new stack */
1174 memmove( newFrame, oldFrame, copySize );
1175 newFrame->bp = ptr;
1176 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1180 /***********************************************************************
1181 * SwitchStackBack (KERNEL.109)
1183 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1185 STACK16FRAME *oldFrame, *newFrame;
1186 INSTANCEDATA *pData;
1188 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1189 return;
1190 if (!pData->old_ss_sp)
1192 WARN("No previous SwitchStackTo\n" );
1193 return;
1195 TRACE("restoring stack %04x:%04x\n",
1196 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1198 oldFrame = CURRENT_STACK16;
1200 /* Pop bp from the previous stack */
1202 BP_reg(context) = *(WORD *)MapSL(pData->old_ss_sp);
1203 pData->old_ss_sp += sizeof(WORD);
1205 /* Switch back to the old stack */
1207 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1208 context->SegSs = SELECTOROF(pData->old_ss_sp);
1209 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1210 pData->old_ss_sp = 0;
1212 /* Build a stack frame for the return */
1214 newFrame = CURRENT_STACK16;
1215 newFrame->frame32 = oldFrame->frame32;
1216 newFrame->module_cs = oldFrame->module_cs;
1217 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1218 newFrame->entry_ip = oldFrame->entry_ip;
1222 /***********************************************************************
1223 * GetTaskQueueDS16 (KERNEL.118)
1225 void WINAPI GetTaskQueueDS16(void)
1227 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1231 /***********************************************************************
1232 * GetTaskQueueES16 (KERNEL.119)
1234 void WINAPI GetTaskQueueES16(void)
1236 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1240 /***********************************************************************
1241 * GetCurrentTask (KERNEL.36)
1243 HTASK16 WINAPI GetCurrentTask(void)
1245 return NtCurrentTeb()->htask16;
1248 DWORD WINAPI WIN16_GetCurrentTask(void)
1250 /* This is the version used by relay code; the first task is */
1251 /* returned in the high word of the result */
1252 return MAKELONG( GetCurrentTask(), hFirstTask );
1256 /***********************************************************************
1257 * GetCurrentPDB (KERNEL.37)
1259 * UNDOC: returns PSP of KERNEL in high word
1261 DWORD WINAPI GetCurrentPDB16(void)
1263 TDB *pTask;
1265 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1266 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1270 /***********************************************************************
1271 * GetCurPID16 (KERNEL.157)
1273 DWORD WINAPI GetCurPID16( DWORD unused )
1275 return 0;
1279 /***********************************************************************
1280 * GetInstanceData (KERNEL.54)
1282 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1284 char *ptr = (char *)GlobalLock16( instance );
1285 if (!ptr || !len) return 0;
1286 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1287 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1288 return len;
1292 /***********************************************************************
1293 * GetExeVersion (KERNEL.105)
1295 WORD WINAPI GetExeVersion16(void)
1297 TDB *pTask;
1299 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1300 return pTask->version;
1304 /***********************************************************************
1305 * SetErrorMode16 (KERNEL.107)
1307 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1309 TDB *pTask;
1310 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1311 pTask->error_mode = mode;
1312 return SetErrorMode( mode );
1316 /***********************************************************************
1317 * GetNumTasks (KERNEL.152)
1319 UINT16 WINAPI GetNumTasks16(void)
1321 return nTaskCount;
1325 /***********************************************************************
1326 * GetTaskDS (KERNEL.155)
1328 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1329 * I don't think we need to bother with this.
1331 HINSTANCE16 WINAPI GetTaskDS16(void)
1333 TDB *pTask;
1335 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1336 return GlobalHandleToSel16(pTask->hInstance);
1339 /***********************************************************************
1340 * GetDummyModuleHandleDS (KERNEL.602)
1342 WORD WINAPI GetDummyModuleHandleDS16(void)
1344 TDB *pTask;
1345 WORD selector;
1347 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1348 if (!(pTask->flags & TDBF_WIN32)) return 0;
1349 selector = GlobalHandleToSel16( pTask->hModule );
1350 CURRENT_DS = selector;
1351 return selector;
1354 /***********************************************************************
1355 * IsTask (KERNEL.320)
1357 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1359 TDB *pTask;
1361 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1362 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1363 return (pTask->magic == TDB_MAGIC);
1367 /***********************************************************************
1368 * IsWinOldApTask16 (KERNEL.158)
1370 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1372 /* should return bit 0 of byte 0x48 in PSP */
1373 return FALSE;
1376 /***********************************************************************
1377 * SetTaskSignalProc (KERNEL.38)
1379 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1381 TDB *pTask;
1382 FARPROC16 oldProc;
1384 if (!hTask) hTask = GetCurrentTask();
1385 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1386 oldProc = pTask->userhandler;
1387 pTask->userhandler = proc;
1388 return oldProc;
1391 /***********************************************************************
1392 * TASK_CallTaskSignalProc
1394 /* ### start build ### */
1395 extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1396 /* ### stop build ### */
1397 void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1399 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1400 if ( !pTask || !pTask->userhandler ) return;
1402 TASK_CallTo16_word_wwwww( pTask->userhandler,
1403 hTaskOrModule, uCode, 0,
1404 pTask->hInstance, pTask->hQueue );
1407 /***********************************************************************
1408 * SetSigHandler (KERNEL.140)
1410 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1411 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1413 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1414 newhandler,oldhandler,oldmode,newmode,flag );
1416 if (flag != 1) return 0;
1417 if (!newmode) newhandler = NULL; /* Default handler */
1418 if (newmode != 4)
1420 TDB *pTask;
1422 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1423 if (oldmode) *oldmode = pTask->signal_flags;
1424 pTask->signal_flags = newmode;
1425 if (oldhandler) *oldhandler = pTask->sighandler;
1426 pTask->sighandler = newhandler;
1428 return 0;
1432 /***********************************************************************
1433 * GlobalNotify (KERNEL.154)
1435 * Note that GlobalNotify does _not_ return the old NotifyProc
1436 * -- contrary to LocalNotify !!
1438 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1440 TDB *pTask;
1442 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1443 pTask->discardhandler = proc;
1447 /***********************************************************************
1448 * GetExePtr (KERNEL.133)
1450 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1452 char *ptr;
1453 HANDLE16 owner;
1455 /* Check for module handle */
1457 if (!(ptr = GlobalLock16( handle ))) return 0;
1458 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1460 /* Search for this handle inside all tasks */
1462 *hTask = hFirstTask;
1463 while (*hTask)
1465 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1466 if ((*hTask == handle) ||
1467 (pTask->hInstance == handle) ||
1468 (pTask->hQueue == handle) ||
1469 (pTask->hPDB == handle)) return pTask->hModule;
1470 *hTask = pTask->hNext;
1473 /* Check the owner for module handle */
1475 owner = FarGetOwner16( handle );
1476 if (!(ptr = GlobalLock16( owner ))) return 0;
1477 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1479 /* Search for the owner inside all tasks */
1481 *hTask = hFirstTask;
1482 while (*hTask)
1484 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1485 if ((*hTask == owner) ||
1486 (pTask->hInstance == owner) ||
1487 (pTask->hQueue == owner) ||
1488 (pTask->hPDB == owner)) return pTask->hModule;
1489 *hTask = pTask->hNext;
1492 return 0;
1495 /**********************************************************************/
1497 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1499 HTASK16 hTask = 0;
1500 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1501 STACK16FRAME *frame = CURRENT_STACK16;
1502 frame->ecx = hModule;
1503 if (hTask) frame->es = hTask;
1504 return hModule;
1507 /**********************************************************************/
1509 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1511 HTASK16 hTask = 0;
1512 return GetExePtrHelper( handle, &hTask );
1516 /***********************************************************************
1517 * TaskFirst (TOOLHELP.63)
1519 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1521 lpte->hNext = hFirstTask;
1522 return TaskNext16( lpte );
1526 /***********************************************************************
1527 * TaskNext (TOOLHELP.64)
1529 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1531 TDB *pTask;
1532 INSTANCEDATA *pInstData;
1534 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1535 if (!lpte->hNext) return FALSE;
1537 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1538 while (1) {
1539 pTask = (TDB *)GlobalLock16( lpte->hNext );
1540 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1541 if (pTask->hInstance)
1542 break;
1543 lpte->hNext = pTask->hNext;
1545 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1546 lpte->hTask = lpte->hNext;
1547 lpte->hTaskParent = pTask->hParent;
1548 lpte->hInst = pTask->hInstance;
1549 lpte->hModule = pTask->hModule;
1550 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1551 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1552 lpte->wStackTop = pInstData->stacktop;
1553 lpte->wStackMinimum = pInstData->stackmin;
1554 lpte->wStackBottom = pInstData->stackbottom;
1555 lpte->wcEvents = pTask->nEvents;
1556 lpte->hQueue = pTask->hQueue;
1557 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1558 lpte->wPSPOffset = 0x100; /*??*/
1559 lpte->hNext = pTask->hNext;
1560 return TRUE;
1564 /***********************************************************************
1565 * TaskFindHandle (TOOLHELP.65)
1567 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1569 lpte->hNext = hTask;
1570 return TaskNext16( lpte );
1574 /**************************************************************************
1575 * FatalAppExit16 (KERNEL.137)
1577 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1579 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1581 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1583 if (Callout.MessageBoxA)
1584 Callout.MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1585 else
1586 ERR( "%s\n", debugstr_a(str) );
1588 ExitThread(0xff);
1592 /***********************************************************************
1593 * TerminateApp16 (TOOLHELP.77)
1595 * See "Undocumented Windows".
1597 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1599 if (hTask && hTask != GetCurrentTask())
1601 FIXME("cannot terminate task %x\n", hTask);
1602 return;
1605 if (wFlags & NO_UAE_BOX)
1607 UINT16 old_mode;
1608 old_mode = SetErrorMode16(0);
1609 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1611 FatalAppExit16( 0, NULL );
1613 /* hmm, we're still alive ?? */
1615 /* check undocumented flag */
1616 if (!(wFlags & 0x8000))
1617 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1619 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1620 but let's just call ExitThread */
1621 ExitThread(0xff);
1625 /***********************************************************************
1626 * GetAppCompatFlags16 (KERNEL.354)
1628 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1630 return GetAppCompatFlags( hTask );
1634 /***********************************************************************
1635 * GetAppCompatFlags (USER32.206)
1637 DWORD WINAPI GetAppCompatFlags( HTASK hTask )
1639 TDB *pTask;
1641 if (!hTask) hTask = GetCurrentTask();
1642 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1643 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1644 return pTask->compat_flags;