fix timer handling in posix run loop

This commit is contained in:
matthias.ringwald 2009-11-05 22:26:43 +00:00
parent e5304ee6d1
commit c21e623905
2 changed files with 28 additions and 22 deletions

View File

@ -46,7 +46,7 @@
// temp // temp
#include "l2cap.h" #include "l2cap.h"
#define HCI_CONNECTION_TIMEOUT 10000 #define HCI_CONNECTION_TIMEOUT_MS 5000
// the STACK is here // the STACK is here
static hci_stack_t hci_stack; static hci_stack_t hci_stack;
@ -70,13 +70,13 @@ static void hci_connection_timeout_handler(timer_t *timer){
hci_connection_t * connection = linked_item_get_user(&timer->item); hci_connection_t * connection = linked_item_get_user(&timer->item);
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
if (tv.tv_sec > connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT) { if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
// connections might be timed out // connections might be timed out
hci_emit_l2cap_check_timeout(connection); hci_emit_l2cap_check_timeout(connection);
run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT); run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
} else { } else {
// next timeout check at // next timeout check at
timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT; timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
} }
run_loop_add_timer(timer); run_loop_add_timer(timer);
} }
@ -193,7 +193,7 @@ static void event_handler(uint8_t *packet, int size){
conn->flags = 0; conn->flags = 0;
gettimeofday(&conn->timestamp, NULL); gettimeofday(&conn->timestamp, NULL);
run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT); run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
run_loop_add_timer(&conn->timeout); run_loop_add_timer(&conn->timeout);
printf("New connection: handle %u, ", conn->con_handle); printf("New connection: handle %u, ", conn->con_handle);

View File

@ -65,19 +65,22 @@ int posix_remove_data_source(data_source_t *ds){
*/ */
void posix_add_timer(timer_t *ts){ void posix_add_timer(timer_t *ts){
linked_item_t *it; linked_item_t *it;
for (it = (linked_item_t *) &timers; it ; it = it->next){ for (it = (linked_item_t *) &timers; it->next ; it = it->next){
if ( run_loop_timer_compare( (timer_t *) it->next, ts) >= 0) { if (run_loop_timer_compare( (timer_t *) it->next, ts) >= 0) {
ts->item.next = it->next; break;
it->next = (linked_item_t *) ts;
return;
} }
} }
ts->item.next = it->next;
it->next = (linked_item_t *) ts;
// printf("Added timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
// posix_dump_timer();
} }
/** /**
* Remove timer from run loop * Remove timer from run loop
*/ */
int posix_remove_timer(timer_t *ts){ int posix_remove_timer(timer_t *ts){
// printf("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
return linked_list_remove(&timers, (linked_item_t *) ts); return linked_list_remove(&timers, (linked_item_t *) ts);
} }
@ -97,7 +100,8 @@ void posix_execute() {
fd_set descriptors; fd_set descriptors;
data_source_t *ds; data_source_t *ds;
timer_t *ts; timer_t *ts;
struct timeval tv; struct timeval current_tv;
struct timeval next_tv;
struct timeval *timeout; struct timeval *timeout;
while (1) { while (1) {
@ -117,17 +121,19 @@ void posix_execute() {
// pre: 0 <= tv_usec < 1000000 // pre: 0 <= tv_usec < 1000000
timeout = NULL; timeout = NULL;
if (timers) { if (timers) {
gettimeofday(&tv, NULL); gettimeofday(&current_tv, NULL);
ts = (timer_t *) timers; ts = (timer_t *) timers;
tv.tv_sec -= ts->timeout.tv_sec; next_tv.tv_usec = ts->timeout.tv_usec - current_tv.tv_usec;
tv.tv_usec -= ts->timeout.tv_usec; next_tv.tv_sec = ts->timeout.tv_sec - current_tv.tv_sec;
if (tv.tv_usec < 0){ while (next_tv.tv_usec < 0){
tv.tv_usec += 1000000; next_tv.tv_usec += 1000000;
tv.tv_sec--; next_tv.tv_sec--;
} }
if (tv.tv_sec > 0 || (tv.tv_sec == 0 && tv.tv_usec > 0)){ if (next_tv.tv_sec < 0 || (next_tv.tv_sec == 0 && next_tv.tv_usec < 0)){
timeout = &tv; next_tv.tv_sec = 0;
next_tv.tv_usec = 0;
} }
timeout = &next_tv;
} }
// wait for ready FDs // wait for ready FDs
@ -145,10 +151,10 @@ void posix_execute() {
// process timers // process timers
// pre: 0 <= tv_usec < 1000000 // pre: 0 <= tv_usec < 1000000
while (timers) { while (timers) {
gettimeofday(&tv, NULL); gettimeofday(&current_tv, NULL);
ts = (timer_t *) timers; ts = (timer_t *) timers;
if (ts->timeout.tv_sec > tv.tv_sec) break; if (ts->timeout.tv_sec > current_tv.tv_sec) break;
if (ts->timeout.tv_sec == tv.tv_sec && ts->timeout.tv_usec > tv.tv_usec) break; if (ts->timeout.tv_sec == current_tv.tv_sec && ts->timeout.tv_usec > current_tv.tv_usec) break;
run_loop_remove_timer(ts); run_loop_remove_timer(ts);
ts->process(ts); ts->process(ts);
} }