4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
26 #include "selectors.h"
27 #include "stackframe.h"
34 #include "debugtools.h"
39 DEFAULT_DEBUG_CHANNEL(task
)
40 DECLARE_DEBUG_CHANNEL(relay
)
41 DECLARE_DEBUG_CHANNEL(toolhelp
)
43 /* Min. number of thunks allocated when creating a new segment */
46 /* Pointer to function to switch to a larger stack */
47 int (*IF1632_CallLargeStack
)( int (*func
)(), void *arg
) = NULL
;
49 static THHOOK DefaultThhook
= { 0 };
50 THHOOK
*pThhook
= &DefaultThhook
;
52 #define hCurrentTask (pThhook->CurTDB)
53 #define hFirstTask (pThhook->HeadTDB)
54 #define hLockedTask (pThhook->LockTDB)
56 static UINT16 nTaskCount
= 0;
58 static HTASK initial_task
;
60 /***********************************************************************
63 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
65 THHOOK
*pOldThhook
= pThhook
;
67 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
69 *pThhook
= *pOldThhook
;
72 /***********************************************************************
75 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
77 TDB
* pTask
= (TDB
*)GlobalLock16(hTask
);
79 if (pTask
->hNext
) return pTask
->hNext
;
80 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
83 /***********************************************************************
86 static void TASK_LinkTask( HTASK16 hTask
)
91 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
92 prevTask
= &hFirstTask
;
95 TDB
*prevTaskPtr
= (TDB
*)GlobalLock16( *prevTask
);
96 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
97 prevTask
= &prevTaskPtr
->hNext
;
99 pTask
->hNext
= *prevTask
;
105 /***********************************************************************
108 static void TASK_UnlinkTask( HTASK16 hTask
)
113 prevTask
= &hFirstTask
;
114 while (*prevTask
&& (*prevTask
!= hTask
))
116 pTask
= (TDB
*)GlobalLock16( *prevTask
);
117 prevTask
= &pTask
->hNext
;
121 pTask
= (TDB
*)GlobalLock16( *prevTask
);
122 *prevTask
= pTask
->hNext
;
129 /***********************************************************************
132 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
133 * and containing 'count' entries.
135 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
141 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
143 pThunk
->magic
= THUNK_MAGIC
;
144 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
146 for (i
= 0; i
< count
-1; i
++)
148 free
+= 8; /* Offset of next thunk */
149 pThunk
->thunks
[4*i
] = free
;
151 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
155 /***********************************************************************
158 * Allocate a thunk for MakeProcInstance().
160 static SEGPTR
TASK_AllocThunk( HTASK16 hTask
)
166 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
167 sel
= pTask
->hCSAlias
;
168 pThunk
= &pTask
->thunks
;
169 base
= (int)pThunk
- (int)pTask
;
170 while (!pThunk
->free
)
173 if (!sel
) /* Allocate a new segment */
175 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
176 pTask
->hPDB
, TRUE
, FALSE
, FALSE
);
177 if (!sel
) return (SEGPTR
)0;
178 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
181 pThunk
= (THUNKS
*)GlobalLock16( sel
);
184 base
+= pThunk
->free
;
185 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
186 return PTR_SEG_OFF_TO_SEGPTR( sel
, base
);
190 /***********************************************************************
193 * Free a MakeProcInstance() thunk.
195 static BOOL
TASK_FreeThunk( HTASK16 hTask
, SEGPTR thunk
)
201 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
202 sel
= pTask
->hCSAlias
;
203 pThunk
= &pTask
->thunks
;
204 base
= (int)pThunk
- (int)pTask
;
205 while (sel
&& (sel
!= HIWORD(thunk
)))
208 pThunk
= (THUNKS
*)GlobalLock16( sel
);
211 if (!sel
) return FALSE
;
212 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
213 pThunk
->free
= LOWORD(thunk
) - base
;
218 /***********************************************************************
221 * 32-bit entry point for a new task. This function is responsible for
222 * setting up the registers and jumping to the 16-bit entry point.
224 void TASK_CallToStart(void)
226 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
227 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
228 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
231 SYSLEVEL_EnterWin16Lock();
233 /* Add task to 16-bit scheduler pool if necessary */
234 if ( hCurrentTask
!= GetCurrentTask() )
237 /* Registers at initialization must be:
239 * bx stack size in bytes
240 * cx heap size in bytes
241 * si previous app instance
242 * di current app instance
244 * es selector to the PSP
245 * ds dgroup of the application
247 * sp top of the stack
250 memset( &context
, 0, sizeof(context
) );
251 CS_reg(&context
) = GlobalHandleToSel16(pSegTable
[pModule
->cs
- 1].hSeg
);
252 DS_reg(&context
) = GlobalHandleToSel16(pTask
->hInstance
);
253 ES_reg(&context
) = pTask
->hPDB
;
254 EIP_reg(&context
) = pModule
->ip
;
255 EBX_reg(&context
) = pModule
->stack_size
;
256 ECX_reg(&context
) = pModule
->heap_size
;
257 EDI_reg(&context
) = pTask
->hInstance
;
258 ESI_reg(&context
) = pTask
->hPrevInstance
;
260 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
261 CS_reg(&context
), EIP_reg(&context
), DS_reg(&context
),
262 SELECTOROF(pTask
->teb
->cur_stack
),
263 OFFSETOF(pTask
->teb
->cur_stack
) );
265 Callbacks
->CallRegisterShortProc( &context
, 0 );
269 /***********************************************************************
272 * NOTE: This routine might be called by a Win32 thread. Thus, we need
273 * to be careful to protect global data structures. We do this
274 * by entering the Win16Lock while linking the task into the
277 BOOL
TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
282 PDB
*pdb32
= PROCESS_Current();
284 /* Allocate the task structure */
286 hTask
= GLOBAL_Alloc( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
),
287 pModule
->self
, FALSE
, FALSE
, FALSE
);
288 if (!hTask
) return FALSE
;
289 pTask
= (TDB
*)GlobalLock16( hTask
);
291 /* Fill the task structure */
294 pTask
->hSelf
= hTask
;
297 if (teb
->tibflags
& TEBF_WIN32
) pTask
->flags
|= TDBF_WIN32
;
298 if (pModule
->lpDosTask
) pTask
->flags
|= TDBF_WINOLDAP
;
300 pTask
->hInstance
= pModule
->self
;
301 pTask
->hPrevInstance
= 0;
302 /* NOTE: for 16-bit tasks, the instance handles are updated later on
305 pTask
->version
= pModule
->expected_version
;
306 pTask
->hModule
= pModule
->self
;
307 pTask
->hParent
= GetCurrentTask();
308 pTask
->magic
= TDB_MAGIC
;
309 pTask
->nCmdShow
= cmdShow
;
311 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
312 strcpy( pTask
->curdir
, "\\" );
313 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
314 sizeof(pTask
->curdir
) - 1 );
316 /* Create the thunks block */
318 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
320 /* Copy the module name */
322 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
323 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
325 /* Allocate a selector for the PDB */
327 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
328 pModule
->self
, FALSE
, FALSE
, FALSE
, NULL
);
332 pTask
->pdb
.int20
= 0x20cd;
333 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
334 PUT_DWORD(&pTask
->pdb
.dispatcher
[1], (DWORD
)NE_GetEntryPoint(
335 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
336 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
337 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
338 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
339 pTask
->pdb
.fileHandlesPtr
=
340 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask
->hPDB
),
341 (int)&((PDB16
*)0)->fileHandles
);
342 pTask
->pdb
.hFileHandles
= 0;
343 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
344 pTask
->pdb
.environment
= pdb32
->env_db
->env_sel
;
345 pTask
->pdb
.nbFiles
= 20;
347 /* Fill the command line */
351 cmdline
= pdb32
->env_db
->cmd_line
;
352 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
353 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
354 len
= strlen(cmdline
);
356 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
357 pTask
->pdb
.cmdLine
[0] = len
;
358 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
359 /* pTask->pdb.cmdLine[len+1] = 0; */
361 /* Get the compatibility flags */
363 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
365 /* Allocate a code segment alias for the TDB */
367 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
368 sizeof(TDB
), pTask
->hPDB
, TRUE
,
369 FALSE
, FALSE
, NULL
);
371 /* Set the owner of the environment block */
373 FarSetOwner16( pTask
->pdb
.environment
, pTask
->hPDB
);
375 /* Default DTA overwrites command-line */
377 pTask
->dta
= PTR_SEG_OFF_TO_SEGPTR( pTask
->hPDB
,
378 (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
380 /* Create scheduler event for 16-bit tasks */
382 if ( !(pTask
->flags
& TDBF_WIN32
) )
384 pTask
->hEvent
= CreateEventA( NULL
, TRUE
, FALSE
, NULL
);
385 pTask
->hEvent
= ConvertToGlobalHandle( pTask
->hEvent
);
388 /* Enter task handle into thread and process */
390 teb
->htask16
= hTask
;
391 if (!initial_task
) initial_task
= hTask
;
393 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, *cmdline
, cmdline
+1, hTask
);
395 /* Add the task to the linked list */
397 SYSLEVEL_EnterWin16Lock();
398 TASK_LinkTask( hTask
);
399 SYSLEVEL_LeaveWin16Lock();
404 /***********************************************************************
407 static void TASK_DeleteTask( HTASK16 hTask
)
412 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
415 pTask
->magic
= 0xdead; /* invalidate signature */
417 /* Free the selector aliases */
419 GLOBAL_FreeBlock( pTask
->hCSAlias
);
420 GLOBAL_FreeBlock( pTask
->hPDB
);
422 /* Free the task module */
424 FreeModule16( pTask
->hModule
);
426 /* Free the task structure itself */
428 GlobalFree16( hTask
);
430 /* Free all memory used by this task (including the 32-bit stack, */
431 /* the environment block and the thunk segments). */
433 GlobalFreeAll16( hPDB
);
436 /***********************************************************************
439 void TASK_KillTask( HTASK16 hTask
)
443 /* Enter the Win16Lock to protect global data structures */
444 SYSLEVEL_EnterWin16Lock();
446 if ( !hTask
) hTask
= GetCurrentTask();
447 pTask
= (TDB
*)GlobalLock16( hTask
);
450 SYSLEVEL_LeaveWin16Lock();
454 TRACE("Killing task %04x\n", hTask
);
458 /* Kill DOS VM task */
459 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
460 if ( pModule
->lpDosTask
)
461 MZ_KillModule( pModule
->lpDosTask
);
465 /* Perform USER cleanup */
467 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
468 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
469 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
470 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
474 TRACE("this is the last task, exiting\n" );
478 /* FIXME: Hack! Send a message to the initial task so that
479 * the GetMessage wakes up and the initial task can check whether
480 * it is the only remaining one and terminate itself ...
481 * The initial task should probably install hooks or something
482 * to get informed about task termination :-/
484 Callout
.PostAppMessage16( initial_task
, WM_NULL
, 0, 0 );
486 /* Remove the task from the list to be sure we never switch back to it */
487 TASK_UnlinkTask( hTask
);
490 TDB
* p
= (TDB
*)GlobalLock16( hFirstTask
);
493 if( p
->hYieldTo
== hTask
) p
->hYieldTo
= 0;
494 p
= (TDB
*)GlobalLock16( p
->hNext
);
500 if ( hLockedTask
== hTask
)
503 TASK_DeleteTask( hTask
);
505 /* When deleting the current task ... */
506 if ( hTask
== hCurrentTask
)
510 /* ... schedule another one ... */
513 /* ... and completely release the Win16Lock, just in case. */
514 ReleaseThunkLock( &lockCount
);
519 SYSLEVEL_LeaveWin16Lock();
522 /***********************************************************************
525 * This is where all the magic of task-switching happens!
527 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
528 * This means that each 16-bit task runs until it voluntarily yields
529 * control, at which point the scheduler gets active and selects the
532 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
533 * by the standard scheduler of the underlying OS. As many 16-bit apps
534 * *rely* on the behaviour of the Windows scheduler, however, we have
535 * to simulate that behaviour.
537 * This is achieved as follows: every 16-bit task is at time (except
538 * during task creation and deletion) in one of two states: either it
539 * is the one currently running, then the global variable hCurrentTask
540 * contains its task handle, or it is not currently running, then it
541 * is blocked on a special scheduler event, a global handle to which
542 * is stored in the task struct.
544 * When the current task yields control, this routine gets called. Its
545 * purpose is to determine the next task to be active, signal the
546 * scheduler event of that task, and then put the current task to sleep
547 * waiting for *its* scheduler event to get signalled again.
549 * This routine can get called in a few other special situations as well:
551 * - On creation of a 16-bit task, the Unix process executing the task
552 * calls TASK_Reschedule once it has completed its initialization.
553 * At this point, the task needs to be blocked until its scheduler
554 * event is signalled the first time (this will be done by the parent
555 * process to get the task up and running).
557 * - When the task currently running terminates itself, this routine gets
558 * called and has to schedule another task, *without* blocking the
561 * - When a 32-bit thread posts an event for a 16-bit task, it might be
562 * the case that *no* 16-bit task is currently running. In this case
563 * the task that has now an event pending is to be scheduled.
566 void TASK_Reschedule(void)
568 TDB
*pOldTask
= NULL
, *pNewTask
= NULL
;
569 HTASK16 hOldTask
= 0, hNewTask
= 0;
570 enum { MODE_YIELD
, MODE_SLEEP
, MODE_WAKEUP
} mode
;
573 SYSLEVEL_EnterWin16Lock();
575 /* Check what we need to do */
576 hOldTask
= GetCurrentTask();
577 pOldTask
= (TDB
*)GlobalLock16( hOldTask
);
578 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
579 hCurrentTask
, hOldTask
, (long) getpid() );
581 if ( pOldTask
&& THREAD_IsWin16( NtCurrentTeb() ) )
583 /* We are called by an active (non-deleted) 16-bit task */
585 /* If we don't even have a current task, or else the current
586 task has yielded, we'll need to schedule a new task and
587 (possibly) put the calling task to sleep. Otherwise, we
588 only block the caller. */
590 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
597 /* We are called by a deleted 16-bit task or a 32-bit thread */
599 /* The only situation where we need to do something is if we
600 now do not have a current task. Then, we'll need to wake up
601 some task that has events pending. */
603 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
608 SYSLEVEL_LeaveWin16Lock();
613 /* Find a task to yield to: check for DirectedYield() */
614 if ( mode
== MODE_YIELD
&& pOldTask
&& pOldTask
->hYieldTo
)
616 hNewTask
= pOldTask
->hYieldTo
;
617 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
618 if( !pNewTask
|| !pNewTask
->nEvents
) hNewTask
= 0;
619 pOldTask
->hYieldTo
= 0;
622 /* Find a task to yield to: check for pending events */
623 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && !hNewTask
)
625 hNewTask
= hFirstTask
;
628 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
630 TRACE( "\ttask = %04x, events = %i\n", hNewTask
, pNewTask
->nEvents
);
632 if (pNewTask
->nEvents
) break;
633 hNewTask
= pNewTask
->hNext
;
635 if (hLockedTask
&& (hNewTask
!= hLockedTask
)) hNewTask
= 0;
638 /* If we are still the task with highest priority, just return ... */
639 if ( mode
== MODE_YIELD
&& hNewTask
&& hNewTask
== hCurrentTask
)
641 TRACE("returning to the current task (%04x)\n", hCurrentTask
);
642 SYSLEVEL_LeaveWin16Lock();
644 /* Allow Win32 threads to thunk down even while a Win16 task is
645 in a tight PeekMessage() or Yield() loop ... */
646 ReleaseThunkLock( &lockCount
);
647 RestoreThunkLock( lockCount
);
651 /* If no task to yield to found, suspend 16-bit scheduler ... */
652 if ( mode
== MODE_YIELD
&& !hNewTask
)
654 TRACE("No currently active task\n");
658 /* If we found a task to wake up, do it ... */
659 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && hNewTask
)
661 TRACE("Switching to task %04x (%.8s)\n",
662 hNewTask
, pNewTask
->module_name
);
664 pNewTask
->priority
++;
665 TASK_UnlinkTask( hNewTask
);
666 TASK_LinkTask( hNewTask
);
667 pNewTask
->priority
--;
669 hCurrentTask
= hNewTask
;
670 SetEvent( pNewTask
->hEvent
);
672 /* This is set just in case some app reads it ... */
673 pNewTask
->ss_sp
= pNewTask
->teb
->cur_stack
;
676 /* If we need to put the current task to sleep, do it ... */
677 if ( (mode
== MODE_YIELD
|| mode
== MODE_SLEEP
) && hOldTask
!= hCurrentTask
)
679 ResetEvent( pOldTask
->hEvent
);
681 ReleaseThunkLock( &lockCount
);
682 SYSLEVEL_CheckNotLevel( 1 );
683 WaitForSingleObject( pOldTask
->hEvent
, INFINITE
);
684 RestoreThunkLock( lockCount
);
687 SYSLEVEL_LeaveWin16Lock();
690 /***********************************************************************
691 * InitTask (KERNEL.91)
693 * Called by the application startup code.
695 void WINAPI
InitTask16( CONTEXT86
*context
)
698 INSTANCEDATA
*pinstance
;
701 EAX_reg(context
) = 0;
702 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
704 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
705 as some apps, notably Visual Basic apps, *modify* the heap/stack
706 size of the instance data segment before calling InitTask() */
708 /* Initialize the INSTANCEDATA structure */
709 pinstance
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN(CURRENT_DS
, 0);
710 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
711 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
712 pinstance
->stacktop
= ( pinstance
->stackmin
> BX_reg(context
)?
713 pinstance
->stackmin
- BX_reg(context
) : 0 ) + 150;
715 /* Initialize the local heap */
716 if ( CX_reg(context
) )
717 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, CX_reg(context
) );
719 /* Initialize implicitly loaded DLLs */
720 NE_InitializeDLLs( pTask
->hModule
);
721 NE_DllProcessAttach( pTask
->hModule
);
723 /* Registers on return are:
724 * ax 1 if OK, 0 on error
725 * cx stack limit in bytes
726 * dx cmdShow parameter
727 * si instance handle of the previous instance
728 * di instance handle of the new task
729 * es:bx pointer to command-line inside PSP
731 * 0 (=%bp) is pushed on the stack
733 ptr
= stack16_push( sizeof(WORD
) );
734 *(WORD
*)PTR_SEG_TO_LIN(ptr
) = 0;
735 ESP_reg(context
) -= 2;
737 EAX_reg(context
) = 1;
739 if (!pTask
->pdb
.cmdLine
[0]) EBX_reg(context
) = 0x80;
742 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
743 while ((*p
== ' ') || (*p
== '\t')) p
++;
744 EBX_reg(context
) = 0x80 + (p
- pTask
->pdb
.cmdLine
);
746 ECX_reg(context
) = pinstance
->stacktop
;
747 EDX_reg(context
) = pTask
->nCmdShow
;
748 ESI_reg(context
) = (DWORD
)pTask
->hPrevInstance
;
749 EDI_reg(context
) = (DWORD
)pTask
->hInstance
;
750 ES_reg (context
) = (WORD
)pTask
->hPDB
;
754 /***********************************************************************
755 * WaitEvent (KERNEL.30)
757 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
761 if (!hTask
) hTask
= GetCurrentTask();
762 pTask
= (TDB
*)GlobalLock16( hTask
);
764 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
766 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
770 if (pTask
->nEvents
> 0)
777 /* When we get back here, we have an event */
779 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
784 /***********************************************************************
785 * PostEvent (KERNEL.31)
787 void WINAPI
PostEvent16( HTASK16 hTask
)
791 if (!hTask
) hTask
= GetCurrentTask();
792 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
794 if ( !THREAD_IsWin16( pTask
->teb
) )
796 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
802 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
803 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
808 /***********************************************************************
809 * SetPriority (KERNEL.32)
811 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
816 if (!hTask
) hTask
= GetCurrentTask();
817 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
818 newpriority
= pTask
->priority
+ delta
;
819 if (newpriority
< -32) newpriority
= -32;
820 else if (newpriority
> 15) newpriority
= 15;
822 pTask
->priority
= newpriority
+ 1;
823 TASK_UnlinkTask( hTask
);
824 TASK_LinkTask( hTask
);
829 /***********************************************************************
830 * LockCurrentTask (KERNEL.33)
832 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
834 if (bLock
) hLockedTask
= GetCurrentTask();
835 else hLockedTask
= 0;
840 /***********************************************************************
841 * IsTaskLocked (KERNEL.122)
843 HTASK16 WINAPI
IsTaskLocked16(void)
849 /***********************************************************************
850 * OldYield (KERNEL.117)
852 void WINAPI
OldYield16(void)
854 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
856 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
858 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
862 if (pCurTask
) pCurTask
->nEvents
++; /* Make sure we get back here */
864 if (pCurTask
) pCurTask
->nEvents
--;
867 /***********************************************************************
868 * WIN32_OldYield16 (KERNEL.447)
870 void WINAPI
WIN32_OldYield16(void)
874 ReleaseThunkLock(&count
);
875 RestoreThunkLock(count
);
878 /***********************************************************************
879 * DirectedYield (KERNEL.150)
881 void WINAPI
DirectedYield16( HTASK16 hTask
)
883 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
885 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
887 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
891 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
893 pCurTask
->hYieldTo
= hTask
;
896 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
899 /***********************************************************************
900 * Yield16 (KERNEL.29)
902 void WINAPI
Yield16(void)
904 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
906 if (pCurTask
) pCurTask
->hYieldTo
= 0;
907 if (pCurTask
&& pCurTask
->hQueue
) Callout
.UserYield16();
911 /***********************************************************************
912 * KERNEL_490 (KERNEL.490)
914 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
916 if ( !someTask
) return 0;
918 FIXME("(%04x): stub\n", someTask
);
922 /***********************************************************************
923 * MakeProcInstance16 (KERNEL.51)
925 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
929 WORD hInstanceSelector
;
931 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
933 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
936 /* Win95 actually protects via SEH, but this is better for debugging */
937 ERR("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
943 if ( (!(hInstance
& 4)) ||
944 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
946 ERR("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
952 if ( (CURRENT_DS
!= hInstanceSelector
)
954 && (hInstance
!= 0xffff) )
956 /* calling MPI with a foreign DSEG is invalid ! */
957 ERR("Problem with hInstance? Got %04x, using %04x instead\n",
958 hInstance
,CURRENT_DS
);
961 /* Always use the DSEG that MPI was entered with.
962 * We used to set hInstance to GetTaskDS16(), but this should be wrong
963 * as CURRENT_DS provides the DSEG value we need.
964 * ("calling" DS, *not* "task" DS !) */
965 hInstanceSelector
= CURRENT_DS
;
966 hInstance
= GlobalHandle16(hInstanceSelector
);
968 /* no thunking for DLLs */
969 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
972 thunkaddr
= TASK_AllocThunk( GetCurrentTask() );
973 if (!thunkaddr
) return (FARPROC16
)0;
974 thunk
= PTR_SEG_TO_LIN( thunkaddr
);
975 lfunc
= PTR_SEG_TO_LIN( func
);
977 TRACE("(%08lx,%04x): got thunk %08lx\n",
978 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
979 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
980 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
982 FIXME("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
985 *thunk
++ = 0xb8; /* movw instance, %ax */
986 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
987 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
988 *thunk
++ = 0xea; /* ljmp func */
989 *(DWORD
*)thunk
= (DWORD
)func
;
990 return (FARPROC16
)thunkaddr
;
991 /* CX reg indicates if thunkaddr != NULL, implement if needed */
995 /***********************************************************************
996 * FreeProcInstance16 (KERNEL.52)
998 void WINAPI
FreeProcInstance16( FARPROC16 func
)
1000 TRACE("(%08lx)\n", (DWORD
)func
);
1001 TASK_FreeThunk( GetCurrentTask(), (SEGPTR
)func
);
1004 /**********************************************************************
1005 * TASK_GetCodeSegment
1007 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
1008 * and logical segment number of a given code segment.
1010 * 'proc' either *is* already a pair of module handle and segment number,
1011 * in which case there's nothing to do. Otherwise, it is a pointer to
1012 * a function, and we need to retrieve the code segment. If the pointer
1013 * happens to point to a thunk, we'll retrieve info about the code segment
1014 * where the function pointed to by the thunk resides, not the thunk itself.
1016 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
1017 * the function the snoop code will return to ...
1020 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
1021 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
1023 NE_MODULE
*pModule
= NULL
;
1024 SEGTABLEENTRY
*pSeg
= NULL
;
1027 /* Try pair of module handle / segment number */
1028 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
1029 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
1031 segNr
= LOWORD( proc
);
1032 if ( segNr
&& segNr
<= pModule
->seg_count
)
1033 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
1036 /* Try thunk or function */
1039 BYTE
*thunk
= (BYTE
*)PTR_SEG_TO_LIN( proc
);
1042 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
1043 selector
= thunk
[6] + (thunk
[7] << 8);
1045 selector
= HIWORD( proc
);
1047 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
1048 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
1051 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
1052 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
1055 if ( pModule
&& segNr
> pModule
->seg_count
)
1059 /* Abort if segment not found */
1061 if ( !pModule
|| !pSeg
)
1064 /* Return segment data */
1066 if ( ppModule
) *ppModule
= pModule
;
1067 if ( ppSeg
) *ppSeg
= pSeg
;
1068 if ( pSegNr
) *pSegNr
= segNr
;
1073 /**********************************************************************
1074 * GetCodeHandle (KERNEL.93)
1076 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
1078 SEGTABLEENTRY
*pSeg
;
1080 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
1086 /**********************************************************************
1087 * GetCodeInfo (KERNEL.104)
1089 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
1092 SEGTABLEENTRY
*pSeg
;
1095 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
1098 /* Fill in segment information */
1100 segInfo
->offSegment
= pSeg
->filepos
;
1101 segInfo
->cbSegment
= pSeg
->size
;
1102 segInfo
->flags
= pSeg
->flags
;
1103 segInfo
->cbAlloc
= pSeg
->minsize
;
1104 segInfo
->h
= pSeg
->hSeg
;
1105 segInfo
->alignShift
= pModule
->alignment
;
1107 if ( segNr
== pModule
->dgroup
)
1108 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
1110 /* Return module handle in %es */
1112 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
1118 /**********************************************************************
1119 * DefineHandleTable16 (KERNEL.94)
1121 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
1123 return TRUE
; /* FIXME */
1127 /***********************************************************************
1128 * SetTaskQueue (KERNEL.34)
1130 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
1135 if (!hTask
) hTask
= GetCurrentTask();
1136 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1138 hPrev
= pTask
->hQueue
;
1139 pTask
->hQueue
= hQueue
;
1145 /***********************************************************************
1146 * GetTaskQueue (KERNEL.35)
1148 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1152 if (!hTask
) hTask
= GetCurrentTask();
1153 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1154 return pTask
->hQueue
;
1157 /***********************************************************************
1158 * SetThreadQueue (KERNEL.463)
1160 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1162 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1163 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1167 teb
->queue
= hQueue
;
1169 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1170 SetTaskQueue16( teb
->htask16
, hQueue
);
1176 /***********************************************************************
1177 * GetThreadQueue (KERNEL.464)
1179 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1183 teb
= NtCurrentTeb();
1184 else if ( HIWORD(thread
) )
1185 teb
= THREAD_IdToTEB( thread
);
1186 else if ( IsTask16( (HTASK16
)thread
) )
1187 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1189 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1192 /***********************************************************************
1193 * SetFastQueue (KERNEL.624)
1195 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1199 teb
= NtCurrentTeb();
1200 else if ( HIWORD(thread
) )
1201 teb
= THREAD_IdToTEB( thread
);
1202 else if ( IsTask16( (HTASK16
)thread
) )
1203 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1205 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1208 /***********************************************************************
1209 * GetFastQueue (KERNEL.625)
1211 HANDLE WINAPI
GetFastQueue16( void )
1213 TEB
*teb
= NtCurrentTeb();
1217 Callout
.InitThreadInput16( 0, THREAD_IsWin16(teb
)? 4 : 5 );
1220 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1222 return (HANDLE
)teb
->queue
;
1225 /***********************************************************************
1226 * SwitchStackTo (KERNEL.108)
1228 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1231 STACK16FRAME
*oldFrame
, *newFrame
;
1232 INSTANCEDATA
*pData
;
1235 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1236 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1237 TRACE("old=%04x:%04x new=%04x:%04x\n",
1238 SELECTOROF( pTask
->teb
->cur_stack
),
1239 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1241 /* Save the old stack */
1243 oldFrame
= THREAD_STACK16( pTask
->teb
);
1244 /* pop frame + args and push bp */
1245 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1247 *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
) = oldFrame
->bp
;
1248 pData
->stacktop
= top
;
1249 pData
->stackmin
= ptr
;
1250 pData
->stackbottom
= ptr
;
1252 /* Switch to the new stack */
1254 /* Note: we need to take the 3 arguments into account; otherwise,
1255 * the stack will underflow upon return from this function.
1257 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1258 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1259 pTask
->teb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( seg
, ptr
- copySize
);
1260 newFrame
= THREAD_STACK16( pTask
->teb
);
1262 /* Copy the stack frame and the local variables to the new stack */
1264 memmove( newFrame
, oldFrame
, copySize
);
1266 *(WORD
*)PTR_SEG_OFF_TO_LIN( seg
, ptr
) = 0; /* clear previous bp */
1270 /***********************************************************************
1271 * SwitchStackBack (KERNEL.109)
1273 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1275 STACK16FRAME
*oldFrame
, *newFrame
;
1276 INSTANCEDATA
*pData
;
1278 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1280 if (!pData
->old_ss_sp
)
1282 WARN("No previous SwitchStackTo\n" );
1285 TRACE("restoring stack %04x:%04x\n",
1286 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1288 oldFrame
= CURRENT_STACK16
;
1290 /* Pop bp from the previous stack */
1292 BP_reg(context
) = *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
);
1293 pData
->old_ss_sp
+= sizeof(WORD
);
1295 /* Switch back to the old stack */
1297 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1298 SS_reg(context
) = SELECTOROF(pData
->old_ss_sp
);
1299 ESP_reg(context
) = OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1300 pData
->old_ss_sp
= 0;
1302 /* Build a stack frame for the return */
1304 newFrame
= CURRENT_STACK16
;
1305 newFrame
->frame32
= oldFrame
->frame32
;
1306 newFrame
->module_cs
= oldFrame
->module_cs
;
1307 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1308 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1312 /***********************************************************************
1313 * GetTaskQueueDS16 (KERNEL.118)
1315 void WINAPI
GetTaskQueueDS16(void)
1317 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1321 /***********************************************************************
1322 * GetTaskQueueES16 (KERNEL.119)
1324 void WINAPI
GetTaskQueueES16(void)
1326 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1330 /***********************************************************************
1331 * GetCurrentTask (KERNEL.36)
1333 HTASK16 WINAPI
GetCurrentTask(void)
1335 return NtCurrentTeb()->htask16
;
1338 DWORD WINAPI
WIN16_GetCurrentTask(void)
1340 /* This is the version used by relay code; the first task is */
1341 /* returned in the high word of the result */
1342 return MAKELONG( GetCurrentTask(), hFirstTask
);
1346 /***********************************************************************
1347 * GetCurrentPDB (KERNEL.37)
1349 * UNDOC: returns PSP of KERNEL in high word
1351 DWORD WINAPI
GetCurrentPDB16(void)
1355 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1356 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1360 /***********************************************************************
1361 * GetCurPID16 (KERNEL.157)
1363 DWORD WINAPI
GetCurPID16( DWORD unused
)
1369 /***********************************************************************
1370 * GetInstanceData (KERNEL.54)
1372 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1374 char *ptr
= (char *)GlobalLock16( instance
);
1375 if (!ptr
|| !len
) return 0;
1376 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1377 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1382 /***********************************************************************
1383 * GetExeVersion (KERNEL.105)
1385 WORD WINAPI
GetExeVersion16(void)
1389 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1390 return pTask
->version
;
1394 /***********************************************************************
1395 * SetErrorMode16 (KERNEL.107)
1397 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1400 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1401 pTask
->error_mode
= mode
;
1402 return SetErrorMode( mode
);
1406 /***********************************************************************
1407 * GetDOSEnvironment (KERNEL.131)
1409 SEGPTR WINAPI
GetDOSEnvironment16(void)
1413 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1414 return PTR_SEG_OFF_TO_SEGPTR( pTask
->pdb
.environment
, 0 );
1418 /***********************************************************************
1419 * GetNumTasks (KERNEL.152)
1421 UINT16 WINAPI
GetNumTasks16(void)
1427 /***********************************************************************
1428 * GetTaskDS (KERNEL.155)
1430 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1431 * I don't think we need to bother with this.
1433 HINSTANCE16 WINAPI
GetTaskDS16(void)
1437 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1438 return GlobalHandleToSel16(pTask
->hInstance
);
1441 /***********************************************************************
1442 * GetDummyModuleHandleDS (KERNEL.602)
1444 WORD WINAPI
GetDummyModuleHandleDS16(void)
1449 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1450 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1451 selector
= GlobalHandleToSel16( pTask
->hModule
);
1452 CURRENT_DS
= selector
;
1456 /***********************************************************************
1457 * IsTask (KERNEL.320)
1459 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1463 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return FALSE
;
1464 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1465 return (pTask
->magic
== TDB_MAGIC
);
1469 /***********************************************************************
1470 * IsWinOldApTask16 (KERNEL.158)
1472 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1474 /* should return bit 0 of byte 0x48 in PSP */
1478 /***********************************************************************
1479 * SetTaskSignalProc (KERNEL.38)
1481 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1486 if (!hTask
) hTask
= GetCurrentTask();
1487 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return NULL
;
1488 oldProc
= pTask
->userhandler
;
1489 pTask
->userhandler
= proc
;
1493 /***********************************************************************
1494 * TASK_CallTaskSignalProc
1496 /* ### start build ### */
1497 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1498 /* ### stop build ### */
1499 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1501 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1502 if ( !pTask
|| !pTask
->userhandler
) return;
1504 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1505 hTaskOrModule
, uCode
, 0,
1506 pTask
->hInstance
, pTask
->hQueue
);
1509 /***********************************************************************
1510 * SetSigHandler (KERNEL.140)
1512 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1513 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1515 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1516 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1518 if (flag
!= 1) return 0;
1519 if (!newmode
) newhandler
= NULL
; /* Default handler */
1524 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1525 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1526 pTask
->signal_flags
= newmode
;
1527 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1528 pTask
->sighandler
= newhandler
;
1534 /***********************************************************************
1535 * GlobalNotify (KERNEL.154)
1537 * Note that GlobalNotify does _not_ return the old NotifyProc
1538 * -- contrary to LocalNotify !!
1540 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1544 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1545 pTask
->discardhandler
= proc
;
1549 /***********************************************************************
1550 * GetExePtr (KERNEL.133)
1552 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1557 /* Check for module handle */
1559 if (!(ptr
= GlobalLock16( handle
))) return 0;
1560 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1562 /* Search for this handle inside all tasks */
1564 *hTask
= hFirstTask
;
1567 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1568 if ((*hTask
== handle
) ||
1569 (pTask
->hInstance
== handle
) ||
1570 (pTask
->hQueue
== handle
) ||
1571 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1572 *hTask
= pTask
->hNext
;
1575 /* Check the owner for module handle */
1577 owner
= FarGetOwner16( handle
);
1578 if (!(ptr
= GlobalLock16( owner
))) return 0;
1579 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1581 /* Search for the owner inside all tasks */
1583 *hTask
= hFirstTask
;
1586 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1587 if ((*hTask
== owner
) ||
1588 (pTask
->hInstance
== owner
) ||
1589 (pTask
->hQueue
== owner
) ||
1590 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1591 *hTask
= pTask
->hNext
;
1597 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1600 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1601 STACK16FRAME
*frame
= CURRENT_STACK16
;
1602 frame
->ecx
= hModule
;
1603 if (hTask
) frame
->es
= hTask
;
1607 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1610 return GetExePtrHelper( handle
, &hTask
);
1614 /***********************************************************************
1615 * TaskFirst (TOOLHELP.63)
1617 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1619 lpte
->hNext
= hFirstTask
;
1620 return TaskNext16( lpte
);
1624 /***********************************************************************
1625 * TaskNext (TOOLHELP.64)
1627 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1630 INSTANCEDATA
*pInstData
;
1632 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1633 if (!lpte
->hNext
) return FALSE
;
1634 pTask
= (TDB
*)GlobalLock16( lpte
->hNext
);
1635 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1636 pInstData
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN( GlobalHandleToSel16(pTask
->hInstance
), 0 );
1637 lpte
->hTask
= lpte
->hNext
;
1638 lpte
->hTaskParent
= pTask
->hParent
;
1639 lpte
->hInst
= pTask
->hInstance
;
1640 lpte
->hModule
= pTask
->hModule
;
1641 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1642 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1643 lpte
->wStackTop
= pInstData
->stacktop
;
1644 lpte
->wStackMinimum
= pInstData
->stackmin
;
1645 lpte
->wStackBottom
= pInstData
->stackbottom
;
1646 lpte
->wcEvents
= pTask
->nEvents
;
1647 lpte
->hQueue
= pTask
->hQueue
;
1648 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1649 lpte
->wPSPOffset
= 0x100; /*??*/
1650 lpte
->hNext
= pTask
->hNext
;
1655 /***********************************************************************
1656 * TaskFindHandle (TOOLHELP.65)
1658 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1660 lpte
->hNext
= hTask
;
1661 return TaskNext16( lpte
);
1665 /***********************************************************************
1666 * GetAppCompatFlags16 (KERNEL.354)
1668 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1670 return GetAppCompatFlags( hTask
);
1674 /***********************************************************************
1675 * GetAppCompatFlags (USER32.206)
1677 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1681 if (!hTask
) hTask
= GetCurrentTask();
1682 if (!(pTask
=(TDB
*)GlobalLock16( (HTASK16
)hTask
))) return 0;
1683 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1684 return pTask
->compat_flags
;