4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
27 #include "selectors.h"
28 #include "stackframe.h"
36 #include "debugtools.h"
41 DECLARE_DEBUG_CHANNEL(relay
)
42 DECLARE_DEBUG_CHANNEL(task
)
43 DECLARE_DEBUG_CHANNEL(toolhelp
)
45 /* Min. number of thunks allocated when creating a new segment */
48 /* Pointer to function to switch to a larger stack */
49 int (*IF1632_CallLargeStack
)( int (*func
)(), void *arg
) = NULL
;
51 static THHOOK DefaultThhook
= { 0 };
52 THHOOK
*pThhook
= &DefaultThhook
;
54 #define hCurrentTask (pThhook->CurTDB)
55 #define hFirstTask (pThhook->HeadTDB)
56 #define hLockedTask (pThhook->LockTDB)
58 static UINT16 nTaskCount
= 0;
61 /***********************************************************************
64 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
66 THHOOK
*pOldThhook
= pThhook
;
68 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
70 *pThhook
= *pOldThhook
;
73 /***********************************************************************
76 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
78 TDB
* pTask
= (TDB
*)GlobalLock16(hTask
);
80 if (pTask
->hNext
) return pTask
->hNext
;
81 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
84 /***********************************************************************
87 static void TASK_LinkTask( HTASK16 hTask
)
92 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
93 prevTask
= &hFirstTask
;
96 TDB
*prevTaskPtr
= (TDB
*)GlobalLock16( *prevTask
);
97 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
98 prevTask
= &prevTaskPtr
->hNext
;
100 pTask
->hNext
= *prevTask
;
106 /***********************************************************************
109 static void TASK_UnlinkTask( HTASK16 hTask
)
114 prevTask
= &hFirstTask
;
115 while (*prevTask
&& (*prevTask
!= hTask
))
117 pTask
= (TDB
*)GlobalLock16( *prevTask
);
118 prevTask
= &pTask
->hNext
;
122 pTask
= (TDB
*)GlobalLock16( *prevTask
);
123 *prevTask
= pTask
->hNext
;
130 /***********************************************************************
133 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
134 * and containing 'count' entries.
136 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
142 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
144 pThunk
->magic
= THUNK_MAGIC
;
145 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
147 for (i
= 0; i
< count
-1; i
++)
149 free
+= 8; /* Offset of next thunk */
150 pThunk
->thunks
[4*i
] = free
;
152 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
156 /***********************************************************************
159 * Allocate a thunk for MakeProcInstance().
161 static SEGPTR
TASK_AllocThunk( HTASK16 hTask
)
167 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
168 sel
= pTask
->hCSAlias
;
169 pThunk
= &pTask
->thunks
;
170 base
= (int)pThunk
- (int)pTask
;
171 while (!pThunk
->free
)
174 if (!sel
) /* Allocate a new segment */
176 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
177 pTask
->hPDB
, TRUE
, FALSE
, FALSE
);
178 if (!sel
) return (SEGPTR
)0;
179 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
182 pThunk
= (THUNKS
*)GlobalLock16( sel
);
185 base
+= pThunk
->free
;
186 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
187 return PTR_SEG_OFF_TO_SEGPTR( sel
, base
);
191 /***********************************************************************
194 * Free a MakeProcInstance() thunk.
196 static BOOL
TASK_FreeThunk( HTASK16 hTask
, SEGPTR thunk
)
202 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
203 sel
= pTask
->hCSAlias
;
204 pThunk
= &pTask
->thunks
;
205 base
= (int)pThunk
- (int)pTask
;
206 while (sel
&& (sel
!= HIWORD(thunk
)))
209 pThunk
= (THUNKS
*)GlobalLock16( sel
);
212 if (!sel
) return FALSE
;
213 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
214 pThunk
->free
= LOWORD(thunk
) - base
;
219 /***********************************************************************
222 * 32-bit entry point for a new task. This function is responsible for
223 * setting up the registers and jumping to the 16-bit entry point.
225 void TASK_CallToStart(void)
227 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
228 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
229 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
232 /* Add task to 16-bit scheduler pool */
235 /* Registers at initialization must be:
237 * bx stack size in bytes
238 * cx heap size in bytes
239 * si previous app instance
240 * di current app instance
242 * es selector to the PSP
243 * ds dgroup of the application
245 * sp top of the stack
248 memset( &context
, 0, sizeof(context
) );
249 CS_reg(&context
) = GlobalHandleToSel16(pSegTable
[pModule
->cs
- 1].hSeg
);
250 DS_reg(&context
) = GlobalHandleToSel16(pTask
->hInstance
);
251 ES_reg(&context
) = pTask
->hPDB
;
252 EIP_reg(&context
) = pModule
->ip
;
253 EBX_reg(&context
) = pModule
->stack_size
;
254 ECX_reg(&context
) = pModule
->heap_size
;
255 EDI_reg(&context
) = pTask
->hInstance
;
256 ESI_reg(&context
) = pTask
->hPrevInstance
;
258 TRACE_(task
)("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
259 CS_reg(&context
), EIP_reg(&context
), DS_reg(&context
),
260 SELECTOROF(pTask
->teb
->cur_stack
),
261 OFFSETOF(pTask
->teb
->cur_stack
) );
263 Callbacks
->CallRegisterShortProc( &context
, 0 );
267 /***********************************************************************
270 * NOTE: This routine might be called by a Win32 thread. Thus, we need
271 * to be careful to protect global data structures. We do this
272 * by entering the Win16Lock while linking the task into the
275 BOOL
TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
)
281 PDB
*pdb32
= PROCESS_Current();
283 /* Allocate the task structure */
285 hTask
= GLOBAL_Alloc( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
),
286 pModule
->self
, FALSE
, FALSE
, FALSE
);
287 if (!hTask
) return FALSE
;
288 pTask
= (TDB
*)GlobalLock16( hTask
);
290 /* Fill the task structure */
293 pTask
->hSelf
= hTask
;
296 if (pModule
->flags
& NE_FFLAGS_WIN32
)
297 pTask
->flags
|= TDBF_WIN32
;
298 if (pModule
->lpDosTask
)
299 pTask
->flags
|= TDBF_WINOLDAP
;
301 pTask
->hInstance
= pModule
->self
;
302 pTask
->hPrevInstance
= 0;
303 /* NOTE: for 16-bit tasks, the instance handles are updated later on
306 pTask
->version
= pModule
->expected_version
;
307 pTask
->hModule
= pModule
->self
;
308 pTask
->hParent
= GetCurrentTask();
309 pTask
->magic
= TDB_MAGIC
;
310 pTask
->nCmdShow
= cmdShow
;
311 pTask
->teb
= NtCurrentTeb();
312 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
313 strcpy( pTask
->curdir
, "\\" );
314 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
315 sizeof(pTask
->curdir
) - 1 );
317 /* Create the thunks block */
319 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
321 /* Copy the module name */
323 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
324 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
326 /* Allocate a selector for the PDB */
328 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
329 pModule
->self
, FALSE
, FALSE
, FALSE
, NULL
);
333 pTask
->pdb
.int20
= 0x20cd;
334 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
335 PUT_DWORD(&pTask
->pdb
.dispatcher
[1], (DWORD
)NE_GetEntryPoint(
336 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
337 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
338 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
339 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
340 pTask
->pdb
.fileHandlesPtr
=
341 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask
->hPDB
),
342 (int)&((PDB16
*)0)->fileHandles
);
343 pTask
->pdb
.hFileHandles
= 0;
344 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
345 pTask
->pdb
.environment
= pdb32
->env_db
->env_sel
;
346 pTask
->pdb
.nbFiles
= 20;
348 /* Fill the command line */
350 cmd_line
= pdb32
->env_db
->cmd_line
;
351 while (*cmd_line
&& (*cmd_line
!= ' ') && (*cmd_line
!= '\t')) cmd_line
++;
352 while ((*cmd_line
== ' ') || (*cmd_line
== '\t')) cmd_line
++;
353 lstrcpynA( pTask
->pdb
.cmdLine
+1, cmd_line
, sizeof(pTask
->pdb
.cmdLine
)-1);
354 pTask
->pdb
.cmdLine
[0] = strlen( pTask
->pdb
.cmdLine
+ 1 );
356 /* Get the compatibility flags */
358 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
360 /* Allocate a code segment alias for the TDB */
362 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
363 sizeof(TDB
), pTask
->hPDB
, TRUE
,
364 FALSE
, FALSE
, NULL
);
366 /* Set the owner of the environment block */
368 FarSetOwner16( pTask
->pdb
.environment
, pTask
->hPDB
);
370 /* Default DTA overwrites command-line */
372 pTask
->dta
= PTR_SEG_OFF_TO_SEGPTR( pTask
->hPDB
,
373 (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
375 /* Create scheduler event for 16-bit tasks */
377 if ( !(pTask
->flags
& TDBF_WIN32
) )
379 pTask
->hEvent
= CreateEventA( NULL
, TRUE
, FALSE
, NULL
);
380 pTask
->hEvent
= ConvertToGlobalHandle( pTask
->hEvent
);
383 /* Enter task handle into thread and process */
385 pTask
->teb
->htask16
= pTask
->teb
->process
->task
= hTask
;
386 TRACE_(task
)("module='%s' cmdline='%s' task=%04x\n", name
, cmd_line
, hTask
);
388 /* Add the task to the linked list */
390 SYSLEVEL_EnterWin16Lock();
391 TASK_LinkTask( hTask
);
392 SYSLEVEL_LeaveWin16Lock();
397 /***********************************************************************
400 static void TASK_DeleteTask( HTASK16 hTask
)
405 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
408 pTask
->magic
= 0xdead; /* invalidate signature */
410 /* Delete the Win32 part of the task */
412 /* PROCESS_FreePDB( pTask->teb->process ); FIXME */
413 /* K32OBJ_DecCount( &pTask->teb->header ); FIXME */
415 /* Free the selector aliases */
417 GLOBAL_FreeBlock( pTask
->hCSAlias
);
418 GLOBAL_FreeBlock( pTask
->hPDB
);
420 /* Free the task module */
422 FreeModule16( pTask
->hModule
);
424 /* Free the task structure itself */
426 GlobalFree16( hTask
);
428 /* Free all memory used by this task (including the 32-bit stack, */
429 /* the environment block and the thunk segments). */
431 GlobalFreeAll16( hPDB
);
434 /***********************************************************************
437 void TASK_KillTask( HTASK16 hTask
)
441 /* Enter the Win16Lock to protect global data structures */
442 SYSLEVEL_EnterWin16Lock();
444 if ( !hTask
) hTask
= GetCurrentTask();
445 pTask
= (TDB
*)GlobalLock16( hTask
);
448 SYSLEVEL_LeaveWin16Lock();
452 TRACE_(task
)("Killing task %04x\n", hTask
);
454 /* Delete active sockets */
457 WINSOCK_DeleteTaskWSI( pTask
, pTask
->pwsi
);
461 /* Kill DOS VM task */
462 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
463 if ( pModule
->lpDosTask
)
464 MZ_KillModule( pModule
->lpDosTask
);
468 /* Perform USER cleanup */
470 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
471 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
472 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 ); /* FIXME */
473 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
477 TRACE_(task
)("this is the last task, exiting\n" );
481 /* FIXME: Hack! Send a message to the initial task so that
482 * the GetMessage wakes up and the initial task can check whether
483 * it is the only remaining one and terminate itself ...
484 * The initial task should probably install hooks or something
485 * to get informed about task termination :-/
487 Callout
.PostAppMessage16( PROCESS_Initial()->task
, WM_NULL
, 0, 0 );
489 /* Remove the task from the list to be sure we never switch back to it */
490 TASK_UnlinkTask( hTask
);
493 TDB
* p
= (TDB
*)GlobalLock16( hFirstTask
);
496 if( p
->hYieldTo
== hTask
) p
->hYieldTo
= 0;
497 p
= (TDB
*)GlobalLock16( p
->hNext
);
503 if ( hLockedTask
== hTask
)
506 TASK_DeleteTask( hTask
);
508 /* When deleting the current task ... */
509 if ( hTask
== hCurrentTask
)
513 /* ... schedule another one ... */
516 /* ... and completely release the Win16Lock, just in case. */
517 ReleaseThunkLock( &lockCount
);
522 SYSLEVEL_LeaveWin16Lock();
525 /***********************************************************************
528 * This is where all the magic of task-switching happens!
530 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
531 * This means that each 16-bit task runs until it voluntarily yields
532 * control, at which point the scheduler gets active and selects the
535 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
536 * by the standard scheduler of the underlying OS. As many 16-bit apps
537 * *rely* on the behaviour of the Windows scheduler, however, we have
538 * to simulate that behaviour.
540 * This is achieved as follows: every 16-bit task is at time (except
541 * during task creation and deletion) in one of two states: either it
542 * is the one currently running, then the global variable hCurrentTask
543 * contains its task handle, or it is not currently running, then it
544 * is blocked on a special scheduler event, a global handle to which
545 * is stored in the task struct.
547 * When the current task yields control, this routine gets called. Its
548 * purpose is to determine the next task to be active, signal the
549 * scheduler event of that task, and then put the current task to sleep
550 * waiting for *its* scheduler event to get signalled again.
552 * This routine can get called in a few other special situations as well:
554 * - On creation of a 16-bit task, the Unix process executing the task
555 * calls TASK_Reschedule once it has completed its initialization.
556 * At this point, the task needs to be blocked until its scheduler
557 * event is signalled the first time (this will be done by the parent
558 * process to get the task up and running).
560 * - When the task currently running terminates itself, this routine gets
561 * called and has to schedule another task, *without* blocking the
564 * - When a 32-bit thread posts an event for a 16-bit task, it might be
565 * the case that *no* 16-bit task is currently running. In this case
566 * the task that has now an event pending is to be scheduled.
569 void TASK_Reschedule(void)
571 TDB
*pOldTask
= NULL
, *pNewTask
= NULL
;
572 HTASK16 hOldTask
= 0, hNewTask
= 0;
573 enum { MODE_YIELD
, MODE_SLEEP
, MODE_WAKEUP
} mode
;
576 SYSLEVEL_EnterWin16Lock();
578 /* Check what we need to do */
579 hOldTask
= GetCurrentTask();
580 pOldTask
= (TDB
*)GlobalLock16( hOldTask
);
581 TRACE_(task
)( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
582 hCurrentTask
, hOldTask
, (long) getpid() );
584 if ( pOldTask
&& THREAD_IsWin16( NtCurrentTeb() ) )
586 /* We are called by an active (non-deleted) 16-bit task */
588 /* If we don't even have a current task, or else the current
589 task has yielded, we'll need to schedule a new task and
590 (possibly) put the calling task to sleep. Otherwise, we
591 only block the caller. */
593 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
600 /* We are called by a deleted 16-bit task or a 32-bit thread */
602 /* The only situation where we need to do something is if we
603 now do not have a current task. Then, we'll need to wake up
604 some task that has events pending. */
606 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
611 SYSLEVEL_LeaveWin16Lock();
616 /* Find a task to yield to: check for DirectedYield() */
617 if ( mode
== MODE_YIELD
&& pOldTask
&& pOldTask
->hYieldTo
)
619 hNewTask
= pOldTask
->hYieldTo
;
620 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
621 if( !pNewTask
|| !pNewTask
->nEvents
) hNewTask
= 0;
622 pOldTask
->hYieldTo
= 0;
625 /* Find a task to yield to: check for pending events */
626 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && !hNewTask
)
628 hNewTask
= hFirstTask
;
631 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
633 TRACE_(task
)( "\ttask = %04x, events = %i\n",
634 hNewTask
, pNewTask
->nEvents
);
636 if (pNewTask
->nEvents
) break;
637 hNewTask
= pNewTask
->hNext
;
639 if (hLockedTask
&& (hNewTask
!= hLockedTask
)) hNewTask
= 0;
642 /* If we are still the task with highest priority, just return ... */
643 if ( mode
== MODE_YIELD
&& hNewTask
== hCurrentTask
)
645 TRACE_(task
)("returning to the current task (%04x)\n", hCurrentTask
);
646 SYSLEVEL_LeaveWin16Lock();
648 /* Allow Win32 threads to thunk down even while a Win16 task is
649 in a tight PeekMessage() or Yield() loop ... */
650 ReleaseThunkLock( &lockCount
);
651 RestoreThunkLock( lockCount
);
655 /* If no task to yield to found, suspend 16-bit scheduler ... */
656 if ( mode
== MODE_YIELD
&& !hNewTask
)
658 TRACE_(task
)("No currently active task\n");
662 /* If we found a task to wake up, do it ... */
663 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && hNewTask
)
665 TRACE_(task
)("Switching to task %04x (%.8s)\n",
666 hNewTask
, pNewTask
->module_name
);
668 pNewTask
->priority
++;
669 TASK_UnlinkTask( hNewTask
);
670 TASK_LinkTask( hNewTask
);
671 pNewTask
->priority
--;
673 hCurrentTask
= hNewTask
;
674 SetEvent( pNewTask
->hEvent
);
676 /* This is set just in case some app reads it ... */
677 pNewTask
->ss_sp
= pNewTask
->teb
->cur_stack
;
680 /* If we need to put the current task to sleep, do it ... */
681 if ( (mode
== MODE_YIELD
|| mode
== MODE_SLEEP
) && hOldTask
!= hCurrentTask
)
683 ResetEvent( pOldTask
->hEvent
);
685 ReleaseThunkLock( &lockCount
);
686 SYSLEVEL_CheckNotLevel( 1 );
687 WaitForSingleObject( pOldTask
->hEvent
, INFINITE
);
688 RestoreThunkLock( lockCount
);
691 SYSLEVEL_LeaveWin16Lock();
694 /***********************************************************************
695 * InitTask (KERNEL.91)
697 * Called by the application startup code.
699 void WINAPI
InitTask16( CONTEXT86
*context
)
702 INSTANCEDATA
*pinstance
;
705 EAX_reg(context
) = 0;
706 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
708 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
709 as some apps, notably Visual Basic apps, *modify* the heap/stack
710 size of the instance data segment before calling InitTask() */
712 /* Initialize the INSTANCEDATA structure */
713 pinstance
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN(CURRENT_DS
, 0);
714 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
);
715 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
716 pinstance
->stacktop
= ( pinstance
->stackmin
> BX_reg(context
)?
717 pinstance
->stackmin
- BX_reg(context
) : 0 ) + 150;
719 /* Initialize the local heap */
720 if ( CX_reg(context
) )
721 LocalInit16( pTask
->hInstance
, 0, CX_reg(context
) );
723 /* Initialize implicitly loaded DLLs */
724 NE_InitializeDLLs( pTask
->hModule
);
726 /* Registers on return are:
727 * ax 1 if OK, 0 on error
728 * cx stack limit in bytes
729 * dx cmdShow parameter
730 * si instance handle of the previous instance
731 * di instance handle of the new task
732 * es:bx pointer to command-line inside PSP
734 * 0 (=%bp) is pushed on the stack
736 ptr
= stack16_push( sizeof(WORD
) );
737 *(WORD
*)PTR_SEG_TO_LIN(ptr
) = 0;
738 ESP_reg(context
) -= 2;
740 EAX_reg(context
) = 1;
742 if (!pTask
->pdb
.cmdLine
[0]) EBX_reg(context
) = 0x80;
745 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
746 while ((*p
== ' ') || (*p
== '\t')) p
++;
747 EBX_reg(context
) = 0x80 + (p
- pTask
->pdb
.cmdLine
);
749 ECX_reg(context
) = pinstance
->stacktop
;
750 EDX_reg(context
) = pTask
->nCmdShow
;
751 ESI_reg(context
) = (DWORD
)pTask
->hPrevInstance
;
752 EDI_reg(context
) = (DWORD
)pTask
->hInstance
;
753 ES_reg (context
) = (WORD
)pTask
->hPDB
;
757 /***********************************************************************
758 * WaitEvent (KERNEL.30)
760 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
764 if (!hTask
) hTask
= GetCurrentTask();
765 pTask
= (TDB
*)GlobalLock16( hTask
);
767 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
769 FIXME_(task
)("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
773 if (pTask
->nEvents
> 0)
780 /* When we get back here, we have an event */
782 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
787 /***********************************************************************
788 * PostEvent (KERNEL.31)
790 void WINAPI
PostEvent16( HTASK16 hTask
)
794 if (!hTask
) hTask
= GetCurrentTask();
795 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
797 if ( !THREAD_IsWin16( pTask
->teb
) )
799 FIXME_(task
)("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
805 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
806 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
811 /***********************************************************************
812 * SetPriority (KERNEL.32)
814 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
819 if (!hTask
) hTask
= GetCurrentTask();
820 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
821 newpriority
= pTask
->priority
+ delta
;
822 if (newpriority
< -32) newpriority
= -32;
823 else if (newpriority
> 15) newpriority
= 15;
825 pTask
->priority
= newpriority
+ 1;
826 TASK_UnlinkTask( hTask
);
827 TASK_LinkTask( hTask
);
832 /***********************************************************************
833 * LockCurrentTask (KERNEL.33)
835 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
837 if (bLock
) hLockedTask
= GetCurrentTask();
838 else hLockedTask
= 0;
843 /***********************************************************************
844 * IsTaskLocked (KERNEL.122)
846 HTASK16 WINAPI
IsTaskLocked16(void)
852 /***********************************************************************
853 * OldYield (KERNEL.117)
855 void WINAPI
OldYield16(void)
857 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
859 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
861 FIXME_(task
)("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
865 if (pCurTask
) pCurTask
->nEvents
++; /* Make sure we get back here */
867 if (pCurTask
) pCurTask
->nEvents
--;
870 /***********************************************************************
871 * WIN32_OldYield16 (KERNEL.447)
873 void WINAPI
WIN32_OldYield16(void)
877 ReleaseThunkLock(&count
);
878 RestoreThunkLock(count
);
881 /***********************************************************************
882 * DirectedYield (KERNEL.150)
884 void WINAPI
DirectedYield16( HTASK16 hTask
)
886 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
888 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
890 FIXME_(task
)("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
894 TRACE_(task
)("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
896 pCurTask
->hYieldTo
= hTask
;
899 TRACE_(task
)("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
902 /***********************************************************************
903 * Yield16 (KERNEL.29)
905 void WINAPI
Yield16(void)
907 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
909 if (pCurTask
) pCurTask
->hYieldTo
= 0;
910 if (pCurTask
&& pCurTask
->hQueue
) Callout
.UserYield16();
914 /***********************************************************************
915 * KERNEL_490 (KERNEL.490)
917 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
919 if ( !someTask
) return 0;
921 FIXME_(task
)("(%04x): stub\n", someTask
);
925 /***********************************************************************
926 * MakeProcInstance16 (KERNEL.51)
928 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
934 ERR_(task
)("Ouch ! MakeProcInstance called with func == NULL !\n");
935 return (FARPROC16
)0; /* Windows seems to do the same */
937 if (!hInstance
) hInstance
= CURRENT_DS
;
938 thunkaddr
= TASK_AllocThunk( GetCurrentTask() );
939 if (!thunkaddr
) return (FARPROC16
)0;
940 thunk
= PTR_SEG_TO_LIN( thunkaddr
);
941 lfunc
= PTR_SEG_TO_LIN( func
);
943 TRACE_(task
)("(%08lx,%04x): got thunk %08lx\n",
944 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
945 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) ||
946 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58))
948 FIXME_(task
)("thunk would be useless for %p, overwriting with nop;nop;\n", func
);
949 lfunc
[0]=0x90; /* nop */
950 lfunc
[1]=0x90; /* nop */
953 *thunk
++ = 0xb8; /* movw instance, %ax */
954 *thunk
++ = (BYTE
)(hInstance
& 0xff);
955 *thunk
++ = (BYTE
)(hInstance
>> 8);
956 *thunk
++ = 0xea; /* ljmp func */
957 *(DWORD
*)thunk
= (DWORD
)func
;
958 return (FARPROC16
)thunkaddr
;
962 /***********************************************************************
963 * FreeProcInstance16 (KERNEL.52)
965 void WINAPI
FreeProcInstance16( FARPROC16 func
)
967 TRACE_(task
)("(%08lx)\n", (DWORD
)func
);
968 TASK_FreeThunk( GetCurrentTask(), (SEGPTR
)func
);
971 /**********************************************************************
972 * TASK_GetCodeSegment
974 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
975 * and logical segment number of a given code segment.
977 * 'proc' either *is* already a pair of module handle and segment number,
978 * in which case there's nothing to do. Otherwise, it is a pointer to
979 * a function, and we need to retrieve the code segment. If the pointer
980 * happens to point to a thunk, we'll retrieve info about the code segment
981 * where the function pointed to by the thunk resides, not the thunk itself.
983 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
984 * the function the snoop code will return to ...
987 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
988 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
990 NE_MODULE
*pModule
= NULL
;
991 SEGTABLEENTRY
*pSeg
= NULL
;
994 /* Try pair of module handle / segment number */
995 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
996 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
998 segNr
= LOWORD( proc
);
999 if ( segNr
&& segNr
<= pModule
->seg_count
)
1000 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
1003 /* Try thunk or function */
1006 BYTE
*thunk
= (BYTE
*)PTR_SEG_TO_LIN( proc
);
1009 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
1010 selector
= thunk
[6] + (thunk
[7] << 8);
1012 selector
= HIWORD( proc
);
1014 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
1015 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
1018 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
1019 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
1022 if ( pModule
&& segNr
> pModule
->seg_count
)
1026 /* Abort if segment not found */
1028 if ( !pModule
|| !pSeg
)
1031 /* Return segment data */
1033 if ( ppModule
) *ppModule
= pModule
;
1034 if ( ppSeg
) *ppSeg
= pSeg
;
1035 if ( pSegNr
) *pSegNr
= segNr
;
1040 /**********************************************************************
1041 * GetCodeHandle (KERNEL.93)
1043 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
1045 SEGTABLEENTRY
*pSeg
;
1047 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
1053 /**********************************************************************
1054 * GetCodeInfo (KERNEL.104)
1056 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
1059 SEGTABLEENTRY
*pSeg
;
1062 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
1065 /* Fill in segment information */
1067 segInfo
->offSegment
= pSeg
->filepos
;
1068 segInfo
->cbSegment
= pSeg
->size
;
1069 segInfo
->flags
= pSeg
->flags
;
1070 segInfo
->cbAlloc
= pSeg
->minsize
;
1071 segInfo
->h
= pSeg
->hSeg
;
1072 segInfo
->alignShift
= pModule
->alignment
;
1074 if ( segNr
== pModule
->dgroup
)
1075 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
1077 /* Return module handle in %es */
1079 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
1085 /**********************************************************************
1086 * DefineHandleTable16 (KERNEL.94)
1088 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
1090 return TRUE
; /* FIXME */
1094 /***********************************************************************
1095 * SetTaskQueue (KERNEL.34)
1097 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
1102 if (!hTask
) hTask
= GetCurrentTask();
1103 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1105 hPrev
= pTask
->hQueue
;
1106 pTask
->hQueue
= hQueue
;
1112 /***********************************************************************
1113 * GetTaskQueue (KERNEL.35)
1115 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1119 if (!hTask
) hTask
= GetCurrentTask();
1120 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1121 return pTask
->hQueue
;
1124 /***********************************************************************
1125 * SetThreadQueue (KERNEL.463)
1127 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1129 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1130 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1134 teb
->queue
= hQueue
;
1136 if ( GetTaskQueue16( teb
->process
->task
) == oldQueue
)
1137 SetTaskQueue16( teb
->process
->task
, hQueue
);
1143 /***********************************************************************
1144 * GetThreadQueue (KERNEL.464)
1146 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1150 teb
= NtCurrentTeb();
1151 else if ( HIWORD(thread
) )
1152 teb
= THREAD_IdToTEB( thread
);
1153 else if ( IsTask16( (HTASK16
)thread
) )
1154 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1156 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1159 /***********************************************************************
1160 * SetFastQueue (KERNEL.624)
1162 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1166 teb
= NtCurrentTeb();
1167 else if ( HIWORD(thread
) )
1168 teb
= THREAD_IdToTEB( thread
);
1169 else if ( IsTask16( (HTASK16
)thread
) )
1170 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1172 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1175 /***********************************************************************
1176 * GetFastQueue (KERNEL.625)
1178 HANDLE WINAPI
GetFastQueue16( void )
1180 TEB
*teb
= NtCurrentTeb();
1184 Callout
.InitThreadInput16( 0, THREAD_IsWin16(teb
)? 4 : 5 );
1187 FIXME_(task
)("(): should initialize thread-local queue, expect failure!\n" );
1189 return (HANDLE
)teb
->queue
;
1192 /***********************************************************************
1193 * SwitchStackTo (KERNEL.108)
1195 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1198 STACK16FRAME
*oldFrame
, *newFrame
;
1199 INSTANCEDATA
*pData
;
1202 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1203 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1204 TRACE_(task
)("old=%04x:%04x new=%04x:%04x\n",
1205 SELECTOROF( pTask
->teb
->cur_stack
),
1206 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1208 /* Save the old stack */
1210 oldFrame
= THREAD_STACK16( pTask
->teb
);
1211 /* pop frame + args and push bp */
1212 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1214 *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
) = oldFrame
->bp
;
1215 pData
->stacktop
= top
;
1216 pData
->stackmin
= ptr
;
1217 pData
->stackbottom
= ptr
;
1219 /* Switch to the new stack */
1221 /* Note: we need to take the 3 arguments into account; otherwise,
1222 * the stack will underflow upon return from this function.
1224 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1225 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1226 pTask
->teb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( seg
, ptr
- copySize
);
1227 newFrame
= THREAD_STACK16( pTask
->teb
);
1229 /* Copy the stack frame and the local variables to the new stack */
1231 memmove( newFrame
, oldFrame
, copySize
);
1233 *(WORD
*)PTR_SEG_OFF_TO_LIN( seg
, ptr
) = 0; /* clear previous bp */
1237 /***********************************************************************
1238 * SwitchStackBack (KERNEL.109)
1240 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1242 STACK16FRAME
*oldFrame
, *newFrame
;
1243 INSTANCEDATA
*pData
;
1245 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1247 if (!pData
->old_ss_sp
)
1249 WARN_(task
)("No previous SwitchStackTo\n" );
1252 TRACE_(task
)("restoring stack %04x:%04x\n",
1253 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1255 oldFrame
= CURRENT_STACK16
;
1257 /* Pop bp from the previous stack */
1259 BP_reg(context
) = *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
);
1260 pData
->old_ss_sp
+= sizeof(WORD
);
1262 /* Switch back to the old stack */
1264 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1265 SS_reg(context
) = SELECTOROF(pData
->old_ss_sp
);
1266 ESP_reg(context
) = OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1267 pData
->old_ss_sp
= 0;
1269 /* Build a stack frame for the return */
1271 newFrame
= CURRENT_STACK16
;
1272 newFrame
->frame32
= oldFrame
->frame32
;
1273 newFrame
->module_cs
= oldFrame
->module_cs
;
1274 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1275 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1279 /***********************************************************************
1280 * GetTaskQueueDS16 (KERNEL.118)
1282 void WINAPI
GetTaskQueueDS16(void)
1284 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1288 /***********************************************************************
1289 * GetTaskQueueES16 (KERNEL.119)
1291 void WINAPI
GetTaskQueueES16(void)
1293 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1297 /***********************************************************************
1298 * GetCurrentTask (KERNEL.36)
1300 HTASK16 WINAPI
GetCurrentTask(void)
1302 return PROCESS_Current()->task
;
1305 DWORD WINAPI
WIN16_GetCurrentTask(void)
1307 /* This is the version used by relay code; the first task is */
1308 /* returned in the high word of the result */
1309 return MAKELONG( GetCurrentTask(), hFirstTask
);
1313 /***********************************************************************
1314 * GetCurrentPDB (KERNEL.37)
1316 * UNDOC: returns PSP of KERNEL in high word
1318 DWORD WINAPI
GetCurrentPDB16(void)
1322 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1323 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1327 /***********************************************************************
1328 * GetCurPID16 (KERNEL.157)
1330 DWORD WINAPI
GetCurPID16( DWORD unused
)
1336 /***********************************************************************
1337 * GetInstanceData (KERNEL.54)
1339 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1341 char *ptr
= (char *)GlobalLock16( instance
);
1342 if (!ptr
|| !len
) return 0;
1343 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1344 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1349 /***********************************************************************
1350 * GetExeVersion (KERNEL.105)
1352 WORD WINAPI
GetExeVersion16(void)
1356 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1357 return pTask
->version
;
1361 /***********************************************************************
1362 * SetErrorMode16 (KERNEL.107)
1364 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1367 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1368 pTask
->error_mode
= mode
;
1369 return SetErrorMode( mode
);
1373 /***********************************************************************
1374 * GetDOSEnvironment (KERNEL.131)
1376 SEGPTR WINAPI
GetDOSEnvironment16(void)
1380 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1381 return PTR_SEG_OFF_TO_SEGPTR( pTask
->pdb
.environment
, 0 );
1385 /***********************************************************************
1386 * GetNumTasks (KERNEL.152)
1388 UINT16 WINAPI
GetNumTasks16(void)
1394 /***********************************************************************
1395 * GetTaskDS (KERNEL.155)
1397 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1398 * I don't think we need to bother with this.
1400 HINSTANCE16 WINAPI
GetTaskDS16(void)
1404 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1405 return pTask
->hInstance
;
1408 /***********************************************************************
1409 * GetDummyModuleHandleDS (KERNEL.602)
1411 WORD WINAPI
GetDummyModuleHandleDS16(void)
1416 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1417 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1418 selector
= GlobalHandleToSel16( pTask
->hModule
);
1419 CURRENT_DS
= selector
;
1423 /***********************************************************************
1424 * IsTask (KERNEL.320)
1426 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1430 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return FALSE
;
1431 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1432 return (pTask
->magic
== TDB_MAGIC
);
1436 /***********************************************************************
1437 * IsWinOldApTask16 (KERNEL.158)
1439 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1441 /* should return bit 0 of byte 0x48 in PSP */
1445 /***********************************************************************
1446 * SetTaskSignalProc (KERNEL.38)
1448 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1453 if (!hTask
) hTask
= GetCurrentTask();
1454 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return NULL
;
1455 oldProc
= pTask
->userhandler
;
1456 pTask
->userhandler
= proc
;
1460 /***********************************************************************
1461 * TASK_CallTaskSignalProc
1463 /* ### start build ### */
1464 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1465 /* ### stop build ### */
1466 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1468 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1469 if ( !pTask
|| !pTask
->userhandler
) return;
1471 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1472 hTaskOrModule
, uCode
, 0,
1473 pTask
->hInstance
, pTask
->hQueue
);
1476 /***********************************************************************
1477 * SetSigHandler (KERNEL.140)
1479 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1480 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1482 FIXME_(task
)("(%p,%p,%p,%d,%d), unimplemented.\n",
1483 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1485 if (flag
!= 1) return 0;
1486 if (!newmode
) newhandler
= NULL
; /* Default handler */
1491 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1492 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1493 pTask
->signal_flags
= newmode
;
1494 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1495 pTask
->sighandler
= newhandler
;
1501 /***********************************************************************
1502 * GlobalNotify (KERNEL.154)
1504 * Note that GlobalNotify does _not_ return the old NotifyProc
1505 * -- contrary to LocalNotify !!
1507 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1511 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1512 pTask
->discardhandler
= proc
;
1516 /***********************************************************************
1517 * GetExePtr (KERNEL.133)
1519 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1524 /* Check for module handle */
1526 if (!(ptr
= GlobalLock16( handle
))) return 0;
1527 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1529 /* Search for this handle inside all tasks */
1531 *hTask
= hFirstTask
;
1534 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1535 if ((*hTask
== handle
) ||
1536 (pTask
->hInstance
== handle
) ||
1537 (pTask
->hQueue
== handle
) ||
1538 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1539 *hTask
= pTask
->hNext
;
1542 /* Check the owner for module handle */
1544 owner
= FarGetOwner16( handle
);
1545 if (!(ptr
= GlobalLock16( owner
))) return 0;
1546 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1548 /* Search for the owner inside all tasks */
1550 *hTask
= hFirstTask
;
1553 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1554 if ((*hTask
== owner
) ||
1555 (pTask
->hInstance
== owner
) ||
1556 (pTask
->hQueue
== owner
) ||
1557 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1558 *hTask
= pTask
->hNext
;
1564 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1567 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1568 STACK16FRAME
*frame
= CURRENT_STACK16
;
1569 frame
->ecx
= hModule
;
1570 if (hTask
) frame
->es
= hTask
;
1574 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1577 return GetExePtrHelper( handle
, &hTask
);
1581 /***********************************************************************
1582 * TaskFirst (TOOLHELP.63)
1584 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1586 lpte
->hNext
= hFirstTask
;
1587 return TaskNext16( lpte
);
1591 /***********************************************************************
1592 * TaskNext (TOOLHELP.64)
1594 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1597 INSTANCEDATA
*pInstData
;
1599 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1600 if (!lpte
->hNext
) return FALSE
;
1601 pTask
= (TDB
*)GlobalLock16( lpte
->hNext
);
1602 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1603 pInstData
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN( pTask
->hInstance
, 0 );
1604 lpte
->hTask
= lpte
->hNext
;
1605 lpte
->hTaskParent
= pTask
->hParent
;
1606 lpte
->hInst
= pTask
->hInstance
;
1607 lpte
->hModule
= pTask
->hModule
;
1608 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1609 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1610 lpte
->wStackTop
= pInstData
->stacktop
;
1611 lpte
->wStackMinimum
= pInstData
->stackmin
;
1612 lpte
->wStackBottom
= pInstData
->stackbottom
;
1613 lpte
->wcEvents
= pTask
->nEvents
;
1614 lpte
->hQueue
= pTask
->hQueue
;
1615 strncpy( lpte
->szModule
, pTask
->module_name
, 8 );
1616 lpte
->szModule
[8] = '\0';
1617 lpte
->wPSPOffset
= 0x100; /*??*/
1618 lpte
->hNext
= pTask
->hNext
;
1623 /***********************************************************************
1624 * TaskFindHandle (TOOLHELP.65)
1626 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1628 lpte
->hNext
= hTask
;
1629 return TaskNext16( lpte
);
1633 /***********************************************************************
1634 * GetAppCompatFlags16 (KERNEL.354)
1636 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1638 return GetAppCompatFlags( hTask
);
1642 /***********************************************************************
1643 * GetAppCompatFlags32 (USER32.206)
1645 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1649 if (!hTask
) hTask
= GetCurrentTask();
1650 if (!(pTask
=(TDB
*)GlobalLock16( (HTASK16
)hTask
))) return 0;
1651 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1652 return pTask
->compat_flags
;