FreeRDP
thread.c File Reference
#include <winpr/config.h>
#include <winpr/assert.h>
#include <winpr/handle.h>
#include <winpr/thread.h>
#include <winpr/crt.h>
#include <winpr/platform.h>
#include <string.h>
#include <winpr/debug.h>
#include <errno.h>
#include <fcntl.h>
#include <winpr/collections.h>
#include "thread.h"
#include "apc.h"
#include "../handle/handle.h"
#include "../log.h"

Macros

#define MIN(x, y)   (((x) < (y)) ? (x) : (y))
 
#define MAX(x, y)   (((x) > (y)) ? (x) : (y))
 
#define TAG   WINPR_TAG("thread")
 
#define run_mutex_init(fkt, mux, arg)   run_mutex_init_(fkt, #fkt, mux, arg)
 
#define run_mutex_fkt(fkt, mux)   run_mutex_fkt_(fkt, #fkt, mux)
 
#define run_cond_init(fkt, cond, arg)   run_cond_init_(fkt, #fkt, cond, arg)
 
#define run_cond_fkt(fkt, cond)   run_cond_fkt_(fkt, #fkt, cond)
 

Functions

static BOOL ThreadCloseHandle (HANDLE handle)
 
static void cleanup_handle (void *obj)
 
static BOOL ThreadIsHandled (HANDLE handle)
 
static int ThreadGetFd (HANDLE handle)
 
