<commit_before>// Copyright 2010-2012 RethinkDB, all rights reserved.
#ifndef ARCH_RUNTIME_THREAD_POOL_HPP_
#define ARCH_RUNTIME_THREAD_POOL_HPP_

#include <pthread.h>

#include <map>
#include <string>

#include "config/args.hpp"
#include "arch/runtime/event_queue.hpp"
#include "arch/runtime/system_event.hpp"
#include "arch/runtime/message_hub.hpp"
#include "arch/runtime/coroutines.hpp"
#include "arch/io/blocker_pool.hpp"
#include "arch/io/timer_provider.hpp"
#include "arch/timer.hpp"

class linux_thread_t;
class os_signal_cond_t;

/* coro_runtime_t is borrowed from coroutines.hpp.  Please only
construct one coro_runtime_t per thread. Coroutines can only be used
when a coro_runtime_t exists. It exists to take advantage of RAII. */

struct coro_runtime_t {
    coro_runtime_t();
    ~coro_runtime_t();

#ifndef NDEBUG
    void get_coroutine_counts(std::map<std::string, size_t> *dest);
#endif
};


/* A thread pool represents a group of threads, each of which is associated with an
event queue. There is one thread pool per server. It is responsible for starting up
and shutting down the threads and event queues. */

class linux_thread_pool_t {
public:
    linux_thread_pool_t(int worker_threads, bool do_set_affinity);

    // When the process receives a SIGINT or SIGTERM, interrupt_message will be delivered to the
    // same thread that initial_message was delivered to, and interrupt_message will be set to
    // NULL. If you want to receive notification of further SIGINTs or SIGTERMs, you must call
    // set_interrupt_message() again. Returns the previous value of interrupt_message.
    static MUST_USE os_signal_cond_t *set_interrupt_message(os_signal_cond_t *interrupt_message);

    // Blocks while threads are working. Only returns after shutdown() is called. initial_message
    // is a thread message that will be delivered to thread zero after all of the event queues
    // have been started; it is used to start the server's activity.
    void run_thread_pool(linux_thread_message_t *initial_message);

#ifndef NDEBUG
    void enable_coroutine_summary();
#endif

    // Shut down all the threads. Can be called from any thread.
    void shutdown_thread_pool();

    ~linux_thread_pool_t();

private:
#ifndef NDEBUG
    bool coroutine_summary;
#endif

    static void *start_thread(void*);
    static void interrupt_handler(int signo, siginfo_t *siginfo, void *);
    static void fatal_signal_handler(int, siginfo_t *, void *) NORETURN;
    spinlock_t interrupt_message_lock;
    os_signal_cond_t *interrupt_message;

    // Used to signal the main thread for shutdown
    volatile bool do_shutdown;
    pthread_cond_t shutdown_cond;
    pthread_mutex_t shutdown_cond_mutex;
<commit_msg>Add comment to fatal_signal_handler method<commit_after>// Copyright 2010-2012 RethinkDB, all rights reserved.
#ifndef ARCH_RUNTIME_THREAD_POOL_HPP_
#define ARCH_RUNTIME_THREAD_POOL_HPP_

#include <pthread.h>

#include <map>
#include <string>

#include "config/args.hpp"
#include "arch/runtime/event_queue.hpp"
#include "arch/runtime/system_event.hpp"
#include "arch/runtime/message_hub.hpp"
#include "arch/runtime/coroutines.hpp"
#include "arch/io/blocker_pool.hpp"
#include "arch/io/timer_provider.hpp"
#include "arch/timer.hpp"

class linux_thread_t;
class os_signal_cond_t;

/* coro_runtime_t is borrowed from coroutines.hpp.  Please only
construct one coro_runtime_t per thread. Coroutines can only be used
when a coro_runtime_t exists. It exists to take advantage of RAII. */

struct coro_runtime_t {
    coro_runtime_t();
    ~coro_runtime_t();

#ifndef NDEBUG
    void get_coroutine_counts(std::map<std::string, size_t> *dest);
#endif
};


/* A thread pool represents a group of threads, each of which is associated with an
event queue. There is one thread pool per server. It is responsible for starting up
and shutting down the threads and event queues. */

class linux_thread_pool_t {
public:
    linux_thread_pool_t(int worker_threads, bool do_set_affinity);

    // When the process receives a SIGINT or SIGTERM, interrupt_message will be delivered to the
    // same thread that initial_message was delivered to, and interrupt_message will be set to
    // NULL. If you want to receive notification of further SIGINTs or SIGTERMs, you must call
    // set_interrupt_message() again. Returns the previous value of interrupt_message.
    static MUST_USE os_signal_cond_t *set_interrupt_message(os_signal_cond_t *interrupt_message);

    // Blocks while threads are working. Only returns after shutdown() is called. initial_message
    // is a thread message that will be delivered to thread zero after all of the event queues
    // have been started; it is used to start the server's activity.
    void run_thread_pool(linux_thread_message_t *initial_message);

#ifndef NDEBUG
    void enable_coroutine_summary();
#endif

    // Shut down all the threads. Can be called from any thread.
    void shutdown_thread_pool();

    ~linux_thread_pool_t();

private:
#ifndef NDEBUG
    bool coroutine_summary;
#endif

    static void *start_thread(void*);
    static void interrupt_handler(int signo, siginfo_t *siginfo, void *);
    // Currently handles SIGSEGV and SIGBUS signals.
    static void fatal_signal_handler(int, siginfo_t *, void *) NORETURN;
    spinlock_t interrupt_message_lock;
    os_signal_cond_t *interrupt_message;

    // Used to signal the main thread for shutdown
    volatile bool do_shutdown;
    pthread_cond_t shutdown_cond;
    pthread_mutex_t shutdown_cond_mutex;

{'fraction_non_alphanumeric': 0.07269760696156635, 'fraction_numerical': 0.0029006526468455403, 'mean_word_length': 4.5225225225225225, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7227167', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1665, 'n_words': 568}