<commit_before>//
// Created by permal on 6/25/17.
//

#pragma once

#include <string>
#include <chrono>
#include <cstdint>
#include <map>
#include <mutex>
#include <condition_variable>

#include <thread>

#include <smooth/core/ipc/ITaskEventQueue.h>
#include <smooth/core/ipc/IPolledTaskQueue.h>
#include <smooth/core/ipc/QueueNotification.h>
#include <smooth/core/ipc/Queue.h>
#include <smooth/core/timer/ElapsedTime.h>
#include <atomic>

#ifdef ESP_PLATFORM
    #include <freertos/FreeRTOS.h>
    #include <freertos/task.h>

    #if CONFIG_FREERTOS_HZ < 1000
        #error "Smooth requires CONFIG_FREERTOS_HZ at 1000Hz for proper functionality (spec. timers)"
    #endif
#else
    const int tskNO_AFFINITY = std::numeric_limits<int>::max();
#endif

namespace smooth
{
    namespace core
    {
        /// The Task class encapsulates management and execution of a task.
        /// The intent is to provide the scaffolding needed by nearly every task in an
        /// embedded system; an initialization method, a periodically called tick(),
        /// the ability to receive events in a thread-safe manner.
        class Task
        {
            public:
                virtual ~Task();

                /// Starts the task.
                void start();

                void register_queue_with_task(smooth::core::ipc::ITaskEventQueue *task_queue);
                void register_polled_queue_with_task(smooth::core::ipc::IPolledTaskQueue *polled_queue);

                void unregister_polled_queue_with_task(smooth::core::ipc::IPolledTaskQueue *polled_queue);

                Task(const Task&) = delete;

                Task& operator=(const Task&) = delete;

            protected:

                /// Use this constructor to attach to an existing task, i.e. the main task.
                /// \param task_to_attach_to The task to attach to.
                /// \param priority Task priority
                /// \param tick_interval Tick interval
                Task(uint32_t priority, std::chrono::milliseconds tick_interval);

                /// Use this constructor when creating your own task.
                /// \param task_name Name of task.
                /// \param stack_size Tack size, in bytes.
                /// \param priority Task priority
                /// \param tick_interval Tick interval
                /// \param core Core affinity, defaults to no affinity
                Task(std::string  task_name,
                     uint32_t stack_size,
                     uint32_t priority,
                     std::chrono::milliseconds tick_interval,
                     int core = tskNO_AFFINITY);

                /// The tick() method is where the task shall perform its work.
                /// It is called every 'tick_interval' when there no events available.
                /// Note that if there is a constant stream of event received via a TaskEventQueue,
                /// the tick may be delayed (depending on the tick_interval).
                virtual void tick()
                {
                }

                /// Called once when task is started.
                virtual void init()
                {
                }

                void print_stack_status();

                void disable_status_print()
                { status_print_enabled = false; }

                const std::string name;
            private:

                void exec();

                std::thread worker;
                uint32_t stack_size;
                uint32_t priority;
                std::chrono::milliseconds tick_interval;
                smooth::core::ipc::QueueNotification notification{};
                bool is_attached;
                int affinity;
                std::atomic_bool started{false};
                std::mutex start_mutex{};
                std::mutex queue_mutex{}; //TODO: QQQ Needed or can we use start_mutex?
                std::condition_variable start_condition{};
                smooth::core::timer::ElapsedTime status_report_timer{};
                std::vector<smooth::core::ipc::IPolledTaskQueue *> polled_queues{};
                bool status_print_enabled = true;
        };
    }
}<commit_msg>Solved TODO - cannot use same mutex.<commit_after>//
// Created by permal on 6/25/17.
//

#pragma once

#include <string>
#include <chrono>
#include <cstdint>
#include <map>
#include <mutex>
#include <condition_variable>

#include <thread>

#include <smooth/core/ipc/ITaskEventQueue.h>
#include <smooth/core/ipc/IPolledTaskQueue.h>
#include <smooth/core/ipc/QueueNotification.h>
#include <smooth/core/ipc/Queue.h>
#include <smooth/core/timer/ElapsedTime.h>
#include <atomic>

#ifdef ESP_PLATFORM
    #include <freertos/FreeRTOS.h>
    #include <freertos/task.h>

    #if CONFIG_FREERTOS_HZ < 1000
        #error "Smooth requires CONFIG_FREERTOS_HZ at 1000Hz for proper functionality (spec. timers)"
    #endif
#else
    const int tskNO_AFFINITY = std::numeric_limits<int>::max();
#endif

namespace smooth
{
    namespace core
    {
        /// The Task class encapsulates management and execution of a task.
        /// The intent is to provide the scaffolding needed by nearly every task in an
        /// embedded system; an initialization method, a periodically called tick(),
        /// the ability to receive events in a thread-safe manner.
        class Task
        {
            public:
                virtual ~Task();

                /// Starts the task.
                void start();

                void register_queue_with_task(smooth::core::ipc::ITaskEventQueue *task_queue);
                void register_polled_queue_with_task(smooth::core::ipc::IPolledTaskQueue *polled_queue);

                void unregister_polled_queue_with_task(smooth::core::ipc::IPolledTaskQueue *polled_queue);

                Task(const Task&) = delete;

                Task& operator=(const Task&) = delete;

            protected:

                /// Use this constructor to attach to an existing task, i.e. the main task.
                /// \param task_to_attach_to The task to attach to.
                /// \param priority Task priority
                /// \param tick_interval Tick interval
                Task(uint32_t priority, std::chrono::milliseconds tick_interval);

                /// Use this constructor when creating your own task.
                /// \param task_name Name of task.
                /// \param stack_size Tack size, in bytes.
                /// \param priority Task priority
                /// \param tick_interval Tick interval
                /// \param core Core affinity, defaults to no affinity
                Task(std::string  task_name,
                     uint32_t stack_size,
                     uint32_t priority,
                     std::chrono::milliseconds tick_interval,
                     int core = tskNO_AFFINITY);

                /// The tick() method is where the task shall perform its work.
                /// It is called every 'tick_interval' when there no events available.
                /// Note that if there is a constant stream of event received via a TaskEventQueue,
                /// the tick may be delayed (depending on the tick_interval).
                virtual void tick()
                {
                }

                /// Called once when task is started.
                virtual void init()
                {
                }

                void print_stack_status();

                void disable_status_print()
                { status_print_enabled = false; }

                const std::string name;
            private:

                void exec();

                std::thread worker;
                uint32_t stack_size;
                uint32_t priority;
                std::chrono::milliseconds tick_interval;
                smooth::core::ipc::QueueNotification notification{};
                bool is_attached;
                int affinity;
                std::atomic_bool started{false};
                std::mutex start_mutex{};
                std::mutex queue_mutex{};
                std::condition_variable start_condition{};
                smooth::core::timer::ElapsedTime status_report_timer{};
                std::vector<smooth::core::ipc::IPolledTaskQueue *> polled_queues{};
                bool status_print_enabled = true;
        };
    }
}
{'fraction_non_alphanumeric': 0.08978810008380222, 'fraction_numerical': 0.0055070034718065364, 'mean_word_length': 1.7561860772022435, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, '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': '4922811', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2091, 'n_words': 632}