Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1582 from janvorli/osx-thread-suspension-activation
Browse files Browse the repository at this point in the history
Change PAL_InjectActivation to use pthread_kill
  • Loading branch information
jkotas committed Sep 18, 2015
2 parents b844d2a + f65f177 commit 4486bcf
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
9 changes: 7 additions & 2 deletions src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5447,12 +5447,17 @@ FlushProcessWriteBuffers();

typedef void (*PAL_ActivationFunction)(CONTEXT *context);

PALIMPORT
VOID
PALAPI
PAL_SetActivationFunction(
IN PAL_ActivationFunction pActivationFunction);

PALIMPORT
BOOL
PALAPI
PAL_InjectActivation(
IN HANDLE hThread,
IN PAL_ActivationFunction pActivationFunction
IN HANDLE hThread
);

#define VER_PLATFORM_WIN32_WINDOWS 1
Expand Down
51 changes: 34 additions & 17 deletions src/pal/src/exception/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ int g_signalPipe[2] = { 0, 0 };

DWORD g_dwExternalSignalHandlerThreadId = 0;

// Activation function that gets called when an activation is injected into a thread.
PAL_ActivationFunction g_activationFunction = NULL;

/* public function definitions ************************************************/

/*++
Expand Down Expand Up @@ -565,6 +568,28 @@ static void sigbus_handler(int code, siginfo_t *siginfo, void *context)
}
}

/*++
Function:
PAL_SetActivationFunction
Register an activation function that gets called when an activation is injected
into a thread.
Parameters:
pActivationFunction - activation function
Return value:
None
--*/
PALIMPORT
VOID
PALAPI
PAL_SetActivationFunction(
IN PAL_ActivationFunction pActivationFunction)
{
g_activationFunction = pActivationFunction;
}

/*++
Function :
inject_activation_handler
Expand All @@ -582,8 +607,7 @@ static void inject_activation_handler(int code, siginfo_t *siginfo, void *contex
// Only accept activations from the current process
if (siginfo->si_pid == getpid())
{
PAL_ActivationFunction activation = (PAL_ActivationFunction)siginfo->si_value.sival_ptr;
if (activation != NULL)
if (g_activationFunction != NULL)
{
native_context_t *ucontext = (native_context_t *)context;

Expand All @@ -593,7 +617,7 @@ static void inject_activation_handler(int code, siginfo_t *siginfo, void *contex
&winContext,
CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT);

activation(&winContext);
g_activationFunction(&winContext);

// Activation function may have modified the context, so update it.
CONTEXTToNativeContext(&winContext, ucontext);
Expand All @@ -613,45 +637,38 @@ Parameters :
(no return value)
--*/
void InjectActivationInternal(CorUnix::CPalThread* pThread, PAL_ActivationFunction activationFunction)
void InjectActivationInternal(CorUnix::CPalThread* pThread)
{
#if HAVE_PTHREAD_SIGQUEUE
sigval value;
value.sival_ptr = (void*)activationFunction;
int status = pthread_sigqueue(pThread->GetPThreadSelf(), INJECT_ACTIVATION_SIGNAL, value);
int status = pthread_kill(pThread->GetPThreadSelf(), INJECT_ACTIVATION_SIGNAL);
if (status != 0)
{
// Failure to send the signal is fatal. There are only two cases when sending
// the signal can fail. First, if the signal ID is invalid and second,
// if the thread doesn't exist anymore.
abort();
}
#else
ASSERT("InjectActivationInternal not yet implemented on this platform!");
#endif
}

/*++
Function:
PAL_InjectActivation
Interrupt the specified thread and have it call the activation function passed in
Interrupt the specified thread and have it call an activation function registered
using the PAL_SetActivationFunction
Parameters:
hThread - handle of the target thread
activationFunction - function to call
Return:
TRUE if it succeeded, FALSE otherwise.
--*/
BOOL
PALAPI
PAL_InjectActivation(
IN HANDLE hThread,
IN PAL_ActivationFunction pActivationFunction)
IN HANDLE hThread)
{
PERF_ENTRY(PAL_InjectActivation);
ENTRY("PAL_InjectActivation(hThread=%p, pActivationFunction=%p)\n", hThread, pActivationFunction);
ENTRY("PAL_InjectActivation(hThread=%p)\n", hThread);

CPalThread *pCurrentThread;
CPalThread *pTargetThread;
Expand All @@ -669,7 +686,7 @@ PAL_InjectActivation(

if (palError == NO_ERROR)
{
InjectActivationInternal(pTargetThread, pActivationFunction);
InjectActivationInternal(pTargetThread);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/vm/ceemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ void EEStartupHelper(COINITIEE fFlags)
#endif // PROFILING_SUPPORTED

InitializeExceptionHandling();

//
// Install our global exception filter
//
Expand Down
2 changes: 2 additions & 0 deletions src/vm/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,8 @@ void InitThreadManager()
// Randomize OBJREF_HASH to handle hash collision.
Thread::OBJREF_HASH = OBJREF_TABSIZE - (DbgGetEXETimeStamp()%10);
#endif // _DEBUG

ThreadSuspend::Initialize();
}


Expand Down
10 changes: 9 additions & 1 deletion src/vm/threadsuspend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8370,7 +8370,7 @@ bool Thread::InjectGcSuspension()
hThread = GetThreadHandle();
if (hThread != INVALID_HANDLE_VALUE && hThread != SWITCHOUT_HANDLE_VALUE)
{
::PAL_InjectActivation(hThread, HandleGCSuspensionForInterruptedThread);
::PAL_InjectActivation(hThread);
return true;
}

Expand All @@ -8379,6 +8379,14 @@ bool Thread::InjectGcSuspension()

#endif // FEATURE_HIJACK && PLATFORM_UNIX

// Initialize thread suspension support
void ThreadSuspend::Initialize()
{
#if defined(FEATURE_HIJACK) && defined(PLATFORM_UNIX)
::PAL_SetActivationFunction(HandleGCSuspensionForInterruptedThread);
#endif
}

#ifdef _DEBUG
BOOL Debug_IsLockedViaThreadSuspension()
{
Expand Down
3 changes: 3 additions & 0 deletions src/vm/threadsuspend.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ class ThreadSuspend
static HRESULT SuspendRuntime(ThreadSuspend::SUSPEND_REASON reason);
static void ResumeRuntime(BOOL bFinishedGC, BOOL SuspendSucceded);

// Initialize thread suspension support
static void Initialize();

private:
static CLREvent * g_pGCSuspendEvent;

Expand Down

0 comments on commit 4486bcf

Please sign in to comment.