4 * Copyright 1995 Alexandre Julliard
22 #include "selectors.h"
28 /* Min. number of thunks allocated when creating a new segment */
31 /* 32-bit stack size for each task */
32 /* Must not be greater than 64k, or MAKE_SEGPTR won't work */
33 #define STACK32_SIZE 0x10000
35 /* ------ Internal variables ------ */
37 static HTASK hFirstTask
= 0;
38 static HTASK hCurrentTask
= 0;
39 static HTASK hTaskToKill
= 0;
40 static HTASK hLockedTask
= 0;
41 static WORD nTaskCount
= 0;
42 static HANDLE hDOSEnvironment
= 0;
44 /* ------ Internal declarations ------ */
46 /* TASK_Reschedule() 16-bit entry point */
47 static FARPROC TASK_RescheduleProc
;
50 #define TASK_SCHEDULE() TASK_Reschedule();
52 #define TASK_SCHEDULE() CallTo16_word_(TASK_RescheduleProc,0)
55 static HANDLE
TASK_CreateDOSEnvironment(void);
57 /***********************************************************************
62 TASK_RescheduleProc
= (FARPROC
)GetWndProcEntry16( "TASK_Reschedule" );
63 if (!(hDOSEnvironment
= TASK_CreateDOSEnvironment()))
64 fprintf( stderr
, "Not enough memory for DOS Environment\n" );
65 return (hDOSEnvironment
!= 0);
69 /***********************************************************************
70 * TASK_CreateDOSEnvironment
72 * Create the original DOS environment.
74 static HANDLE
TASK_CreateDOSEnvironment(void)
76 static const char program_name
[] = "KRNL386.EXE";
78 int initial_size
, size
;
81 extern char **environ
;
82 extern char WindowsDirectory
[], SystemDirectory
[];
84 /* DOS environment format:
93 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
96 /* First compute the size of the fixed part of the environment */
98 initial_size
= 5 + /* PATH= */
99 strlen(WindowsPath
) + 1 + /* path value */
101 strlen(WindowsDirectory
) + 1 + /* windir value */
102 1 + /* BYTE 0 at end */
103 sizeof(WORD
) + /* WORD 1 */
104 strlen(SystemDirectory
) + 1 + /* program directory */
105 strlen(program_name
) + 1; /* program name */
107 /* Compute the total size of the Unix environment (except path) */
109 for (e
= environ
, size
= initial_size
; *e
; e
++)
111 if (strncasecmp(*e
, "path=", 5))
113 int len
= strlen(*e
) + 1;
114 if (size
+ len
>= 32767)
116 fprintf( stderr
, "Warning: environment larger than 32k.\n" );
124 /* Now allocate the environment */
126 if (!(handle
= GlobalAlloc( GMEM_FIXED
, size
))) return 0;
127 p
= (char *)GlobalLock( handle
);
129 /* And fill it with the Unix environment */
131 for (e
= environ
, size
= initial_size
; *e
; e
++)
133 if (strncasecmp(*e
, "path=", 5))
135 int len
= strlen(*e
) + 1;
136 if (size
+ len
>= 32767) break;
143 /* Now add the path and Windows directory */
145 strcpy( p
, "PATH=" );
146 strcat( p
, WindowsPath
);
149 strcpy( p
, "windir=" );
150 strcat( p
, WindowsDirectory
);
153 /* Now add the program name */
158 strcpy( p
, SystemDirectory
);
160 strcat( p
, program_name
);
164 p
= (char *) GlobalLock( handle
);
165 dprintf_task(stddeb
, "Master DOS environment at %p\n", p
);
166 for (; *p
; p
+= strlen(p
) + 1) dprintf_task(stddeb
, " %s\n", p
);
167 dprintf_task( stddeb
, "Progname: %s\n", p
+3 );
173 /***********************************************************************
176 static void TASK_LinkTask( HTASK hTask
)
181 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return;
182 prevTask
= &hFirstTask
;
185 TDB
*prevTaskPtr
= (TDB
*)GlobalLock( *prevTask
);
186 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
187 prevTask
= &prevTaskPtr
->hNext
;
189 pTask
->hNext
= *prevTask
;
195 /***********************************************************************
198 static void TASK_UnlinkTask( HTASK hTask
)
203 prevTask
= &hFirstTask
;
204 while (*prevTask
&& (*prevTask
!= hTask
))
206 pTask
= (TDB
*)GlobalLock( *prevTask
);
207 prevTask
= &pTask
->hNext
;
211 pTask
= (TDB
*)GlobalLock( *prevTask
);
212 *prevTask
= pTask
->hNext
;
219 /***********************************************************************
222 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
223 * and containing 'count' entries.
225 static void TASK_CreateThunks( HGLOBAL handle
, WORD offset
, WORD count
)
231 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock( handle
) + offset
);
233 pThunk
->magic
= THUNK_MAGIC
;
234 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
236 for (i
= 0; i
< count
-1; i
++)
238 free
+= 8; /* Offset of next thunk */
239 pThunk
->thunks
[4*i
] = free
;
241 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
245 /***********************************************************************
248 * Allocate a thunk for MakeProcInstance().
250 static SEGPTR
TASK_AllocThunk( HTASK hTask
)
256 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return 0;
257 sel
= pTask
->hCSAlias
;
258 pThunk
= &pTask
->thunks
;
259 base
= (int)pThunk
- (int)pTask
;
260 while (!pThunk
->free
)
263 if (!sel
) /* Allocate a new segment */
265 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
266 pTask
->hPDB
, TRUE
, FALSE
, FALSE
);
267 if (!sel
) return (SEGPTR
)0;
268 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
271 pThunk
= (THUNKS
*)GlobalLock( sel
);
274 base
+= pThunk
->free
;
275 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
276 return MAKELONG( base
, sel
);
280 /***********************************************************************
283 * Free a MakeProcInstance() thunk.
285 static BOOL
TASK_FreeThunk( HTASK hTask
, SEGPTR thunk
)
291 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return 0;
292 sel
= pTask
->hCSAlias
;
293 pThunk
= &pTask
->thunks
;
294 base
= (int)pThunk
- (int)pTask
;
295 while (sel
&& (sel
!= HIWORD(thunk
)))
298 pThunk
= (THUNKS
*)GlobalLock( sel
);
301 if (!sel
) return FALSE
;
302 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
303 pThunk
->free
= LOWORD(thunk
) - base
;
308 /***********************************************************************
311 * 32-bit entry point for a new task. This function is responsible for
312 * setting up the registers and jumping to the 16-bit entry point.
315 static void TASK_CallToStart(void)
317 int cs_reg
, ds_reg
, ip_reg
;
318 TDB
*pTask
= (TDB
*)GlobalLock( hCurrentTask
);
319 NE_MODULE
*pModule
= (NE_MODULE
*)GlobalLock( pTask
->hModule
);
320 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
322 /* Registers at initialization must be:
324 * bx stack size in bytes
325 * cx heap size in bytes
326 * si previous app instance
327 * di current app instance
329 * es selector to the PSP
330 * ds dgroup of the application
332 * sp top of the stack
335 cs_reg
= pSegTable
[pModule
->cs
- 1].selector
;
336 ip_reg
= pModule
->ip
;
337 ds_reg
= pSegTable
[pModule
->dgroup
- 1].selector
;
339 /* JBP: I doubt a CallTo16_regs_ is possible in libwine.a, and IF1632 is not
342 IF1632_Saved16_ss
= pTask
->ss
;
343 IF1632_Saved16_sp
= pTask
->sp
;
344 dprintf_task( stddeb
, "Starting main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
345 cs_reg
, ip_reg
, ds_reg
,
346 IF1632_Saved16_ss
, IF1632_Saved16_sp
);
348 CallTo16_regs_( (FARPROC
)(cs_reg
<< 16 | ip_reg
), ds_reg
,
349 pTask
->hPDB
/*es*/, 0 /*bp*/, 0 /*ax*/,
350 pModule
->stack_size
/*bx*/, pModule
->heap_size
/*cx*/,
351 0 /*dx*/, 0 /*si*/, ds_reg
/*di*/ );
353 fprintf(stderr
, "JBP: Ignoring main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
354 cs_reg
, ip_reg
, ds_reg
,
355 pTask
->ss
, pTask
->sp
);
357 /* This should never return */
358 fprintf( stderr
, "TASK_CallToStart: Main program returned!\n" );
359 TASK_KillCurrentTask( 1 );
364 /***********************************************************************
367 HTASK
TASK_CreateTask( HMODULE hModule
, HANDLE hInstance
, HANDLE hPrevInstance
,
368 HANDLE hEnvironment
, char *cmdLine
, WORD cmdShow
)
374 SEGTABLEENTRY
*pSegTable
;
378 char *stack16Top
, *stack32Top
;
379 STACK16FRAME
*frame16
;
380 STACK32FRAME
*frame32
;
381 extern DWORD CALL16_RetAddr_word
;
384 if (!(pModule
= (NE_MODULE
*)GlobalLock( hModule
))) return 0;
385 pSegTable
= NE_SEG_TABLE( pModule
);
387 /* Allocate the task structure */
389 hTask
= GLOBAL_Alloc( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
),
390 hModule
, FALSE
, FALSE
, FALSE
);
391 if (!hTask
) return 0;
392 pTask
= (TDB
*)GlobalLock( hTask
);
394 /* Allocate the new environment block */
396 if (!(hParentEnv
= hEnvironment
))
398 TDB
*pParent
= (TDB
*)GlobalLock( hCurrentTask
);
399 hParentEnv
= pParent
? pParent
->pdb
.environment
: hDOSEnvironment
;
401 /* FIXME: do we really need to make a copy also when */
402 /* we don't use the parent environment? */
403 if (!(hEnvironment
= GlobalAlloc( GMEM_FIXED
, GlobalSize( hParentEnv
) )))
408 memcpy( GlobalLock( hEnvironment
), GlobalLock( hParentEnv
),
409 GlobalSize( hParentEnv
) );
411 /* Get current directory */
413 GetModuleFileName( hModule
, filename
, sizeof(filename
) );
414 name
= strrchr(filename
, '\\');
415 if (name
) *(name
+1) = 0;
417 /* Fill the task structure */
419 pTask
->nEvents
= 1; /* So the task can be started */
420 pTask
->hSelf
= hTask
;
422 pTask
->version
= pModule
->expected_version
;
423 pTask
->hInstance
= hInstance
;
424 pTask
->hPrevInstance
= hPrevInstance
;
425 pTask
->hModule
= hModule
;
426 pTask
->hParent
= hCurrentTask
;
428 pTask
->curdrive
= 'C' - 'A' + 0x80;
430 pTask
->curdrive
= filename
[0] - 'A' + 0x80;
432 pTask
->magic
= TDB_MAGIC
;
433 pTask
->nCmdShow
= cmdShow
;
434 strcpy( pTask
->curdir
, filename
+2 );
436 /* Create the thunks block */
438 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
440 /* Copy the module name */
442 name
= MODULE_GetModuleName( hModule
);
443 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
447 pTask
->pdb
.int20
= 0x20cd;
448 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
449 *(DWORD
*)&pTask
->pdb
.dispatcher
[1] = MODULE_GetEntryPoint( GetModuleHandle("KERNEL"), 102 ); /* KERNEL.102 is DOS3Call() */
451 pTask
->pdb
.savedint22
= INT_GetHandler( 0x22 );
452 pTask
->pdb
.savedint23
= INT_GetHandler( 0x23 );
453 pTask
->pdb
.savedint24
= INT_GetHandler( 0x24 );
455 pTask
->pdb
.environment
= hEnvironment
;
456 strncpy( pTask
->pdb
.cmdLine
+ 1, cmdLine
, 126 );
457 pTask
->pdb
.cmdLine
[127] = '\0';
458 pTask
->pdb
.cmdLine
[0] = strlen( pTask
->pdb
.cmdLine
+ 1 );
460 /* Get the compatibility flags */
462 pTask
->compat_flags
= GetProfileInt( name
, "Compatibility", 0 );
464 /* Allocate a selector for the PDB */
466 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB
),
467 hModule
, FALSE
, FALSE
, FALSE
, NULL
);
469 /* Allocate a code segment alias for the TDB */
471 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
472 sizeof(TDB
), pTask
->hPDB
, TRUE
,
473 FALSE
, FALSE
, NULL
);
475 /* Set the owner of the environment block */
477 FarSetOwner( pTask
->pdb
.environment
, pTask
->hPDB
);
479 /* Default DTA overwrites command-line */
481 pTask
->dta
= MAKELONG( (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
,
484 /* Allocate the 32-bit stack */
487 pTask
->hStack32
= GLOBAL_Alloc( GMEM_FIXED
, STACK32_SIZE
, pTask
->hPDB
,
488 FALSE
, FALSE
, FALSE
);
490 /* Create the 32-bit stack frame */
492 *(DWORD
*)GlobalLock(pTask
->hStack32
) = 0xDEADBEEF;
493 stack32Top
= (char*)GlobalLock(pTask
->hStack32
) + STACK32_SIZE
;
494 frame32
= (STACK32FRAME
*)stack32Top
- 1;
495 frame32
->saved_esp
= (DWORD
)stack32Top
;
502 frame32
->retaddr
= (DWORD
)TASK_CallToStart
;
503 frame32
->codeselector
= WINE_CODE_SELECTOR
;
504 pTask
->esp
= (DWORD
)frame32
;
506 /* Create the 16-bit stack frame */
508 pTask
->ss
= hInstance
;
509 pTask
->sp
= ((pModule
->sp
!= 0) ? pModule
->sp
:
510 pSegTable
[pModule
->ss
-1].minsize
+ pModule
->stack_size
) & ~1;
511 stack16Top
= (char *)PTR_SEG_OFF_TO_LIN( pTask
->ss
, pTask
->sp
);
512 frame16
= (STACK16FRAME
*)stack16Top
- 1;
513 frame16
->saved_ss
= 0; /*pTask->ss;*/
514 frame16
->saved_sp
= 0; /*pTask->sp;*/
515 frame16
->ds
= frame16
->es
= pTask
->hInstance
;
516 frame16
->entry_point
= 0;
517 frame16
->ordinal_number
= 24; /* WINPROCS.24 is TASK_Reschedule */
518 frame16
->dll_id
= 24; /* WINPROCS */
520 frame16
->ip
= LOWORD( CALL16_RetAddr_word
);
521 frame16
->cs
= HIWORD( CALL16_RetAddr_word
);
522 pTask
->sp
-= sizeof(STACK16FRAME
);
524 /* If there's no 16-bit stack yet, use a part of the new task stack */
525 /* This is only needed to have a stack to switch from on the first */
526 /* call to DirectedYield(). */
528 if (!IF1632_Saved16_ss
)
530 IF1632_Saved16_ss
= pTask
->ss
;
531 IF1632_Saved16_sp
= pTask
->sp
;
534 /* Add a breakpoint at the start of the task */
538 DBG_ADDR addr
= { pSegTable
[pModule
->cs
-1].selector
, pModule
->ip
};
539 fprintf( stderr
, "Task '%s': ", name
);
540 DEBUG_AddBreakpoint( &addr
);
544 /* Add the task to the linked list */
546 TASK_LinkTask( hTask
);
548 dprintf_task( stddeb
, "CreateTask: module='%s' cmdline='%s' task="NPFMT
"\n",
549 name
, cmdLine
, hTask
);
555 /***********************************************************************
558 static void TASK_DeleteTask( HTASK hTask
)
562 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return;
564 /* Free the task module */
566 FreeModule( pTask
->hModule
);
568 /* Free all memory used by this task (including the 32-bit stack, */
569 /* the environment block and the thunk segments). */
571 GlobalFreeAll( pTask
->hPDB
);
573 /* Free message queue */
575 MSG_DeleteMsgQueue( pTask
->hQueue
);
577 /* Free the selector aliases */
579 GLOBAL_FreeBlock( pTask
->hCSAlias
);
580 GLOBAL_FreeBlock( pTask
->hPDB
);
582 /* Free the task structure itself */
588 /***********************************************************************
589 * TASK_KillCurrentTask
591 * Kill the currently running task. As it's not possible to kill the
592 * current task like this, it is simply marked for destruction, and will
593 * be killed when either TASK_Reschedule or this function is called again
594 * in the context of another task.
596 void TASK_KillCurrentTask( int exitCode
)
598 if (hTaskToKill
&& (hTaskToKill
!= hCurrentTask
))
600 /* If another task is already marked for destruction, */
601 /* we call kill it now, as we are in another context. */
602 TASK_DeleteTask( hTaskToKill
);
607 dprintf_task( stddeb
, "Killing the last task, exiting\n" );
611 /* Remove the task from the list to be sure we never switch back to it */
612 TASK_UnlinkTask( hCurrentTask
);
614 hTaskToKill
= hCurrentTask
;
618 /* We should never return from this Yield() */
620 fprintf(stderr
,"It's alive! Alive!!!\n");
625 /***********************************************************************
628 * This is where all the magic of task-switching happens!
630 * This function should only be called via the TASK_SCHEDULE() macro, to make
631 * sure that all the context is saved correctly.
633 void TASK_Reschedule(void)
635 TDB
*pOldTask
= NULL
, *pNewTask
;
641 /* First check if there's a task to kill */
643 if (hTaskToKill
&& (hTaskToKill
!= hCurrentTask
))
645 TASK_DeleteTask( hTaskToKill
);
649 /* If current task is locked, simply return */
651 if (hLockedTask
) return;
653 /* Find a task to yield to */
655 pOldTask
= (TDB
*)GlobalLock( hCurrentTask
);
656 if (pOldTask
&& pOldTask
->hYieldTo
)
658 /* If a task is stored in hYieldTo of the current task (put there */
659 /* by DirectedYield), yield to it only if it has events pending. */
660 hTask
= pOldTask
->hYieldTo
;
661 if (!(pNewTask
= (TDB
*)GlobalLock( hTask
)) || !pNewTask
->nEvents
)
670 pNewTask
= (TDB
*)GlobalLock( hTask
);
671 if (pNewTask
->nEvents
&& (hTask
!= hCurrentTask
)) break;
672 hTask
= pNewTask
->hNext
;
676 /* If there's a task to kill, switch to any other task, */
677 /* even if it doesn't have events pending. */
679 if (!hTask
&& hTaskToKill
) hTask
= hFirstTask
;
681 if (!hTask
) return; /* Do nothing */
683 pNewTask
= (TDB
*)GlobalLock( hTask
);
684 dprintf_task( stddeb
, "Switching to task "NPFMT
" (%.8s)\n",
685 hTask
, pNewTask
->module_name
);
687 /* Save the stacks of the previous task (if any) */
689 #ifndef WINELIB /* FIXME: JBP: IF1632 not allowed in libwine.a */
692 pOldTask
->ss
= IF1632_Saved16_ss
;
693 pOldTask
->sp
= IF1632_Saved16_sp
;
694 pOldTask
->esp
= IF1632_Saved32_esp
;
696 else IF1632_Original32_esp
= IF1632_Saved32_esp
;
699 /* Make the task the last in the linked list (round-robin scheduling) */
701 pNewTask
->priority
++;
702 TASK_UnlinkTask( hTask
);
703 TASK_LinkTask( hTask
);
704 pNewTask
->priority
--;
706 /* Switch to the new stack */
708 hCurrentTask
= hTask
;
709 #ifndef WINELIB /* FIXME: JBP: IF1632 not allowed in libwine.a */
710 IF1632_Saved16_ss
= pNewTask
->ss
;
711 IF1632_Saved16_sp
= pNewTask
->sp
;
712 IF1632_Saved32_esp
= pNewTask
->esp
;
713 IF1632_Stack32_base
= WIN16_GlobalLock( pNewTask
->hStack32
);
718 /***********************************************************************
719 * InitTask (KERNEL.91)
721 void InitTask( struct sigcontext_struct context
)
723 static int firstTask
= 1;
726 SEGTABLEENTRY
*pSegTable
;
727 INSTANCEDATA
*pinstance
;
728 LONG stacklow
, stackhi
;
730 EAX_reg(&context
) = 0;
731 if (!(pTask
= (TDB
*)GlobalLock( hCurrentTask
))) return;
732 if (!(pModule
= (NE_MODULE
*)GlobalLock( pTask
->hModule
))) return;
736 extern BOOL
WIDGETS_Init(void);
737 extern BOOL
WIN_CreateDesktopWindow(void);
739 /* Perform global initialisations that need a task context */
741 /* Initialize built-in window classes */
742 if (!WIDGETS_Init()) return;
744 /* Create desktop window */
745 if (!WIN_CreateDesktopWindow()) return;
751 NE_InitializeDLLs( pTask
->hModule
);
753 /* Registers on return are:
754 * ax 1 if OK, 0 on error
755 * cx stack limit in bytes
756 * dx cmdShow parameter
757 * si instance handle of the previous instance
758 * di instance handle of the new task
759 * es:bx pointer to command-line inside PSP
761 EAX_reg(&context
) = 1;
762 EBX_reg(&context
) = 0x81;
763 ECX_reg(&context
) = pModule
->stack_size
;
764 EDX_reg(&context
) = pTask
->nCmdShow
;
765 ESI_reg(&context
) = (DWORD
)pTask
->hPrevInstance
;
766 EDI_reg(&context
) = (DWORD
)pTask
->hInstance
;
767 ES_reg (&context
) = (WORD
)pTask
->hPDB
;
770 /* Initialize the local heap */
771 if ( pModule
->heap_size
)
773 LocalInit( pTask
->hInstance
, 0, pModule
->heap_size
);
777 /* Initialize the INSTANCEDATA structure */
778 pSegTable
= NE_SEG_TABLE( pModule
);
779 stacklow
= pSegTable
[pModule
->ss
- 1].minsize
;
780 stackhi
= stacklow
+ pModule
->stack_size
;
781 if (stackhi
> 0xffff) stackhi
= 0xffff;
782 pinstance
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN(CURRENT_DS
, 0);
783 pinstance
->stackbottom
= stackhi
; /* yup, that's right. Confused me too. */
784 pinstance
->stacktop
= stacklow
;
785 #ifndef WINELIB /* FIXME: JBP: IF1632 not allowed in libwine.a */
786 pinstance
->stackmin
= IF1632_Saved16_sp
;
791 /***********************************************************************
792 * WaitEvent (KERNEL.30)
794 BOOL
WaitEvent( HTASK hTask
)
798 if (!hTask
) hTask
= hCurrentTask
;
799 pTask
= (TDB
*)GlobalLock( hTask
);
800 if (pTask
->nEvents
> 0)
806 /* When we get back here, we have an event (or the task is the only one) */
807 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
812 /***********************************************************************
813 * PostEvent (KERNEL.31)
815 void PostEvent( HTASK hTask
)
819 if (!hTask
) hTask
= hCurrentTask
;
820 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return;
825 /***********************************************************************
826 * SetPriority (KERNEL.32)
828 void SetPriority( HTASK hTask
, int delta
)
833 if (!hTask
) hTask
= hCurrentTask
;
834 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return;
835 newpriority
= pTask
->priority
+ delta
;
836 if (newpriority
< -32) newpriority
= -32;
837 else if (newpriority
> 15) newpriority
= 15;
839 pTask
->priority
= newpriority
+ 1;
840 TASK_UnlinkTask( hTask
);
841 TASK_LinkTask( hTask
);
846 /***********************************************************************
847 * LockCurrentTask (KERNEL.33)
849 HTASK
LockCurrentTask( BOOL bLock
)
851 if (bLock
) hLockedTask
= hCurrentTask
;
852 else hLockedTask
= 0;
857 /***********************************************************************
858 * IsTaskLocked (KERNEL.122)
860 WORD
IsTaskLocked(void)
866 /***********************************************************************
867 * OldYield (KERNEL.117)
873 pCurTask
= (TDB
*)GlobalLock( hCurrentTask
);
874 if (pCurTask
) pCurTask
->nEvents
++; /* Make sure we get back here */
876 if (pCurTask
) pCurTask
->nEvents
--;
880 /***********************************************************************
881 * DirectedYield (KERNEL.150)
883 void DirectedYield( HTASK hTask
)
887 if ((pCurTask
= (TDB
*)GlobalLock( hCurrentTask
)) != NULL
)
888 pCurTask
->hYieldTo
= hTask
;
894 /***********************************************************************
903 /***********************************************************************
904 * MakeProcInstance (KERNEL.51)
906 FARPROC
MakeProcInstance( FARPROC func
, HANDLE hInstance
)
911 thunkaddr
= TASK_AllocThunk( hCurrentTask
);
912 if (!thunkaddr
) return (FARPROC
)0;
913 thunk
= PTR_SEG_TO_LIN( thunkaddr
);
915 dprintf_task( stddeb
, "MakeProcInstance("SPFMT
","NPFMT
"): got thunk "SPFMT
"\n",
916 (SEGPTR
)func
, hInstance
, (SEGPTR
)thunkaddr
);
918 *thunk
++ = 0xb8; /* movw instance, %ax */
920 *thunk
++ = (BYTE
)(hInstance
& 0xff);
921 *thunk
++ = (BYTE
)(hInstance
>> 8);
923 *thunk
++ = 0xea; /* ljmp func */
924 *(DWORD
*)thunk
= (DWORD
)func
;
925 return (FARPROC
)thunkaddr
;
929 /***********************************************************************
930 * FreeProcInstance (KERNEL.52)
932 void FreeProcInstance( FARPROC func
)
934 dprintf_task( stddeb
, "FreeProcInstance("SPFMT
")\n", (SEGPTR
)func
);
935 TASK_FreeThunk( hCurrentTask
, (SEGPTR
)func
);
939 /**********************************************************************
940 * GetCodeHandle (KERNEL.93)
942 HANDLE
GetCodeHandle( FARPROC proc
)
945 BYTE
*thunk
= (BYTE
*)PTR_SEG_TO_LIN( proc
);
947 /* Return the code segment containing 'proc'. */
948 /* Not sure if this is really correct (shouldn't matter that much). */
950 /* Check if it is really a thunk */
951 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
952 handle
= GlobalHandle( thunk
[6] + (thunk
[7] << 8) );
954 handle
= GlobalHandle( HIWORD(proc
) );
956 printf( "STUB: GetCodeHandle(%08lx) returning "NPFMT
"\n",
957 (DWORD
)proc
, handle
);
962 /***********************************************************************
963 * SetTaskQueue (KERNEL.34)
965 HGLOBAL
SetTaskQueue( HANDLE hTask
, HGLOBAL hQueue
)
970 if (!hTask
) hTask
= hCurrentTask
;
971 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return 0;
972 hPrev
= pTask
->hQueue
;
973 pTask
->hQueue
= hQueue
;
978 /***********************************************************************
979 * GetTaskQueue (KERNEL.35)
981 HGLOBAL
GetTaskQueue( HANDLE hTask
)
985 if (!hTask
) hTask
= hCurrentTask
;
986 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return 0;
987 return pTask
->hQueue
;
991 /***********************************************************************
992 * GetTaskQueueDS (KERNEL.118)
995 void GetTaskQueueDS( struct sigcontext_struct context
)
997 DS_reg(&context
) = GlobalHandleToSel( GetTaskQueue(0) );
1002 /***********************************************************************
1003 * GetTaskQueueES (KERNEL.119)
1006 void GetTaskQueueES( struct sigcontext_struct context
)
1008 ES_reg(&context
) = GlobalHandleToSel( GetTaskQueue(0) );
1010 #endif /* WINELIB */
1013 /***********************************************************************
1014 * GetCurrentTask (KERNEL.36)
1016 HTASK
GetCurrentTask(void)
1018 /* Undocumented: first task is returned in high word */
1020 return hCurrentTask
;
1022 return MAKELONG( hCurrentTask
, hFirstTask
);
1027 /***********************************************************************
1028 * GetCurrentPDB (KERNEL.37)
1030 HANDLE
GetCurrentPDB(void)
1034 if (!(pTask
= (TDB
*)GlobalLock( hCurrentTask
))) return 0;
1039 /***********************************************************************
1040 * GetInstanceData (KERNEL.54)
1042 int GetInstanceData( HANDLE instance
, WORD buffer
, int len
)
1044 char *ptr
= (char *)GlobalLock( instance
);
1045 if (!ptr
|| !len
) return 0;
1046 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1047 memcpy( ptr
+ buffer
, (char *)GlobalLock( CURRENT_DS
) + buffer
, len
);
1052 /***********************************************************************
1053 * GetDOSEnvironment (KERNEL.131)
1055 SEGPTR
GetDOSEnvironment(void)
1059 if (!(pTask
= (TDB
*)GlobalLock( hCurrentTask
))) return 0;
1060 return (SEGPTR
)WIN16_GlobalLock( pTask
->pdb
.environment
);
1064 /***********************************************************************
1065 * GetNumTasks (KERNEL.152)
1067 WORD
GetNumTasks(void)
1073 /***********************************************************************
1074 * GetTaskDS (KERNEL.155)
1076 HINSTANCE
GetTaskDS(void)
1080 if (!(pTask
= (TDB
*)GlobalLock( hCurrentTask
))) return 0;
1081 return pTask
->hInstance
;
1085 /***********************************************************************
1086 * IsTask (KERNEL.320)
1088 BOOL
IsTask( HTASK hTask
)
1092 if (!(pTask
= (TDB
*)GlobalLock( hTask
))) return FALSE
;
1093 if (GlobalSize( hTask
) < sizeof(TDB
)) return FALSE
;
1094 return (pTask
->magic
== TDB_MAGIC
);
1098 /***********************************************************************
1099 * GetExePtr (KERNEL.133)
1101 HMODULE
GetExePtr( HANDLE handle
)
1107 /* Check for module handle */
1109 if (!(ptr
= GlobalLock( handle
))) return 0;
1110 if (((NE_MODULE
*)ptr
)->magic
== NE_SIGNATURE
) return handle
;
1112 /* Check the owner for module handle */
1115 owner
= FarGetOwner( handle
);
1118 fprintf(stderr
,"JBP: FarGetOwner() ignored.\n");
1120 if (!(ptr
= GlobalLock( owner
))) return 0;
1121 if (((NE_MODULE
*)ptr
)->magic
== NE_SIGNATURE
) return owner
;
1123 /* Search for this handle and its owner inside all tasks */
1128 TDB
*pTask
= (TDB
*)GlobalLock( hTask
);
1129 if ((hTask
== handle
) ||
1130 (pTask
->hInstance
== handle
) ||
1131 (pTask
->hQueue
== handle
) ||
1132 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1133 if ((hTask
== owner
) ||
1134 (pTask
->hInstance
== owner
) ||
1135 (pTask
->hQueue
== owner
) ||
1136 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1137 hTask
= pTask
->hNext
;
1143 /***********************************************************************
1144 * TaskFirst (TOOLHELP.63)
1146 BOOL
TaskFirst( TASKENTRY
*lpte
)
1148 lpte
->hNext
= hFirstTask
;
1149 return TaskNext( lpte
);
1153 /***********************************************************************
1154 * TaskNext (TOOLHELP.64)
1156 BOOL
TaskNext( TASKENTRY
*lpte
)
1159 INSTANCEDATA
*pInstData
;
1161 dprintf_toolhelp( stddeb
, "TaskNext(%p): task="NPFMT
"\n", lpte
, lpte
->hNext
);
1162 if (!lpte
->hNext
) return FALSE
;
1163 pTask
= (TDB
*)GlobalLock( lpte
->hNext
);
1164 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1165 pInstData
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN( pTask
->hInstance
, 0 );
1166 lpte
->hTask
= lpte
->hNext
;
1167 lpte
->hTaskParent
= pTask
->hParent
;
1168 lpte
->hInst
= pTask
->hInstance
;
1169 lpte
->hModule
= pTask
->hModule
;
1170 lpte
->wSS
= pTask
->ss
;
1171 lpte
->wSP
= pTask
->sp
;
1172 lpte
->wStackTop
= pInstData
->stacktop
;
1173 lpte
->wStackMinimum
= pInstData
->stackmin
;
1174 lpte
->wStackBottom
= pInstData
->stackbottom
;
1175 lpte
->wcEvents
= pTask
->nEvents
;
1176 lpte
->hQueue
= pTask
->hQueue
;
1177 strncpy( lpte
->szModule
, pTask
->module_name
, 8 );
1178 lpte
->szModule
[8] = '\0';
1179 lpte
->wPSPOffset
= 0x100; /*??*/
1180 lpte
->hNext
= pTask
->hNext
;
1185 /***********************************************************************
1186 * TaskFindHandle (TOOLHELP.65)
1188 BOOL
TaskFindHandle( TASKENTRY
*lpte
, HTASK hTask
)
1190 lpte
->hNext
= hTask
;
1191 return TaskNext( lpte
);