Changed error values MCI_INTERNAL to their correct values.
[wine/multimedia.git] / loader / task.c
blob6310e88dd159b87e39a6e847cb19bd0bf9fad291
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 "windows.h"
13 #include "user.h"
14 #include "callback.h"
15 #include "drive.h"
16 #include "file.h"
17 #include "global.h"
18 #include "instance.h"
19 #include "message.h"
20 #include "miscemu.h"
21 #include "module.h"
22 #include "neexe.h"
23 #include "peexe.h"
24 #include "pe_image.h"
25 #include "process.h"
26 #include "queue.h"
27 #include "selectors.h"
28 #include "stackframe.h"
29 #include "task.h"
30 #include "thread.h"
31 #include "toolhelp.h"
32 #include "winnt.h"
33 #include "winsock.h"
34 #include "thread.h"
35 #include "syslevel.h"
36 #include "debug.h"
37 #include "dosexe.h"
38 #include "dde_proc.h"
39 #include "server.h"
41 /* Min. number of thunks allocated when creating a new segment */
42 #define MIN_THUNKS 32
44 /* Pointer to function to switch to a larger stack */
45 int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL;
47 /* Pointer to debugger callback routine */
48 void (*TASK_AddTaskEntryBreakpoint)( HTASK16 hTask ) = NULL;
50 static THHOOK DefaultThhook = { 0 };
51 THHOOK *pThhook = &DefaultThhook;
53 #define hCurrentTask (pThhook->CurTDB)
54 #define hFirstTask (pThhook->HeadTDB)
55 #define hLockedTask (pThhook->LockTDB)
57 static HTASK16 hTaskToKill = 0;
58 static UINT16 nTaskCount = 0;
60 static void TASK_YieldToSystem(TDB*);
62 extern BOOL32 THREAD_InitDone;
65 /***********************************************************************
66 * TASK_InstallTHHook
68 void TASK_InstallTHHook( THHOOK *pNewThhook )
70 THHOOK *pOldThhook = pThhook;
72 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
74 *pThhook = *pOldThhook;
77 /***********************************************************************
78 * TASK_GetNextTask
80 HTASK16 TASK_GetNextTask( HTASK16 hTask )
82 TDB* pTask = (TDB*)GlobalLock16(hTask);
84 if (pTask->hNext) return pTask->hNext;
85 return (hFirstTask != hTask) ? hFirstTask : 0;
88 /***********************************************************************
89 * TASK_LinkTask
91 static void TASK_LinkTask( HTASK16 hTask )
93 HTASK16 *prevTask;
94 TDB *pTask;
96 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
97 prevTask = &hFirstTask;
98 while (*prevTask)
100 TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
101 if (prevTaskPtr->priority >= pTask->priority) break;
102 prevTask = &prevTaskPtr->hNext;
104 pTask->hNext = *prevTask;
105 *prevTask = hTask;
106 nTaskCount++;
110 /***********************************************************************
111 * TASK_UnlinkTask
113 static void TASK_UnlinkTask( HTASK16 hTask )
115 HTASK16 *prevTask;
116 TDB *pTask;
118 prevTask = &hFirstTask;
119 while (*prevTask && (*prevTask != hTask))
121 pTask = (TDB *)GlobalLock16( *prevTask );
122 prevTask = &pTask->hNext;
124 if (*prevTask)
126 pTask = (TDB *)GlobalLock16( *prevTask );
127 *prevTask = pTask->hNext;
128 pTask->hNext = 0;
129 nTaskCount--;
134 /***********************************************************************
135 * TASK_CreateThunks
137 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
138 * and containing 'count' entries.
140 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
142 int i;
143 WORD free;
144 THUNKS *pThunk;
146 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
147 pThunk->next = 0;
148 pThunk->magic = THUNK_MAGIC;
149 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
150 free = pThunk->free;
151 for (i = 0; i < count-1; i++)
153 free += 8; /* Offset of next thunk */
154 pThunk->thunks[4*i] = free;
156 pThunk->thunks[4*i] = 0; /* Last thunk */
160 /***********************************************************************
161 * TASK_AllocThunk
163 * Allocate a thunk for MakeProcInstance().
165 static SEGPTR TASK_AllocThunk( HTASK16 hTask )
167 TDB *pTask;
168 THUNKS *pThunk;
169 WORD sel, base;
171 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
172 sel = pTask->hCSAlias;
173 pThunk = &pTask->thunks;
174 base = (int)pThunk - (int)pTask;
175 while (!pThunk->free)
177 sel = pThunk->next;
178 if (!sel) /* Allocate a new segment */
180 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
181 pTask->hPDB, TRUE, FALSE, FALSE );
182 if (!sel) return (SEGPTR)0;
183 TASK_CreateThunks( sel, 0, MIN_THUNKS );
184 pThunk->next = sel;
186 pThunk = (THUNKS *)GlobalLock16( sel );
187 base = 0;
189 base += pThunk->free;
190 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
191 return PTR_SEG_OFF_TO_SEGPTR( sel, base );
195 /***********************************************************************
196 * TASK_FreeThunk
198 * Free a MakeProcInstance() thunk.
200 static BOOL32 TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
202 TDB *pTask;
203 THUNKS *pThunk;
204 WORD sel, base;
206 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
207 sel = pTask->hCSAlias;
208 pThunk = &pTask->thunks;
209 base = (int)pThunk - (int)pTask;
210 while (sel && (sel != HIWORD(thunk)))
212 sel = pThunk->next;
213 pThunk = (THUNKS *)GlobalLock16( sel );
214 base = 0;
216 if (!sel) return FALSE;
217 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
218 pThunk->free = LOWORD(thunk) - base;
219 return TRUE;
223 /***********************************************************************
224 * TASK_CallToStart
226 * 32-bit entry point for a new task. This function is responsible for
227 * setting up the registers and jumping to the 16-bit entry point.
229 static void TASK_CallToStart(void)
231 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
232 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
233 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
235 SET_CUR_THREAD( pTask->thdb );
236 CLIENT_InitThread();
238 /* Terminate the stack frame chain */
239 memset(THREAD_STACK16( pTask->thdb ), '\0', sizeof(STACK16FRAME));
241 if (pModule->flags & NE_FFLAGS_WIN32)
243 /* FIXME: all this is an ugly hack */
245 LPTHREAD_START_ROUTINE entry = (LPTHREAD_START_ROUTINE)
246 RVA_PTR(pModule->module32, OptionalHeader.AddressOfEntryPoint);
248 if (PE_HEADER(pModule->module32)->OptionalHeader.Subsystem==IMAGE_SUBSYSTEM_WINDOWS_CUI)
249 AllocConsole();
251 if (pModule->heap_size)
252 LocalInit( pTask->hInstance, 0, pModule->heap_size );
254 InitApp( pTask->hModule );
255 MODULE_InitializeDLLs( PROCESS_Current(), 0, DLL_PROCESS_ATTACH, (LPVOID)-1 );
256 TRACE(relay, "(entryproc=%p)\n", entry );
258 #if 1
259 ExitProcess( entry(NULL) );
260 #else
262 DWORD size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfStackReserve;
263 DWORD id;
264 THDB *thdb;
266 CreateThread( NULL, size, entry, NULL, 0, &id );
267 thdb = THREAD_ID_TO_THDB( id );
269 while ( thdb->exit_code == 0x103 )
271 WaitEvent( 0 );
272 QUEUE_Signal( pTask->hSelf );
275 ExitProcess( thdb->exit_code );
277 #endif
279 else if (pModule->dos_image)
281 DOSVM_Enter( NULL );
282 ExitProcess( 0 );
284 else
286 /* Registers at initialization must be:
287 * ax zero
288 * bx stack size in bytes
289 * cx heap size in bytes
290 * si previous app instance
291 * di current app instance
292 * bp zero
293 * es selector to the PSP
294 * ds dgroup of the application
295 * ss stack selector
296 * sp top of the stack
298 CONTEXT context;
300 memset( &context, 0, sizeof(context) );
301 CS_reg(&context) = GlobalHandleToSel(pSegTable[pModule->cs - 1].hSeg);
302 DS_reg(&context) = GlobalHandleToSel(pSegTable[pModule->dgroup - 1].hSeg);
303 ES_reg(&context) = pTask->hPDB;
304 EIP_reg(&context) = pModule->ip;
305 EBX_reg(&context) = pModule->stack_size;
306 ECX_reg(&context) = pModule->heap_size;
307 EDI_reg(&context) = context.SegDs;
309 TRACE(task, "Starting main program: cs:ip=%04lx:%04x ds=%04lx ss:sp=%04x:%04x\n",
310 CS_reg(&context), IP_reg(&context), DS_reg(&context),
311 SELECTOROF(pTask->thdb->cur_stack),
312 OFFSETOF(pTask->thdb->cur_stack) );
314 Callbacks->CallRegisterShortProc( &context, 0 );
315 /* This should never return */
316 ERR( task, "Main program returned! (should never happen)\n" );
317 ExitProcess( 1 );
322 /***********************************************************************
323 * TASK_Create
325 * NOTE: This routine might be called by a Win32 thread. We don't have
326 * any real problems with that, since we operated merely on a private
327 * TDB structure that is not yet linked into the task list.
329 HTASK16 TASK_Create( THDB *thdb, NE_MODULE *pModule, HINSTANCE16 hInstance,
330 HINSTANCE16 hPrevInstance, UINT16 cmdShow)
332 HTASK16 hTask;
333 TDB *pTask, *pInitialTask;
334 LPSTR cmd_line;
335 WORD sp;
336 char *stack32Top;
337 char name[10];
338 STACK16FRAME *frame16;
339 STACK32FRAME *frame32;
340 PDB32 *pdb32 = thdb->process;
341 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
343 /* Allocate the task structure */
345 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
346 pModule->self, FALSE, FALSE, FALSE );
347 if (!hTask) return 0;
348 pTask = (TDB *)GlobalLock16( hTask );
350 /* Fill the task structure */
352 pTask->nEvents = 1; /* So the task can be started */
353 pTask->hSelf = hTask;
354 pTask->flags = 0;
356 if (pModule->flags & NE_FFLAGS_WIN32)
357 pTask->flags |= TDBF_WIN32;
358 if (pModule->lpDosTask)
359 pTask->flags |= TDBF_WINOLDAP;
361 pTask->version = pModule->expected_version;
362 pTask->hInstance = hInstance;
363 pTask->hPrevInstance = hPrevInstance;
364 pTask->hModule = pModule->self;
365 pTask->hParent = GetCurrentTask();
366 pTask->magic = TDB_MAGIC;
367 pTask->nCmdShow = cmdShow;
368 pTask->thdb = thdb;
369 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
370 strcpy( pTask->curdir, "\\" );
371 lstrcpyn32A( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
372 sizeof(pTask->curdir) - 1 );
374 /* Create the thunks block */
376 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
378 /* Copy the module name */
380 GetModuleName( pModule->self, name, sizeof(name) );
381 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
383 /* Allocate a selector for the PDB */
385 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB),
386 pModule->self, FALSE, FALSE, FALSE, NULL );
388 /* Fill the PDB */
390 pTask->pdb.int20 = 0x20cd;
391 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
392 PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)NE_GetEntryPoint(
393 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
394 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
395 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
396 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
397 pTask->pdb.fileHandlesPtr =
398 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel(pTask->hPDB),
399 (int)&((PDB *)0)->fileHandles );
400 pTask->pdb.hFileHandles = 0;
401 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
402 pTask->pdb.environment = pdb32->env_db->env_sel;
403 pTask->pdb.nbFiles = 20;
405 /* Fill the command line */
407 cmd_line = pdb32->env_db->cmd_line;
408 while (*cmd_line && (*cmd_line != ' ') && (*cmd_line != '\t')) cmd_line++;
409 while ((*cmd_line == ' ') || (*cmd_line == '\t')) cmd_line++;
410 lstrcpyn32A( pTask->pdb.cmdLine+1, cmd_line, sizeof(pTask->pdb.cmdLine)-1);
411 pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 );
413 /* Get the compatibility flags */
415 pTask->compat_flags = GetProfileInt32A( "Compatibility", name, 0 );
417 /* Allocate a code segment alias for the TDB */
419 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
420 sizeof(TDB), pTask->hPDB, TRUE,
421 FALSE, FALSE, NULL );
423 /* Set the owner of the environment block */
425 FarSetOwner( pTask->pdb.environment, pTask->hPDB );
427 /* Default DTA overwrites command-line */
429 pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB,
430 (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
432 /* Inherit default UserSignalHandler from initial process */
434 pInitialTask = (TDB *)GlobalLock16( PROCESS_Initial()->task );
435 if ( pInitialTask )
436 pTask->userhandler = pInitialTask->userhandler;
438 /* Create the 16-bit stack frame */
440 if (!(sp = pModule->sp))
441 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
442 sp &= ~1; sp -= 2*sizeof(STACK16FRAME);
443 pTask->thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( pTask->hInstance, sp );
444 frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN( pTask->thdb->cur_stack );
445 frame16->ebp = sp + (int)&((STACK16FRAME *)0)->bp;
446 frame16->bp = LOWORD(frame16->ebp);
447 frame16->ds = frame16->es = pTask->hInstance;
448 frame16->fs = 0;
449 frame16->entry_point = 0;
450 frame16->entry_cs = 0;
451 frame16->mutex_count = 1; /* TASK_Reschedule is called from 16-bit code */
452 /* The remaining fields will be initialized in TASK_Reschedule */
454 /* Create the 32-bit stack frame */
456 stack32Top = (char*)pTask->thdb->teb.stack_top;
457 frame16->frame32 = frame32 = (STACK32FRAME *)stack32Top - 1;
458 frame32->frame16 = pTask->thdb->cur_stack + sizeof(STACK16FRAME);
459 frame32->edi = 0;
460 frame32->esi = 0;
461 frame32->edx = 0;
462 frame32->ecx = 0;
463 frame32->ebx = 0;
464 frame32->retaddr = (DWORD)TASK_CallToStart;
465 /* The remaining fields will be initialized in TASK_Reschedule */
468 TRACE(task, "module='%s' cmdline='%s' task=%04x\n",
469 name, cmd_line, hTask );
471 return hTask;
474 /***********************************************************************
475 * TASK_StartTask
477 * NOTE: This routine might be called by a Win32 thread. Thus, we need
478 * to be careful to protect global data structures. We do this
479 * by entering the Win16Lock while linking the task into the
480 * global task list.
482 void TASK_StartTask( HTASK16 hTask )
484 /* Add the task to the linked list */
486 SYSLEVEL_EnterWin16Lock();
487 TASK_LinkTask( hTask );
488 SYSLEVEL_LeaveWin16Lock();
490 TRACE(task, "linked task %04x\n", hTask );
492 /* If requested, add entry point breakpoint */
494 if ( TASK_AddTaskEntryBreakpoint )
495 TASK_AddTaskEntryBreakpoint( hTask );
497 /* Get the task up and running. If we ourselves are a 16-bit task,
498 we simply Yield(). If we are 32-bit however, we need to signal
499 the main process somehow (NOT YET IMPLEMENTED!) */
501 if ( THREAD_IsWin16( THREAD_Current() ) )
502 OldYield();
503 else
504 FIXME(task, "Don't know how to start 16-bit task from 32-bit thread. Move the mouse!\n");
508 /***********************************************************************
509 * TASK_DeleteTask
511 static void TASK_DeleteTask( HTASK16 hTask )
513 TDB *pTask;
514 HGLOBAL16 hPDB;
516 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
517 hPDB = pTask->hPDB;
519 pTask->magic = 0xdead; /* invalidate signature */
521 /* Delete the Win32 part of the task */
523 K32OBJ_DecCount( &pTask->thdb->process->header );
524 K32OBJ_DecCount( &pTask->thdb->header );
526 /* Free the selector aliases */
528 GLOBAL_FreeBlock( pTask->hCSAlias );
529 GLOBAL_FreeBlock( pTask->hPDB );
531 /* Free the task module */
533 FreeModule16( pTask->hModule );
535 /* Free the task structure itself */
537 GlobalFree16( hTask );
539 /* Free all memory used by this task (including the 32-bit stack, */
540 /* the environment block and the thunk segments). */
542 GlobalFreeAll( hPDB );
546 /***********************************************************************
547 * TASK_KillCurrentTask
549 * Kill the currently running task. As it's not possible to kill the
550 * current task like this, it is simply marked for destruction, and will
551 * be killed when either TASK_Reschedule or this function is called again
552 * in the context of another task.
554 void TASK_KillCurrentTask( INT16 exitCode )
556 TDB* pTask = (TDB*) GlobalLock16( GetCurrentTask() );
557 NE_MODULE* pModule = NE_GetPtr( pTask->hModule );
558 if (!pTask) USER_ExitWindows(); /* No current task yet */
560 if ( !THREAD_IsWin16( THREAD_Current() ) )
562 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
563 return;
566 /* Enter the Win16Lock to protect global data structures
567 NOTE: We never explicitly leave it again. This shouldn't matter
568 though, as it will be released in TASK_Reschedule and this
569 task won't ever get scheduled again ... */
571 SYSLEVEL_EnterWin16Lock();
573 assert(hCurrentTask == GetCurrentTask());
575 TRACE(task, "Killing task %04x\n", hCurrentTask );
577 /* Delete active sockets */
579 if( pTask->pwsi )
580 WINSOCK_DeleteTaskWSI( pTask, pTask->pwsi );
582 #ifdef MZ_SUPPORTED
583 /* Kill DOS VM task */
584 if ( pModule->lpDosTask )
585 MZ_KillModule( pModule->lpDosTask );
586 #endif
588 /* Perform USER cleanup */
590 if (pTask->userhandler)
591 pTask->userhandler( hCurrentTask, USIG_TERMINATION, 0,
592 pTask->hInstance, pTask->hQueue );
594 if (hTaskToKill && (hTaskToKill != hCurrentTask))
596 /* If another task is already marked for destruction, */
597 /* we can kill it now, as we are in another context. */
598 TASK_DeleteTask( hTaskToKill );
601 if (nTaskCount <= 1)
603 TRACE(task, "this is the last task, exiting\n" );
604 USER_ExitWindows();
607 if (!__winelib)
609 /* FIXME: Hack! Send a message to the initial task so that
610 * the GetMessage wakes up and the initial task can check whether
611 * it is the only remaining one and terminate itself ...
612 * The initial task should probably install hooks or something
613 * to get informed about task termination :-/
615 HTASK16 hTask = PROCESS_Initial()->task;
616 HMODULE16 hModule = GetModuleHandle16( "USER" );
617 FARPROC16 postFunc = WIN32_GetProcAddress16( hModule, "PostAppMessage" );
618 if (postFunc)
619 Callbacks->CallPostAppMessageProc( postFunc, hTask, WM_NULL, 0, 0 );
622 /* Remove the task from the list to be sure we never switch back to it */
623 TASK_UnlinkTask( hCurrentTask );
624 if( nTaskCount )
626 TDB* p = (TDB *)GlobalLock16( hFirstTask );
627 while( p )
629 if( p->hYieldTo == hCurrentTask ) p->hYieldTo = 0;
630 p = (TDB *)GlobalLock16( p->hNext );
634 hTaskToKill = hCurrentTask;
635 hLockedTask = 0;
637 pTask->nEvents = 0;
638 TASK_YieldToSystem(pTask);
640 /* We should never return from this Yield() */
642 ERR(task,"Return of the living dead %04x!!!\n", hCurrentTask);
643 exit(1);
646 /***********************************************************************
647 * TASK_Reschedule
649 * This is where all the magic of task-switching happens!
651 * Note: This function should only be called via the TASK_YieldToSystem()
652 * wrapper, to make sure that all the context is saved correctly.
654 * It must not call functions that may yield control.
656 void TASK_Reschedule(void)
658 TDB *pOldTask = NULL, *pNewTask;
659 HTASK16 hTask = 0;
660 STACK16FRAME *newframe16;
662 /* Get the initial task up and running */
663 if (!hCurrentTask && GetCurrentTask())
665 /* We need to remove one pair of stackframes (exept for Winelib) */
666 STACK16FRAME *oldframe16 = CURRENT_STACK16;
667 STACK32FRAME *oldframe32 = oldframe16->frame32;
668 STACK16FRAME *newframe16 = PTR_SEG_TO_LIN( oldframe32->frame16 );
669 STACK32FRAME *newframe32 = newframe16->frame32;
670 if (newframe32)
672 newframe16->entry_ip = oldframe16->entry_ip;
673 newframe16->entry_cs = oldframe16->entry_cs;
674 newframe16->ip = oldframe16->ip;
675 newframe16->cs = oldframe16->cs;
676 newframe32->ebp = oldframe32->ebp;
677 newframe32->restore_addr = oldframe32->restore_addr;
678 newframe32->codeselector = oldframe32->codeselector;
680 THREAD_Current()->cur_stack = oldframe32->frame16;
683 hCurrentTask = GetCurrentTask();
684 pNewTask = (TDB *)GlobalLock16( hCurrentTask );
685 pNewTask->ss_sp = pNewTask->thdb->cur_stack;
686 return;
689 /* NOTE: As we are entered from 16-bit code, we hold the Win16Lock.
690 We hang onto it thoughout most of this routine, so that accesses
691 to global variables (most notably the task list) are protected. */
692 assert(hCurrentTask == GetCurrentTask());
694 TRACE(task, "entered with hTask %04x (pid %d)\n", hCurrentTask, getpid());
696 #ifdef CONFIG_IPC
697 /* FIXME: What about the Win16Lock ??? */
698 dde_reschedule();
699 #endif
700 /* First check if there's a task to kill */
702 if (hTaskToKill && (hTaskToKill != hCurrentTask))
704 TASK_DeleteTask( hTaskToKill );
705 hTaskToKill = 0;
708 /* Find a task to yield to */
710 pOldTask = (TDB *)GlobalLock16( hCurrentTask );
711 if (pOldTask && pOldTask->hYieldTo)
713 /* check for DirectedYield() */
715 hTask = pOldTask->hYieldTo;
716 pNewTask = (TDB *)GlobalLock16( hTask );
717 if( !pNewTask || !pNewTask->nEvents) hTask = 0;
718 pOldTask->hYieldTo = 0;
721 /* extract hardware events only! */
723 if (!hTask) EVENT_WaitNetEvent( FALSE, TRUE );
725 while (!hTask)
727 /* Find a task that has an event pending */
729 hTask = hFirstTask;
730 while (hTask)
732 pNewTask = (TDB *)GlobalLock16( hTask );
734 TRACE(task, "\ttask = %04x, events = %i\n", hTask, pNewTask->nEvents);
736 if (pNewTask->nEvents) break;
737 hTask = pNewTask->hNext;
739 if (hLockedTask && (hTask != hLockedTask)) hTask = 0;
740 if (hTask) break;
742 /* No task found, wait for some events to come in */
744 /* NOTE: We release the Win16Lock while waiting for events. This is to enable
745 Win32 threads to thunk down to 16-bit temporarily. Since Win16
746 tasks won't execute and Win32 threads are not allowed to enter
747 TASK_Reschedule anyway, there should be no re-entrancy problem ... */
749 SYSLEVEL_ReleaseWin16Lock();
750 EVENT_WaitNetEvent( TRUE, TRUE );
751 SYSLEVEL_RestoreWin16Lock();
754 if (hTask == hCurrentTask)
756 TRACE(task, "returning to the current task(%04x)\n", hTask );
757 return; /* Nothing to do */
759 pNewTask = (TDB *)GlobalLock16( hTask );
760 TRACE(task, "Switching to task %04x (%.8s)\n",
761 hTask, pNewTask->module_name );
763 /* Make the task the last in the linked list (round-robin scheduling) */
765 pNewTask->priority++;
766 TASK_UnlinkTask( hTask );
767 TASK_LinkTask( hTask );
768 pNewTask->priority--;
770 /* Finish initializing the new task stack if necessary */
772 newframe16 = THREAD_STACK16( pNewTask->thdb );
773 if (!newframe16->entry_cs)
775 STACK16FRAME *oldframe16 = CURRENT_STACK16;
776 STACK32FRAME *oldframe32 = oldframe16->frame32;
777 STACK32FRAME *newframe32 = newframe16->frame32;
778 newframe16->entry_ip = oldframe16->entry_ip;
779 newframe16->entry_cs = oldframe16->entry_cs;
780 newframe16->ip = oldframe16->ip;
781 newframe16->cs = oldframe16->cs;
782 newframe32->ebp = oldframe32->ebp;
783 newframe32->restore_addr = oldframe32->restore_addr;
784 newframe32->codeselector = oldframe32->codeselector;
787 /* Switch to the new stack */
789 /* NOTE: We need to release/restore the Win16Lock, as the task
790 switched to might be at another recursion level than
791 the old task ... */
793 SYSLEVEL_ReleaseWin16Lock();
795 hCurrentTask = hTask;
796 SET_CUR_THREAD( pNewTask->thdb );
797 pNewTask->ss_sp = pNewTask->thdb->cur_stack;
799 SYSLEVEL_RestoreWin16Lock();
803 /***********************************************************************
804 * TASK_YieldToSystem
806 * Scheduler interface, this way we ensure that all "unsafe" events are
807 * processed outside the scheduler.
809 void TASK_YieldToSystem(TDB* pTask)
811 MESSAGEQUEUE* pQ;
813 if ( !THREAD_IsWin16( THREAD_Current() ) )
815 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
816 return;
819 Callbacks->CallTaskRescheduleProc();
821 if( pTask )
823 pQ = (MESSAGEQUEUE*)GlobalLock16(pTask->hQueue);
824 if( pQ && pQ->flags & QUEUE_FLAG_XEVENT &&
825 !(pQ->wakeBits & (QS_SENDMESSAGE | QS_SMRESULT)) )
827 pQ->flags &= ~QUEUE_FLAG_XEVENT;
828 EVENT_WaitNetEvent( FALSE, FALSE );
834 /***********************************************************************
835 * InitTask (KERNEL.91)
837 * Called by the application startup code.
839 void WINAPI InitTask( CONTEXT *context )
841 TDB *pTask;
842 NE_MODULE *pModule;
843 SEGTABLEENTRY *pSegTable;
844 INSTANCEDATA *pinstance;
845 LONG stacklow, stackhi;
847 if (context) EAX_reg(context) = 0;
848 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
849 if (!(pModule = NE_GetPtr( pTask->hModule ))) return;
851 /* Initialize implicitly loaded DLLs */
852 NE_InitializeDLLs( pTask->hModule );
854 if (context)
856 /* Registers on return are:
857 * ax 1 if OK, 0 on error
858 * cx stack limit in bytes
859 * dx cmdShow parameter
860 * si instance handle of the previous instance
861 * di instance handle of the new task
862 * es:bx pointer to command-line inside PSP
864 * 0 (=%bp) is pushed on the stack
866 SEGPTR ptr = STACK16_PUSH( pTask->thdb, sizeof(WORD) );
867 *(WORD *)PTR_SEG_TO_LIN(ptr) = 0;
868 SP_reg(context) -= 2;
870 EAX_reg(context) = 1;
872 if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80;
873 else
875 LPBYTE p = &pTask->pdb.cmdLine[1];
876 while ((*p == ' ') || (*p == '\t')) p++;
877 EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine);
879 ECX_reg(context) = pModule->stack_size;
880 EDX_reg(context) = pTask->nCmdShow;
881 ESI_reg(context) = (DWORD)pTask->hPrevInstance;
882 EDI_reg(context) = (DWORD)pTask->hInstance;
883 ES_reg (context) = (WORD)pTask->hPDB;
886 /* Initialize the local heap */
887 if ( pModule->heap_size )
889 LocalInit( pTask->hInstance, 0, pModule->heap_size );
892 /* Initialize the INSTANCEDATA structure */
893 pSegTable = NE_SEG_TABLE( pModule );
894 stacklow = pSegTable[pModule->ss - 1].minsize;
895 stackhi = stacklow + pModule->stack_size;
896 if (stackhi > 0xffff) stackhi = 0xffff;
897 pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
898 pinstance->stackbottom = stackhi; /* yup, that's right. Confused me too. */
899 pinstance->stacktop = stacklow;
900 pinstance->stackmin = OFFSETOF( pTask->thdb->cur_stack );
904 /***********************************************************************
905 * WaitEvent (KERNEL.30)
907 BOOL16 WINAPI WaitEvent( HTASK16 hTask )
909 TDB *pTask;
911 if (!hTask) hTask = GetCurrentTask();
912 pTask = (TDB *)GlobalLock16( hTask );
914 if ( !THREAD_IsWin16( THREAD_Current() ) )
916 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
917 return TRUE;
920 if (pTask->nEvents > 0)
922 pTask->nEvents--;
923 return FALSE;
925 TASK_YieldToSystem(pTask);
927 /* When we get back here, we have an event */
929 if (pTask->nEvents > 0) pTask->nEvents--;
930 return TRUE;
934 /***********************************************************************
935 * PostEvent (KERNEL.31)
937 void WINAPI PostEvent( HTASK16 hTask )
939 TDB *pTask;
941 if (!hTask) hTask = GetCurrentTask();
942 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
944 if ( !THREAD_IsWin16( THREAD_Current() ) )
946 WARN(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
947 /* return; */
950 pTask->nEvents++;
954 /***********************************************************************
955 * SetPriority (KERNEL.32)
957 void WINAPI SetPriority( HTASK16 hTask, INT16 delta )
959 TDB *pTask;
960 INT16 newpriority;
962 if (!hTask) hTask = GetCurrentTask();
963 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
964 newpriority = pTask->priority + delta;
965 if (newpriority < -32) newpriority = -32;
966 else if (newpriority > 15) newpriority = 15;
968 pTask->priority = newpriority + 1;
969 TASK_UnlinkTask( hTask );
970 TASK_LinkTask( hTask );
971 pTask->priority--;
975 /***********************************************************************
976 * LockCurrentTask (KERNEL.33)
978 HTASK16 WINAPI LockCurrentTask( BOOL16 bLock )
980 if (bLock) hLockedTask = GetCurrentTask();
981 else hLockedTask = 0;
982 return hLockedTask;
986 /***********************************************************************
987 * IsTaskLocked (KERNEL.122)
989 HTASK16 WINAPI IsTaskLocked(void)
991 return hLockedTask;
995 /***********************************************************************
996 * OldYield (KERNEL.117)
998 void WINAPI OldYield(void)
1000 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
1002 if ( !THREAD_IsWin16( THREAD_Current() ) )
1004 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
1005 return;
1008 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
1009 TASK_YieldToSystem(pCurTask);
1010 if (pCurTask) pCurTask->nEvents--;
1014 /***********************************************************************
1015 * DirectedYield (KERNEL.150)
1017 void WINAPI DirectedYield( HTASK16 hTask )
1019 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
1021 if ( !THREAD_IsWin16( THREAD_Current() ) )
1023 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
1024 return;
1027 TRACE(task, "%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
1029 pCurTask->hYieldTo = hTask;
1030 OldYield();
1032 TRACE(task, "%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
1036 /***********************************************************************
1037 * UserYield (USER.332)
1039 void WINAPI UserYield(void)
1041 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
1042 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16( pCurTask->hQueue );
1044 if ( !THREAD_IsWin16( THREAD_Current() ) )
1046 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
1047 return;
1050 /* Handle sent messages */
1051 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1052 QUEUE_ReceiveMessage( queue );
1054 OldYield();
1056 queue = (MESSAGEQUEUE *)GlobalLock16( pCurTask->hQueue );
1057 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1058 QUEUE_ReceiveMessage( queue );
1062 /***********************************************************************
1063 * Yield16 (KERNEL.29)
1065 void WINAPI Yield16(void)
1067 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
1069 if ( !THREAD_IsWin16( THREAD_Current() ) )
1071 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
1072 return;
1075 if (pCurTask) pCurTask->hYieldTo = 0;
1076 if (pCurTask && pCurTask->hQueue) UserYield();
1077 else OldYield();
1080 /***********************************************************************
1081 * KERNEL_490 (KERNEL.490)
1083 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
1085 if ( !someTask ) return 0;
1087 FIXME( task, "(%04x): stub\n", someTask );
1088 return 0;
1091 /***********************************************************************
1092 * MakeProcInstance16 (KERNEL.51)
1094 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
1096 BYTE *thunk,*lfunc;
1097 SEGPTR thunkaddr;
1099 if (!func) {
1100 ERR(task, "Ouch ! MakeProcInstance called with func == NULL !\n");
1101 return (FARPROC16)0; /* Windows seems to do the same */
1103 if (!hInstance) hInstance = CURRENT_DS;
1104 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
1105 if (!thunkaddr) return (FARPROC16)0;
1106 thunk = PTR_SEG_TO_LIN( thunkaddr );
1107 lfunc = PTR_SEG_TO_LIN( func );
1109 TRACE(task, "(%08lx,%04x): got thunk %08lx\n",
1110 (DWORD)func, hInstance, (DWORD)thunkaddr );
1111 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) ||
1112 ((lfunc[0]==0x1e) && (lfunc[1]==0x58))
1114 FIXME(task,"thunk would be useless for %p, overwriting with nop;nop;\n", func );
1115 lfunc[0]=0x90; /* nop */
1116 lfunc[1]=0x90; /* nop */
1119 *thunk++ = 0xb8; /* movw instance, %ax */
1120 *thunk++ = (BYTE)(hInstance & 0xff);
1121 *thunk++ = (BYTE)(hInstance >> 8);
1122 *thunk++ = 0xea; /* ljmp func */
1123 *(DWORD *)thunk = (DWORD)func;
1124 return (FARPROC16)thunkaddr;
1128 /***********************************************************************
1129 * FreeProcInstance16 (KERNEL.52)
1131 void WINAPI FreeProcInstance16( FARPROC16 func )
1133 TRACE(task, "(%08lx)\n", (DWORD)func );
1134 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
1138 /**********************************************************************
1139 * GetCodeHandle (KERNEL.93)
1141 HANDLE16 WINAPI GetCodeHandle( FARPROC16 proc )
1143 HANDLE16 handle;
1144 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
1146 /* Return the code segment containing 'proc'. */
1147 /* Not sure if this is really correct (shouldn't matter that much). */
1149 /* Check if it is really a thunk */
1150 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1151 handle = GlobalHandle16( thunk[6] + (thunk[7] << 8) );
1152 else
1153 handle = GlobalHandle16( HIWORD(proc) );
1155 return handle;
1158 /**********************************************************************
1159 * GetCodeInfo (KERNEL.104)
1161 VOID WINAPI GetCodeInfo( FARPROC16 proc, SEGINFO *segInfo )
1163 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
1164 NE_MODULE *pModule = NULL;
1165 SEGTABLEENTRY *pSeg = NULL;
1166 WORD segNr;
1168 /* proc is either a thunk, or else a pair of module handle
1169 and segment number. In the first case, we also need to
1170 extract module and segment number. */
1172 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1174 WORD selector = thunk[6] + (thunk[7] << 8);
1175 pModule = NE_GetPtr( GlobalHandle16( selector ) );
1176 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
1178 if ( pModule )
1179 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
1180 if ( GlobalHandleToSel(pSeg->hSeg) == selector )
1181 break;
1183 if ( pModule && segNr >= pModule->seg_count )
1184 pSeg = NULL;
1186 else
1188 pModule = NE_GetPtr( HIWORD( proc ) );
1189 segNr = LOWORD( proc );
1191 if ( pModule && segNr < pModule->seg_count )
1192 pSeg = NE_SEG_TABLE( pModule ) + segNr;
1195 /* fill in segment information */
1197 segInfo->offSegment = pSeg? pSeg->filepos : 0;
1198 segInfo->cbSegment = pSeg? pSeg->size : 0;
1199 segInfo->flags = pSeg? pSeg->flags : 0;
1200 segInfo->cbAlloc = pSeg? pSeg->minsize : 0;
1201 segInfo->h = pSeg? pSeg->hSeg : 0;
1202 segInfo->alignShift = pModule? pModule->alignment : 0;
1206 /**********************************************************************
1207 * DefineHandleTable16 (KERNEL.94)
1209 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1211 return TRUE; /* FIXME */
1215 /***********************************************************************
1216 * SetTaskQueue (KERNEL.34)
1218 HQUEUE16 WINAPI SetTaskQueue( HTASK16 hTask, HQUEUE16 hQueue )
1220 HQUEUE16 hPrev;
1221 TDB *pTask;
1223 if (!hTask) hTask = GetCurrentTask();
1224 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1226 hPrev = pTask->hQueue;
1227 pTask->hQueue = hQueue;
1229 TIMER_SwitchQueue( hPrev, hQueue );
1231 return hPrev;
1235 /***********************************************************************
1236 * GetTaskQueue (KERNEL.35)
1238 HQUEUE16 WINAPI GetTaskQueue( HTASK16 hTask )
1240 TDB *pTask;
1242 if (!hTask) hTask = GetCurrentTask();
1243 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1244 return pTask->hQueue;
1247 /***********************************************************************
1248 * SetThreadQueue (KERNEL.463)
1250 HQUEUE16 WINAPI SetThreadQueue( DWORD thread, HQUEUE16 hQueue )
1252 THDB *thdb = THREAD_IdToTHDB( thread );
1253 HQUEUE16 oldQueue = thdb? thdb->teb.queue : 0;
1255 if ( thdb )
1257 thdb->teb.queue = hQueue;
1259 if ( GetTaskQueue( thdb->process->task ) == oldQueue )
1260 SetTaskQueue( thdb->process->task, hQueue );
1263 return oldQueue;
1266 /***********************************************************************
1267 * GetThreadQueue (KERNEL.464)
1269 HQUEUE16 WINAPI GetThreadQueue( DWORD thread )
1271 THDB *thdb = THREAD_IdToTHDB( thread );
1272 return (HQUEUE16)(thdb? thdb->teb.queue : 0);
1275 /***********************************************************************
1276 * SetFastQueue (KERNEL.624)
1278 VOID WINAPI SetFastQueue( DWORD thread, HANDLE32 hQueue )
1280 THDB *thdb = THREAD_IdToTHDB( thread );
1281 if ( thdb ) thdb->teb.queue = (HQUEUE16) hQueue;
1284 /***********************************************************************
1285 * GetFastQueue (KERNEL.625)
1287 HANDLE32 WINAPI GetFastQueue( void )
1289 THDB *thdb = THREAD_Current();
1290 if (!thdb) return 0;
1292 if (!(thdb->teb.queue))
1294 HMODULE16 hModule = GetModuleHandle16( "USER" );
1295 FARPROC16 proc = WIN32_GetProcAddress16( hModule, "InitThreadInput" );
1296 Callbacks->CallBootAppProc( proc, 0, THREAD_IsWin16(thdb)? 4 : 5 ); /* FIXME! */
1299 if (!(thdb->teb.queue))
1300 FIXME( task, "(): should initialize thread-local queue, expect failure!\n" );
1302 return (HANDLE32)thdb->teb.queue;
1305 /***********************************************************************
1306 * SwitchStackTo (KERNEL.108)
1308 void WINAPI SwitchStackTo( WORD seg, WORD ptr, WORD top )
1310 TDB *pTask;
1311 STACK16FRAME *oldFrame, *newFrame;
1312 INSTANCEDATA *pData;
1313 UINT16 copySize;
1315 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1316 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1317 TRACE(task, "old=%04x:%04x new=%04x:%04x\n",
1318 SELECTOROF( pTask->thdb->cur_stack ),
1319 OFFSETOF( pTask->thdb->cur_stack ), seg, ptr );
1321 /* Save the old stack */
1323 oldFrame = THREAD_STACK16( pTask->thdb );
1324 /* pop frame + args and push bp */
1325 pData->old_ss_sp = pTask->thdb->cur_stack + sizeof(STACK16FRAME)
1326 + 2 * sizeof(WORD);
1327 *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp;
1328 pData->stacktop = top;
1329 pData->stackmin = ptr;
1330 pData->stackbottom = ptr;
1332 /* Switch to the new stack */
1334 /* Note: we need to take the 3 arguments into account; otherwise,
1335 * the stack will underflow upon return from this function.
1337 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1338 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1339 pTask->thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize );
1340 newFrame = THREAD_STACK16( pTask->thdb );
1342 /* Copy the stack frame and the local variables to the new stack */
1344 memmove( newFrame, oldFrame, copySize );
1345 newFrame->bp = ptr;
1346 *(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */
1350 /***********************************************************************
1351 * SwitchStackBack (KERNEL.109)
1353 void WINAPI SwitchStackBack( CONTEXT *context )
1355 TDB *pTask;
1356 STACK16FRAME *oldFrame, *newFrame;
1357 INSTANCEDATA *pData;
1359 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1360 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(pTask->thdb->cur_stack))))
1361 return;
1362 if (!pData->old_ss_sp)
1364 WARN( task, "No previous SwitchStackTo\n" );
1365 return;
1367 TRACE(task, "restoring stack %04x:%04x\n",
1368 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1370 oldFrame = THREAD_STACK16( pTask->thdb );
1372 /* Pop bp from the previous stack */
1374 BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp);
1375 pData->old_ss_sp += sizeof(WORD);
1377 /* Switch back to the old stack */
1379 pTask->thdb->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1380 SS_reg(context) = SELECTOROF(pData->old_ss_sp);
1381 ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1382 pData->old_ss_sp = 0;
1384 /* Build a stack frame for the return */
1386 newFrame = THREAD_STACK16( pTask->thdb );
1387 newFrame->frame32 = oldFrame->frame32;
1388 if (TRACE_ON(relay))
1390 newFrame->entry_ip = oldFrame->entry_ip;
1391 newFrame->entry_cs = oldFrame->entry_cs;
1396 /***********************************************************************
1397 * GetTaskQueueDS (KERNEL.118)
1399 void WINAPI GetTaskQueueDS( CONTEXT *context )
1401 DS_reg(context) = GlobalHandleToSel( GetTaskQueue(0) );
1405 /***********************************************************************
1406 * GetTaskQueueES (KERNEL.119)
1408 void WINAPI GetTaskQueueES( CONTEXT *context )
1410 ES_reg(context) = GlobalHandleToSel( GetTaskQueue(0) );
1414 /***********************************************************************
1415 * GetCurrentTask (KERNEL.36)
1417 HTASK16 WINAPI GetCurrentTask(void)
1419 return THREAD_InitDone? PROCESS_Current()->task : 0;
1422 DWORD WINAPI WIN16_GetCurrentTask(void)
1424 /* This is the version used by relay code; the first task is */
1425 /* returned in the high word of the result */
1426 return MAKELONG( GetCurrentTask(), hFirstTask );
1430 /***********************************************************************
1431 * GetCurrentPDB (KERNEL.37)
1433 * UNDOC: returns PSP of KERNEL in high word
1435 DWORD WINAPI GetCurrentPDB(void)
1437 TDB *pTask;
1439 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1440 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1444 /***********************************************************************
1445 * GetInstanceData (KERNEL.54)
1447 INT16 WINAPI GetInstanceData( HINSTANCE16 instance, WORD buffer, INT16 len )
1449 char *ptr = (char *)GlobalLock16( instance );
1450 if (!ptr || !len) return 0;
1451 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1452 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1453 return len;
1457 /***********************************************************************
1458 * GetExeVersion (KERNEL.105)
1460 WORD WINAPI GetExeVersion(void)
1462 TDB *pTask;
1464 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1465 return pTask->version;
1469 /***********************************************************************
1470 * SetErrorMode16 (KERNEL.107)
1472 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1474 TDB *pTask;
1475 UINT16 oldMode;
1477 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1478 oldMode = pTask->error_mode;
1479 pTask->error_mode = mode;
1480 pTask->thdb->process->error_mode = mode;
1481 return oldMode;
1485 /***********************************************************************
1486 * SetErrorMode32 (KERNEL32.486)
1488 UINT32 WINAPI SetErrorMode32( UINT32 mode )
1490 return SetErrorMode16( (UINT16)mode );
1494 /***********************************************************************
1495 * GetDOSEnvironment (KERNEL.131)
1497 SEGPTR WINAPI GetDOSEnvironment(void)
1499 TDB *pTask;
1501 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1502 return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 );
1506 /***********************************************************************
1507 * GetNumTasks (KERNEL.152)
1509 UINT16 WINAPI GetNumTasks(void)
1511 return nTaskCount;
1515 /***********************************************************************
1516 * GetTaskDS (KERNEL.155)
1518 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1519 * I don't think we need to bother with this.
1521 HINSTANCE16 WINAPI GetTaskDS(void)
1523 TDB *pTask;
1525 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1526 return pTask->hInstance;
1529 /***********************************************************************
1530 * GetDummyModuleHandleDS (KERNEL.602)
1532 VOID WINAPI GetDummyModuleHandleDS( CONTEXT *context )
1534 TDB *pTask;
1535 WORD selector;
1537 AX_reg( context ) = 0;
1538 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1539 if (!(pTask->flags & TDBF_WIN32)) return;
1541 selector = GlobalHandleToSel( pTask->hModule );
1542 DS_reg( context ) = selector;
1543 AX_reg( context ) = selector;
1546 /***********************************************************************
1547 * IsTask (KERNEL.320)
1549 BOOL16 WINAPI IsTask( HTASK16 hTask )
1551 TDB *pTask;
1553 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1554 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1555 return (pTask->magic == TDB_MAGIC);
1559 /***********************************************************************
1560 * SetTaskSignalProc (KERNEL.38)
1562 * Real 16-bit interface is provided by the THUNK_SetTaskSignalProc.
1564 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1566 TDB *pTask;
1567 FARPROC16 oldProc;
1569 if (!hTask) hTask = GetCurrentTask();
1570 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1571 oldProc = (FARPROC16)pTask->userhandler;
1572 pTask->userhandler = (USERSIGNALPROC)proc;
1573 return oldProc;
1577 /***********************************************************************
1578 * SetSigHandler (KERNEL.140)
1580 WORD WINAPI SetSigHandler( FARPROC16 newhandler, FARPROC16* oldhandler,
1581 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1583 FIXME(task,"(%p,%p,%p,%d,%d), unimplemented.\n",
1584 newhandler,oldhandler,oldmode,newmode,flag );
1586 if (flag != 1) return 0;
1587 if (!newmode) newhandler = NULL; /* Default handler */
1588 if (newmode != 4)
1590 TDB *pTask;
1592 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1593 if (oldmode) *oldmode = pTask->signal_flags;
1594 pTask->signal_flags = newmode;
1595 if (oldhandler) *oldhandler = pTask->sighandler;
1596 pTask->sighandler = newhandler;
1598 return 0;
1602 /***********************************************************************
1603 * GlobalNotify (KERNEL.154)
1605 * Note that GlobalNotify does _not_ return the old NotifyProc
1606 * -- contrary to LocalNotify !!
1608 VOID WINAPI GlobalNotify( FARPROC16 proc )
1610 TDB *pTask;
1612 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1613 pTask->discardhandler = proc;
1617 /***********************************************************************
1618 * GetExePtr (KERNEL.133)
1620 static HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1622 char *ptr;
1623 HANDLE16 owner;
1625 /* Check for module handle */
1627 if (!(ptr = GlobalLock16( handle ))) return 0;
1628 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1630 /* Search for this handle inside all tasks */
1632 *hTask = hFirstTask;
1633 while (*hTask)
1635 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1636 if ((*hTask == handle) ||
1637 (pTask->hInstance == handle) ||
1638 (pTask->hQueue == handle) ||
1639 (pTask->hPDB == handle)) return pTask->hModule;
1640 *hTask = pTask->hNext;
1643 /* Check the owner for module handle */
1645 owner = FarGetOwner( handle );
1646 if (!(ptr = GlobalLock16( owner ))) return 0;
1647 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1649 /* Search for the owner inside all tasks */
1651 *hTask = hFirstTask;
1652 while (*hTask)
1654 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1655 if ((*hTask == owner) ||
1656 (pTask->hInstance == owner) ||
1657 (pTask->hQueue == owner) ||
1658 (pTask->hPDB == owner)) return pTask->hModule;
1659 *hTask = pTask->hNext;
1662 return 0;
1665 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1667 HTASK16 dummy;
1668 return GetExePtrHelper( handle, &dummy );
1671 void WINAPI WIN16_GetExePtr( CONTEXT *context )
1673 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1674 HANDLE16 handle = (HANDLE16)stack[2];
1675 HTASK16 hTask = 0;
1676 HMODULE16 hModule;
1678 hModule = GetExePtrHelper( handle, &hTask );
1680 AX_reg(context) = CX_reg(context) = hModule;
1681 if (hTask) ES_reg(context) = hTask;
1684 /***********************************************************************
1685 * TaskFirst (TOOLHELP.63)
1687 BOOL16 WINAPI TaskFirst( TASKENTRY *lpte )
1689 lpte->hNext = hFirstTask;
1690 return TaskNext( lpte );
1694 /***********************************************************************
1695 * TaskNext (TOOLHELP.64)
1697 BOOL16 WINAPI TaskNext( TASKENTRY *lpte )
1699 TDB *pTask;
1700 INSTANCEDATA *pInstData;
1702 TRACE(toolhelp, "(%p): task=%04x\n", lpte, lpte->hNext );
1703 if (!lpte->hNext) return FALSE;
1704 pTask = (TDB *)GlobalLock16( lpte->hNext );
1705 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1706 pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( pTask->hInstance, 0 );
1707 lpte->hTask = lpte->hNext;
1708 lpte->hTaskParent = pTask->hParent;
1709 lpte->hInst = pTask->hInstance;
1710 lpte->hModule = pTask->hModule;
1711 lpte->wSS = SELECTOROF( pTask->thdb->cur_stack );
1712 lpte->wSP = OFFSETOF( pTask->thdb->cur_stack );
1713 lpte->wStackTop = pInstData->stacktop;
1714 lpte->wStackMinimum = pInstData->stackmin;
1715 lpte->wStackBottom = pInstData->stackbottom;
1716 lpte->wcEvents = pTask->nEvents;
1717 lpte->hQueue = pTask->hQueue;
1718 strncpy( lpte->szModule, pTask->module_name, 8 );
1719 lpte->szModule[8] = '\0';
1720 lpte->wPSPOffset = 0x100; /*??*/
1721 lpte->hNext = pTask->hNext;
1722 return TRUE;
1726 /***********************************************************************
1727 * TaskFindHandle (TOOLHELP.65)
1729 BOOL16 WINAPI TaskFindHandle( TASKENTRY *lpte, HTASK16 hTask )
1731 lpte->hNext = hTask;
1732 return TaskNext( lpte );
1736 /***********************************************************************
1737 * GetAppCompatFlags16 (KERNEL.354)
1739 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1741 return GetAppCompatFlags32( hTask );
1745 /***********************************************************************
1746 * GetAppCompatFlags32 (USER32.206)
1748 DWORD WINAPI GetAppCompatFlags32( HTASK32 hTask )
1750 TDB *pTask;
1752 if (!hTask) hTask = GetCurrentTask();
1753 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1754 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1755 return pTask->compat_flags;