Use the standard CreateThread routine to create 16-bit tasks instead
[wine/multimedia.git] / loader / task.c
blobac86874ca71cf1d319a7bcb30674c0851bf1f8fa
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/port.h"
13 #include "wine/winbase16.h"
14 #include "ntddk.h"
15 #include "callback.h"
16 #include "drive.h"
17 #include "file.h"
18 #include "global.h"
19 #include "instance.h"
20 #include "miscemu.h"
21 #include "module.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 "syslevel.h"
30 #include "winsock.h"
31 #include "debugtools.h"
32 #include "services.h"
33 #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;
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, WINE_LDT_FLAGS_CODE );
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 MAKESEGPTR( 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 static TDB *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 = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
230 if (!hTask) return NULL;
231 pTask = (TDB *)GlobalLock16( hTask );
232 FarSetOwner16( hTask, pModule->self );
234 /* Fill the task structure */
236 pTask->hSelf = hTask;
238 if (teb && 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 */
247 pTask->version = pModule->expected_version;
248 pTask->hModule = pModule->self;
249 pTask->hParent = GetCurrentTask();
250 pTask->magic = TDB_MAGIC;
251 pTask->nCmdShow = cmdShow;
252 pTask->teb = teb;
253 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
254 strcpy( pTask->curdir, "\\" );
255 lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
256 sizeof(pTask->curdir) - 1 );
258 /* Create the thunks block */
260 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
262 /* Copy the module name */
264 GetModuleName16( pModule->self, name, sizeof(name) );
265 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
267 /* Allocate a selector for the PDB */
269 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
270 pModule->self, WINE_LDT_FLAGS_DATA );
272 /* Fill the PDB */
274 pTask->pdb.int20 = 0x20cd;
275 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
276 PUT_UA_DWORD(&pTask->pdb.dispatcher[1],
277 (DWORD)GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" ));
278 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
279 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
280 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
281 pTask->pdb.fileHandlesPtr =
282 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
283 pTask->pdb.hFileHandles = 0;
284 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
285 /* FIXME: should we make a copy of the environment? */
286 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
287 pTask->pdb.nbFiles = 20;
289 /* Fill the command line */
291 if (!cmdline)
293 cmdline = GetCommandLineA();
294 /* remove the first word (program name) */
295 if (*cmdline == '"')
296 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
297 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
298 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
299 len = strlen(cmdline);
301 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
302 pTask->pdb.cmdLine[0] = len;
303 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
304 /* pTask->pdb.cmdLine[len+1] = 0; */
306 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name, len, cmdline, hTask );
308 /* Get the compatibility flags */
310 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
312 /* Allocate a code segment alias for the TDB */
314 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
315 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
317 /* Default DTA overwrites command line */
319 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
321 /* Create scheduler event for 16-bit tasks */
323 if ( !(pTask->flags & TDBF_WIN32) )
324 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
326 /* Enter task handle into thread */
328 if (teb) teb->htask16 = hTask;
329 if (!initial_task) initial_task = hTask;
331 return pTask;
335 /***********************************************************************
336 * TASK_DeleteTask
338 static void TASK_DeleteTask( HTASK16 hTask )
340 TDB *pTask;
341 HGLOBAL16 hPDB;
343 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
344 hPDB = pTask->hPDB;
346 pTask->magic = 0xdead; /* invalidate signature */
348 /* Free the selector aliases */
350 GLOBAL_FreeBlock( pTask->hCSAlias );
351 GLOBAL_FreeBlock( pTask->hPDB );
353 /* Free the task module */
355 FreeModule16( pTask->hModule );
357 /* Free the task structure itself */
359 GlobalFree16( hTask );
361 /* Free all memory used by this task (including the 32-bit stack, */
362 /* the environment block and the thunk segments). */
364 GlobalFreeAll16( hPDB );
368 /***********************************************************************
369 * TASK_CreateMainTask
371 * Create a task for the main (32-bit) process.
373 void TASK_CreateMainTask(void)
375 TDB *pTask;
376 STARTUPINFOA startup_info;
377 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
379 GetStartupInfoA( &startup_info );
380 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
381 pTask = TASK_Create( (NE_MODULE *)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
382 cmdShow, NtCurrentTeb(), NULL, 0 );
383 if (!pTask)
385 ERR("could not create task for main process\n");
386 ExitProcess(1);
389 /* Add the task to the linked list */
390 /* (no need to get the win16 lock, we are the only thread at this point) */
391 TASK_LinkTask( pTask->hSelf );
395 /* startup routine for a new 16-bit thread */
396 static DWORD CALLBACK task_start( TDB *pTask )
398 DWORD ret;
400 NtCurrentTeb()->tibflags &= ~TEBF_WIN32;
401 NtCurrentTeb()->htask16 = pTask->hSelf;
403 _EnterWin16Lock();
404 TASK_LinkTask( pTask->hSelf );
405 pTask->teb = NtCurrentTeb();
406 ret = NE_StartTask();
407 _LeaveWin16Lock();
408 return ret;
412 /***********************************************************************
413 * TASK_SpawnTask
415 * Spawn a new 16-bit task.
417 HTASK TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline, BYTE len, HANDLE *hThread )
419 TDB *pTask;
421 if (!(pTask = TASK_Create( pModule, cmdShow, NULL, cmdline, len ))) return 0;
422 if (!(*hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)task_start, pTask, 0, NULL )))
424 TASK_DeleteTask( pTask->hSelf );
425 return 0;
427 return pTask->hSelf;
431 /***********************************************************************
432 * TASK_KillTask
434 void TASK_ExitTask(void)
436 TDB *pTask;
437 DWORD lockCount;
439 /* Enter the Win16Lock to protect global data structures */
440 _EnterWin16Lock();
442 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
443 if ( !pTask )
445 _LeaveWin16Lock();
446 return;
449 TRACE("Killing task %04x\n", pTask->hSelf );
451 /* Perform USER cleanup */
453 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
454 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
455 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
456 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
458 /* Remove the task from the list to be sure we never switch back to it */
459 TASK_UnlinkTask( pTask->hSelf );
461 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
463 TRACE("this is the last task, exiting\n" );
464 ExitKernel16();
467 if( nTaskCount )
469 TDB* p = (TDB *)GlobalLock16( hFirstTask );
470 while( p )
472 if( p->hYieldTo == pTask->hSelf ) p->hYieldTo = 0;
473 p = (TDB *)GlobalLock16( p->hNext );
477 pTask->nEvents = 0;
479 if ( hLockedTask == pTask->hSelf )
480 hLockedTask = 0;
482 TASK_DeleteTask( pTask->hSelf );
484 /* ... schedule another one ... */
485 TASK_Reschedule();
487 /* ... and completely release the Win16Lock, just in case. */
488 ReleaseThunkLock( &lockCount );
491 /***********************************************************************
492 * TASK_Reschedule
494 * This is where all the magic of task-switching happens!
496 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
497 * This means that each 16-bit task runs until it voluntarily yields
498 * control, at which point the scheduler gets active and selects the
499 * next task to run.
501 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
502 * by the standard scheduler of the underlying OS. As many 16-bit apps
503 * *rely* on the behaviour of the Windows scheduler, however, we have
504 * to simulate that behaviour.
506 * This is achieved as follows: every 16-bit task is at time (except
507 * during task creation and deletion) in one of two states: either it
508 * is the one currently running, then the global variable hCurrentTask
509 * contains its task handle, or it is not currently running, then it
510 * is blocked on a special scheduler event, a global handle which
511 * is stored in the task struct.
513 * When the current task yields control, this routine gets called. Its
514 * purpose is to determine the next task to be active, signal the
515 * scheduler event of that task, and then put the current task to sleep
516 * waiting for *its* scheduler event to get signalled again.
518 * This routine can get called in a few other special situations as well:
520 * - On creation of a 16-bit task, the Unix process executing the task
521 * calls TASK_Reschedule once it has completed its initialization.
522 * At this point, the task needs to be blocked until its scheduler
523 * event is signalled the first time (this will be done by the parent
524 * process to get the task up and running).
526 * - When the task currently running terminates itself, this routine gets
527 * called and has to schedule another task, *without* blocking the
528 * terminating task.
530 * - When a 32-bit thread posts an event for a 16-bit task, it might be
531 * the case that *no* 16-bit task is currently running. In this case
532 * the task that has now an event pending is to be scheduled.
535 void TASK_Reschedule(void)
537 TDB *pOldTask = NULL, *pNewTask = NULL;
538 HTASK16 hOldTask = 0, hNewTask = 0;
539 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
540 DWORD lockCount;
542 _EnterWin16Lock();
544 /* Check what we need to do */
545 hOldTask = GetCurrentTask();
546 pOldTask = (TDB *)GlobalLock16( hOldTask );
547 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
548 hCurrentTask, hOldTask, (long) getpid() );
550 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
552 /* We are called by an active (non-deleted) 16-bit task */
554 /* If we don't even have a current task, or else the current
555 task has yielded, we'll need to schedule a new task and
556 (possibly) put the calling task to sleep. Otherwise, we
557 only block the caller. */
559 if ( !hCurrentTask || hCurrentTask == hOldTask )
560 mode = MODE_YIELD;
561 else
562 mode = MODE_SLEEP;
564 else
566 /* We are called by a deleted 16-bit task or a 32-bit thread */
568 /* The only situation where we need to do something is if we
569 now do not have a current task. Then, we'll need to wake up
570 some task that has events pending. */
572 if ( !hCurrentTask || hCurrentTask == hOldTask )
573 mode = MODE_WAKEUP;
574 else
576 /* nothing to do */
577 _LeaveWin16Lock();
578 return;
582 /* Find a task to yield to: check for DirectedYield() */
583 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
585 hNewTask = pOldTask->hYieldTo;
586 pNewTask = (TDB *)GlobalLock16( hNewTask );
587 if( !pNewTask || !pNewTask->nEvents || !pNewTask->teb) hNewTask = 0;
588 pOldTask->hYieldTo = 0;
591 /* Find a task to yield to: check for pending events */
592 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
594 hNewTask = hFirstTask;
595 while (hNewTask)
597 pNewTask = (TDB *)GlobalLock16( hNewTask );
599 TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
601 if (pNewTask->nEvents) break;
602 hNewTask = pNewTask->hNext;
604 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
607 /* If we are still the task with highest priority, just return ... */
608 if ( mode == MODE_YIELD && hNewTask && hNewTask == hCurrentTask )
610 TRACE("returning to the current task (%04x)\n", hCurrentTask );
611 _LeaveWin16Lock();
613 /* Allow Win32 threads to thunk down even while a Win16 task is
614 in a tight PeekMessage() or Yield() loop ... */
615 ReleaseThunkLock( &lockCount );
616 RestoreThunkLock( lockCount );
617 return;
620 /* If no task to yield to found, suspend 16-bit scheduler ... */
621 if ( mode == MODE_YIELD && !hNewTask )
623 TRACE("No currently active task\n");
624 hCurrentTask = 0;
627 /* If we found a task to wake up, do it ... */
628 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
630 TRACE("Switching to task %04x (%.8s)\n",
631 hNewTask, pNewTask->module_name );
633 pNewTask->priority++;
634 TASK_UnlinkTask( hNewTask );
635 TASK_LinkTask( hNewTask );
636 pNewTask->priority--;
638 hCurrentTask = hNewTask;
639 NtSetEvent( pNewTask->hEvent, NULL );
641 /* This is set just in case some app reads it ... */
642 pNewTask->ss_sp = pNewTask->teb->cur_stack;
645 /* If we need to put the current task to sleep, do it ... */
646 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
648 NtResetEvent( pOldTask->hEvent, NULL );
650 ReleaseThunkLock( &lockCount );
651 SYSLEVEL_CheckNotLevel( 1 );
652 WaitForSingleObject( pOldTask->hEvent, INFINITE );
653 RestoreThunkLock( lockCount );
656 _LeaveWin16Lock();
659 /***********************************************************************
660 * InitTask (KERNEL.91)
662 * Called by the application startup code.
664 void WINAPI InitTask16( CONTEXT86 *context )
666 TDB *pTask;
667 INSTANCEDATA *pinstance;
668 SEGPTR ptr;
670 context->Eax = 0;
671 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
673 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
674 as some apps, notably Visual Basic apps, *modify* the heap/stack
675 size of the instance data segment before calling InitTask() */
677 /* Initialize the INSTANCEDATA structure */
678 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
679 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack ) + sizeof( STACK16FRAME );
680 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
681 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
682 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
684 /* Initialize the local heap */
685 if (LOWORD(context->Ecx))
686 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
688 /* Initialize implicitly loaded DLLs */
689 NE_InitializeDLLs( pTask->hModule );
690 NE_DllProcessAttach( pTask->hModule );
692 /* Registers on return are:
693 * ax 1 if OK, 0 on error
694 * cx stack limit in bytes
695 * dx cmdShow parameter
696 * si instance handle of the previous instance
697 * di instance handle of the new task
698 * es:bx pointer to command line inside PSP
700 * 0 (=%bp) is pushed on the stack
702 ptr = stack16_push( sizeof(WORD) );
703 *(WORD *)MapSL(ptr) = 0;
704 context->Esp -= 2;
706 context->Eax = 1;
708 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
709 else
711 LPBYTE p = &pTask->pdb.cmdLine[1];
712 while ((*p == ' ') || (*p == '\t')) p++;
713 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
715 context->Ecx = pinstance->stacktop;
716 context->Edx = pTask->nCmdShow;
717 context->Esi = (DWORD)pTask->hPrevInstance;
718 context->Edi = (DWORD)pTask->hInstance;
719 context->SegEs = (WORD)pTask->hPDB;
723 /***********************************************************************
724 * WaitEvent (KERNEL.30)
726 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
728 TDB *pTask;
730 if (!hTask) hTask = GetCurrentTask();
731 pTask = (TDB *)GlobalLock16( hTask );
733 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
735 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
736 return TRUE;
739 if (pTask->nEvents > 0)
741 pTask->nEvents--;
742 return FALSE;
744 TASK_Reschedule();
746 /* When we get back here, we have an event */
748 if (pTask->nEvents > 0) pTask->nEvents--;
749 return TRUE;
753 /***********************************************************************
754 * PostEvent (KERNEL.31)
756 void WINAPI PostEvent16( HTASK16 hTask )
758 TDB *pTask;
760 if (!hTask) hTask = GetCurrentTask();
761 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
763 if ( !THREAD_IsWin16( pTask->teb ) )
765 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
766 return;
769 pTask->nEvents++;
771 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
772 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
773 TASK_Reschedule();
777 /***********************************************************************
778 * SetPriority (KERNEL.32)
780 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
782 TDB *pTask;
783 INT16 newpriority;
785 if (!hTask) hTask = GetCurrentTask();
786 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
787 newpriority = pTask->priority + delta;
788 if (newpriority < -32) newpriority = -32;
789 else if (newpriority > 15) newpriority = 15;
791 pTask->priority = newpriority + 1;
792 TASK_UnlinkTask( hTask );
793 TASK_LinkTask( hTask );
794 pTask->priority--;
798 /***********************************************************************
799 * LockCurrentTask (KERNEL.33)
801 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
803 if (bLock) hLockedTask = GetCurrentTask();
804 else hLockedTask = 0;
805 return hLockedTask;
809 /***********************************************************************
810 * IsTaskLocked (KERNEL.122)
812 HTASK16 WINAPI IsTaskLocked16(void)
814 return hLockedTask;
818 /***********************************************************************
819 * OldYield (KERNEL.117)
821 void WINAPI OldYield16(void)
823 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
825 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
827 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
828 return;
831 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
832 TASK_Reschedule();
833 if (pCurTask) pCurTask->nEvents--;
836 /***********************************************************************
837 * WIN32_OldYield16 (KERNEL.447)
839 void WINAPI WIN32_OldYield16(void)
841 DWORD count;
843 ReleaseThunkLock(&count);
844 RestoreThunkLock(count);
847 /***********************************************************************
848 * DirectedYield (KERNEL.150)
850 void WINAPI DirectedYield16( HTASK16 hTask )
852 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
854 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
856 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
857 return;
860 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
862 pCurTask->hYieldTo = hTask;
863 OldYield16();
865 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
868 /***********************************************************************
869 * Yield16 (KERNEL.29)
871 void WINAPI Yield16(void)
873 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
875 if (pCurTask) pCurTask->hYieldTo = 0;
876 if (pCurTask && pCurTask->hQueue && Callout.UserYield16) Callout.UserYield16();
877 else OldYield16();
880 /***********************************************************************
881 * KERNEL_490 (KERNEL.490)
883 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
885 if ( !someTask ) return 0;
887 FIXME("(%04x): stub\n", someTask );
888 return 0;
891 /***********************************************************************
892 * MakeProcInstance16 (KERNEL.51)
894 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
896 BYTE *thunk,*lfunc;
897 SEGPTR thunkaddr;
898 WORD hInstanceSelector;
900 hInstanceSelector = GlobalHandleToSel16(hInstance);
902 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
904 if (!HIWORD(func)) {
905 /* Win95 actually protects via SEH, but this is better for debugging */
906 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
907 return (FARPROC16)0;
910 if (hInstance)
912 if ( (!(hInstance & 4)) ||
913 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
915 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
916 hInstance);
917 return 0;
921 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
922 && (hInstance != 0)
923 && (hInstance != 0xffff) )
925 /* calling MPI with a foreign DSEG is invalid ! */
926 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
927 hInstance,CURRENT_DS);
930 /* Always use the DSEG that MPI was entered with.
931 * We used to set hInstance to GetTaskDS16(), but this should be wrong
932 * as CURRENT_DS provides the DSEG value we need.
933 * ("calling" DS, *not* "task" DS !) */
934 hInstanceSelector = CURRENT_DS;
935 hInstance = GlobalHandle16(hInstanceSelector);
937 /* no thunking for DLLs */
938 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
939 return func;
941 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
942 if (!thunkaddr) return (FARPROC16)0;
943 thunk = MapSL( thunkaddr );
944 lfunc = MapSL( (SEGPTR)func );
946 TRACE("(%08lx,%04x): got thunk %08lx\n",
947 (DWORD)func, hInstance, (DWORD)thunkaddr );
948 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
949 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
951 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
954 *thunk++ = 0xb8; /* movw instance, %ax */
955 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
956 *thunk++ = (BYTE)(hInstanceSelector >> 8);
957 *thunk++ = 0xea; /* ljmp func */
958 *(DWORD *)thunk = (DWORD)func;
959 return (FARPROC16)thunkaddr;
960 /* CX reg indicates if thunkaddr != NULL, implement if needed */
964 /***********************************************************************
965 * FreeProcInstance16 (KERNEL.52)
967 void WINAPI FreeProcInstance16( FARPROC16 func )
969 TRACE("(%08lx)\n", (DWORD)func );
970 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
973 /**********************************************************************
974 * TASK_GetCodeSegment
976 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
977 * and logical segment number of a given code segment.
979 * 'proc' either *is* already a pair of module handle and segment number,
980 * in which case there's nothing to do. Otherwise, it is a pointer to
981 * a function, and we need to retrieve the code segment. If the pointer
982 * happens to point to a thunk, we'll retrieve info about the code segment
983 * where the function pointed to by the thunk resides, not the thunk itself.
985 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
986 * the function the snoop code will return to ...
989 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
990 SEGTABLEENTRY **ppSeg, int *pSegNr )
992 NE_MODULE *pModule = NULL;
993 SEGTABLEENTRY *pSeg = NULL;
994 int segNr=0;
996 /* Try pair of module handle / segment number */
997 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
998 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
1000 segNr = LOWORD( proc );
1001 if ( segNr && segNr <= pModule->seg_count )
1002 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
1005 /* Try thunk or function */
1006 else
1008 BYTE *thunk = MapSL( (SEGPTR)proc );
1009 WORD selector;
1011 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1012 selector = thunk[6] + (thunk[7] << 8);
1013 else
1014 selector = HIWORD( proc );
1016 pModule = NE_GetPtr( GlobalHandle16( selector ) );
1017 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
1019 if ( pModule )
1020 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
1021 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
1022 break;
1024 if ( pModule && segNr > pModule->seg_count )
1025 pSeg = NULL;
1028 /* Abort if segment not found */
1030 if ( !pModule || !pSeg )
1031 return FALSE;
1033 /* Return segment data */
1035 if ( ppModule ) *ppModule = pModule;
1036 if ( ppSeg ) *ppSeg = pSeg;
1037 if ( pSegNr ) *pSegNr = segNr;
1039 return TRUE;
1042 /**********************************************************************
1043 * GetCodeHandle (KERNEL.93)
1045 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
1047 SEGTABLEENTRY *pSeg;
1049 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
1050 return (HANDLE16)0;
1052 return pSeg->hSeg;
1055 /**********************************************************************
1056 * GetCodeInfo (KERNEL.104)
1058 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1060 NE_MODULE *pModule;
1061 SEGTABLEENTRY *pSeg;
1062 int segNr;
1064 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1065 return FALSE;
1067 /* Fill in segment information */
1069 segInfo->offSegment = pSeg->filepos;
1070 segInfo->cbSegment = pSeg->size;
1071 segInfo->flags = pSeg->flags;
1072 segInfo->cbAlloc = pSeg->minsize;
1073 segInfo->h = pSeg->hSeg;
1074 segInfo->alignShift = pModule->alignment;
1076 if ( segNr == pModule->dgroup )
1077 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
1079 /* Return module handle in %es */
1081 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1083 return TRUE;
1087 /**********************************************************************
1088 * DefineHandleTable16 (KERNEL.94)
1090 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1092 FIXME("(%04x): stub ?\n", wOffset);
1093 return TRUE;
1097 /***********************************************************************
1098 * SetTaskQueue (KERNEL.34)
1100 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1102 HQUEUE16 hPrev;
1103 TDB *pTask;
1105 if (!hTask) hTask = GetCurrentTask();
1106 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1108 hPrev = pTask->hQueue;
1109 pTask->hQueue = hQueue;
1111 return hPrev;
1115 /***********************************************************************
1116 * GetTaskQueue (KERNEL.35)
1118 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1120 TDB *pTask;
1122 if (!hTask) hTask = GetCurrentTask();
1123 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1124 return pTask->hQueue;
1127 /***********************************************************************
1128 * SetThreadQueue (KERNEL.463)
1130 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1132 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1133 HQUEUE16 oldQueue = teb? teb->queue : 0;
1135 if ( teb )
1137 teb->queue = hQueue;
1139 if ( GetTaskQueue16( teb->htask16 ) == oldQueue )
1140 SetTaskQueue16( teb->htask16, hQueue );
1143 return oldQueue;
1146 /***********************************************************************
1147 * GetThreadQueue (KERNEL.464)
1149 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1151 TEB *teb = NULL;
1152 if ( !thread )
1153 teb = NtCurrentTeb();
1154 else if ( HIWORD(thread) )
1155 teb = THREAD_IdToTEB( thread );
1156 else if ( IsTask16( (HTASK16)thread ) )
1157 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1159 return (HQUEUE16)(teb? teb->queue : 0);
1162 /***********************************************************************
1163 * SetFastQueue (KERNEL.624)
1165 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1167 TEB *teb = NULL;
1168 if ( !thread )
1169 teb = NtCurrentTeb();
1170 else if ( HIWORD(thread) )
1171 teb = THREAD_IdToTEB( thread );
1172 else if ( IsTask16( (HTASK16)thread ) )
1173 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1175 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1178 /***********************************************************************
1179 * GetFastQueue (KERNEL.625)
1181 HANDLE WINAPI GetFastQueue16( void )
1183 TEB *teb = NtCurrentTeb();
1184 if (!teb) return 0;
1186 if (!teb->queue && Callout.InitThreadInput16)
1187 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
1189 if (!teb->queue)
1190 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1192 return (HANDLE)teb->queue;
1195 /***********************************************************************
1196 * SwitchStackTo (KERNEL.108)
1198 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1200 TDB *pTask;
1201 STACK16FRAME *oldFrame, *newFrame;
1202 INSTANCEDATA *pData;
1203 UINT16 copySize;
1205 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1206 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1207 TRACE("old=%04x:%04x new=%04x:%04x\n",
1208 SELECTOROF( pTask->teb->cur_stack ),
1209 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1211 /* Save the old stack */
1213 oldFrame = THREAD_STACK16( pTask->teb );
1214 /* pop frame + args and push bp */
1215 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1216 + 2 * sizeof(WORD);
1217 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1218 pData->stacktop = top;
1219 pData->stackmin = ptr;
1220 pData->stackbottom = ptr;
1222 /* Switch to the new stack */
1224 /* Note: we need to take the 3 arguments into account; otherwise,
1225 * the stack will underflow upon return from this function.
1227 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1228 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1229 pTask->teb->cur_stack = MAKESEGPTR( seg, ptr - copySize );
1230 newFrame = THREAD_STACK16( pTask->teb );
1232 /* Copy the stack frame and the local variables to the new stack */
1234 memmove( newFrame, oldFrame, copySize );
1235 newFrame->bp = ptr;
1236 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1240 /***********************************************************************
1241 * SwitchStackBack (KERNEL.109)
1243 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1245 STACK16FRAME *oldFrame, *newFrame;
1246 INSTANCEDATA *pData;
1248 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1249 return;
1250 if (!pData->old_ss_sp)
1252 WARN("No previous SwitchStackTo\n" );
1253 return;
1255 TRACE("restoring stack %04x:%04x\n",
1256 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1258 oldFrame = CURRENT_STACK16;
1260 /* Pop bp from the previous stack */
1262 BP_reg(context) = *(WORD *)MapSL(pData->old_ss_sp);
1263 pData->old_ss_sp += sizeof(WORD);
1265 /* Switch back to the old stack */
1267 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1268 context->SegSs = SELECTOROF(pData->old_ss_sp);
1269 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1270 pData->old_ss_sp = 0;
1272 /* Build a stack frame for the return */
1274 newFrame = CURRENT_STACK16;
1275 newFrame->frame32 = oldFrame->frame32;
1276 newFrame->module_cs = oldFrame->module_cs;
1277 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1278 newFrame->entry_ip = oldFrame->entry_ip;
1282 /***********************************************************************
1283 * GetTaskQueueDS16 (KERNEL.118)
1285 void WINAPI GetTaskQueueDS16(void)
1287 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1291 /***********************************************************************
1292 * GetTaskQueueES16 (KERNEL.119)
1294 void WINAPI GetTaskQueueES16(void)
1296 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1300 /***********************************************************************
1301 * GetCurrentTask (KERNEL.36)
1303 HTASK16 WINAPI GetCurrentTask(void)
1305 return NtCurrentTeb()->htask16;
1308 DWORD WINAPI WIN16_GetCurrentTask(void)
1310 /* This is the version used by relay code; the first task is */
1311 /* returned in the high word of the result */
1312 return MAKELONG( GetCurrentTask(), hFirstTask );
1316 /***********************************************************************
1317 * GetCurrentPDB (KERNEL.37)
1319 * UNDOC: returns PSP of KERNEL in high word
1321 DWORD WINAPI GetCurrentPDB16(void)
1323 TDB *pTask;
1325 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1326 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1330 /***********************************************************************
1331 * GetCurPID16 (KERNEL.157)
1333 DWORD WINAPI GetCurPID16( DWORD unused )
1335 return 0;
1339 /***********************************************************************
1340 * GetInstanceData (KERNEL.54)
1342 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1344 char *ptr = (char *)GlobalLock16( instance );
1345 if (!ptr || !len) return 0;
1346 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1347 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1348 return len;
1352 /***********************************************************************
1353 * GetExeVersion (KERNEL.105)
1355 WORD WINAPI GetExeVersion16(void)
1357 TDB *pTask;
1359 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1360 return pTask->version;
1364 /***********************************************************************
1365 * SetErrorMode16 (KERNEL.107)
1367 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1369 TDB *pTask;
1370 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1371 pTask->error_mode = mode;
1372 return SetErrorMode( mode );
1376 /***********************************************************************
1377 * GetNumTasks (KERNEL.152)
1379 UINT16 WINAPI GetNumTasks16(void)
1381 return nTaskCount;
1385 /***********************************************************************
1386 * GetTaskDS (KERNEL.155)
1388 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1389 * I don't think we need to bother with this.
1391 HINSTANCE16 WINAPI GetTaskDS16(void)
1393 TDB *pTask;
1395 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1396 return GlobalHandleToSel16(pTask->hInstance);
1399 /***********************************************************************
1400 * GetDummyModuleHandleDS (KERNEL.602)
1402 WORD WINAPI GetDummyModuleHandleDS16(void)
1404 TDB *pTask;
1405 WORD selector;
1407 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1408 if (!(pTask->flags & TDBF_WIN32)) return 0;
1409 selector = GlobalHandleToSel16( pTask->hModule );
1410 CURRENT_DS = selector;
1411 return selector;
1414 /***********************************************************************
1415 * IsTask (KERNEL.320)
1416 * IsTask16 (KERNEL32.@)
1418 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1420 TDB *pTask;
1422 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1423 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1424 return (pTask->magic == TDB_MAGIC);
1428 /***********************************************************************
1429 * IsWinOldApTask16 (KERNEL.158)
1431 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1433 /* should return bit 0 of byte 0x48 in PSP */
1434 return FALSE;
1437 /***********************************************************************
1438 * SetTaskSignalProc (KERNEL.38)
1440 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1442 TDB *pTask;
1443 FARPROC16 oldProc;
1445 if (!hTask) hTask = GetCurrentTask();
1446 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1447 oldProc = pTask->userhandler;
1448 pTask->userhandler = proc;
1449 return oldProc;
1452 /***********************************************************************
1453 * TASK_CallTaskSignalProc
1455 /* ### start build ### */
1456 extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1457 /* ### stop build ### */
1458 void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1460 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1461 if ( !pTask || !pTask->userhandler ) return;
1463 TASK_CallTo16_word_wwwww( pTask->userhandler,
1464 hTaskOrModule, uCode, 0,
1465 pTask->hInstance, pTask->hQueue );
1468 /***********************************************************************
1469 * SetSigHandler (KERNEL.140)
1471 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1472 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1474 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1475 newhandler,oldhandler,oldmode,newmode,flag );
1477 if (flag != 1) return 0;
1478 if (!newmode) newhandler = NULL; /* Default handler */
1479 if (newmode != 4)
1481 TDB *pTask;
1483 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1484 if (oldmode) *oldmode = pTask->signal_flags;
1485 pTask->signal_flags = newmode;
1486 if (oldhandler) *oldhandler = pTask->sighandler;
1487 pTask->sighandler = newhandler;
1489 return 0;
1493 /***********************************************************************
1494 * GlobalNotify (KERNEL.154)
1496 * Note that GlobalNotify does _not_ return the old NotifyProc
1497 * -- contrary to LocalNotify !!
1499 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1501 TDB *pTask;
1503 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1504 pTask->discardhandler = proc;
1508 /***********************************************************************
1509 * GetExePtr (KERNEL.133)
1511 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1513 char *ptr;
1514 HANDLE16 owner;
1516 /* Check for module handle */
1518 if (!(ptr = GlobalLock16( handle ))) return 0;
1519 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1521 /* Search for this handle inside all tasks */
1523 *hTask = hFirstTask;
1524 while (*hTask)
1526 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1527 if ((*hTask == handle) ||
1528 (pTask->hInstance == handle) ||
1529 (pTask->hQueue == handle) ||
1530 (pTask->hPDB == handle)) return pTask->hModule;
1531 *hTask = pTask->hNext;
1534 /* Check the owner for module handle */
1536 owner = FarGetOwner16( handle );
1537 if (!(ptr = GlobalLock16( owner ))) return 0;
1538 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1540 /* Search for the owner inside all tasks */
1542 *hTask = hFirstTask;
1543 while (*hTask)
1545 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1546 if ((*hTask == owner) ||
1547 (pTask->hInstance == owner) ||
1548 (pTask->hQueue == owner) ||
1549 (pTask->hPDB == owner)) return pTask->hModule;
1550 *hTask = pTask->hNext;
1553 return 0;
1556 /**********************************************************************/
1558 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1560 HTASK16 hTask = 0;
1561 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1562 STACK16FRAME *frame = CURRENT_STACK16;
1563 frame->ecx = hModule;
1564 if (hTask) frame->es = hTask;
1565 return hModule;
1568 /**********************************************************************/
1570 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1572 HTASK16 hTask = 0;
1573 return GetExePtrHelper( handle, &hTask );
1577 /***********************************************************************
1578 * TaskFirst (TOOLHELP.63)
1580 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1582 lpte->hNext = hFirstTask;
1583 return TaskNext16( lpte );
1587 /***********************************************************************
1588 * TaskNext (TOOLHELP.64)
1590 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1592 TDB *pTask;
1593 INSTANCEDATA *pInstData;
1595 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1596 if (!lpte->hNext) return FALSE;
1598 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1599 while (1) {
1600 pTask = (TDB *)GlobalLock16( lpte->hNext );
1601 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1602 if (pTask->hInstance)
1603 break;
1604 lpte->hNext = pTask->hNext;
1606 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1607 lpte->hTask = lpte->hNext;
1608 lpte->hTaskParent = pTask->hParent;
1609 lpte->hInst = pTask->hInstance;
1610 lpte->hModule = pTask->hModule;
1611 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1612 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1613 lpte->wStackTop = pInstData->stacktop;
1614 lpte->wStackMinimum = pInstData->stackmin;
1615 lpte->wStackBottom = pInstData->stackbottom;
1616 lpte->wcEvents = pTask->nEvents;
1617 lpte->hQueue = pTask->hQueue;
1618 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1619 lpte->wPSPOffset = 0x100; /*??*/
1620 lpte->hNext = pTask->hNext;
1621 return TRUE;
1625 /***********************************************************************
1626 * TaskFindHandle (TOOLHELP.65)
1628 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1630 lpte->hNext = hTask;
1631 return TaskNext16( lpte );
1635 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1637 /**************************************************************************
1638 * FatalAppExit16 (KERNEL.137)
1640 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1642 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1644 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1646 HMODULE mod = GetModuleHandleA( "user32.dll" );
1647 if (mod)
1649 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1650 if (pMessageBoxA)
1652 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1653 goto done;
1656 ERR( "%s\n", debugstr_a(str) );
1658 done:
1659 ExitThread(0xff);
1663 /***********************************************************************
1664 * TerminateApp16 (TOOLHELP.77)
1666 * See "Undocumented Windows".
1668 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1670 if (hTask && hTask != GetCurrentTask())
1672 FIXME("cannot terminate task %x\n", hTask);
1673 return;
1676 if (wFlags & NO_UAE_BOX)
1678 UINT16 old_mode;
1679 old_mode = SetErrorMode16(0);
1680 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1682 FatalAppExit16( 0, NULL );
1684 /* hmm, we're still alive ?? */
1686 /* check undocumented flag */
1687 if (!(wFlags & 0x8000))
1688 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1690 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1691 but let's just call ExitThread */
1692 ExitThread(0xff);
1696 /***********************************************************************
1697 * GetAppCompatFlags16 (KERNEL.354)
1699 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1701 return GetAppCompatFlags( hTask );
1705 /***********************************************************************
1706 * GetAppCompatFlags (USER32.206)
1708 DWORD WINAPI GetAppCompatFlags( HTASK hTask )
1710 TDB *pTask;
1712 if (!hTask) hTask = GetCurrentTask();
1713 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1714 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1715 return pTask->compat_flags;