Added checking for broken font cache.
[wine.git] / loader / task.c
blob5825a2b88e73e3051d8654707497b5ed71728712
1 /*
2 * Task functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <unistd.h>
12 #include "wine/winbase16.h"
13 #include "callback.h"
14 #include "drive.h"
15 #include "file.h"
16 #include "global.h"
17 #include "instance.h"
18 #include "miscemu.h"
19 #include "module.h"
20 #include "neexe.h"
21 #include "process.h"
22 #include "queue.h"
23 #include "selectors.h"
24 #include "stackframe.h"
25 #include "task.h"
26 #include "thread.h"
27 #include "toolhelp.h"
28 #include "winnt.h"
29 #include "winsock.h"
30 #include "syslevel.h"
31 #include "debugtools.h"
32 #include "dosexe.h"
33 #include "services.h"
34 #include "server.h"
36 DEFAULT_DEBUG_CHANNEL(task)
37 DECLARE_DEBUG_CHANNEL(relay)
38 DECLARE_DEBUG_CHANNEL(toolhelp)
40 /* Min. number of thunks allocated when creating a new segment */
41 #define MIN_THUNKS 32
44 static THHOOK DefaultThhook = { 0 };
45 THHOOK *pThhook = &DefaultThhook;
47 #define hCurrentTask (pThhook->CurTDB)
48 #define hFirstTask (pThhook->HeadTDB)
49 #define hLockedTask (pThhook->LockTDB)
51 static UINT16 nTaskCount = 0;
53 static HTASK initial_task;
55 /***********************************************************************
56 * TASK_InstallTHHook
58 void TASK_InstallTHHook( THHOOK *pNewThhook )
60 THHOOK *pOldThhook = pThhook;
62 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
64 *pThhook = *pOldThhook;
67 /***********************************************************************
68 * TASK_GetNextTask
70 HTASK16 TASK_GetNextTask( HTASK16 hTask )
72 TDB* pTask = (TDB*)GlobalLock16(hTask);
74 if (pTask->hNext) return pTask->hNext;
75 return (hFirstTask != hTask) ? hFirstTask : 0;
78 /***********************************************************************
79 * TASK_LinkTask
81 static void TASK_LinkTask( HTASK16 hTask )
83 HTASK16 *prevTask;
84 TDB *pTask;
86 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
87 prevTask = &hFirstTask;
88 while (*prevTask)
90 TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
91 if (prevTaskPtr->priority >= pTask->priority) break;
92 prevTask = &prevTaskPtr->hNext;
94 pTask->hNext = *prevTask;
95 *prevTask = hTask;
96 nTaskCount++;
100 /***********************************************************************
101 * TASK_UnlinkTask
103 static void TASK_UnlinkTask( HTASK16 hTask )
105 HTASK16 *prevTask;
106 TDB *pTask;
108 prevTask = &hFirstTask;
109 while (*prevTask && (*prevTask != hTask))
111 pTask = (TDB *)GlobalLock16( *prevTask );
112 prevTask = &pTask->hNext;
114 if (*prevTask)
116 pTask = (TDB *)GlobalLock16( *prevTask );
117 *prevTask = pTask->hNext;
118 pTask->hNext = 0;
119 nTaskCount--;
124 /***********************************************************************
125 * TASK_CreateThunks
127 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
128 * and containing 'count' entries.
130 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
132 int i;
133 WORD free;
134 THUNKS *pThunk;
136 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
137 pThunk->next = 0;
138 pThunk->magic = THUNK_MAGIC;
139 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
140 free = pThunk->free;
141 for (i = 0; i < count-1; i++)
143 free += 8; /* Offset of next thunk */
144 pThunk->thunks[4*i] = free;
146 pThunk->thunks[4*i] = 0; /* Last thunk */
150 /***********************************************************************
151 * TASK_AllocThunk
153 * Allocate a thunk for MakeProcInstance().
155 static SEGPTR TASK_AllocThunk( HTASK16 hTask )
157 TDB *pTask;
158 THUNKS *pThunk;
159 WORD sel, base;
161 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
162 sel = pTask->hCSAlias;
163 pThunk = &pTask->thunks;
164 base = (int)pThunk - (int)pTask;
165 while (!pThunk->free)
167 sel = pThunk->next;
168 if (!sel) /* Allocate a new segment */
170 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
171 pTask->hPDB, TRUE, FALSE, FALSE );
172 if (!sel) return (SEGPTR)0;
173 TASK_CreateThunks( sel, 0, MIN_THUNKS );
174 pThunk->next = sel;
176 pThunk = (THUNKS *)GlobalLock16( sel );
177 base = 0;
179 base += pThunk->free;
180 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
181 return PTR_SEG_OFF_TO_SEGPTR( sel, base );
185 /***********************************************************************
186 * TASK_FreeThunk
188 * Free a MakeProcInstance() thunk.
190 static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
192 TDB *pTask;
193 THUNKS *pThunk;
194 WORD sel, base;
196 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
197 sel = pTask->hCSAlias;
198 pThunk = &pTask->thunks;
199 base = (int)pThunk - (int)pTask;
200 while (sel && (sel != HIWORD(thunk)))
202 sel = pThunk->next;
203 pThunk = (THUNKS *)GlobalLock16( sel );
204 base = 0;
206 if (!sel) return FALSE;
207 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
208 pThunk->free = LOWORD(thunk) - base;
209 return TRUE;
213 /***********************************************************************
214 * TASK_Create
216 * NOTE: This routine might be called by a Win32 thread. Thus, we need
217 * to be careful to protect global data structures. We do this
218 * by entering the Win16Lock while linking the task into the
219 * global task list.
221 BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, TEB *teb, LPCSTR cmdline, BYTE len )
223 HTASK16 hTask;
224 TDB *pTask;
225 char name[10];
227 /* Allocate the task structure */
229 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
230 pModule->self, FALSE, FALSE, FALSE );
231 if (!hTask) return FALSE;
232 pTask = (TDB *)GlobalLock16( hTask );
234 /* Fill the task structure */
236 pTask->hSelf = hTask;
238 if (teb->tibflags & TEBF_WIN32)
240 pTask->flags |= TDBF_WIN32;
241 pTask->hInstance = pModule->self;
242 pTask->hPrevInstance = 0;
243 /* NOTE: for 16-bit tasks, the instance handles are updated later on
244 in NE_InitProcess */
246 if (MZ_Current() && MZ_Current()->load_seg) pTask->flags |= TDBF_WINOLDAP;
248 pTask->version = pModule->expected_version;
249 pTask->hModule = pModule->self;
250 pTask->hParent = GetCurrentTask();
251 pTask->magic = TDB_MAGIC;
252 pTask->nCmdShow = cmdShow;
253 pTask->teb = teb;
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, FALSE, FALSE, FALSE );
273 /* Fill the PDB */
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 */
292 if (!cmdline)
294 cmdline = current_envdb.cmd_line;
295 /* remove the first word (program name) */
296 if (*cmdline == '"')
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, TRUE,
317 FALSE, FALSE );
319 /* Set the owner of the environment block */
321 FarSetOwner16( pTask->pdb.environment, pTask->hPDB );
323 /* Default DTA overwrites command line */
325 pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB,
326 (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
328 /* Create scheduler event for 16-bit tasks */
330 if ( !(pTask->flags & TDBF_WIN32) )
332 pTask->hEvent = CreateEventA( NULL, TRUE, FALSE, NULL );
333 pTask->hEvent = ConvertToGlobalHandle( pTask->hEvent );
336 /* Enter task handle into thread and process */
338 teb->htask16 = hTask;
339 if (!initial_task) initial_task = hTask;
341 /* Add the task to the linked list */
343 SYSLEVEL_EnterWin16Lock();
344 TASK_LinkTask( hTask );
345 SYSLEVEL_LeaveWin16Lock();
347 return TRUE;
350 /***********************************************************************
351 * TASK_DeleteTask
353 static void TASK_DeleteTask( HTASK16 hTask )
355 TDB *pTask;
356 HGLOBAL16 hPDB;
358 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
359 hPDB = pTask->hPDB;
361 pTask->magic = 0xdead; /* invalidate signature */
363 /* Free the selector aliases */
365 GLOBAL_FreeBlock( pTask->hCSAlias );
366 GLOBAL_FreeBlock( pTask->hPDB );
368 /* Free the task module */
370 FreeModule16( pTask->hModule );
372 /* Free the task structure itself */
374 GlobalFree16( hTask );
376 /* Free all memory used by this task (including the 32-bit stack, */
377 /* the environment block and the thunk segments). */
379 GlobalFreeAll16( hPDB );
382 /***********************************************************************
383 * TASK_KillTask
385 void TASK_KillTask( HTASK16 hTask )
387 TDB *pTask;
389 /* Enter the Win16Lock to protect global data structures */
390 SYSLEVEL_EnterWin16Lock();
392 if ( !hTask ) hTask = GetCurrentTask();
393 pTask = (TDB *)GlobalLock16( hTask );
394 if ( !pTask )
396 SYSLEVEL_LeaveWin16Lock();
397 return;
400 TRACE("Killing task %04x\n", hTask );
402 /* Perform USER cleanup */
404 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
405 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
406 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
407 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
409 if (nTaskCount <= 1)
411 TRACE("this is the last task, exiting\n" );
412 ExitKernel16();
415 /* FIXME: Hack! Send a message to the initial task so that
416 * the GetMessage wakes up and the initial task can check whether
417 * it is the only remaining one and terminate itself ...
418 * The initial task should probably install hooks or something
419 * to get informed about task termination :-/
421 Callout.PostAppMessage16( initial_task, WM_NULL, 0, 0 );
423 /* Remove the task from the list to be sure we never switch back to it */
424 TASK_UnlinkTask( hTask );
425 if( nTaskCount )
427 TDB* p = (TDB *)GlobalLock16( hFirstTask );
428 while( p )
430 if( p->hYieldTo == hTask ) p->hYieldTo = 0;
431 p = (TDB *)GlobalLock16( p->hNext );
435 pTask->nEvents = 0;
437 if ( hLockedTask == hTask )
438 hLockedTask = 0;
440 TASK_DeleteTask( hTask );
442 /* When deleting the current task ... */
443 if ( hTask == hCurrentTask )
445 DWORD lockCount;
447 /* ... schedule another one ... */
448 TASK_Reschedule();
450 /* ... and completely release the Win16Lock, just in case. */
451 ReleaseThunkLock( &lockCount );
453 return;
456 SYSLEVEL_LeaveWin16Lock();
459 /***********************************************************************
460 * TASK_Reschedule
462 * This is where all the magic of task-switching happens!
464 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
465 * This means that each 16-bit task runs until it voluntarily yields
466 * control, at which point the scheduler gets active and selects the
467 * next task to run.
469 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
470 * by the standard scheduler of the underlying OS. As many 16-bit apps
471 * *rely* on the behaviour of the Windows scheduler, however, we have
472 * to simulate that behaviour.
474 * This is achieved as follows: every 16-bit task is at time (except
475 * during task creation and deletion) in one of two states: either it
476 * is the one currently running, then the global variable hCurrentTask
477 * contains its task handle, or it is not currently running, then it
478 * is blocked on a special scheduler event, a global handle to which
479 * is stored in the task struct.
481 * When the current task yields control, this routine gets called. Its
482 * purpose is to determine the next task to be active, signal the
483 * scheduler event of that task, and then put the current task to sleep
484 * waiting for *its* scheduler event to get signalled again.
486 * This routine can get called in a few other special situations as well:
488 * - On creation of a 16-bit task, the Unix process executing the task
489 * calls TASK_Reschedule once it has completed its initialization.
490 * At this point, the task needs to be blocked until its scheduler
491 * event is signalled the first time (this will be done by the parent
492 * process to get the task up and running).
494 * - When the task currently running terminates itself, this routine gets
495 * called and has to schedule another task, *without* blocking the
496 * terminating task.
498 * - When a 32-bit thread posts an event for a 16-bit task, it might be
499 * the case that *no* 16-bit task is currently running. In this case
500 * the task that has now an event pending is to be scheduled.
503 void TASK_Reschedule(void)
505 TDB *pOldTask = NULL, *pNewTask = NULL;
506 HTASK16 hOldTask = 0, hNewTask = 0;
507 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
508 DWORD lockCount;
510 SYSLEVEL_EnterWin16Lock();
512 /* Check what we need to do */
513 hOldTask = GetCurrentTask();
514 pOldTask = (TDB *)GlobalLock16( hOldTask );
515 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
516 hCurrentTask, hOldTask, (long) getpid() );
518 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
520 /* We are called by an active (non-deleted) 16-bit task */
522 /* If we don't even have a current task, or else the current
523 task has yielded, we'll need to schedule a new task and
524 (possibly) put the calling task to sleep. Otherwise, we
525 only block the caller. */
527 if ( !hCurrentTask || hCurrentTask == hOldTask )
528 mode = MODE_YIELD;
529 else
530 mode = MODE_SLEEP;
532 else
534 /* We are called by a deleted 16-bit task or a 32-bit thread */
536 /* The only situation where we need to do something is if we
537 now do not have a current task. Then, we'll need to wake up
538 some task that has events pending. */
540 if ( !hCurrentTask || hCurrentTask == hOldTask )
541 mode = MODE_WAKEUP;
542 else
544 /* nothing to do */
545 SYSLEVEL_LeaveWin16Lock();
546 return;
550 /* Find a task to yield to: check for DirectedYield() */
551 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
553 hNewTask = pOldTask->hYieldTo;
554 pNewTask = (TDB *)GlobalLock16( hNewTask );
555 if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
556 pOldTask->hYieldTo = 0;
559 /* Find a task to yield to: check for pending events */
560 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
562 hNewTask = hFirstTask;
563 while (hNewTask)
565 pNewTask = (TDB *)GlobalLock16( hNewTask );
567 TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
569 if (pNewTask->nEvents) break;
570 hNewTask = pNewTask->hNext;
572 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
575 /* If we are still the task with highest priority, just return ... */
576 if ( mode == MODE_YIELD && hNewTask && hNewTask == hCurrentTask )
578 TRACE("returning to the current task (%04x)\n", hCurrentTask );
579 SYSLEVEL_LeaveWin16Lock();
581 /* Allow Win32 threads to thunk down even while a Win16 task is
582 in a tight PeekMessage() or Yield() loop ... */
583 ReleaseThunkLock( &lockCount );
584 RestoreThunkLock( lockCount );
585 return;
588 /* If no task to yield to found, suspend 16-bit scheduler ... */
589 if ( mode == MODE_YIELD && !hNewTask )
591 TRACE("No currently active task\n");
592 hCurrentTask = 0;
595 /* If we found a task to wake up, do it ... */
596 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
598 TRACE("Switching to task %04x (%.8s)\n",
599 hNewTask, pNewTask->module_name );
601 pNewTask->priority++;
602 TASK_UnlinkTask( hNewTask );
603 TASK_LinkTask( hNewTask );
604 pNewTask->priority--;
606 hCurrentTask = hNewTask;
607 SetEvent( pNewTask->hEvent );
609 /* This is set just in case some app reads it ... */
610 pNewTask->ss_sp = pNewTask->teb->cur_stack;
613 /* If we need to put the current task to sleep, do it ... */
614 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
616 ResetEvent( pOldTask->hEvent );
618 ReleaseThunkLock( &lockCount );
619 SYSLEVEL_CheckNotLevel( 1 );
620 WaitForSingleObject( pOldTask->hEvent, INFINITE );
621 RestoreThunkLock( lockCount );
624 SYSLEVEL_LeaveWin16Lock();
627 /***********************************************************************
628 * InitTask (KERNEL.91)
630 * Called by the application startup code.
632 void WINAPI InitTask16( CONTEXT86 *context )
634 TDB *pTask;
635 INSTANCEDATA *pinstance;
636 SEGPTR ptr;
638 EAX_reg(context) = 0;
639 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
641 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
642 as some apps, notably Visual Basic apps, *modify* the heap/stack
643 size of the instance data segment before calling InitTask() */
645 /* Initialize the INSTANCEDATA structure */
646 pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
647 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack ) + sizeof( STACK16FRAME );
648 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
649 pinstance->stacktop = ( pinstance->stackmin > BX_reg(context)?
650 pinstance->stackmin - BX_reg(context) : 0 ) + 150;
652 /* Initialize the local heap */
653 if ( CX_reg(context) )
654 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, CX_reg(context) );
656 /* Initialize implicitly loaded DLLs */
657 NE_InitializeDLLs( pTask->hModule );
658 NE_DllProcessAttach( pTask->hModule );
660 /* Registers on return are:
661 * ax 1 if OK, 0 on error
662 * cx stack limit in bytes
663 * dx cmdShow parameter
664 * si instance handle of the previous instance
665 * di instance handle of the new task
666 * es:bx pointer to command line inside PSP
668 * 0 (=%bp) is pushed on the stack
670 ptr = stack16_push( sizeof(WORD) );
671 *(WORD *)PTR_SEG_TO_LIN(ptr) = 0;
672 ESP_reg(context) -= 2;
674 EAX_reg(context) = 1;
676 if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80;
677 else
679 LPBYTE p = &pTask->pdb.cmdLine[1];
680 while ((*p == ' ') || (*p == '\t')) p++;
681 EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine);
683 ECX_reg(context) = pinstance->stacktop;
684 EDX_reg(context) = pTask->nCmdShow;
685 ESI_reg(context) = (DWORD)pTask->hPrevInstance;
686 EDI_reg(context) = (DWORD)pTask->hInstance;
687 ES_reg (context) = (WORD)pTask->hPDB;
691 /***********************************************************************
692 * WaitEvent (KERNEL.30)
694 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
696 TDB *pTask;
698 if (!hTask) hTask = GetCurrentTask();
699 pTask = (TDB *)GlobalLock16( hTask );
701 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
703 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
704 return TRUE;
707 if (pTask->nEvents > 0)
709 pTask->nEvents--;
710 return FALSE;
712 TASK_Reschedule();
714 /* When we get back here, we have an event */
716 if (pTask->nEvents > 0) pTask->nEvents--;
717 return TRUE;
721 /***********************************************************************
722 * PostEvent (KERNEL.31)
724 void WINAPI PostEvent16( HTASK16 hTask )
726 TDB *pTask;
728 if (!hTask) hTask = GetCurrentTask();
729 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
731 if ( !THREAD_IsWin16( pTask->teb ) )
733 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
734 return;
737 pTask->nEvents++;
739 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
740 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
741 TASK_Reschedule();
745 /***********************************************************************
746 * SetPriority (KERNEL.32)
748 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
750 TDB *pTask;
751 INT16 newpriority;
753 if (!hTask) hTask = GetCurrentTask();
754 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
755 newpriority = pTask->priority + delta;
756 if (newpriority < -32) newpriority = -32;
757 else if (newpriority > 15) newpriority = 15;
759 pTask->priority = newpriority + 1;
760 TASK_UnlinkTask( hTask );
761 TASK_LinkTask( hTask );
762 pTask->priority--;
766 /***********************************************************************
767 * LockCurrentTask (KERNEL.33)
769 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
771 if (bLock) hLockedTask = GetCurrentTask();
772 else hLockedTask = 0;
773 return hLockedTask;
777 /***********************************************************************
778 * IsTaskLocked (KERNEL.122)
780 HTASK16 WINAPI IsTaskLocked16(void)
782 return hLockedTask;
786 /***********************************************************************
787 * OldYield (KERNEL.117)
789 void WINAPI OldYield16(void)
791 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
793 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
795 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
796 return;
799 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
800 TASK_Reschedule();
801 if (pCurTask) pCurTask->nEvents--;
804 /***********************************************************************
805 * WIN32_OldYield16 (KERNEL.447)
807 void WINAPI WIN32_OldYield16(void)
809 DWORD count;
811 ReleaseThunkLock(&count);
812 RestoreThunkLock(count);
815 /***********************************************************************
816 * DirectedYield (KERNEL.150)
818 void WINAPI DirectedYield16( HTASK16 hTask )
820 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
822 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
824 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
825 return;
828 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
830 pCurTask->hYieldTo = hTask;
831 OldYield16();
833 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
836 /***********************************************************************
837 * Yield16 (KERNEL.29)
839 void WINAPI Yield16(void)
841 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
843 if (pCurTask) pCurTask->hYieldTo = 0;
844 if (pCurTask && pCurTask->hQueue) Callout.UserYield16();
845 else OldYield16();
848 /***********************************************************************
849 * KERNEL_490 (KERNEL.490)
851 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
853 if ( !someTask ) return 0;
855 FIXME("(%04x): stub\n", someTask );
856 return 0;
859 /***********************************************************************
860 * MakeProcInstance16 (KERNEL.51)
862 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
864 BYTE *thunk,*lfunc;
865 SEGPTR thunkaddr;
866 WORD hInstanceSelector;
868 hInstanceSelector = GlobalHandleToSel16(hInstance);
870 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
872 if (!HIWORD(func)) {
873 /* Win95 actually protects via SEH, but this is better for debugging */
874 ERR("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
875 return (FARPROC16)0;
878 if (hInstance)
880 if ( (!(hInstance & 4)) ||
881 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
883 ERR("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
884 hInstance);
885 return 0;
889 if ( (CURRENT_DS != hInstanceSelector)
890 && (hInstance != 0)
891 && (hInstance != 0xffff) )
893 /* calling MPI with a foreign DSEG is invalid ! */
894 ERR("Problem with hInstance? Got %04x, using %04x instead\n",
895 hInstance,CURRENT_DS);
898 /* Always use the DSEG that MPI was entered with.
899 * We used to set hInstance to GetTaskDS16(), but this should be wrong
900 * as CURRENT_DS provides the DSEG value we need.
901 * ("calling" DS, *not* "task" DS !) */
902 hInstanceSelector = CURRENT_DS;
903 hInstance = GlobalHandle16(hInstanceSelector);
905 /* no thunking for DLLs */
906 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
907 return func;
909 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
910 if (!thunkaddr) return (FARPROC16)0;
911 thunk = PTR_SEG_TO_LIN( thunkaddr );
912 lfunc = PTR_SEG_TO_LIN( func );
914 TRACE("(%08lx,%04x): got thunk %08lx\n",
915 (DWORD)func, hInstance, (DWORD)thunkaddr );
916 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
917 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
919 FIXME("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
922 *thunk++ = 0xb8; /* movw instance, %ax */
923 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
924 *thunk++ = (BYTE)(hInstanceSelector >> 8);
925 *thunk++ = 0xea; /* ljmp func */
926 *(DWORD *)thunk = (DWORD)func;
927 return (FARPROC16)thunkaddr;
928 /* CX reg indicates if thunkaddr != NULL, implement if needed */
932 /***********************************************************************
933 * FreeProcInstance16 (KERNEL.52)
935 void WINAPI FreeProcInstance16( FARPROC16 func )
937 TRACE("(%08lx)\n", (DWORD)func );
938 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
941 /**********************************************************************
942 * TASK_GetCodeSegment
944 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
945 * and logical segment number of a given code segment.
947 * 'proc' either *is* already a pair of module handle and segment number,
948 * in which case there's nothing to do. Otherwise, it is a pointer to
949 * a function, and we need to retrieve the code segment. If the pointer
950 * happens to point to a thunk, we'll retrieve info about the code segment
951 * where the function pointed to by the thunk resides, not the thunk itself.
953 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
954 * the function the snoop code will return to ...
957 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
958 SEGTABLEENTRY **ppSeg, int *pSegNr )
960 NE_MODULE *pModule = NULL;
961 SEGTABLEENTRY *pSeg = NULL;
962 int segNr;
964 /* Try pair of module handle / segment number */
965 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
966 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
968 segNr = LOWORD( proc );
969 if ( segNr && segNr <= pModule->seg_count )
970 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
973 /* Try thunk or function */
974 else
976 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
977 WORD selector;
979 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
980 selector = thunk[6] + (thunk[7] << 8);
981 else
982 selector = HIWORD( proc );
984 pModule = NE_GetPtr( GlobalHandle16( selector ) );
985 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
987 if ( pModule )
988 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
989 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
990 break;
992 if ( pModule && segNr > pModule->seg_count )
993 pSeg = NULL;
996 /* Abort if segment not found */
998 if ( !pModule || !pSeg )
999 return FALSE;
1001 /* Return segment data */
1003 if ( ppModule ) *ppModule = pModule;
1004 if ( ppSeg ) *ppSeg = pSeg;
1005 if ( pSegNr ) *pSegNr = segNr;
1007 return TRUE;
1010 /**********************************************************************
1011 * GetCodeHandle (KERNEL.93)
1013 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
1015 SEGTABLEENTRY *pSeg;
1017 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
1018 return (HANDLE16)0;
1020 return pSeg->hSeg;
1023 /**********************************************************************
1024 * GetCodeInfo (KERNEL.104)
1026 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1028 NE_MODULE *pModule;
1029 SEGTABLEENTRY *pSeg;
1030 int segNr;
1032 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1033 return FALSE;
1035 /* Fill in segment information */
1037 segInfo->offSegment = pSeg->filepos;
1038 segInfo->cbSegment = pSeg->size;
1039 segInfo->flags = pSeg->flags;
1040 segInfo->cbAlloc = pSeg->minsize;
1041 segInfo->h = pSeg->hSeg;
1042 segInfo->alignShift = pModule->alignment;
1044 if ( segNr == pModule->dgroup )
1045 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
1047 /* Return module handle in %es */
1049 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1051 return TRUE;
1055 /**********************************************************************
1056 * DefineHandleTable16 (KERNEL.94)
1058 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1060 FIXME("(%04x): stub ?\n", wOffset);
1061 return TRUE;
1065 /***********************************************************************
1066 * SetTaskQueue (KERNEL.34)
1068 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1070 HQUEUE16 hPrev;
1071 TDB *pTask;
1073 if (!hTask) hTask = GetCurrentTask();
1074 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1076 hPrev = pTask->hQueue;
1077 pTask->hQueue = hQueue;
1079 return hPrev;
1083 /***********************************************************************
1084 * GetTaskQueue (KERNEL.35)
1086 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1088 TDB *pTask;
1090 if (!hTask) hTask = GetCurrentTask();
1091 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1092 return pTask->hQueue;
1095 /***********************************************************************
1096 * SetThreadQueue (KERNEL.463)
1098 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1100 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1101 HQUEUE16 oldQueue = teb? teb->queue : 0;
1103 if ( teb )
1105 teb->queue = hQueue;
1107 if ( GetTaskQueue16( teb->htask16 ) == oldQueue )
1108 SetTaskQueue16( teb->htask16, hQueue );
1111 return oldQueue;
1114 /***********************************************************************
1115 * GetThreadQueue (KERNEL.464)
1117 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1119 TEB *teb = NULL;
1120 if ( !thread )
1121 teb = NtCurrentTeb();
1122 else if ( HIWORD(thread) )
1123 teb = THREAD_IdToTEB( thread );
1124 else if ( IsTask16( (HTASK16)thread ) )
1125 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1127 return (HQUEUE16)(teb? teb->queue : 0);
1130 /***********************************************************************
1131 * SetFastQueue (KERNEL.624)
1133 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1135 TEB *teb = NULL;
1136 if ( !thread )
1137 teb = NtCurrentTeb();
1138 else if ( HIWORD(thread) )
1139 teb = THREAD_IdToTEB( thread );
1140 else if ( IsTask16( (HTASK16)thread ) )
1141 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1143 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1146 /***********************************************************************
1147 * GetFastQueue (KERNEL.625)
1149 HANDLE WINAPI GetFastQueue16( void )
1151 TEB *teb = NtCurrentTeb();
1152 if (!teb) return 0;
1154 if (!teb->queue)
1155 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
1157 if (!teb->queue)
1158 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1160 return (HANDLE)teb->queue;
1163 /***********************************************************************
1164 * SwitchStackTo (KERNEL.108)
1166 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1168 TDB *pTask;
1169 STACK16FRAME *oldFrame, *newFrame;
1170 INSTANCEDATA *pData;
1171 UINT16 copySize;
1173 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1174 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1175 TRACE("old=%04x:%04x new=%04x:%04x\n",
1176 SELECTOROF( pTask->teb->cur_stack ),
1177 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1179 /* Save the old stack */
1181 oldFrame = THREAD_STACK16( pTask->teb );
1182 /* pop frame + args and push bp */
1183 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1184 + 2 * sizeof(WORD);
1185 *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp;
1186 pData->stacktop = top;
1187 pData->stackmin = ptr;
1188 pData->stackbottom = ptr;
1190 /* Switch to the new stack */
1192 /* Note: we need to take the 3 arguments into account; otherwise,
1193 * the stack will underflow upon return from this function.
1195 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1196 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1197 pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize );
1198 newFrame = THREAD_STACK16( pTask->teb );
1200 /* Copy the stack frame and the local variables to the new stack */
1202 memmove( newFrame, oldFrame, copySize );
1203 newFrame->bp = ptr;
1204 *(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */
1208 /***********************************************************************
1209 * SwitchStackBack (KERNEL.109)
1211 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1213 STACK16FRAME *oldFrame, *newFrame;
1214 INSTANCEDATA *pData;
1216 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1217 return;
1218 if (!pData->old_ss_sp)
1220 WARN("No previous SwitchStackTo\n" );
1221 return;
1223 TRACE("restoring stack %04x:%04x\n",
1224 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1226 oldFrame = CURRENT_STACK16;
1228 /* Pop bp from the previous stack */
1230 BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp);
1231 pData->old_ss_sp += sizeof(WORD);
1233 /* Switch back to the old stack */
1235 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1236 SS_reg(context) = SELECTOROF(pData->old_ss_sp);
1237 ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1238 pData->old_ss_sp = 0;
1240 /* Build a stack frame for the return */
1242 newFrame = CURRENT_STACK16;
1243 newFrame->frame32 = oldFrame->frame32;
1244 newFrame->module_cs = oldFrame->module_cs;
1245 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1246 newFrame->entry_ip = oldFrame->entry_ip;
1250 /***********************************************************************
1251 * GetTaskQueueDS16 (KERNEL.118)
1253 void WINAPI GetTaskQueueDS16(void)
1255 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1259 /***********************************************************************
1260 * GetTaskQueueES16 (KERNEL.119)
1262 void WINAPI GetTaskQueueES16(void)
1264 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1268 /***********************************************************************
1269 * GetCurrentTask (KERNEL.36)
1271 HTASK16 WINAPI GetCurrentTask(void)
1273 return NtCurrentTeb()->htask16;
1276 DWORD WINAPI WIN16_GetCurrentTask(void)
1278 /* This is the version used by relay code; the first task is */
1279 /* returned in the high word of the result */
1280 return MAKELONG( GetCurrentTask(), hFirstTask );
1284 /***********************************************************************
1285 * GetCurrentPDB (KERNEL.37)
1287 * UNDOC: returns PSP of KERNEL in high word
1289 DWORD WINAPI GetCurrentPDB16(void)
1291 TDB *pTask;
1293 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1294 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1298 /***********************************************************************
1299 * GetCurPID16 (KERNEL.157)
1301 DWORD WINAPI GetCurPID16( DWORD unused )
1303 return 0;
1307 /***********************************************************************
1308 * GetInstanceData (KERNEL.54)
1310 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1312 char *ptr = (char *)GlobalLock16( instance );
1313 if (!ptr || !len) return 0;
1314 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1315 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1316 return len;
1320 /***********************************************************************
1321 * GetExeVersion (KERNEL.105)
1323 WORD WINAPI GetExeVersion16(void)
1325 TDB *pTask;
1327 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1328 return pTask->version;
1332 /***********************************************************************
1333 * SetErrorMode16 (KERNEL.107)
1335 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1337 TDB *pTask;
1338 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1339 pTask->error_mode = mode;
1340 return SetErrorMode( mode );
1344 /***********************************************************************
1345 * GetDOSEnvironment (KERNEL.131)
1347 SEGPTR WINAPI GetDOSEnvironment16(void)
1349 TDB *pTask;
1351 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1352 return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 );
1356 /***********************************************************************
1357 * GetNumTasks (KERNEL.152)
1359 UINT16 WINAPI GetNumTasks16(void)
1361 return nTaskCount;
1365 /***********************************************************************
1366 * GetTaskDS (KERNEL.155)
1368 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1369 * I don't think we need to bother with this.
1371 HINSTANCE16 WINAPI GetTaskDS16(void)
1373 TDB *pTask;
1375 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1376 return GlobalHandleToSel16(pTask->hInstance);
1379 /***********************************************************************
1380 * GetDummyModuleHandleDS (KERNEL.602)
1382 WORD WINAPI GetDummyModuleHandleDS16(void)
1384 TDB *pTask;
1385 WORD selector;
1387 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1388 if (!(pTask->flags & TDBF_WIN32)) return 0;
1389 selector = GlobalHandleToSel16( pTask->hModule );
1390 CURRENT_DS = selector;
1391 return selector;
1394 /***********************************************************************
1395 * IsTask (KERNEL.320)
1397 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1399 TDB *pTask;
1401 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1402 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1403 return (pTask->magic == TDB_MAGIC);
1407 /***********************************************************************
1408 * IsWinOldApTask16 (KERNEL.158)
1410 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1412 /* should return bit 0 of byte 0x48 in PSP */
1413 return FALSE;
1416 /***********************************************************************
1417 * SetTaskSignalProc (KERNEL.38)
1419 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1421 TDB *pTask;
1422 FARPROC16 oldProc;
1424 if (!hTask) hTask = GetCurrentTask();
1425 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1426 oldProc = pTask->userhandler;
1427 pTask->userhandler = proc;
1428 return oldProc;
1431 /***********************************************************************
1432 * TASK_CallTaskSignalProc
1434 /* ### start build ### */
1435 extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1436 /* ### stop build ### */
1437 void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1439 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1440 if ( !pTask || !pTask->userhandler ) return;
1442 TASK_CallTo16_word_wwwww( pTask->userhandler,
1443 hTaskOrModule, uCode, 0,
1444 pTask->hInstance, pTask->hQueue );
1447 /***********************************************************************
1448 * SetSigHandler (KERNEL.140)
1450 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1451 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1453 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1454 newhandler,oldhandler,oldmode,newmode,flag );
1456 if (flag != 1) return 0;
1457 if (!newmode) newhandler = NULL; /* Default handler */
1458 if (newmode != 4)
1460 TDB *pTask;
1462 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1463 if (oldmode) *oldmode = pTask->signal_flags;
1464 pTask->signal_flags = newmode;
1465 if (oldhandler) *oldhandler = pTask->sighandler;
1466 pTask->sighandler = newhandler;
1468 return 0;
1472 /***********************************************************************
1473 * GlobalNotify (KERNEL.154)
1475 * Note that GlobalNotify does _not_ return the old NotifyProc
1476 * -- contrary to LocalNotify !!
1478 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1480 TDB *pTask;
1482 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1483 pTask->discardhandler = proc;
1487 /***********************************************************************
1488 * GetExePtr (KERNEL.133)
1490 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1492 char *ptr;
1493 HANDLE16 owner;
1495 /* Check for module handle */
1497 if (!(ptr = GlobalLock16( handle ))) return 0;
1498 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1500 /* Search for this handle inside all tasks */
1502 *hTask = hFirstTask;
1503 while (*hTask)
1505 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1506 if ((*hTask == handle) ||
1507 (pTask->hInstance == handle) ||
1508 (pTask->hQueue == handle) ||
1509 (pTask->hPDB == handle)) return pTask->hModule;
1510 *hTask = pTask->hNext;
1513 /* Check the owner for module handle */
1515 owner = FarGetOwner16( handle );
1516 if (!(ptr = GlobalLock16( owner ))) return 0;
1517 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1519 /* Search for the owner inside all tasks */
1521 *hTask = hFirstTask;
1522 while (*hTask)
1524 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1525 if ((*hTask == owner) ||
1526 (pTask->hInstance == owner) ||
1527 (pTask->hQueue == owner) ||
1528 (pTask->hPDB == owner)) return pTask->hModule;
1529 *hTask = pTask->hNext;
1532 return 0;
1535 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1537 HTASK16 hTask = 0;
1538 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1539 STACK16FRAME *frame = CURRENT_STACK16;
1540 frame->ecx = hModule;
1541 if (hTask) frame->es = hTask;
1542 return hModule;
1545 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1547 HTASK16 hTask = 0;
1548 return GetExePtrHelper( handle, &hTask );
1552 /***********************************************************************
1553 * TaskFirst (TOOLHELP.63)
1555 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1557 lpte->hNext = hFirstTask;
1558 return TaskNext16( lpte );
1562 /***********************************************************************
1563 * TaskNext (TOOLHELP.64)
1565 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1567 TDB *pTask;
1568 INSTANCEDATA *pInstData;
1570 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1571 if (!lpte->hNext) return FALSE;
1572 pTask = (TDB *)GlobalLock16( lpte->hNext );
1573 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1574 pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( GlobalHandleToSel16(pTask->hInstance), 0 );
1575 lpte->hTask = lpte->hNext;
1576 lpte->hTaskParent = pTask->hParent;
1577 lpte->hInst = pTask->hInstance;
1578 lpte->hModule = pTask->hModule;
1579 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1580 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1581 lpte->wStackTop = pInstData->stacktop;
1582 lpte->wStackMinimum = pInstData->stackmin;
1583 lpte->wStackBottom = pInstData->stackbottom;
1584 lpte->wcEvents = pTask->nEvents;
1585 lpte->hQueue = pTask->hQueue;
1586 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1587 lpte->wPSPOffset = 0x100; /*??*/
1588 lpte->hNext = pTask->hNext;
1589 return TRUE;
1593 /***********************************************************************
1594 * TaskFindHandle (TOOLHELP.65)
1596 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1598 lpte->hNext = hTask;
1599 return TaskNext16( lpte );
1603 /***********************************************************************
1604 * GetAppCompatFlags16 (KERNEL.354)
1606 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1608 return GetAppCompatFlags( hTask );
1612 /***********************************************************************
1613 * GetAppCompatFlags (USER32.206)
1615 DWORD WINAPI GetAppCompatFlags( HTASK hTask )
1617 TDB *pTask;
1619 if (!hTask) hTask = GetCurrentTask();
1620 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1621 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1622 return pTask->compat_flags;