4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
24 #include "selectors.h"
25 #include "stackframe.h"
32 #include "debugtools.h"
37 DEFAULT_DEBUG_CHANNEL(task
);
38 DECLARE_DEBUG_CHANNEL(relay
);
39 DECLARE_DEBUG_CHANNEL(toolhelp
);
41 /* Min. number of thunks allocated when creating a new segment */
45 static THHOOK DefaultThhook
;
46 THHOOK
*pThhook
= &DefaultThhook
;
48 #define hCurrentTask (pThhook->CurTDB)
49 #define hFirstTask (pThhook->HeadTDB)
50 #define hLockedTask (pThhook->LockTDB)
52 static UINT16 nTaskCount
= 0;
54 static HTASK initial_task
;
56 /***********************************************************************
59 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
61 THHOOK
*pOldThhook
= pThhook
;
63 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
65 *pThhook
= *pOldThhook
;
68 /***********************************************************************
71 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
73 TDB
* pTask
= (TDB
*)GlobalLock16(hTask
);
75 if (pTask
->hNext
) return pTask
->hNext
;
76 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
79 /***********************************************************************
82 static void TASK_LinkTask( HTASK16 hTask
)
87 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
88 prevTask
= &hFirstTask
;
91 TDB
*prevTaskPtr
= (TDB
*)GlobalLock16( *prevTask
);
92 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
93 prevTask
= &prevTaskPtr
->hNext
;
95 pTask
->hNext
= *prevTask
;
101 /***********************************************************************
104 static void TASK_UnlinkTask( HTASK16 hTask
)
109 prevTask
= &hFirstTask
;
110 while (*prevTask
&& (*prevTask
!= hTask
))
112 pTask
= (TDB
*)GlobalLock16( *prevTask
);
113 prevTask
= &pTask
->hNext
;
117 pTask
= (TDB
*)GlobalLock16( *prevTask
);
118 *prevTask
= pTask
->hNext
;
125 /***********************************************************************
128 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
129 * and containing 'count' entries.
131 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
137 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
139 pThunk
->magic
= THUNK_MAGIC
;
140 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
142 for (i
= 0; i
< count
-1; i
++)
144 free
+= 8; /* Offset of next thunk */
145 pThunk
->thunks
[4*i
] = free
;
147 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
151 /***********************************************************************
154 * Allocate a thunk for MakeProcInstance().
156 static SEGPTR
TASK_AllocThunk( HTASK16 hTask
)
162 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
163 sel
= pTask
->hCSAlias
;
164 pThunk
= &pTask
->thunks
;
165 base
= (int)pThunk
- (int)pTask
;
166 while (!pThunk
->free
)
169 if (!sel
) /* Allocate a new segment */
171 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
172 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
173 if (!sel
) return (SEGPTR
)0;
174 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
177 pThunk
= (THUNKS
*)GlobalLock16( sel
);
180 base
+= pThunk
->free
;
181 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
182 return PTR_SEG_OFF_TO_SEGPTR( sel
, base
);
186 /***********************************************************************
189 * Free a MakeProcInstance() thunk.
191 static BOOL
TASK_FreeThunk( HTASK16 hTask
, SEGPTR thunk
)
197 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
198 sel
= pTask
->hCSAlias
;
199 pThunk
= &pTask
->thunks
;
200 base
= (int)pThunk
- (int)pTask
;
201 while (sel
&& (sel
!= HIWORD(thunk
)))
204 pThunk
= (THUNKS
*)GlobalLock16( sel
);
207 if (!sel
) return FALSE
;
208 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
209 pThunk
->free
= LOWORD(thunk
) - base
;
214 /***********************************************************************
217 * NOTE: This routine might be called by a Win32 thread. Thus, we need
218 * to be careful to protect global data structures. We do this
219 * by entering the Win16Lock while linking the task into the
222 BOOL
TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
228 /* Allocate the task structure */
230 hTask
= GLOBAL_Alloc( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
),
231 pModule
->self
, WINE_LDT_FLAGS_DATA
);
232 if (!hTask
) return FALSE
;
233 pTask
= (TDB
*)GlobalLock16( hTask
);
235 /* Fill the task structure */
237 pTask
->hSelf
= hTask
;
239 if (teb
->tibflags
& TEBF_WIN32
)
241 pTask
->flags
|= TDBF_WIN32
;
242 pTask
->hInstance
= pModule
->self
;
243 pTask
->hPrevInstance
= 0;
244 /* NOTE: for 16-bit tasks, the instance handles are updated later on
248 pTask
->version
= pModule
->expected_version
;
249 pTask
->hModule
= pModule
->self
;
250 pTask
->hParent
= GetCurrentTask();
251 pTask
->magic
= TDB_MAGIC
;
252 pTask
->nCmdShow
= cmdShow
;
254 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
255 strcpy( pTask
->curdir
, "\\" );
256 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
257 sizeof(pTask
->curdir
) - 1 );
259 /* Create the thunks block */
261 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
263 /* Copy the module name */
265 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
266 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
268 /* Allocate a selector for the PDB */
270 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
271 pModule
->self
, WINE_LDT_FLAGS_DATA
);
275 pTask
->pdb
.int20
= 0x20cd;
276 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
277 PUT_DWORD(&pTask
->pdb
.dispatcher
[1], (DWORD
)NE_GetEntryPoint(
278 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
279 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
280 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
281 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
282 pTask
->pdb
.fileHandlesPtr
=
283 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask
->hPDB
),
284 (int)&((PDB16
*)0)->fileHandles
);
285 pTask
->pdb
.hFileHandles
= 0;
286 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
287 pTask
->pdb
.environment
= current_envdb
.env_sel
;
288 pTask
->pdb
.nbFiles
= 20;
290 /* Fill the command line */
294 cmdline
= current_envdb
.cmd_line
;
295 /* remove the first word (program name) */
297 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= current_envdb
.cmd_line
;
298 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
299 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
300 len
= strlen(cmdline
);
302 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
303 pTask
->pdb
.cmdLine
[0] = len
;
304 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
305 /* pTask->pdb.cmdLine[len+1] = 0; */
307 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
309 /* Get the compatibility flags */
311 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
313 /* Allocate a code segment alias for the TDB */
315 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
316 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
318 /* Set the owner of the environment block */
320 FarSetOwner16( pTask
->pdb
.environment
, pTask
->hPDB
);
322 /* Default DTA overwrites command line */
324 pTask
->dta
= PTR_SEG_OFF_TO_SEGPTR( pTask
->hPDB
,
325 (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
327 /* Create scheduler event for 16-bit tasks */
329 if ( !(pTask
->flags
& TDBF_WIN32
) )
330 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
332 /* Enter task handle into thread and process */
334 teb
->htask16
= hTask
;
335 if (!initial_task
) initial_task
= hTask
;
337 /* Add the task to the linked list */
340 TASK_LinkTask( hTask
);
346 /***********************************************************************
349 static void TASK_DeleteTask( HTASK16 hTask
)
354 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
357 pTask
->magic
= 0xdead; /* invalidate signature */
359 /* Free the selector aliases */
361 GLOBAL_FreeBlock( pTask
->hCSAlias
);
362 GLOBAL_FreeBlock( pTask
->hPDB
);
364 /* Free the task module */
366 FreeModule16( pTask
->hModule
);
368 /* Free the task structure itself */
370 GlobalFree16( hTask
);
372 /* Free all memory used by this task (including the 32-bit stack, */
373 /* the environment block and the thunk segments). */
375 GlobalFreeAll16( hPDB
);
378 /***********************************************************************
381 void TASK_ExitTask(void)
386 /* Enter the Win16Lock to protect global data structures */
389 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
396 TRACE("Killing task %04x\n", pTask
->hSelf
);
398 /* Perform USER cleanup */
400 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
401 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
402 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
403 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
405 /* Remove the task from the list to be sure we never switch back to it */
406 TASK_UnlinkTask( pTask
->hSelf
);
408 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
410 TRACE("this is the last task, exiting\n" );
416 TDB
* p
= (TDB
*)GlobalLock16( hFirstTask
);
419 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
420 p
= (TDB
*)GlobalLock16( p
->hNext
);
426 if ( hLockedTask
== pTask
->hSelf
)
429 TASK_DeleteTask( pTask
->hSelf
);
431 /* ... schedule another one ... */
434 /* ... and completely release the Win16Lock, just in case. */
435 ReleaseThunkLock( &lockCount
);
438 /***********************************************************************
441 * This is where all the magic of task-switching happens!
443 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
444 * This means that each 16-bit task runs until it voluntarily yields
445 * control, at which point the scheduler gets active and selects the
448 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
449 * by the standard scheduler of the underlying OS. As many 16-bit apps
450 * *rely* on the behaviour of the Windows scheduler, however, we have
451 * to simulate that behaviour.
453 * This is achieved as follows: every 16-bit task is at time (except
454 * during task creation and deletion) in one of two states: either it
455 * is the one currently running, then the global variable hCurrentTask
456 * contains its task handle, or it is not currently running, then it
457 * is blocked on a special scheduler event, a global handle which
458 * is stored in the task struct.
460 * When the current task yields control, this routine gets called. Its
461 * purpose is to determine the next task to be active, signal the
462 * scheduler event of that task, and then put the current task to sleep
463 * waiting for *its* scheduler event to get signalled again.
465 * This routine can get called in a few other special situations as well:
467 * - On creation of a 16-bit task, the Unix process executing the task
468 * calls TASK_Reschedule once it has completed its initialization.
469 * At this point, the task needs to be blocked until its scheduler
470 * event is signalled the first time (this will be done by the parent
471 * process to get the task up and running).
473 * - When the task currently running terminates itself, this routine gets
474 * called and has to schedule another task, *without* blocking the
477 * - When a 32-bit thread posts an event for a 16-bit task, it might be
478 * the case that *no* 16-bit task is currently running. In this case
479 * the task that has now an event pending is to be scheduled.
482 void TASK_Reschedule(void)
484 TDB
*pOldTask
= NULL
, *pNewTask
= NULL
;
485 HTASK16 hOldTask
= 0, hNewTask
= 0;
486 enum { MODE_YIELD
, MODE_SLEEP
, MODE_WAKEUP
} mode
;
491 /* Check what we need to do */
492 hOldTask
= GetCurrentTask();
493 pOldTask
= (TDB
*)GlobalLock16( hOldTask
);
494 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
495 hCurrentTask
, hOldTask
, (long) getpid() );
497 if ( pOldTask
&& THREAD_IsWin16( NtCurrentTeb() ) )
499 /* We are called by an active (non-deleted) 16-bit task */
501 /* If we don't even have a current task, or else the current
502 task has yielded, we'll need to schedule a new task and
503 (possibly) put the calling task to sleep. Otherwise, we
504 only block the caller. */
506 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
513 /* We are called by a deleted 16-bit task or a 32-bit thread */
515 /* The only situation where we need to do something is if we
516 now do not have a current task. Then, we'll need to wake up
517 some task that has events pending. */
519 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
529 /* Find a task to yield to: check for DirectedYield() */
530 if ( mode
== MODE_YIELD
&& pOldTask
&& pOldTask
->hYieldTo
)
532 hNewTask
= pOldTask
->hYieldTo
;
533 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
534 if( !pNewTask
|| !pNewTask
->nEvents
) hNewTask
= 0;
535 pOldTask
->hYieldTo
= 0;
538 /* Find a task to yield to: check for pending events */
539 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && !hNewTask
)
541 hNewTask
= hFirstTask
;
544 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
546 TRACE( "\ttask = %04x, events = %i\n", hNewTask
, pNewTask
->nEvents
);
548 if (pNewTask
->nEvents
) break;
549 hNewTask
= pNewTask
->hNext
;
551 if (hLockedTask
&& (hNewTask
!= hLockedTask
)) hNewTask
= 0;
554 /* If we are still the task with highest priority, just return ... */
555 if ( mode
== MODE_YIELD
&& hNewTask
&& hNewTask
== hCurrentTask
)
557 TRACE("returning to the current task (%04x)\n", hCurrentTask
);
560 /* Allow Win32 threads to thunk down even while a Win16 task is
561 in a tight PeekMessage() or Yield() loop ... */
562 ReleaseThunkLock( &lockCount
);
563 RestoreThunkLock( lockCount
);
567 /* If no task to yield to found, suspend 16-bit scheduler ... */
568 if ( mode
== MODE_YIELD
&& !hNewTask
)
570 TRACE("No currently active task\n");
574 /* If we found a task to wake up, do it ... */
575 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && hNewTask
)
577 TRACE("Switching to task %04x (%.8s)\n",
578 hNewTask
, pNewTask
->module_name
);
580 pNewTask
->priority
++;
581 TASK_UnlinkTask( hNewTask
);
582 TASK_LinkTask( hNewTask
);
583 pNewTask
->priority
--;
585 hCurrentTask
= hNewTask
;
586 NtSetEvent( pNewTask
->hEvent
, NULL
);
588 /* This is set just in case some app reads it ... */
589 pNewTask
->ss_sp
= pNewTask
->teb
->cur_stack
;
592 /* If we need to put the current task to sleep, do it ... */
593 if ( (mode
== MODE_YIELD
|| mode
== MODE_SLEEP
) && hOldTask
!= hCurrentTask
)
595 NtResetEvent( pOldTask
->hEvent
, NULL
);
597 ReleaseThunkLock( &lockCount
);
598 SYSLEVEL_CheckNotLevel( 1 );
599 WaitForSingleObject( pOldTask
->hEvent
, INFINITE
);
600 RestoreThunkLock( lockCount
);
606 /***********************************************************************
607 * InitTask (KERNEL.91)
609 * Called by the application startup code.
611 void WINAPI
InitTask16( CONTEXT86
*context
)
614 INSTANCEDATA
*pinstance
;
618 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
620 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
621 as some apps, notably Visual Basic apps, *modify* the heap/stack
622 size of the instance data segment before calling InitTask() */
624 /* Initialize the INSTANCEDATA structure */
625 pinstance
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN(CURRENT_DS
, 0);
626 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
627 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
628 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
629 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
631 /* Initialize the local heap */
632 if (LOWORD(context
->Ecx
))
633 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
635 /* Initialize implicitly loaded DLLs */
636 NE_InitializeDLLs( pTask
->hModule
);
637 NE_DllProcessAttach( pTask
->hModule
);
639 /* Registers on return are:
640 * ax 1 if OK, 0 on error
641 * cx stack limit in bytes
642 * dx cmdShow parameter
643 * si instance handle of the previous instance
644 * di instance handle of the new task
645 * es:bx pointer to command line inside PSP
647 * 0 (=%bp) is pushed on the stack
649 ptr
= stack16_push( sizeof(WORD
) );
650 *(WORD
*)PTR_SEG_TO_LIN(ptr
) = 0;
655 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
658 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
659 while ((*p
== ' ') || (*p
== '\t')) p
++;
660 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
662 context
->Ecx
= pinstance
->stacktop
;
663 context
->Edx
= pTask
->nCmdShow
;
664 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
665 context
->Edi
= (DWORD
)pTask
->hInstance
;
666 context
->SegEs
= (WORD
)pTask
->hPDB
;
670 /***********************************************************************
671 * WaitEvent (KERNEL.30)
673 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
677 if (!hTask
) hTask
= GetCurrentTask();
678 pTask
= (TDB
*)GlobalLock16( hTask
);
680 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
682 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
686 if (pTask
->nEvents
> 0)
693 /* When we get back here, we have an event */
695 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
700 /***********************************************************************
701 * PostEvent (KERNEL.31)
703 void WINAPI
PostEvent16( HTASK16 hTask
)
707 if (!hTask
) hTask
= GetCurrentTask();
708 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
710 if ( !THREAD_IsWin16( pTask
->teb
) )
712 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
718 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
719 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
724 /***********************************************************************
725 * SetPriority (KERNEL.32)
727 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
732 if (!hTask
) hTask
= GetCurrentTask();
733 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
734 newpriority
= pTask
->priority
+ delta
;
735 if (newpriority
< -32) newpriority
= -32;
736 else if (newpriority
> 15) newpriority
= 15;
738 pTask
->priority
= newpriority
+ 1;
739 TASK_UnlinkTask( hTask
);
740 TASK_LinkTask( hTask
);
745 /***********************************************************************
746 * LockCurrentTask (KERNEL.33)
748 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
750 if (bLock
) hLockedTask
= GetCurrentTask();
751 else hLockedTask
= 0;
756 /***********************************************************************
757 * IsTaskLocked (KERNEL.122)
759 HTASK16 WINAPI
IsTaskLocked16(void)
765 /***********************************************************************
766 * OldYield (KERNEL.117)
768 void WINAPI
OldYield16(void)
770 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
772 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
774 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
778 if (pCurTask
) pCurTask
->nEvents
++; /* Make sure we get back here */
780 if (pCurTask
) pCurTask
->nEvents
--;
783 /***********************************************************************
784 * WIN32_OldYield16 (KERNEL.447)
786 void WINAPI
WIN32_OldYield16(void)
790 ReleaseThunkLock(&count
);
791 RestoreThunkLock(count
);
794 /***********************************************************************
795 * DirectedYield (KERNEL.150)
797 void WINAPI
DirectedYield16( HTASK16 hTask
)
799 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
801 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
803 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
807 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
809 pCurTask
->hYieldTo
= hTask
;
812 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
815 /***********************************************************************
816 * Yield16 (KERNEL.29)
818 void WINAPI
Yield16(void)
820 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
822 if (pCurTask
) pCurTask
->hYieldTo
= 0;
823 if (pCurTask
&& pCurTask
->hQueue
&& Callout
.UserYield16
) Callout
.UserYield16();
827 /***********************************************************************
828 * KERNEL_490 (KERNEL.490)
830 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
832 if ( !someTask
) return 0;
834 FIXME("(%04x): stub\n", someTask
);
838 /***********************************************************************
839 * MakeProcInstance16 (KERNEL.51)
841 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
845 WORD hInstanceSelector
;
847 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
849 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
852 /* Win95 actually protects via SEH, but this is better for debugging */
853 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
859 if ( (!(hInstance
& 4)) ||
860 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
862 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
868 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
870 && (hInstance
!= 0xffff) )
872 /* calling MPI with a foreign DSEG is invalid ! */
873 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
874 hInstance
,CURRENT_DS
);
877 /* Always use the DSEG that MPI was entered with.
878 * We used to set hInstance to GetTaskDS16(), but this should be wrong
879 * as CURRENT_DS provides the DSEG value we need.
880 * ("calling" DS, *not* "task" DS !) */
881 hInstanceSelector
= CURRENT_DS
;
882 hInstance
= GlobalHandle16(hInstanceSelector
);
884 /* no thunking for DLLs */
885 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
888 thunkaddr
= TASK_AllocThunk( GetCurrentTask() );
889 if (!thunkaddr
) return (FARPROC16
)0;
890 thunk
= PTR_SEG_TO_LIN( thunkaddr
);
891 lfunc
= PTR_SEG_TO_LIN( func
);
893 TRACE("(%08lx,%04x): got thunk %08lx\n",
894 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
895 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
896 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
898 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
901 *thunk
++ = 0xb8; /* movw instance, %ax */
902 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
903 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
904 *thunk
++ = 0xea; /* ljmp func */
905 *(DWORD
*)thunk
= (DWORD
)func
;
906 return (FARPROC16
)thunkaddr
;
907 /* CX reg indicates if thunkaddr != NULL, implement if needed */
911 /***********************************************************************
912 * FreeProcInstance16 (KERNEL.52)
914 void WINAPI
FreeProcInstance16( FARPROC16 func
)
916 TRACE("(%08lx)\n", (DWORD
)func
);
917 TASK_FreeThunk( GetCurrentTask(), (SEGPTR
)func
);
920 /**********************************************************************
921 * TASK_GetCodeSegment
923 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
924 * and logical segment number of a given code segment.
926 * 'proc' either *is* already a pair of module handle and segment number,
927 * in which case there's nothing to do. Otherwise, it is a pointer to
928 * a function, and we need to retrieve the code segment. If the pointer
929 * happens to point to a thunk, we'll retrieve info about the code segment
930 * where the function pointed to by the thunk resides, not the thunk itself.
932 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
933 * the function the snoop code will return to ...
936 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
937 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
939 NE_MODULE
*pModule
= NULL
;
940 SEGTABLEENTRY
*pSeg
= NULL
;
943 /* Try pair of module handle / segment number */
944 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
945 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
947 segNr
= LOWORD( proc
);
948 if ( segNr
&& segNr
<= pModule
->seg_count
)
949 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
952 /* Try thunk or function */
955 BYTE
*thunk
= (BYTE
*)PTR_SEG_TO_LIN( proc
);
958 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
959 selector
= thunk
[6] + (thunk
[7] << 8);
961 selector
= HIWORD( proc
);
963 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
964 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
967 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
968 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
971 if ( pModule
&& segNr
> pModule
->seg_count
)
975 /* Abort if segment not found */
977 if ( !pModule
|| !pSeg
)
980 /* Return segment data */
982 if ( ppModule
) *ppModule
= pModule
;
983 if ( ppSeg
) *ppSeg
= pSeg
;
984 if ( pSegNr
) *pSegNr
= segNr
;
989 /**********************************************************************
990 * GetCodeHandle (KERNEL.93)
992 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
996 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
1002 /**********************************************************************
1003 * GetCodeInfo (KERNEL.104)
1005 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
1008 SEGTABLEENTRY
*pSeg
;
1011 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
1014 /* Fill in segment information */
1016 segInfo
->offSegment
= pSeg
->filepos
;
1017 segInfo
->cbSegment
= pSeg
->size
;
1018 segInfo
->flags
= pSeg
->flags
;
1019 segInfo
->cbAlloc
= pSeg
->minsize
;
1020 segInfo
->h
= pSeg
->hSeg
;
1021 segInfo
->alignShift
= pModule
->alignment
;
1023 if ( segNr
== pModule
->dgroup
)
1024 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
1026 /* Return module handle in %es */
1028 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
1034 /**********************************************************************
1035 * DefineHandleTable16 (KERNEL.94)
1037 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
1039 FIXME("(%04x): stub ?\n", wOffset
);
1044 /***********************************************************************
1045 * SetTaskQueue (KERNEL.34)
1047 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
1052 if (!hTask
) hTask
= GetCurrentTask();
1053 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1055 hPrev
= pTask
->hQueue
;
1056 pTask
->hQueue
= hQueue
;
1062 /***********************************************************************
1063 * GetTaskQueue (KERNEL.35)
1065 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1069 if (!hTask
) hTask
= GetCurrentTask();
1070 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1071 return pTask
->hQueue
;
1074 /***********************************************************************
1075 * SetThreadQueue (KERNEL.463)
1077 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1079 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1080 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1084 teb
->queue
= hQueue
;
1086 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1087 SetTaskQueue16( teb
->htask16
, hQueue
);
1093 /***********************************************************************
1094 * GetThreadQueue (KERNEL.464)
1096 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1100 teb
= NtCurrentTeb();
1101 else if ( HIWORD(thread
) )
1102 teb
= THREAD_IdToTEB( thread
);
1103 else if ( IsTask16( (HTASK16
)thread
) )
1104 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1106 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1109 /***********************************************************************
1110 * SetFastQueue (KERNEL.624)
1112 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1116 teb
= NtCurrentTeb();
1117 else if ( HIWORD(thread
) )
1118 teb
= THREAD_IdToTEB( thread
);
1119 else if ( IsTask16( (HTASK16
)thread
) )
1120 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1122 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1125 /***********************************************************************
1126 * GetFastQueue (KERNEL.625)
1128 HANDLE WINAPI
GetFastQueue16( void )
1130 TEB
*teb
= NtCurrentTeb();
1133 if (!teb
->queue
&& Callout
.InitThreadInput16
)
1134 Callout
.InitThreadInput16( 0, THREAD_IsWin16(teb
)? 4 : 5 );
1137 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1139 return (HANDLE
)teb
->queue
;
1142 /***********************************************************************
1143 * SwitchStackTo (KERNEL.108)
1145 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1148 STACK16FRAME
*oldFrame
, *newFrame
;
1149 INSTANCEDATA
*pData
;
1152 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1153 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1154 TRACE("old=%04x:%04x new=%04x:%04x\n",
1155 SELECTOROF( pTask
->teb
->cur_stack
),
1156 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1158 /* Save the old stack */
1160 oldFrame
= THREAD_STACK16( pTask
->teb
);
1161 /* pop frame + args and push bp */
1162 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1164 *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
) = oldFrame
->bp
;
1165 pData
->stacktop
= top
;
1166 pData
->stackmin
= ptr
;
1167 pData
->stackbottom
= ptr
;
1169 /* Switch to the new stack */
1171 /* Note: we need to take the 3 arguments into account; otherwise,
1172 * the stack will underflow upon return from this function.
1174 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1175 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1176 pTask
->teb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( seg
, ptr
- copySize
);
1177 newFrame
= THREAD_STACK16( pTask
->teb
);
1179 /* Copy the stack frame and the local variables to the new stack */
1181 memmove( newFrame
, oldFrame
, copySize
);
1183 *(WORD
*)PTR_SEG_OFF_TO_LIN( seg
, ptr
) = 0; /* clear previous bp */
1187 /***********************************************************************
1188 * SwitchStackBack (KERNEL.109)
1190 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1192 STACK16FRAME
*oldFrame
, *newFrame
;
1193 INSTANCEDATA
*pData
;
1195 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1197 if (!pData
->old_ss_sp
)
1199 WARN("No previous SwitchStackTo\n" );
1202 TRACE("restoring stack %04x:%04x\n",
1203 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1205 oldFrame
= CURRENT_STACK16
;
1207 /* Pop bp from the previous stack */
1209 BP_reg(context
) = *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
);
1210 pData
->old_ss_sp
+= sizeof(WORD
);
1212 /* Switch back to the old stack */
1214 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1215 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1216 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1217 pData
->old_ss_sp
= 0;
1219 /* Build a stack frame for the return */
1221 newFrame
= CURRENT_STACK16
;
1222 newFrame
->frame32
= oldFrame
->frame32
;
1223 newFrame
->module_cs
= oldFrame
->module_cs
;
1224 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1225 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1229 /***********************************************************************
1230 * GetTaskQueueDS16 (KERNEL.118)
1232 void WINAPI
GetTaskQueueDS16(void)
1234 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1238 /***********************************************************************
1239 * GetTaskQueueES16 (KERNEL.119)
1241 void WINAPI
GetTaskQueueES16(void)
1243 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1247 /***********************************************************************
1248 * GetCurrentTask (KERNEL.36)
1250 HTASK16 WINAPI
GetCurrentTask(void)
1252 return NtCurrentTeb()->htask16
;
1255 DWORD WINAPI
WIN16_GetCurrentTask(void)
1257 /* This is the version used by relay code; the first task is */
1258 /* returned in the high word of the result */
1259 return MAKELONG( GetCurrentTask(), hFirstTask
);
1263 /***********************************************************************
1264 * GetCurrentPDB (KERNEL.37)
1266 * UNDOC: returns PSP of KERNEL in high word
1268 DWORD WINAPI
GetCurrentPDB16(void)
1272 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1273 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1277 /***********************************************************************
1278 * GetCurPID16 (KERNEL.157)
1280 DWORD WINAPI
GetCurPID16( DWORD unused
)
1286 /***********************************************************************
1287 * GetInstanceData (KERNEL.54)
1289 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1291 char *ptr
= (char *)GlobalLock16( instance
);
1292 if (!ptr
|| !len
) return 0;
1293 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1294 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1299 /***********************************************************************
1300 * GetExeVersion (KERNEL.105)
1302 WORD WINAPI
GetExeVersion16(void)
1306 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1307 return pTask
->version
;
1311 /***********************************************************************
1312 * SetErrorMode16 (KERNEL.107)
1314 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1317 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1318 pTask
->error_mode
= mode
;
1319 return SetErrorMode( mode
);
1323 /***********************************************************************
1324 * GetDOSEnvironment (KERNEL.131)
1326 SEGPTR WINAPI
GetDOSEnvironment16(void)
1330 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1331 return PTR_SEG_OFF_TO_SEGPTR( pTask
->pdb
.environment
, 0 );
1335 /***********************************************************************
1336 * GetNumTasks (KERNEL.152)
1338 UINT16 WINAPI
GetNumTasks16(void)
1344 /***********************************************************************
1345 * GetTaskDS (KERNEL.155)
1347 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1348 * I don't think we need to bother with this.
1350 HINSTANCE16 WINAPI
GetTaskDS16(void)
1354 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1355 return GlobalHandleToSel16(pTask
->hInstance
);
1358 /***********************************************************************
1359 * GetDummyModuleHandleDS (KERNEL.602)
1361 WORD WINAPI
GetDummyModuleHandleDS16(void)
1366 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1367 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1368 selector
= GlobalHandleToSel16( pTask
->hModule
);
1369 CURRENT_DS
= selector
;
1373 /***********************************************************************
1374 * IsTask (KERNEL.320)
1376 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1380 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return FALSE
;
1381 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1382 return (pTask
->magic
== TDB_MAGIC
);
1386 /***********************************************************************
1387 * IsWinOldApTask16 (KERNEL.158)
1389 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1391 /* should return bit 0 of byte 0x48 in PSP */
1395 /***********************************************************************
1396 * SetTaskSignalProc (KERNEL.38)
1398 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1403 if (!hTask
) hTask
= GetCurrentTask();
1404 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return NULL
;
1405 oldProc
= pTask
->userhandler
;
1406 pTask
->userhandler
= proc
;
1410 /***********************************************************************
1411 * TASK_CallTaskSignalProc
1413 /* ### start build ### */
1414 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1415 /* ### stop build ### */
1416 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1418 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1419 if ( !pTask
|| !pTask
->userhandler
) return;
1421 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1422 hTaskOrModule
, uCode
, 0,
1423 pTask
->hInstance
, pTask
->hQueue
);
1426 /***********************************************************************
1427 * SetSigHandler (KERNEL.140)
1429 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1430 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1432 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1433 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1435 if (flag
!= 1) return 0;
1436 if (!newmode
) newhandler
= NULL
; /* Default handler */
1441 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1442 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1443 pTask
->signal_flags
= newmode
;
1444 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1445 pTask
->sighandler
= newhandler
;
1451 /***********************************************************************
1452 * GlobalNotify (KERNEL.154)
1454 * Note that GlobalNotify does _not_ return the old NotifyProc
1455 * -- contrary to LocalNotify !!
1457 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1461 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1462 pTask
->discardhandler
= proc
;
1466 /***********************************************************************
1467 * GetExePtr (KERNEL.133)
1469 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1474 /* Check for module handle */
1476 if (!(ptr
= GlobalLock16( handle
))) return 0;
1477 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1479 /* Search for this handle inside all tasks */
1481 *hTask
= hFirstTask
;
1484 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1485 if ((*hTask
== handle
) ||
1486 (pTask
->hInstance
== handle
) ||
1487 (pTask
->hQueue
== handle
) ||
1488 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1489 *hTask
= pTask
->hNext
;
1492 /* Check the owner for module handle */
1494 owner
= FarGetOwner16( handle
);
1495 if (!(ptr
= GlobalLock16( owner
))) return 0;
1496 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1498 /* Search for the owner inside all tasks */
1500 *hTask
= hFirstTask
;
1503 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1504 if ((*hTask
== owner
) ||
1505 (pTask
->hInstance
== owner
) ||
1506 (pTask
->hQueue
== owner
) ||
1507 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1508 *hTask
= pTask
->hNext
;
1514 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1517 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1518 STACK16FRAME
*frame
= CURRENT_STACK16
;
1519 frame
->ecx
= hModule
;
1520 if (hTask
) frame
->es
= hTask
;
1524 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1527 return GetExePtrHelper( handle
, &hTask
);
1531 /***********************************************************************
1532 * TaskFirst (TOOLHELP.63)
1534 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1536 lpte
->hNext
= hFirstTask
;
1537 return TaskNext16( lpte
);
1541 /***********************************************************************
1542 * TaskNext (TOOLHELP.64)
1544 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1547 INSTANCEDATA
*pInstData
;
1549 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1550 if (!lpte
->hNext
) return FALSE
;
1552 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1554 pTask
= (TDB
*)GlobalLock16( lpte
->hNext
);
1555 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1556 if (pTask
->hInstance
)
1558 lpte
->hNext
= pTask
->hNext
;
1560 pInstData
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN( GlobalHandleToSel16(pTask
->hInstance
), 0 );
1561 lpte
->hTask
= lpte
->hNext
;
1562 lpte
->hTaskParent
= pTask
->hParent
;
1563 lpte
->hInst
= pTask
->hInstance
;
1564 lpte
->hModule
= pTask
->hModule
;
1565 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1566 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1567 lpte
->wStackTop
= pInstData
->stacktop
;
1568 lpte
->wStackMinimum
= pInstData
->stackmin
;
1569 lpte
->wStackBottom
= pInstData
->stackbottom
;
1570 lpte
->wcEvents
= pTask
->nEvents
;
1571 lpte
->hQueue
= pTask
->hQueue
;
1572 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1573 lpte
->wPSPOffset
= 0x100; /*??*/
1574 lpte
->hNext
= pTask
->hNext
;
1579 /***********************************************************************
1580 * TaskFindHandle (TOOLHELP.65)
1582 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1584 lpte
->hNext
= hTask
;
1585 return TaskNext16( lpte
);
1589 /**************************************************************************
1590 * FatalAppExit16 (KERNEL.137)
1592 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1594 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1596 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1598 if (Callout
.MessageBoxA
)
1599 Callout
.MessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1601 ERR( "%s\n", debugstr_a(str
) );
1607 /***********************************************************************
1608 * TerminateApp16 (TOOLHELP.77)
1610 * See "Undocumented Windows".
1612 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1614 if (hTask
&& hTask
!= GetCurrentTask())
1616 FIXME("cannot terminate task %x\n", hTask
);
1620 if (wFlags
& NO_UAE_BOX
)
1623 old_mode
= SetErrorMode16(0);
1624 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1626 FatalAppExit16( 0, NULL
);
1628 /* hmm, we're still alive ?? */
1630 /* check undocumented flag */
1631 if (!(wFlags
& 0x8000))
1632 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1634 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1635 but let's just call ExitThread */
1640 /***********************************************************************
1641 * GetAppCompatFlags16 (KERNEL.354)
1643 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1645 return GetAppCompatFlags( hTask
);
1649 /***********************************************************************
1650 * GetAppCompatFlags (USER32.206)
1652 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1656 if (!hTask
) hTask
= GetCurrentTask();
1657 if (!(pTask
=(TDB
*)GlobalLock16( (HTASK16
)hTask
))) return 0;
1658 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1659 return pTask
->compat_flags
;