mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-21 03:40:47 +00:00
btstack_run_loop_windows: use run loop base (code)
This commit is contained in:
parent
6cdca94f97
commit
dfbf4e35dd
@ -42,6 +42,7 @@
|
||||
*/
|
||||
|
||||
#include "btstack_run_loop.h"
|
||||
#include "btstack_run_loop_base.h"
|
||||
#include "btstack_run_loop_windows.h"
|
||||
#include "btstack_linked_list.h"
|
||||
#include "btstack_debug.h"
|
||||
@ -55,77 +56,10 @@
|
||||
static void btstack_run_loop_windows_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.
|
||||
// start time.
|
||||
static ULARGE_INTEGER start_time;
|
||||
|
||||
/**
|
||||
* Add data_source to run_loop
|
||||
*/
|
||||
static void btstack_run_loop_windows_add_data_source(btstack_data_source_t *ds){
|
||||
data_sources_modified = 1;
|
||||
// log_info("btstack_run_loop_windows_add_data_source %x with fd %u\n", (int) ds, ds->fd);
|
||||
btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove data_source from run loop
|
||||
*/
|
||||
static bool btstack_run_loop_windows_remove_data_source(btstack_data_source_t *ds){
|
||||
data_sources_modified = 1;
|
||||
// log_info("btstack_run_loop_windows_remove_data_source %x\n", (int) 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_windows_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;
|
||||
if (next == ts){
|
||||
log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
|
||||
return;
|
||||
}
|
||||
// exit if list timeout is after new timeout
|
||||
uint32_t list_timeout = ((btstack_timer_source_t *) it->next)->timeout;
|
||||
int32_t delta = btstack_time_delta(ts->timeout, list_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_windows_dump_timer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove timer from run loop
|
||||
*/
|
||||
static bool btstack_run_loop_windows_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_windows_dump_timer();
|
||||
return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
|
||||
}
|
||||
|
||||
static void btstack_run_loop_windows_dump_timer(void){
|
||||
btstack_linked_item_t *it;
|
||||
int i = 0;
|
||||
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, timeout %u\n", i, ts->timeout);
|
||||
}
|
||||
}
|
||||
|
||||
static void btstack_run_loop_windows_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
|
||||
ds->flags |= callback_types;
|
||||
}
|
||||
|
||||
static void btstack_run_loop_windows_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
|
||||
ds->flags &= ~callback_types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Queries the current time in ms since start
|
||||
@ -154,11 +88,21 @@ static void btstack_run_loop_windows_execute(void) {
|
||||
|
||||
while (true) {
|
||||
|
||||
// process timers
|
||||
uint32_t now_ms = btstack_run_loop_windows_get_time_ms();
|
||||
btstack_run_loop_base_process_timers(now_ms);
|
||||
|
||||
// get next timeout
|
||||
int32_t timeout_ms = btstack_run_loop_base_get_time_until_timeout(now_ms);
|
||||
if (timeout_ms < 0){
|
||||
timeout_ms = INFINITE;
|
||||
}
|
||||
|
||||
// collect handles to wait for
|
||||
HANDLE handles[100];
|
||||
memset(handles, 0, sizeof(handles));
|
||||
int num_handles = 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)){
|
||||
btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
|
||||
if (ds->source.handle == 0) continue;
|
||||
@ -168,18 +112,7 @@ static void btstack_run_loop_windows_execute(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// get next timeout
|
||||
int32_t timeout_ms = INFINITE;
|
||||
if (timers) {
|
||||
ts = (btstack_timer_source_t *) timers;
|
||||
uint32_t now_ms = btstack_run_loop_windows_get_time_ms();
|
||||
timeout_ms = btstack_time_delta(ts->timeout, now_ms);
|
||||
if (timeout_ms < 0){
|
||||
timeout_ms = 0;
|
||||
}
|
||||
log_debug("btstack_run_loop_execute next timeout in %u ms", timeout_ms);
|
||||
}
|
||||
|
||||
// wait for timeout or data source to become ready
|
||||
int res;
|
||||
if (num_handles){
|
||||
// wait for ready Events or timeout
|
||||
@ -193,7 +126,7 @@ static void btstack_run_loop_windows_execute(void) {
|
||||
// process data source
|
||||
if (WAIT_OBJECT_0 <= res && res < (WAIT_OBJECT_0 + num_handles)){
|
||||
void * triggered_handle = handles[res - WAIT_OBJECT_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)){
|
||||
btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
|
||||
log_debug("btstack_run_loop_windows_execute: check ds %p with handle %p\n", ds, ds->source.handle);
|
||||
@ -209,18 +142,6 @@ static void btstack_run_loop_windows_execute(void) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// process timers
|
||||
uint32_t now_ms = btstack_run_loop_windows_get_time_ms();
|
||||
while (timers) {
|
||||
ts = (btstack_timer_source_t *) timers;
|
||||
if (ts->timeout > now_ms) break;
|
||||
log_debug("btstack_run_loop_windows_execute: process timer %p\n", ts);
|
||||
|
||||
// remove timer before processing it to allow handler to re-register with run loop
|
||||
btstack_run_loop_windows_remove_timer(ts);
|
||||
ts->process(ts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -232,8 +153,7 @@ static void btstack_run_loop_windows_set_timer(btstack_timer_source_t *a, uint32
|
||||
}
|
||||
|
||||
static void btstack_run_loop_windows_init(void){
|
||||
data_sources = NULL;
|
||||
timers = NULL;
|
||||
btstack_run_loop_base_init();
|
||||
|
||||
// store start time
|
||||
FILETIME file_time;
|
||||
@ -251,13 +171,13 @@ static const btstack_run_loop_t btstack_run_loop_windows = {
|
||||
&btstack_run_loop_windows_init,
|
||||
&btstack_run_loop_windows_add_data_source,
|
||||
&btstack_run_loop_windows_remove_data_source,
|
||||
&btstack_run_loop_windows_enable_data_source_callbacks,
|
||||
&btstack_run_loop_windows_disable_data_source_callbacks,
|
||||
&btstack_run_loop_base_enable_data_source_callbacks,
|
||||
&btstack_run_loop_base_disable_data_source_callbacks,
|
||||
&btstack_run_loop_windows_set_timer,
|
||||
&btstack_run_loop_windows_add_timer,
|
||||
&btstack_run_loop_windows_remove_timer,
|
||||
&btstack_run_loop_base_add_timer,
|
||||
&btstack_run_loop_base_remove_timer,
|
||||
&btstack_run_loop_windows_execute,
|
||||
&btstack_run_loop_windows_dump_timer,
|
||||
&btstack_run_loop_base_dump_timer,
|
||||
&btstack_run_loop_windows_get_time_ms,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user