<commit_before>// Copyright (c) 2010 Hewlett-Packard Development Company, L.P. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef Q_WS_MAC
#include <pthread.h>
#endif
#include <cstdlib>
#include <QApplication>
#include <QMetaType>
#include <QMutex>
#include <QMutexLocker>
#include <QPair>
#include <QString>
#include <QWaitCondition>
#include "webvfx/logger.h"
#include "webvfx/webvfx.h"
#include "webvfx/effects_impl.h"


namespace WebVfx
{

static bool s_initialized = false;

static Logger* s_logger = 0;
static bool s_ownApp = false;

static QMutex s_initializedMutex;

#ifdef Q_WS_MAC
bool isMainThread();
#else
static pthread_t s_uiThread;
typedef QPair<QMutex*, QWaitCondition*> ThreadSync;

void* uiEventLoop(void* data)
{
    ThreadSync* threadSync = static_cast<ThreadSync*>(data);

    static const char *const empty = "";
    int argc = 1;
    QApplication app(argc, (char**)&empty);
    s_ownApp = true;

    // Signal s_initialized() that app has been created
    {
        QMutexLocker eventLoopLock(threadSync->first);
        threadSync->second->wakeOne();
        threadSync = 0;
    }

    // Enter event loop
    app.exec();
    QCoreApplication::processEvents();
    return 0;
}
#endif<commit_msg>Trap SIGSEGV during shutdown and exit normally.<commit_after>// Copyright (c) 2010 Hewlett-Packard Development Company, L.P. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define WEBVFX_IGNORE_SEGV

#ifndef Q_WS_MAC
#include <pthread.h>
#ifdef WEBVFX_IGNORE_SEGV
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#endif
#endif
#include <cstdlib>
#include <QApplication>
#include <QMetaType>
#include <QMutex>
#include <QMutexLocker>
#include <QPair>
#include <QString>
#include <QWaitCondition>
#include "webvfx/logger.h"
#include "webvfx/webvfx.h"
#include "webvfx/effects_impl.h"


namespace WebVfx
{

static bool s_initialized = false;

static Logger* s_logger = 0;
static bool s_ownApp = false;

static QMutex s_initializedMutex;

#ifdef Q_WS_MAC
bool isMainThread();
#else
static pthread_t s_uiThread;
typedef QPair<QMutex*, QWaitCondition*> ThreadSync;

#ifdef WEBVFX_IGNORE_SEGV
static void handleSEGV(int)
{
    const char msg[] = "WebVfx: Caught SIGSEGV during shutdown. Exiting successfully.\n";
    write(STDERR_FILENO, msg, sizeof(msg));
    tcflush(STDERR_FILENO, TCOFLUSH);
    _exit(EXIT_SUCCESS);
}
#endif

void* uiEventLoop(void* data)
{
    ThreadSync* threadSync = static_cast<ThreadSync*>(data);

    static const char *const empty = "";
    int argc = 1;
    QApplication app(argc, (char**)&empty);
    s_ownApp = true;

    // Signal s_initialized() that app has been created
    {
        QMutexLocker eventLoopLock(threadSync->first);
        threadSync->second->wakeOne();
        threadSync = 0;
    }

    // Enter event loop
    app.exec();
    QCoreApplication::processEvents();

#ifdef WEBVFX_IGNORE_SEGV
    // WebKit threads can outlive QApplication and global Qt internal state.
    // This can lead to crashes on shutdown.
    // As a temporary workaround, trap SIGSEGV and exit normally during shutdown.
    // https://bugs.webkit.org/show_bug.cgi?id=72155
    struct sigaction action;
    action.sa_handler = handlerSEGV;
    sigemptyset(&action.sa_mask);
    action.sa_flags = 0;
    sigaction(SIGSEGV, &action, NULL);
#endif

    return 0;
}
#endif
{'fraction_non_alphanumeric': 0.11066398390342053, 'fraction_numerical': 0.006323656223052601, 'mean_word_length': 4.12518409425626, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 34, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2711225', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1166, 'n_words': 331}