summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-07-08 14:39:23 -0400
committerirungentoo <irungentoo@gmail.com>2014-07-08 14:39:23 -0400
commitbd4c142e38afd7c03a23721a3e8261e15aacf973 (patch)
treeae5a725a3c8ed15c6f4361b929a674d487c516a6
parent7ce9816e40cdfc448b6306839948bceffb38dd32 (diff)
Fixed some timer related issues.
-rw-r--r--toxav/msi.c64
1 files changed, 47 insertions, 17 deletions
diff --git a/toxav/msi.c b/toxav/msi.c
index 1219c64a..88f62ebd 100644
--- a/toxav/msi.c
+++ b/toxav/msi.c
@@ -538,7 +538,7 @@ GENERIC_SETTER_DEFINITION ( callid )
538 538
539 539
540typedef struct _Timer { 540typedef struct _Timer {
541 void *(*func)(void *, int); 541 void *(*func)(void *);
542 void *func_arg1; 542 void *func_arg1;
543 int func_arg2; 543 int func_arg2;
544 uint64_t timeout; 544 uint64_t timeout;
@@ -558,6 +558,10 @@ typedef struct _TimerHandler {
558 558
559} TimerHandler; 559} TimerHandler;
560 560
561struct timer_function_args {
562 void *arg1;
563 int arg2;
564};
561 565
562/** 566/**
563 * @brief Allocate timer in array 567 * @brief Allocate timer in array
@@ -568,8 +572,9 @@ typedef struct _TimerHandler {
568 * @param timeout Timeout in ms 572 * @param timeout Timeout in ms
569 * @return int 573 * @return int
570 */ 574 */
571int timer_alloc ( TimerHandler *timers_container, void *(func)(void *, int), void *arg1, int arg2, unsigned timeout) 575int timer_alloc ( TimerHandler *timers_container, void *(func)(void *), void *arg1, int arg2, unsigned timeout)
572{ 576{
577 static int timer_id;
573 pthread_mutex_lock(&timers_container->mutex); 578 pthread_mutex_lock(&timers_container->mutex);
574 579
575 int i = 0; 580 int i = 0;
@@ -596,7 +601,8 @@ int timer_alloc ( TimerHandler *timers_container, void *(func)(void *, int), voi
596 timer->func_arg1 = arg1; 601 timer->func_arg1 = arg1;
597 timer->func_arg2 = arg2; 602 timer->func_arg2 = arg2;
598 timer->timeout = timeout + current_time_monotonic(); /* In ms */ 603 timer->timeout = timeout + current_time_monotonic(); /* In ms */
599 timer->idx = i; 604 ++timer_id;
605 timer->idx = timer_id;
600 606
601 /* reorder */ 607 /* reorder */
602 if (i) { 608 if (i) {
@@ -612,15 +618,15 @@ int timer_alloc ( TimerHandler *timers_container, void *(func)(void *, int), voi
612 pthread_mutex_unlock(&timers_container->mutex); 618 pthread_mutex_unlock(&timers_container->mutex);
613 619
614 LOGGER_DEBUG("Allocated timer index: %d timeout: %d, current size: %d", i, timeout, timers_container->size); 620 LOGGER_DEBUG("Allocated timer index: %d timeout: %d, current size: %d", i, timeout, timers_container->size);
615 return i; 621 return timer->idx;
616} 622}
617 623
618/** 624/**
619 * @brief Remove timer from array 625 * @brief Remove timer from array
620 * 626 *
621 * @param timers_container handler 627 * @param timers_container handler
622 * @param idx index 628 * @param idx timer id
623 * @param idx lock_mutex (does the mutex need to be locked) 629 * @param lock_mutex (does the mutex need to be locked)
624 * @return int 630 * @return int
625 */ 631 */
626int timer_release ( TimerHandler *timers_container, int idx , int lock_mutex) 632int timer_release ( TimerHandler *timers_container, int idx , int lock_mutex)
@@ -630,19 +636,28 @@ int timer_release ( TimerHandler *timers_container, int idx , int lock_mutex)
630 636
631 Timer **timed_events = timers_container->timers; 637 Timer **timed_events = timers_container->timers;
632 638
633 if (!timed_events[idx]) { 639 int i, res = -1;
634 LOGGER_WARNING("No event under index: %d", idx); 640
641 for (i = 0; i < timers_container->max_capacity; ++i) {
642 if (timed_events[i] && timed_events[i]->idx == idx) {
643 res = i;
644 break;
645 }
646 }
647
648 if (res == -1) {
649 LOGGER_WARNING("No event with id: %d", idx);
635 650
636 if (lock_mutex) pthread_mutex_unlock(&timers_container->mutex); 651 if (lock_mutex) pthread_mutex_unlock(&timers_container->mutex);
637 652
638 return -1; 653 return -1;
639 } 654 }
640 655
641 free(timed_events[idx]); 656 free(timed_events[res]);
642 657
643 timed_events[idx] = NULL; 658 timed_events[res] = NULL;
644 659
645 int i = idx + 1; 660 i = res + 1;
646 661
647 for (; i < timers_container->max_capacity && timed_events[i]; i ++) { 662 for (; i < timers_container->max_capacity && timed_events[i]; i ++) {
648 timed_events[i - 1] = timed_events[i]; 663 timed_events[i - 1] = timed_events[i];
@@ -677,10 +692,21 @@ void *timer_poll( void *arg )
677 uint64_t time = current_time_monotonic(); 692 uint64_t time = current_time_monotonic();
678 693
679 if ( handler->timers[0] && handler->timers[0]->timeout < time ) { 694 if ( handler->timers[0] && handler->timers[0]->timeout < time ) {
680 handler->timers[0]->func(handler->timers[0]->func_arg1, handler->timers[0]->func_arg2); 695 pthread_t _tid;
681 LOGGER_DEBUG("Exectued timer assigned at: %d", handler->timers[0]->timeout); 696
697 struct timer_function_args *args = malloc(sizeof(struct timer_function_args));
698 args->arg1 = handler->timers[0]->func_arg1;
699 args->arg2 = handler->timers[0]->func_arg2;
700
701 if ( 0 != pthread_create(&_tid, NULL, handler->timers[0]->func, args) ||
702 0 != pthread_detach(_tid) ) {
703 LOGGER_ERROR("Failed to execute timer at: %d!", handler->timers[0]->timeout);
704 free(args);
705 } else {
706 LOGGER_DEBUG("Exectued timer assigned at: %d", handler->timers[0]->timeout);
707 }
682 708
683 timer_release(handler, 0, 0); 709 timer_release(handler, handler->timers[0]->idx, 0);
684 } 710 }
685 711
686 } 712 }
@@ -1140,14 +1166,15 @@ int terminate_call ( MSISession *session, MSICall *call )
1140 * @param arg Control session 1166 * @param arg Control session
1141 * @return void* 1167 * @return void*
1142 */ 1168 */
1143void *handle_timeout ( void *arg1, int arg2 ) 1169void *handle_timeout ( void *arg )
1144{ 1170{
1145 /* TODO: Cancel might not arrive there; set up 1171 /* TODO: Cancel might not arrive there; set up
1146 * timers on these cancels and terminate call on 1172 * timers on these cancels and terminate call on
1147 * their timeout 1173 * their timeout
1148 */ 1174 */
1149 int call_index = arg2; 1175 struct timer_function_args *args = arg;
1150 MSISession *session = arg1; 1176 int call_index = args->arg2;
1177 MSISession *session = args->arg1;
1151 MSICall *_call = session->calls[call_index]; 1178 MSICall *_call = session->calls[call_index];
1152 1179
1153 if (_call) { 1180 if (_call) {
@@ -1164,6 +1191,9 @@ void *handle_timeout ( void *arg1, int arg2 )
1164 msi_cancel ( _call->session, _call->call_idx, _call->peers [0], "Request timed out" ); 1191 msi_cancel ( _call->session, _call->call_idx, _call->peers [0], "Request timed out" );
1165 /*terminate_call(_call->session, _call);*/ 1192 /*terminate_call(_call->session, _call);*/
1166 } 1193 }
1194
1195 free(arg);
1196 pthread_exit(NULL);
1167} 1197}
1168 1198
1169 1199