2020-06-01 12:52:05 +02:00
|
|
|
/*
|
|
|
|
* NT threads support
|
|
|
|
*
|
|
|
|
* Copyright 1996, 2003 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma makedep unix
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2020-06-03 16:32:02 +02:00
|
|
|
#include <errno.h>
|
2020-06-01 12:52:05 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TIMES_H
|
|
|
|
#include <sys/times.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SYSCALL_H
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "ddk/wdm.h"
|
|
|
|
#include "wine/server.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/exception.h"
|
|
|
|
#include "unix_private.h"
|
|
|
|
|
|
|
|
#ifndef PTHREAD_STACK_MIN
|
|
|
|
#define PTHREAD_STACK_MIN 16384
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int *nb_threads;
|
|
|
|
|
|
|
|
static inline int get_unix_exit_code( NTSTATUS status )
|
|
|
|
{
|
|
|
|
/* prevent a nonzero exit code to end up truncated to zero in unix */
|
|
|
|
if (status && !(status & 0xff)) return 1;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* pthread_exit_wrapper
|
|
|
|
*/
|
|
|
|
static void pthread_exit_wrapper( int status )
|
|
|
|
{
|
|
|
|
close( ntdll_get_thread_data()->wait_fd[0] );
|
|
|
|
close( ntdll_get_thread_data()->wait_fd[1] );
|
|
|
|
close( ntdll_get_thread_data()->reply_fd );
|
|
|
|
close( ntdll_get_thread_data()->request_fd );
|
|
|
|
pthread_exit( UIntToPtr(status) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* init_threading
|
|
|
|
*/
|
|
|
|
void CDECL init_threading( int *nb_threads_ptr, struct ldt_copy **ldt_copy )
|
|
|
|
{
|
|
|
|
#ifdef __i386__
|
|
|
|
extern struct ldt_copy __wine_ldt_copy;
|
|
|
|
*ldt_copy = &__wine_ldt_copy;
|
|
|
|
#endif
|
|
|
|
nb_threads = nb_threads_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2020-06-03 16:32:02 +02:00
|
|
|
* start_thread
|
2020-06-01 12:52:05 +02:00
|
|
|
*/
|
2020-06-03 16:32:02 +02:00
|
|
|
void CDECL start_thread( PRTL_THREAD_START_ROUTINE entry, void *arg, void *relay, TEB *teb )
|
2020-06-01 12:52:05 +02:00
|
|
|
{
|
2020-06-03 16:32:02 +02:00
|
|
|
BOOL suspend;
|
|
|
|
|
2020-06-01 12:52:05 +02:00
|
|
|
signal_init_thread( teb );
|
2020-06-03 16:32:02 +02:00
|
|
|
server_init_thread( entry, &suspend, NULL, NULL, NULL );
|
|
|
|
signal_start_thread( entry, arg, suspend, relay, teb );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* start_process
|
|
|
|
*/
|
|
|
|
void CDECL start_process( PRTL_THREAD_START_ROUTINE entry, BOOL suspend, void *relay )
|
|
|
|
{
|
|
|
|
signal_start_thread( entry, NtCurrentTeb()->Peb, suspend, relay, NtCurrentTeb() );
|
2020-06-01 12:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* abort_thread
|
|
|
|
*/
|
|
|
|
void CDECL abort_thread( int status )
|
|
|
|
{
|
|
|
|
pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
|
|
|
|
if (InterlockedDecrement( nb_threads ) <= 0) _exit( get_unix_exit_code( status ));
|
|
|
|
signal_exit_thread( status, pthread_exit_wrapper );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* exit_thread
|
|
|
|
*/
|
|
|
|
void CDECL exit_thread( int status )
|
|
|
|
{
|
|
|
|
pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
|
|
|
|
signal_exit_thread( status, pthread_exit_wrapper );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* exit_process
|
|
|
|
*/
|
|
|
|
void CDECL exit_process( int status )
|
|
|
|
{
|
|
|
|
pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
|
|
|
|
signal_exit_thread( get_unix_exit_code( status ), exit );
|
|
|
|
}
|
2020-06-02 13:11:54 +02:00
|
|
|
|
|
|
|
|
2020-06-03 16:32:02 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* wait_suspend
|
|
|
|
*
|
|
|
|
* Wait until the thread is no longer suspended.
|
|
|
|
*/
|
|
|
|
void wait_suspend( CONTEXT *context )
|
|
|
|
{
|
|
|
|
int saved_errno = errno;
|
|
|
|
|
|
|
|
/* wait with 0 timeout, will only return once the thread is no longer suspended */
|
|
|
|
server_select( NULL, 0, SELECT_INTERRUPTIBLE, 0, context, NULL, NULL );
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-02 13:11:54 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* set_thread_context
|
|
|
|
*/
|
|
|
|
NTSTATUS set_thread_context( HANDLE handle, const context_t *context, BOOL *self )
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
SERVER_START_REQ( set_thread_context )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( handle );
|
|
|
|
wine_server_add_data( req, context, sizeof(*context) );
|
|
|
|
ret = wine_server_call( req );
|
|
|
|
*self = reply->self;
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-06-02 14:05:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* get_thread_context
|
|
|
|
*/
|
|
|
|
NTSTATUS get_thread_context( HANDLE handle, context_t *context, unsigned int flags, BOOL *self )
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
SERVER_START_REQ( get_thread_context )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( handle );
|
|
|
|
req->flags = flags;
|
|
|
|
wine_server_set_reply( req, context, sizeof(*context) );
|
|
|
|
ret = wine_server_call( req );
|
|
|
|
*self = reply->self;
|
|
|
|
handle = wine_server_ptr_handle( reply->handle );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
if (ret == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
timeout.QuadPart = -1000000;
|
|
|
|
if (NtWaitForSingleObject( handle, FALSE, &timeout ))
|
|
|
|
{
|
|
|
|
NtClose( handle );
|
|
|
|
return STATUS_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
SERVER_START_REQ( get_thread_context )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( handle );
|
|
|
|
req->flags = flags;
|
|
|
|
wine_server_set_reply( req, context, sizeof(*context) );
|
|
|
|
ret = wine_server_call( req );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|