mirror of
git://source.winehq.org/git/wine.git
synced 2024-09-14 18:45:38 +00:00
c7c217b31c
Sun Apr 12 12:22:23 1997 Andreas Mohr <100.30936@germany.net> * [files/drive.c] Fixed "no free space" problem with partition sizes between 1 and 2 GB (cluster_sectors may not exceed 0x40). * [windows/msgbox.c] [if1632/user.spec] [include/windows.h] Implemented MessageBoxIndirect16, corrected MSGBOXPARAMS16. * [loader/task.c] DOS environment strings may never exceed 127 chars -> truncate Unix environment strings if necessary. Sun Apr 12 02:51:44 1998 Dimitrie O. Paun <dimi@mail.cs.toronto.edu> * [files/*.c] All fprintf statements were converted to appropriate debug messages. * [tools/find_debug_channels] Updated comments at the beginning of the file. Sat Apr 11 15:27:21 1998 Alexandre Julliard <julliard@lrc.epfl.ch> * [loader/module.c] [loader/task.c] [scheduler/process.c] Moved some code around to prepare the ground for CreateProcess(). * [memory/environ.c] [loader/task.c] Moved Win32 environment strings functions to environ.c. Unified Win16 and Win32 environment management. * [scheduler/handle.c] [scheduler/k32obj.c] [scheduler/*.c] Implemented handle inheritance and DuplicateHandle(). * [scheduler/thread.c] Create a 16-bit stack for all threads. * [windows/dialog.c] Implemented DIALOGEX resource format. Fri Apr 10 20:21:51 1998 Marcus Meissner <marcus@mud.de> * [configure.in][include/acconfig.h][*/*][multimedia/*] Cleaned up the OSS detection stuff, added some more checks for headerfiles/functions. Removed a lot of OS specific #ifdefs. Lots of dependend multimedia cleanups. * [loader/pe_image.c] Enhanced comment, added missing reference count increase. * [ole/compobj.c] Replaced broken StringFromGUID2 by working one. * [misc/winsock.c] SO_LINGER uses unsigned 16 bit in Win16 and Win32, but unsigned int (32bit) for UNIX. * [memory/global.c] Allow realloc for lockcount 1 too. Fri Apr 10 15:27:34 1998 Morten Welinder <terra@diku.dk> * [graphics/x11drv/text.c] Handle control characters in trace. Ignore terminating newline. * [multimedia/init.c] (MULTIMEDIA_Init): Correct allocations. * [tools/examine-relay] Tidy up. * [windows/syscolor.c] Change highlight colour from lightblue to lightgray. This looks correct for menus. Fri Apr 10 01:49:58 1998 Douglas Ridgway <ridgway@winehq.com> * [configure.in] [Make.rules.in] Add check for c2man before using it. Fri Apr 10 02:59:21 1998 Douglas Ridgway <ridgway@winehq.com> * [DEVELOPERS-HINTS] Simple description of adding API calls. * [include/wintypes.h] [include/windows.h] Get rid of Winelib16, avoid declaring some illegal functions in Winelib, add prototypes for some enhanced metafile functions, fix GetTextExtentPoint32 declarations. * [relay32/gdi32.spec] [objects/enhmetafile.c] Cosmetic and functional improvements. * [include/wincon.h] [programs/view/*] Fixes, improved compatibility with native compilers. Thu Apr 9 15:48:49 1998 Ulrich Weigand <weigand@informatik.uni-erlangen.de> * [win32/kernel32.c] Implemented FT_Thunk / FT_Prolog / FT_Exit / FT_PrologPrime. Fixed Common32ThkLS thunk function. * [tools/build.c] [relay32/relay386.c] [if1632/relay.c] Changed relay code to allow register functions to modify stack layout. * [memory/selector.c] Implemented AllocMappedBuffer / FreeMappedBuffer. * [relay32/kernel32.spec] [if1632/kernel.spec] [win32/ordinals.c] Added names for undocumented functions. * [loader/module.c] Bugfix: LoadLibrary16 should *not* silently load 32-bit DLL. Thu Apr 9 03:54:58 1998 Jim Peterson <jspeter@birch.ee.vt.edu> * [windows/keyboard.c] Fix an erroneous test in TranslateAccelerator{16,32} for the end of the accelerator table. Thu Apr 8 20:36:28 1998 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de> * [misc/crtdll.c] Implement getenv. * [misc/commdlg.c] Make Get[Save/Open]FileName work in most situations. * [misc/lstr.c] Use wvsprintf32A instead of vsprintf in FormatMessage32X * [misc/version] Make NT3.50 a recognised version * [graphics/x11drv/graphics.c] Change the algorithme to draw arcs * [loader/resource.c] Return an empty buffer in LoadString32A if no resource found. * [win32/code_page.c] Try harder to get the right size in MultiByteToWideChar. * [win32/process.c] Call WinExec32 for CreateProcess32A. * [windows/user.c] Install default Int0 Handler in InitApp(). Thu Apr 8 19:29:48 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de> * [misc/imagelist.c] Preliminary fix for drawing selected images. Various improvements. * [controls/progress.c][include/progress.c][include/commctrl.h] Added progress bar messages and styles for IE4.01 (dll version 4.72) compatibility. Fixed led size problem. * [controls/updown.c][include/commctrl.h] Added UDM_GETRANGE32 and UDM_SETRANGE32. * [objects/oembitmaps.c][include/windows.h][include/bitmaps/*] Added Win95 icons and fixed Win95 cursor and restore button bug. Now they should be visible. Sorry!!! * [relay32/comctl32.spec] Added most missing function names. Tue Apr 6 18:48:36 1998 Matthew Becker <mbecker@glasscity.net> * [objects/font.c] [if1632/gdi.spec] GetOutlineTextMetrics: stub * [objects/text.c] GetTextCharset should just call GetTextCharsetInfo. * [misc/mpr.c] [relay32/mpr.spec] WNetCachePassword: stub * [scheduler/thread.c] [relay32/user32.spec] AttachThreadInput: stub Updated documentation. * [objects/palette.c] Updated documentation. Tue Mar 31 17:06:30 1998 James Juran <jrj120@psu.edu> * [*/*.c] Finished fixing USER32 ordinal numbers in function documentation. Mon Mar 30 20:27:38 1998 Morten Welinder <terra@diku.dk> * [misc/debugstr.c] [include/debugstr.h] Moved _dumpstr from relay32/relay386.c. Improved control character handling. * [msdos/int21.c] Implement 215E00 -- get machine name. * [windows/winpos.c] SetWindowPos32: Make an extra sync when mapping managed windows. This makes sure the reconfigure event has been handled. See Mshearts' what's-your-name window. Mon Mar 30 01:13:50 1998 Alexander V. Lukyanov <lav@long.yar.ru> * [Makefile.in] Install includes from TOPSRCDIR.
753 lines
21 KiB
C
753 lines
21 KiB
C
/*
|
|
* Win32 threads
|
|
*
|
|
* Copyright 1996 Alexandre Julliard
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include "thread.h"
|
|
#include "process.h"
|
|
#include "winerror.h"
|
|
#include "heap.h"
|
|
#include "selectors.h"
|
|
#include "miscemu.h"
|
|
#include "winnt.h"
|
|
#include "debug.h"
|
|
|
|
#ifndef __i386__
|
|
THDB *pCurrentThread;
|
|
#endif
|
|
|
|
static BOOL32 THREAD_Signaled( K32OBJ *obj, DWORD thread_id );
|
|
static BOOL32 THREAD_Satisfied( K32OBJ *obj, DWORD thread_id );
|
|
static void THREAD_AddWait( K32OBJ *obj, DWORD thread_id );
|
|
static void THREAD_RemoveWait( K32OBJ *obj, DWORD thread_id );
|
|
static void THREAD_Destroy( K32OBJ *obj );
|
|
|
|
const K32OBJ_OPS THREAD_Ops =
|
|
{
|
|
THREAD_Signaled, /* signaled */
|
|
THREAD_Satisfied, /* satisfied */
|
|
THREAD_AddWait, /* add_wait */
|
|
THREAD_RemoveWait, /* remove_wait */
|
|
NULL, /* read */
|
|
NULL, /* write */
|
|
THREAD_Destroy /* destroy */
|
|
};
|
|
|
|
/* The pseudohandle used for the current thread, see GetCurrentThread */
|
|
#define CURRENT_THREAD_PSEUDOHANDLE 0xfffffffe
|
|
|
|
/* Is threading code initialized? */
|
|
BOOL32 THREAD_InitDone = FALSE;
|
|
|
|
/**********************************************************************
|
|
* THREAD_GetPtr
|
|
*
|
|
* Return a pointer to a thread object. The object count must be decremented
|
|
* when no longer used.
|
|
*/
|
|
THDB *THREAD_GetPtr( HANDLE32 handle, DWORD access )
|
|
{
|
|
THDB *thread;
|
|
|
|
if (handle == CURRENT_THREAD_PSEUDOHANDLE) /* Self-thread handle */
|
|
{
|
|
thread = THREAD_Current();
|
|
K32OBJ_IncCount( &thread->header );
|
|
}
|
|
else thread = (THDB *)HANDLE_GetObjPtr( PROCESS_Current(), handle,
|
|
K32OBJ_THREAD, access );
|
|
return thread;
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_Current
|
|
*
|
|
* Return the current thread THDB pointer.
|
|
*/
|
|
THDB *THREAD_Current(void)
|
|
{
|
|
if (!THREAD_InitDone) return NULL;
|
|
return (THDB *)((char *)NtCurrentTeb() - (int)&((THDB *)0)->teb);
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_AddQueue
|
|
*
|
|
* Add a thread to a queue.
|
|
*/
|
|
void THREAD_AddQueue( THREAD_QUEUE *queue, THDB *thread )
|
|
{
|
|
THREAD_ENTRY *entry = HeapAlloc( SystemHeap, HEAP_NO_SERIALIZE,
|
|
sizeof(*entry) );
|
|
assert(entry);
|
|
SYSTEM_LOCK();
|
|
entry->thread = thread;
|
|
if (*queue)
|
|
{
|
|
entry->next = (*queue)->next;
|
|
(*queue)->next = entry;
|
|
}
|
|
else entry->next = entry;
|
|
*queue = entry;
|
|
SYSTEM_UNLOCK();
|
|
}
|
|
|
|
/***********************************************************************
|
|
* THREAD_RemoveQueue
|
|
*
|
|
* Remove a thread from a queue.
|
|
*/
|
|
void THREAD_RemoveQueue( THREAD_QUEUE *queue, THDB *thread )
|
|
{
|
|
THREAD_ENTRY *entry = *queue;
|
|
SYSTEM_LOCK();
|
|
if (entry->next == entry) /* Only one element in the queue */
|
|
{
|
|
assert( entry->thread == thread );
|
|
*queue = NULL;
|
|
}
|
|
else
|
|
{
|
|
THREAD_ENTRY *next;
|
|
while (entry->next->thread != thread)
|
|
{
|
|
entry = entry->next;
|
|
assert( entry != *queue ); /* Have we come all the way around? */
|
|
}
|
|
if ((next = entry->next) == *queue) *queue = entry;
|
|
entry->next = entry->next->next;
|
|
entry = next; /* This is the one we want to free */
|
|
}
|
|
HeapFree( SystemHeap, 0, entry );
|
|
SYSTEM_UNLOCK();
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_Create
|
|
*/
|
|
THDB *THREAD_Create( PDB32 *pdb, DWORD stack_size, BOOL32 alloc_stack16,
|
|
LPTHREAD_START_ROUTINE start_addr, LPVOID param )
|
|
{
|
|
DWORD old_prot;
|
|
WORD cs, ds;
|
|
|
|
THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
|
|
if (!thdb) return NULL;
|
|
thdb->header.type = K32OBJ_THREAD;
|
|
thdb->header.refcount = 1;
|
|
thdb->process = pdb;
|
|
thdb->teb.except = (void *)-1;
|
|
thdb->teb.htask16 = 0; /* FIXME */
|
|
thdb->teb.self = &thdb->teb;
|
|
thdb->teb.tls_ptr = thdb->tls_array;
|
|
thdb->teb.process = pdb;
|
|
thdb->wait_list = &thdb->wait_struct;
|
|
thdb->exit_code = 0x103; /* STILL_ACTIVE */
|
|
thdb->entry_point = start_addr;
|
|
thdb->entry_arg = param;
|
|
|
|
/* Allocate the stack */
|
|
|
|
/* FIXME:
|
|
* If stacksize smaller than 1 MB, allocate 1MB
|
|
* (one program wanted only 10 kB, which is recommendable, but some WINE
|
|
* functions, noteably in the files subdir, push HUGE structures and
|
|
* arrays on the stack. They probably shouldn't.)
|
|
* If stacksize larger than 16 MB, warn the user. (We could shrink the stack
|
|
* but this could give more or less unexplainable crashes.)
|
|
*/
|
|
if (stack_size<1024*1024)
|
|
stack_size = 1024 * 1024;
|
|
if (stack_size >= 16*1024*1024)
|
|
fprintf(stderr,"Warning:Thread stack size is %ld MB.\n",stack_size/1024/1024);
|
|
thdb->stack_base = VirtualAlloc(NULL,
|
|
stack_size + (alloc_stack16 ? 0x10000 : 0),
|
|
MEM_COMMIT, PAGE_EXECUTE_READWRITE );
|
|
if (!thdb->stack_base) goto error;
|
|
/* Set a guard page at the bottom of the stack */
|
|
VirtualProtect( thdb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD,
|
|
&old_prot );
|
|
thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
|
|
thdb->teb.stack_low = thdb->stack_base;
|
|
thdb->exit_stack = thdb->teb.stack_top;
|
|
|
|
/* Allocate the TEB selector (%fs register) */
|
|
|
|
thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
|
|
TRUE, FALSE );
|
|
if (!thdb->teb_sel) goto error;
|
|
|
|
/* Allocate the 16-bit stack selector */
|
|
|
|
if (alloc_stack16)
|
|
{
|
|
thdb->teb.stack_sel = SELECTOR_AllocBlock( thdb->teb.stack_top,
|
|
0x10000, SEGMENT_DATA,
|
|
FALSE, FALSE );
|
|
if (!thdb->teb.stack_sel) goto error;
|
|
thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( thdb->teb.stack_sel, 0xfffc );
|
|
}
|
|
|
|
/* Allocate the event */
|
|
|
|
if (!(thdb->event = EVENT_Create( TRUE, FALSE ))) goto error;
|
|
|
|
/* Initialize the thread context */
|
|
|
|
GET_CS(cs);
|
|
GET_DS(ds);
|
|
thdb->pcontext = &thdb->context;
|
|
thdb->context.SegCs = cs;
|
|
thdb->context.SegDs = ds;
|
|
thdb->context.SegEs = ds;
|
|
thdb->context.SegGs = ds;
|
|
thdb->context.SegSs = ds;
|
|
thdb->context.SegFs = thdb->teb_sel;
|
|
thdb->context.Eip = (DWORD)start_addr;
|
|
thdb->context.Esp = (DWORD)thdb->teb.stack_top;
|
|
PE_InitTls( thdb );
|
|
return thdb;
|
|
|
|
error:
|
|
if (thdb->event) K32OBJ_DecCount( thdb->event );
|
|
if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
|
|
if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
|
|
if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
|
|
HeapFree( SystemHeap, 0, thdb );
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_Signaled
|
|
*/
|
|
static BOOL32 THREAD_Signaled( K32OBJ *obj, DWORD thread_id )
|
|
{
|
|
THDB *thdb = (THDB *)obj;
|
|
assert( obj->type == K32OBJ_THREAD );
|
|
return K32OBJ_OPS( thdb->event )->signaled( thdb->event, thread_id );
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_Satisfied
|
|
*
|
|
* Wait on this object has been satisfied.
|
|
*/
|
|
static BOOL32 THREAD_Satisfied( K32OBJ *obj, DWORD thread_id )
|
|
{
|
|
THDB *thdb = (THDB *)obj;
|
|
assert( obj->type == K32OBJ_THREAD );
|
|
return K32OBJ_OPS( thdb->event )->satisfied( thdb->event, thread_id );
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_AddWait
|
|
*
|
|
* Add thread to object wait queue.
|
|
*/
|
|
static void THREAD_AddWait( K32OBJ *obj, DWORD thread_id )
|
|
{
|
|
THDB *thdb = (THDB *)obj;
|
|
assert( obj->type == K32OBJ_THREAD );
|
|
return K32OBJ_OPS( thdb->event )->add_wait( thdb->event, thread_id );
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_RemoveWait
|
|
*
|
|
* Remove thread from object wait queue.
|
|
*/
|
|
static void THREAD_RemoveWait( K32OBJ *obj, DWORD thread_id )
|
|
{
|
|
THDB *thdb = (THDB *)obj;
|
|
assert( obj->type == K32OBJ_THREAD );
|
|
return K32OBJ_OPS( thdb->event )->remove_wait( thdb->event, thread_id );
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* THREAD_Destroy
|
|
*/
|
|
static void THREAD_Destroy( K32OBJ *ptr )
|
|
{
|
|
THDB *thdb = (THDB *)ptr;
|
|
|
|
assert( ptr->type == K32OBJ_THREAD );
|
|
ptr->type = K32OBJ_UNKNOWN;
|
|
|
|
/* Free the associated memory */
|
|
|
|
#ifdef __i386__
|
|
{
|
|
/* Check if we are deleting the current thread */
|
|
WORD fs;
|
|
GET_FS( fs );
|
|
if (fs == thdb->teb_sel)
|
|
{
|
|
GET_DS( fs );
|
|
SET_FS( fs );
|
|
}
|
|
}
|
|
#endif
|
|
K32OBJ_DecCount( thdb->event );
|
|
SELECTOR_FreeBlock( thdb->teb_sel, 1 );
|
|
if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
|
|
HeapFree( SystemHeap, 0, thdb );
|
|
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* CreateThread (KERNEL32.63)
|
|
*/
|
|
HANDLE32 WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
|
|
LPTHREAD_START_ROUTINE start, LPVOID param,
|
|
DWORD flags, LPDWORD id )
|
|
{
|
|
HANDLE32 handle;
|
|
BOOL32 inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
|
|
|
|
THDB *thread = THREAD_Create( PROCESS_Current(), stack,
|
|
TRUE, start, param );
|
|
if (!thread) return INVALID_HANDLE_VALUE32;
|
|
handle = HANDLE_Alloc( PROCESS_Current(), &thread->header,
|
|
THREAD_ALL_ACCESS, inherit );
|
|
if (handle == INVALID_HANDLE_VALUE32) goto error;
|
|
if (SYSDEPS_SpawnThread( thread ) == -1) goto error;
|
|
*id = THDB_TO_THREAD_ID( thread );
|
|
return handle;
|
|
|
|
error:
|
|
K32OBJ_DecCount( &thread->header );
|
|
return INVALID_HANDLE_VALUE32;
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* ExitThread [KERNEL32.215] Ends a thread
|
|
*
|
|
* RETURNS
|
|
* None
|
|
*/
|
|
void WINAPI ExitThread(
|
|
DWORD code) /* [in] Exit code for this thread */
|
|
{
|
|
THDB *thdb = THREAD_Current();
|
|
LONG count;
|
|
|
|
SYSTEM_LOCK();
|
|
thdb->exit_code = code;
|
|
EVENT_Set( thdb->event );
|
|
|
|
/* Abandon all owned mutexes */
|
|
while (thdb->mutex_list) MUTEX_Abandon( thdb->mutex_list );
|
|
|
|
/* FIXME: should free the stack somehow */
|
|
#if 0
|
|
/* FIXME: We cannot do this; once the current thread is destroyed,
|
|
synchronization primitives do not work properly. */
|
|
K32OBJ_DecCount( &thdb->header );
|
|
#endif
|
|
/* Completely unlock the system lock just in case */
|
|
count = SYSTEM_LOCK_COUNT();
|
|
while (count--) SYSTEM_UNLOCK();
|
|
SYSDEPS_ExitThread();
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
|
|
*
|
|
* RETURNS
|
|
* Pseudohandle for the current thread
|
|
*/
|
|
HANDLE32 WINAPI GetCurrentThread(void)
|
|
{
|
|
return CURRENT_THREAD_PSEUDOHANDLE;
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
|
|
*
|
|
* RETURNS
|
|
* Thread identifier of calling thread
|
|
*/
|
|
DWORD WINAPI GetCurrentThreadId(void)
|
|
{
|
|
return THDB_TO_THREAD_ID( THREAD_Current() );
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
|
|
*
|
|
* RETURNS
|
|
* Calling thread's last error code value.
|
|
*/
|
|
DWORD WINAPI GetLastError(void)
|
|
{
|
|
THDB *thread = THREAD_Current();
|
|
return thread->last_error;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
|
|
*
|
|
* RETURNS
|
|
* None.
|
|
*/
|
|
void WINAPI SetLastError(
|
|
DWORD error) /* [in] Per-thread error code */
|
|
{
|
|
THDB *thread = THREAD_Current();
|
|
/* This one must work before we have a thread (FIXME) */
|
|
if (thread) thread->last_error = error;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* SetLastErrorEx [USER32.485] Sets the last-error code.
|
|
*
|
|
* RETURNS
|
|
* None.
|
|
*/
|
|
void WINAPI SetLastErrorEx(
|
|
DWORD error, /* [in] Per-thread error code */
|
|
DWORD type) /* [in] Error type */
|
|
{
|
|
TRACE(thread, "(%08lx, %08lx)\n", error,type);
|
|
switch(type) {
|
|
case 0:
|
|
break;
|
|
case SLE_ERROR:
|
|
case SLE_MINORERROR:
|
|
case SLE_WARNING:
|
|
/* Fall through for now */
|
|
default:
|
|
FIXME(thread, "(error=%08lx, type=%08lx): Unhandled type\n", error,type);
|
|
break;
|
|
}
|
|
SetLastError( error );
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* THREAD_TlsAlloc
|
|
*/
|
|
DWORD THREAD_TlsAlloc(THDB *thread)
|
|
{
|
|
DWORD i, mask, ret = 0;
|
|
DWORD *bits = thread->process->tls_bits;
|
|
EnterCriticalSection( &thread->process->crit_section );
|
|
if (*bits == 0xffffffff)
|
|
{
|
|
bits++;
|
|
ret = 32;
|
|
if (*bits == 0xffffffff)
|
|
{
|
|
LeaveCriticalSection( &thread->process->crit_section );
|
|
SetLastError( ERROR_NO_MORE_ITEMS );
|
|
return 0xffffffff;
|
|
}
|
|
}
|
|
for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
|
|
*bits |= mask;
|
|
LeaveCriticalSection( &thread->process->crit_section );
|
|
return ret + i;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* TlsAlloc [KERNEL32.530] Allocates a TLS index.
|
|
*
|
|
* Allocates a thread local storage index
|
|
*
|
|
* RETURNS
|
|
* Success: TLS Index
|
|
* Failure: 0xFFFFFFFF
|
|
*/
|
|
DWORD WINAPI TlsAlloc(void)
|
|
{
|
|
return THREAD_TlsAlloc(THREAD_Current());
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* TlsFree [KERNEL32.531] Releases a TLS index.
|
|
*
|
|
* Releases a thread local storage index, making it available for reuse
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI TlsFree(
|
|
DWORD index) /* [in] TLS Index to free */
|
|
{
|
|
DWORD mask;
|
|
THDB *thread = THREAD_Current();
|
|
DWORD *bits = thread->process->tls_bits;
|
|
if (index >= 64)
|
|
{
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
return FALSE;
|
|
}
|
|
EnterCriticalSection( &thread->process->crit_section );
|
|
if (index >= 32) bits++;
|
|
mask = (1 << (index & 31));
|
|
if (!(*bits & mask)) /* already free? */
|
|
{
|
|
LeaveCriticalSection( &thread->process->crit_section );
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
return FALSE;
|
|
}
|
|
*bits &= ~mask;
|
|
thread->tls_array[index] = 0;
|
|
/* FIXME: should zero all other thread values */
|
|
LeaveCriticalSection( &thread->process->crit_section );
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
|
|
*
|
|
* RETURNS
|
|
* Success: Value stored in calling thread's TLS slot for index
|
|
* Failure: 0 and GetLastError returns NO_ERROR
|
|
*/
|
|
LPVOID WINAPI TlsGetValue(
|
|
DWORD index) /* [in] TLS index to retrieve value for */
|
|
{
|
|
THDB *thread = THREAD_Current();
|
|
if (index >= 64)
|
|
{
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
return NULL;
|
|
}
|
|
SetLastError( ERROR_SUCCESS );
|
|
return thread->tls_array[index];
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI TlsSetValue(
|
|
DWORD index, /* [in] TLS index to set value for */
|
|
LPVOID value) /* [in] Value to be stored */
|
|
{
|
|
THDB *thread = THREAD_Current();
|
|
if (index >= 64)
|
|
{
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
return FALSE;
|
|
}
|
|
thread->tls_array[index] = value;
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
* GetThreadContext [KERNEL32.294] Retrieves context of thread.
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI GetThreadContext(
|
|
HANDLE32 handle, /* [in] Handle to thread with context */
|
|
CONTEXT *context) /* [out] Address of context structure */
|
|
{
|
|
THDB *thread = THREAD_GetPtr( handle, THREAD_GET_CONTEXT );
|
|
if (!thread) return FALSE;
|
|
*context = thread->context;
|
|
K32OBJ_DecCount( &thread->header );
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* GetThreadPriority [KERNEL32.296] Returns priority for thread.
|
|
*
|
|
* RETURNS
|
|
* Success: Thread's priority level.
|
|
* Failure: THREAD_PRIORITY_ERROR_RETURN
|
|
*/
|
|
INT32 WINAPI GetThreadPriority(
|
|
HANDLE32 hthread) /* [in] Handle to thread */
|
|
{
|
|
THDB *thread;
|
|
INT32 ret;
|
|
|
|
if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION )))
|
|
return THREAD_PRIORITY_ERROR_RETURN;
|
|
ret = thread->delta_priority;
|
|
K32OBJ_DecCount( &thread->header );
|
|
return ret;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* SetThreadPriority [KERNEL32.514] Sets priority for thread.
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI SetThreadPriority(
|
|
HANDLE32 hthread, /* [in] Handle to thread */
|
|
INT32 priority) /* [in] Thread priority level */
|
|
{
|
|
THDB *thread;
|
|
|
|
if (!(thread = THREAD_GetPtr( hthread, THREAD_SET_INFORMATION )))
|
|
return FALSE;
|
|
thread->delta_priority = priority;
|
|
K32OBJ_DecCount( &thread->header );
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* TerminateThread [KERNEL32.???] Terminates a thread
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI TerminateThread(
|
|
HANDLE32 handle, /* [in] Handle to thread */
|
|
DWORD exitcode) /* [in] Exit code for thread */
|
|
{
|
|
FIXME(thread,"(0x%08x,%ld): stub\n",handle,exitcode);
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI GetExitCodeThread(
|
|
HANDLE32 hthread, /* [in] Handle to thread */
|
|
LPDWORD exitcode) /* [out] Address to receive termination status */
|
|
{
|
|
THDB *thread;
|
|
|
|
if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION )))
|
|
return FALSE;
|
|
if (exitcode) *exitcode = thread->exit_code;
|
|
K32OBJ_DecCount( &thread->header );
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* ResumeThread [KERNEL32.587] Resumes a thread.
|
|
*
|
|
* Decrements a thread's suspend count. When count is zero, the
|
|
* execution of the thread is resumed.
|
|
*
|
|
* RETURNS
|
|
* Success: Previous suspend count
|
|
* Failure: 0xFFFFFFFF
|
|
*/
|
|
DWORD WINAPI ResumeThread(
|
|
HANDLE32 handle) /* [in] Indentifies thread to restart */
|
|
{
|
|
FIXME(thread,"(0x%08x): stub\n",handle);
|
|
return 0xFFFFFFFF;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* SuspendThread [KERNEL32.681] Suspends a thread.
|
|
*
|
|
* RETURNS
|
|
* Success: Previous suspend count
|
|
* Failure: 0xFFFFFFFF
|
|
*/
|
|
DWORD WINAPI SuspendThread(
|
|
HANDLE32 handle) /* [in] Handle to the thread */
|
|
{
|
|
FIXME(thread,"(0x%08x): stub\n",handle);
|
|
return 0xFFFFFFFF;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* GetThreadTimes [KERNEL32.???] Obtains timing information.
|
|
*
|
|
* NOTES
|
|
* What are the fields where these values are stored?
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI GetThreadTimes(
|
|
HANDLE32 thread, /* [in] Specifies the thread of interest */
|
|
LPFILETIME creationtime, /* [out] When the thread was created */
|
|
LPFILETIME exittime, /* [out] When the thread was destroyed */
|
|
LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
|
|
LPFILETIME usertime) /* [out] Time thread spent in user mode */
|
|
{
|
|
FIXME(thread,"(0x%08x): stub\n",thread);
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
|
|
*
|
|
* Attaches the input processing mechanism of one thread to that of
|
|
* another thread.
|
|
*
|
|
* RETURNS
|
|
* Success: TRUE
|
|
* Failure: FALSE
|
|
*/
|
|
BOOL32 WINAPI AttachThreadInput(
|
|
DWORD idAttach, /* [in] Thread to attach */
|
|
DWORD idAttachTo, /* [in] Thread to attach to */
|
|
BOOL32 fAttach) /* [in] Attach or detach */
|
|
{
|
|
BOOL32 ret;
|
|
|
|
FIXME(thread, "(0x%08lx,0x%08lx,%d): stub\n",idAttach,idAttachTo,fAttach);
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
if (fAttach) {
|
|
/* Attach threads */
|
|
ret = FALSE;
|
|
}
|
|
else {
|
|
/* Detach threads */
|
|
ret = FALSE;
|
|
};
|
|
return ret;
|
|
}
|
|
|