mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-04-16 08:42:28 +00:00
btstack_run_loop_posix: use run loop base (code)
This commit is contained in:
parent
9c30c2b3e9
commit
075edf12f9
@ -49,6 +49,7 @@
|
||||
#include "btstack_run_loop_posix.h"
|
||||
|
||||
#include "btstack_run_loop.h"
|
||||
#include "btstack_run_loop_base.h"
|
||||
#include "btstack_util.h"
|
||||
#include "btstack_linked_list.h"
|
||||
#include "btstack_debug.h"
|
||||
@ -60,12 +61,8 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void btstack_run_loop_posix_dump_timer(void);
|
||||
|
||||
// the run loop
|
||||
static btstack_linked_list_t data_sources;
|
||||
static int data_sources_modified;
|
||||
static btstack_linked_list_t timers;
|
||||
|
||||
// start time. tv_usec/tv_nsec = 0
|
||||
#ifdef _POSIX_MONOTONIC_CLOCK
|
||||
@ -81,8 +78,7 @@ static struct timeval init_tv;
|
||||
*/
|
||||
static void btstack_run_loop_posix_add_data_source(btstack_data_source_t *ds){
|
||||
data_sources_modified = 1;
|
||||
// log_info("btstack_run_loop_posix_add_data_source %x with fd %u\n", (int) ds, ds->source.fd);
|
||||
btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
|
||||
btstack_run_loop_base_add_data_source(ds);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,54 +86,7 @@ static void btstack_run_loop_posix_add_data_source(btstack_data_source_t *ds){
|
||||
*/
|
||||
static bool btstack_run_loop_posix_remove_data_source(btstack_data_source_t *ds){
|
||||
data_sources_modified = 1;
|
||||
log_debug("btstack_run_loop_posix_remove_data_source %p\n", ds);
|
||||
return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add timer to run_loop (keep list sorted)
|
||||
*/
|
||||
static void btstack_run_loop_posix_add_timer(btstack_timer_source_t *ts){
|
||||
btstack_linked_item_t *it;
|
||||
for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
|
||||
btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
|
||||
btstack_assert(next != ts);
|
||||
// exit if new timeout before list timeout
|
||||
int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
|
||||
if (delta < 0) break;
|
||||
}
|
||||
ts->item.next = it->next;
|
||||
it->next = (btstack_linked_item_t *) ts;
|
||||
log_debug("Added timer %p at %u\n", ts, ts->timeout);
|
||||
// btstack_run_loop_posix_dump_timer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove timer from run loop
|
||||
*/
|
||||
static bool btstack_run_loop_posix_remove_timer(btstack_timer_source_t *ts){
|
||||
// log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
||||
// btstack_run_loop_posix_dump_timer();
|
||||
return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
|
||||
}
|
||||
|
||||
static void btstack_run_loop_posix_dump_timer(void){
|
||||
#ifdef ENABLE_LOG_INFO
|
||||
btstack_linked_item_t *it;
|
||||
int i = 1;
|
||||
for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
|
||||
btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
|
||||
log_info("timer %u (%p): timeout %u\n", i++, ts, ts->timeout);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void btstack_run_loop_posix_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
|
||||
ds->flags |= callback_types;
|
||||
}
|
||||
|
||||
static void btstack_run_loop_posix_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
|
||||
ds->flags &= ~callback_types;
|
||||
return btstack_run_loop_base_remove_data_source(ds);
|
||||
}
|
||||
|
||||
#ifdef _POSIX_MONOTONIC_CLOCK
|
||||
@ -200,7 +149,6 @@ static void btstack_run_loop_posix_execute(void) {
|
||||
fd_set descriptors_read;
|
||||
fd_set descriptors_write;
|
||||
|
||||
btstack_timer_source_t *ts;
|
||||
btstack_linked_list_iterator_t it;
|
||||
struct timeval * timeout;
|
||||
struct timeval tv;
|
||||
@ -217,7 +165,7 @@ static void btstack_run_loop_posix_execute(void) {
|
||||
FD_ZERO(&descriptors_read);
|
||||
FD_ZERO(&descriptors_write);
|
||||
int highest_fd = -1;
|
||||
btstack_linked_list_iterator_init(&it, &data_sources);
|
||||
btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources);
|
||||
while (btstack_linked_list_iterator_has_next(&it)){
|
||||
btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
|
||||
if (ds->source.fd < 0) continue;
|
||||
@ -236,21 +184,16 @@ static void btstack_run_loop_posix_execute(void) {
|
||||
log_debug("btstack_run_loop_execute adding fd %u for write", ds->source.fd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// get next timeout
|
||||
timeout = NULL;
|
||||
if (timers) {
|
||||
ts = (btstack_timer_source_t *) timers;
|
||||
now_ms = btstack_run_loop_posix_get_time_ms();
|
||||
int32_t delta_ms = btstack_run_loop_base_get_time_until_timeout(now_ms);
|
||||
if (delta_ms >= 0) {
|
||||
timeout = &tv;
|
||||
uint32_t list_timeout = ts->timeout;
|
||||
now_ms = btstack_run_loop_posix_get_time_ms();
|
||||
int32_t delta = btstack_time_delta(list_timeout, now_ms);
|
||||
if (delta < 0){
|
||||
delta = 0;
|
||||
}
|
||||
tv.tv_sec = delta / 1000;
|
||||
tv.tv_usec = (int) (delta - (tv.tv_sec * 1000)) * 1000;
|
||||
log_debug("btstack_run_loop_execute next timeout in %u ms", delta);
|
||||
tv.tv_sec = delta_ms / 1000;
|
||||
tv.tv_usec = (int) (delta_ms - (tv.tv_sec * 1000)) * 1000;
|
||||
log_debug("btstack_run_loop_execute next timeout in %u ms", delta_ms);
|
||||
}
|
||||
|
||||
// wait for ready FDs
|
||||
@ -258,7 +201,7 @@ static void btstack_run_loop_posix_execute(void) {
|
||||
|
||||
|
||||
data_sources_modified = 0;
|
||||
btstack_linked_list_iterator_init(&it, &data_sources);
|
||||
btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources);
|
||||
while (btstack_linked_list_iterator_has_next(&it) && !data_sources_modified){
|
||||
btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
|
||||
log_debug("btstack_run_loop_posix_execute: check ds %p with fd %u\n", ds, ds->source.fd);
|
||||
@ -273,19 +216,10 @@ static void btstack_run_loop_posix_execute(void) {
|
||||
}
|
||||
}
|
||||
log_debug("btstack_run_loop_posix_execute: after ds check\n");
|
||||
|
||||
|
||||
// process timers
|
||||
now_ms = btstack_run_loop_posix_get_time_ms();
|
||||
while (timers) {
|
||||
ts = (btstack_timer_source_t *) timers;
|
||||
int32_t delta = btstack_time_delta(ts->timeout, now_ms);
|
||||
if (delta > 0) break;
|
||||
log_debug("btstack_run_loop_posix_execute: process timer %p\n", ts);
|
||||
|
||||
// remove timer before processing it to allow handler to re-register with run loop
|
||||
btstack_run_loop_posix_remove_timer(ts);
|
||||
ts->process(ts);
|
||||
}
|
||||
btstack_run_loop_base_process_timers(now_ms);
|
||||
}
|
||||
}
|
||||
|
||||
@ -297,8 +231,8 @@ static void btstack_run_loop_posix_set_timer(btstack_timer_source_t *a, uint32_t
|
||||
}
|
||||
|
||||
static void btstack_run_loop_posix_init(void){
|
||||
data_sources = NULL;
|
||||
timers = NULL;
|
||||
btstack_run_loop_base_init();
|
||||
|
||||
#ifdef _POSIX_MONOTONIC_CLOCK
|
||||
clock_gettime(CLOCK_MONOTONIC, &init_ts);
|
||||
init_ts.tv_nsec = 0;
|
||||
@ -314,13 +248,13 @@ static const btstack_run_loop_t btstack_run_loop_posix = {
|
||||
&btstack_run_loop_posix_init,
|
||||
&btstack_run_loop_posix_add_data_source,
|
||||
&btstack_run_loop_posix_remove_data_source,
|
||||
&btstack_run_loop_posix_enable_data_source_callbacks,
|
||||
&btstack_run_loop_posix_disable_data_source_callbacks,
|
||||
&btstack_run_loop_base_enable_data_source_callbacks,
|
||||
&btstack_run_loop_base_disable_data_source_callbacks,
|
||||
&btstack_run_loop_posix_set_timer,
|
||||
&btstack_run_loop_posix_add_timer,
|
||||
&btstack_run_loop_posix_remove_timer,
|
||||
&btstack_run_loop_base_add_timer,
|
||||
&btstack_run_loop_base_remove_timer,
|
||||
&btstack_run_loop_posix_execute,
|
||||
&btstack_run_loop_posix_dump_timer,
|
||||
&btstack_run_loop_base_dump_timer,
|
||||
&btstack_run_loop_posix_get_time_ms,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user