Skip to content

Commit

Permalink
Revert "Merge pull request #2812 from particle-iot/fix-delay/sc-129892"
Browse files Browse the repository at this point in the history
This reverts commit 99a7914, reversing
changes made to c0c2a55.
  • Loading branch information
sergeuz committed Aug 29, 2024
1 parent 693b11d commit 63b5c0e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 126 deletions.
29 changes: 17 additions & 12 deletions system/inc/active_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ struct ActiveObjectConfiguration
size_t stack_size;

/**
* Default Time to wait for a message in the queue. This governs how often the
* Time to wait for a message in the queue. This governs how often the
* background task is executed.
*/
unsigned default_take_wait;
unsigned take_wait;

/**
* How long to wait to put items in the queue before giving up.
Expand All @@ -80,12 +80,12 @@ struct ActiveObjectConfiguration
static constexpr const char DEFAULT_TASK_NAME[] = "active_object";

public:
ActiveObjectConfiguration(background_task_t task, unsigned default_take_wait, unsigned put_wait_, uint16_t queue_size_,
ActiveObjectConfiguration(background_task_t task, unsigned take_wait_, unsigned put_wait_, uint16_t queue_size_,
size_t stack_size_ = OS_THREAD_STACK_SIZE_DEFAULT, os_thread_prio_t priority = OS_THREAD_PRIORITY_DEFAULT,
const char* name = nullptr) :
background_task(task),
stack_size(stack_size_),
default_take_wait(default_take_wait),
take_wait(take_wait_),
put_wait(put_wait_),
queue_size(queue_size_),
priority(priority) {
Expand Down Expand Up @@ -282,8 +282,8 @@ class ActiveObjectBase


// todo - concurrent queue should be a strategy so it's pluggable without requiring inheritance
virtual bool take(Item& item, int timeout = -1) = 0;
virtual bool put(Item& item, bool dontBlock = false) = 0;
virtual bool take(Item& item)=0;
virtual bool put(Item& item, bool dontBlock = false)=0;

/**
* Static thread entrypoint to run this active object loop.
Expand All @@ -301,7 +301,7 @@ class ActiveObjectBase
started(false) {
}

bool process(int timeout = -1);
bool process();

bool isCurrentThread() {
return os_thread_is_current(_thread);
Expand Down Expand Up @@ -352,12 +352,12 @@ class ActiveObjectQueue : public ActiveObjectBase

os_queue_t queue;

bool take(Item& result, int timeout) override
virtual bool take(Item& result)
{
return !os_queue_take(queue, &result, (timeout < 0) ? configuration.default_take_wait : (unsigned)timeout, nullptr);
return !os_queue_take(queue, &result, configuration.take_wait, nullptr);
}

bool put(Item& item, bool dontBlock) override
virtual bool put(Item& item, bool dontBlock)
{
return !os_queue_put(queue, &item, dontBlock ? 0 : configuration.put_wait, nullptr);
}
Expand Down Expand Up @@ -395,6 +395,11 @@ class ActiveObjectCurrentThreadQueue : public ActiveObjectQueue
setCurrentThread();
run();
}

bool process()
{
return ActiveObjectQueue::process();
}
};


Expand All @@ -411,9 +416,9 @@ class ActiveObjectThreadQueue : public ActiveObjectQueue

// FIXME: some other feature flag?
#if HAL_PLATFORM_SOCKET_IOCTL_NOTIFY
virtual bool take(Item& result, int timeout) override
virtual bool take(Item& result) override
{
auto r = os_thread_wait((timeout < 0) ? configuration.default_take_wait : (unsigned)timeout, nullptr);
auto r = os_thread_wait(configuration.take_wait, nullptr);
if (!os_queue_take(queue, &result, 0, nullptr)) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions system/src/active_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ void ActiveObjectBase::run()
#endif // !HAL_PLATFORM_SOCKET_IOCTL_NOTIFY
}

bool ActiveObjectBase::process(int timeout)
bool ActiveObjectBase::process()
{
bool result = false;
Item item = nullptr;
if (take(item, timeout) && item)
if (take(item) && item)
{
Message& msg = *item;
msg();
Expand Down
2 changes: 1 addition & 1 deletion system/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ namespace particle {
// timeout after attempting to put calls into the application queue, so the system thread does not deadlock (since the application may also
// be trying to put events in the system queue.)
ActiveObjectCurrentThreadQueue ApplicationThread(ActiveObjectConfiguration(app_thread_idle,
0, /* default take time */
0, /* take time */
5000, /* put time */
20 /* queue size */));

Expand Down
38 changes: 5 additions & 33 deletions system/src/system_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,17 @@ void Spark_Idle_Events(bool force_events/*=false*/)
system_shutdown_if_needed();
}

namespace {

// Old implementation for a non-threaded system configuration
void system_delay_pump_no_threading_impl(unsigned long ms, system_tick_t start_millis, bool force_no_background_loop = false)
/*
* @brief This should block for a certain number of milliseconds and also execute spark_wlan_loop
*/
void system_delay_pump(unsigned long ms, bool force_no_background_loop=false)
{
if (ms==0) return;

system_tick_t spark_loop_elapsed_millis = SPARK_LOOP_DELAY_MILLIS;
spark_loop_total_millis += ms;

system_tick_t start_millis = HAL_Timer_Get_Milli_Seconds();
system_tick_t end_micros = HAL_Timer_Get_Micro_Seconds() + (1000*ms);

// Ensure that RTOS vTaskDelay(0) is called to force a reschedule to avoid task starvation in tight delay(1) loops
Expand Down Expand Up @@ -628,35 +629,6 @@ void system_delay_pump_no_threading_impl(unsigned long ms, system_tick_t start_m
}
}

} // namespace

/*
* @brief This should block for a certain number of milliseconds and also execute spark_wlan_loop
*/
void system_delay_pump(unsigned long ms, bool force_no_background_loop)
{
system_tick_t startMillis = HAL_Timer_Get_Milli_Seconds();

if (!PLATFORM_THREADING || !system_thread_get_state(nullptr) || !APPLICATION_THREAD_CURRENT()) {
system_delay_pump_no_threading_impl(ms, startMillis, force_no_background_loop);
return;
}

#if PLATFORM_THREADING
if (!ms || force_no_background_loop) {
HAL_Delay_Milliseconds(ms);
return;
}

system_tick_t timeout;
do {
system_tick_t elapsed = HAL_Timer_Get_Milli_Seconds() - startMillis;
timeout = (ms > elapsed) ? (ms - elapsed) : 0;
ApplicationThread.process(timeout);
} while (timeout > 0);
#endif // PLATFORM_THREADING
}

/**
* On a non threaded platform, or when called from the application thread, then
* run the background loop so that application events are processed.
Expand Down
78 changes: 0 additions & 78 deletions user/tests/wiring/no_fixture_long_running/thread.cpp

This file was deleted.

0 comments on commit 63b5c0e

Please sign in to comment.