static BOOL run_mutex_init_ (int(*fkt)(pthread_mutex_t *, const pthread_mutexattr_t *), const char *name, pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
 
static BOOL run_mutex_fkt_ (int(*fkt)(pthread_mutex_t *mux), const char *name, pthread_mutex_t *mutex)
 
static BOOL run_cond_init_ (int(*fkt)(pthread_cond_t *, const pthread_condattr_t *), const char *name, pthread_cond_t *condition, const pthread_condattr_t *conditionattr)
 
static BOOL run_cond_fkt_ (int(*fkt)(pthread_cond_t *mux), const char *name, pthread_cond_t *condition)
 
static int pthread_mutex_checked_unlock (pthread_mutex_t *mutex)
 
static BOOL mux_condition_bundle_init (mux_condition_bundle *bundle)
 
static void mux_condition_bundle_uninit (mux_condition_bundle *bundle)
 
static BOOL mux_condition_bundle_signal (mux_condition_bundle *bundle)
 
static BOOL mux_condition_bundle_lock (mux_condition_bundle *bundle)
 
static BOOL mux_condition_bundle_unlock (mux_condition_bundle *bundle)
 
static BOOL mux_condition_bundle_wait (mux_condition_bundle *bundle, const char *name)
 
static BOOL signal_thread_ready (WINPR_THREAD *thread)
 
static BOOL signal_thread_is_running (WINPR_THREAD *thread)
 
static DWORD ThreadCleanupHandle (HANDLE handle)
 
static void dump_thread (WINPR_THREAD *thread)
 
static BOOL set_event (WINPR_THREAD *thread)
 
static BOOL reset_event (WINPR_THREAD *thread)
 
static BOOL initializeThreads (PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
 
static BOOL signal_and_wait_for_ready (WINPR_THREAD *thread)
 
static void * thread_launcher (void *arg)
 
static BOOL winpr_StartThread (WINPR_THREAD *thread)
 
BOOL SetThreadPriority (HANDLE hThread, int nPriority)
 
HANDLE CreateThread (LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
 
HANDLE CreateRemoteThread (HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
 
VOID ExitThread (DWORD dwExitCode)
 
BOOL GetExitCodeThread (HANDLE hThread, LPDWORD lpExitCode)
 
WINPR_THREAD * winpr_GetCurrentThread (VOID)
 
HANDLE _GetCurrentThread (VOID)
 
DWORD GetCurrentThreadId (VOID)
 
static void userAPC (LPVOID arg)
 
DWORD QueueUserAPC (PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData)
 
DWORD ResumeThread (HANDLE hThread)
 
DWORD SuspendThread (HANDLE hThread)
 
BOOL SwitchToThread (VOID)
 
BOOL TerminateThread (HANDLE hThread, DWORD dwExitCode)
 
VOID DumpThreadHandles (void)
 

Variables

static WINPR_THREAD mainThread
 
static HANDLE_OPS ops
 
static INIT_ONCE threads_InitOnce = INIT_ONCE_STATIC_INIT
 
static pthread_t mainThreadId
 
static DWORD currentThreadTlsIndex = TLS_OUT_OF_INDEXES
 

Macro Definition Documentation

◆ MAX

#define MAX (   x,
  y 
)    (((x) > (y)) ? (x) : (y))

◆ MIN

#define MIN (   x,
  y 
)    (((x) < (y)) ? (x) : (y))

WinPR: Windows Portable Runtime Process Thread Functions

Copyright 2012 Marc-Andre Moreau marca.nosp@m.ndre.nosp@m..more.nosp@m.au@g.nosp@m.mail..nosp@m.com Copyright 2015 Hewlett-Packard Development Company, L.P. Copyright 2021 David Fort conta.nosp@m.ct@h.nosp@m.arden.nosp@m.ing-.nosp@m.consu.nosp@m.ltin.nosp@m.g.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

◆ run_cond_fkt

#define run_cond_fkt (   fkt,
  cond 
)    run_cond_fkt_(fkt, #fkt, cond)

◆ run_cond_init

#define run_cond_init (   fkt,
  cond,
  arg 
)    run_cond_init_(fkt, #fkt, cond, arg)

◆ run_mutex_fkt

#define run_mutex_fkt (   fkt,
  mux 
)    run_mutex_fkt_(fkt, #fkt, mux)

◆ run_mutex_init

#define run_mutex_init (   fkt,
  mux,
  arg 
)    run_mutex_init_(fkt, #fkt, mux, arg)

◆ TAG

#define TAG   WINPR_TAG("thread")

api-ms-win-core-processthreads-l1-1-1.dll

CreateRemoteThread CreateRemoteThreadEx CreateThread DeleteProcThreadAttributeList ExitThread FlushInstructionCache FlushProcessWriteBuffers GetCurrentThread GetCurrentThreadId GetCurrentThreadStackLimits GetExitCodeThread GetPriorityClass GetStartupInfoW GetThreadContext GetThreadId GetThreadIdealProcessorEx GetThreadPriority GetThreadPriorityBoost GetThreadTimes InitializeProcThreadAttributeList OpenThread OpenThreadToken QueryProcessAffinityUpdateMode QueueUserAPC ResumeThread SetPriorityClass SetThreadContext SetThreadPriority SetThreadPriorityBoost SetThreadStackGuarantee SetThreadToken SuspendThread SwitchToThread TerminateThread UpdateProcThreadAttribute

Function Documentation

◆ _GetCurrentThread()

HANDLE _GetCurrentThread ( VOID  )
Here is the call graph for this function:

◆ cleanup_handle()

void cleanup_handle ( void *  obj)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateRemoteThread()

HANDLE CreateRemoteThread ( HANDLE  hProcess,
LPSECURITY_ATTRIBUTES  lpThreadAttributes,
SIZE_T  dwStackSize,
LPTHREAD_START_ROUTINE  lpStartAddress,
LPVOID  lpParameter,
DWORD  dwCreationFlags,
LPDWORD  lpThreadId 
)
Here is the call graph for this function:

◆ CreateThread()

HANDLE CreateThread ( LPSECURITY_ATTRIBUTES  lpThreadAttributes,
SIZE_T  dwStackSize,
LPTHREAD_START_ROUTINE  lpStartAddress,
LPVOID  lpParameter,
DWORD  dwCreationFlags,
LPDWORD  lpThreadId 
)
Here is the call graph for this function:

◆ dump_thread()

static void dump_thread ( WINPR_THREAD *  thread)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DumpThreadHandles()

VOID DumpThreadHandles ( void  )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ExitThread()

VOID ExitThread ( DWORD  dwExitCode)
Here is the call graph for this function:

◆ GetCurrentThreadId()

DWORD GetCurrentThreadId ( VOID  )

◆ GetExitCodeThread()

BOOL GetExitCodeThread ( HANDLE  hThread,
LPDWORD  lpExitCode 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ initializeThreads()

static BOOL initializeThreads ( PINIT_ONCE  InitOnce,
PVOID  Parameter,
PVOID *  Context 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mux_condition_bundle_init()

static BOOL mux_condition_bundle_init ( mux_condition_bundle bundle)
static
Here is the caller graph for this function:

◆ mux_condition_bundle_lock()

static BOOL mux_condition_bundle_lock ( mux_condition_bundle bundle)
static
Here is the caller graph for this function:

◆ mux_condition_bundle_signal()

static BOOL mux_condition_bundle_signal ( mux_condition_bundle bundle)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mux_condition_bundle_uninit()

static void mux_condition_bundle_uninit ( mux_condition_bundle bundle)
static
Here is the caller graph for this function:

◆ mux_condition_bundle_unlock()

static BOOL mux_condition_bundle_unlock ( mux_condition_bundle bundle)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mux_condition_bundle_wait()

static BOOL mux_condition_bundle_wait ( mux_condition_bundle bundle,
const char *  name 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pthread_mutex_checked_unlock()

static int pthread_mutex_checked_unlock ( pthread_mutex_t *  mutex)
static
Here is the caller graph for this function:

◆ QueueUserAPC()

DWORD QueueUserAPC ( PAPCFUNC  pfnAPC,
HANDLE  hThread,
ULONG_PTR  dwData 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset_event()

static BOOL reset_event ( WINPR_THREAD *  thread)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ResumeThread()

DWORD ResumeThread ( HANDLE  hThread)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_cond_fkt_()

static BOOL run_cond_fkt_ ( int(*)(pthread_cond_t *mux)  fkt,
const char *  name,
pthread_cond_t *  condition 
)
static
Here is the call graph for this function:

◆ run_cond_init_()

static BOOL run_cond_init_ ( int(*)(pthread_cond_t *, const pthread_condattr_t *)  fkt,
const char *  name,
pthread_cond_t *  condition,
const pthread_condattr_t *  conditionattr 
)
static
Here is the call graph for this function:

◆ run_mutex_fkt_()

static BOOL run_mutex_fkt_ ( int(*)(pthread_mutex_t *mux)  fkt,
const char *  name,
pthread_mutex_t *  mutex 
)
static
Here is the call graph for this function:

◆ run_mutex_init_()

static BOOL run_mutex_init_ ( int(*)(pthread_mutex_t *, const pthread_mutexattr_t *)  fkt,
const char *  name,
pthread_mutex_t *  mutex,
const pthread_mutexattr_t *  mutexattr 
)
static
Here is the call graph for this function:

◆ set_event()

static BOOL set_event ( WINPR_THREAD *  thread)
static

TODO: implement thread suspend/resume using pthreads http://stackoverflow.com/questions/3140867/suspend-pthreads-without-using-condition

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetThreadPriority()

BOOL SetThreadPriority ( HANDLE  hThread,
int  nPriority 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ signal_and_wait_for_ready()

static BOOL signal_and_wait_for_ready ( WINPR_THREAD *  thread)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ signal_thread_is_running()

static BOOL signal_thread_is_running ( WINPR_THREAD *  thread)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ signal_thread_ready()

static BOOL signal_thread_ready ( WINPR_THREAD *  thread)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SuspendThread()

DWORD SuspendThread ( HANDLE  hThread)
Here is the call graph for this function:

◆ SwitchToThread()

BOOL SwitchToThread ( VOID  )

Note: on some operating systems sched_yield is a stub returning -1. usleep should at least trigger a context switch if any thread is waiting.

◆ TerminateThread()

BOOL TerminateThread ( HANDLE  hThread,
DWORD  dwExitCode 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ thread_launcher()

static void* thread_launcher ( void *  arg)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ThreadCleanupHandle()

static DWORD ThreadCleanupHandle ( HANDLE  handle)
static
Here is the call graph for this function:

◆ ThreadCloseHandle()

BOOL ThreadCloseHandle ( HANDLE  handle)
static
Here is the call graph for this function:

◆ ThreadGetFd()

static int ThreadGetFd ( HANDLE  handle)
static
Here is the call graph for this function:

◆ ThreadIsHandled()

static BOOL ThreadIsHandled ( HANDLE  handle)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ userAPC()

static void userAPC ( LPVOID  arg)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ winpr_GetCurrentThread()

WINPR_THREAD* winpr_GetCurrentThread ( VOID  )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ winpr_StartThread()

static BOOL winpr_StartThread ( WINPR_THREAD *  thread)
static
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ currentThreadTlsIndex

DWORD currentThreadTlsIndex = TLS_OUT_OF_INDEXES
static

◆ mainThread

WINPR_THREAD mainThread
static

◆ mainThreadId

pthread_t mainThreadId
static

◆ ops

HANDLE_OPS ops
static
Initial value:
NULL }
if availableBytes return NULL
Definition: TPCircularBuffer.h:109
static DWORD ThreadCleanupHandle(HANDLE handle)
Definition: thread.c:312
static BOOL ThreadIsHandled(HANDLE handle)
Definition: thread.c:116
static int ThreadGetFd(HANDLE handle)
Definition: thread.c:121
static BOOL ThreadCloseHandle(HANDLE handle)
Definition: thread.c:772

◆ threads_InitOnce

INIT_ONCE threads_InitOnce = INIT_ONCE_STATIC_INIT
static