Skip to content

Commit

Permalink
Merge pull request #15730 from pkerling/fix-posix-signal
Browse files Browse the repository at this point in the history
Handle signals by setting atomic flag instead of  pop-up thread
  • Loading branch information
pkerling committed Mar 18, 2019
2 parents cb9057e + 6d83845 commit c7bac97
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 28 deletions.
9 changes: 9 additions & 0 deletions xbmc/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
#include "XHandle.h"
#include "XTimeUtils.h"
#include "platform/posix/filesystem/PosixDirectory.h"
#include "platform/posix/PlatformPosix.h"
#endif

#if defined(TARGET_ANDROID)
Expand Down Expand Up @@ -4106,6 +4107,14 @@ void CApplication::ProcessSlow()
CheckShutdown();
}

#if defined(TARGET_POSIX)
if (CPlatformPosix::TestShutdownFlag())
{
CLog::Log(LOGNOTICE, "Shutting down due to POSIX signal");
CApplicationMessenger::GetInstance().PostMsg(TMSG_SHUTDOWN);
}
#endif

// check if we should restart the player
CheckDelayedPlayerRestart();

Expand Down
3 changes: 2 additions & 1 deletion xbmc/platform/darwin/PlatformDarwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ CPlatformDarwin::~CPlatformDarwin()

void CPlatformDarwin::Init()
{
setenv("SSL_CERT_FILE", CSpecialProtocol::TranslatePath("special://xbmc/system/certs/cacert.pem").c_str(), 0);
CPlatformPosix::Init();
setenv("SSL_CERT_FILE", CSpecialProtocol::TranslatePath("special://xbmc/system/certs/cacert.pem").c_str(), 0);
}
4 changes: 2 additions & 2 deletions xbmc/platform/darwin/PlatformDarwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#pragma once

#include "platform/Platform.h"
#include "platform/posix/PlatformPosix.h"

class CPlatformDarwin : public CPlatform
class CPlatformDarwin : public CPlatformPosix
{
public:
/**\brief C'tor */
Expand Down
3 changes: 2 additions & 1 deletion xbmc/platform/overrides/android/PlatformAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ CPlatformAndroid::~CPlatformAndroid()

void CPlatformAndroid::Init()
{
setenv("SSL_CERT_FILE", CSpecialProtocol::TranslatePath("special://xbmc/system/certs/cacert.pem").c_str(), 1);
CPlatformPosix::Init();
setenv("SSL_CERT_FILE", CSpecialProtocol::TranslatePath("special://xbmc/system/certs/cacert.pem").c_str(), 1);
}
4 changes: 2 additions & 2 deletions xbmc/platform/overrides/android/PlatformAndroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#pragma once

#include "platform/Platform.h"
#include "platform/posix/PlatformPosix.h"

class CPlatformAndroid : public CPlatform
class CPlatformAndroid : public CPlatformPosix
{
public:
/**\brief C'tor */
Expand Down
14 changes: 14 additions & 0 deletions xbmc/platform/overrides/freebsd/PlatformPosix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) 2019 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "platform/posix/PlatformPosix.h"

CPlatform* CPlatform::CreateInstance()
{
return new CPlatformPosix();
}
14 changes: 14 additions & 0 deletions xbmc/platform/overrides/linux/PlatformPosix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) 2019 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "platform/posix/PlatformPosix.h"

CPlatform* CPlatform::CreateInstance()
{
return new CPlatformPosix();
}
5 changes: 4 additions & 1 deletion xbmc/platform/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
set(SOURCES Filesystem.cpp
MessagePrinter.cpp)
MessagePrinter.cpp
PlatformPosix.cpp)

set(HEADERS PlatformPosix.h)

core_add_library(platform_posix)
28 changes: 28 additions & 0 deletions xbmc/platform/posix/PlatformPosix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2019 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "PlatformPosix.h"

std::atomic_flag CPlatformPosix::ms_signalFlag;

void CPlatformPosix::Init()
{
// Initialize to "set" state
ms_signalFlag.test_and_set();
}

bool CPlatformPosix::TestShutdownFlag()
{
// Keep set, return true when it was cleared before
return !ms_signalFlag.test_and_set();
}

void CPlatformPosix::RequestShutdown()
{
ms_signalFlag.clear();
}
25 changes: 25 additions & 0 deletions xbmc/platform/posix/PlatformPosix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2019 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#pragma once

#include <atomic>

#include "platform/Platform.h"

class CPlatformPosix : public CPlatform
{
public:
void Init() override;

static bool TestShutdownFlag();
static void RequestShutdown();

private:
static std::atomic_flag ms_signalFlag;
};
26 changes: 5 additions & 21 deletions xbmc/platform/posix/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* See LICENSES/README.md for more information.
*/

#include <sys/resource.h>
#include <signal.h>
#include <sys/resource.h>

#include <cstring>

Expand All @@ -25,6 +25,7 @@
#include "PlayListPlayer.h"
#include "platform/MessagePrinter.h"
#include "platform/xbmc.h"
#include "PlatformPosix.h"
#include "utils/log.h"

#ifdef HAS_LIRC
Expand All @@ -36,31 +37,14 @@
namespace
{

class CPOSIXSignalHandleThread : public CThread
{
public:
CPOSIXSignalHandleThread()
: CThread("POSIX signal handler")
{}
protected:
void Process() override
{
CMessagePrinter::DisplayMessage("Exiting application");
KODI::MESSAGING::CApplicationMessenger::GetInstance().PostMsg(TMSG_QUIT);
}
};

extern "C"
{

void XBMC_POSIX_HandleSignal(int sig)
{
// Spawn handling thread: the current thread that this signal was catched on
// might have been interrupted in a call to PostMsg() while holding a lock
// there, which would lead to a deadlock if PostMsg() was called directly here
// as PostMsg() is not supposed to be reentrant
auto thread = new CPOSIXSignalHandleThread;
thread->Create(true);
// Setting an atomic flag is one of the only useful things that is permitted by POSIX
// in signal handlers
CPlatformPosix::RequestShutdown();
}

}
Expand Down

0 comments on commit c7bac97

Please sign in to comment.