toxcore/toxav/event.c

382 lines
9.2 KiB
C
Raw Normal View History

/** event.c
2014-02-17 09:01:30 +08:00
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
2014-02-17 09:01:30 +08:00
*
2014-02-16 05:36:15 +08:00
* Report bugs/suggestions at #tox-dev @ freenode.net:6667
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdlib.h>
2014-05-05 08:33:08 +08:00
#include "../toxcore/network.h" /* current_time_monotonic() */
#include "event.h"
#define _GNU_SOURCE
#include <assert.h>
#include <unistd.h>
#include <stddef.h>
#include <inttypes.h>
#include <pthread.h>
2014-02-01 19:52:48 +08:00
#include <stdio.h>
2014-02-01 19:52:48 +08:00
#define RUN_IN_THREAD(func, args) { pthread_t _tid; \
pthread_create(&_tid, NULL, func, args); assert( pthread_detach(_tid) == 0 ); }
2014-02-01 19:52:48 +08:00
#define LOCK(event_handler) pthread_mutex_lock (&event_handler->mutex)
#define UNLOCK(event_handler) pthread_mutex_unlock(&event_handler->mutex)
#define FREQUENCY 10000
#define inline__ inline __attribute__((always_inline))
typedef struct _EventContainer {
2014-02-17 09:01:30 +08:00
void *(*func)(void *);
void *func_args;
2014-02-01 19:52:48 +08:00
unsigned timeout;
long long id;
2014-02-17 09:01:30 +08:00
} EventContainer;
typedef struct _EventHandler {
2014-02-17 09:01:30 +08:00
EventContainer *timed_events;
2014-02-01 19:52:48 +08:00
size_t timed_events_count;
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
int running;
2014-02-17 09:01:30 +08:00
pthread_mutex_t mutex;
2014-02-17 09:01:30 +08:00
} EventHandler;
2014-06-15 22:36:57 +08:00
int throw_event( void *(func)(void *), void *arg );
int reset_timer_event ( int id, uint32_t timeout );
2014-06-15 22:36:57 +08:00
int throw_timer_event ( void *(func)(void *), void *arg, unsigned timeout);
int cancel_timer_event ( int id );
int execute_timer_event ( int id );
2014-02-17 09:01:30 +08:00
struct _Event event = {
throw_event,
/* reset_timer_event */ NULL,
throw_timer_event,
cancel_timer_event,
/*execute_timer_event*/ NULL
};
/*
* Random functions used by this file
*/
2014-02-17 09:01:30 +08:00
void clear_events (EventContainer **event_container, size_t *counter)
{
free(*event_container );
2014-02-17 09:01:30 +08:00
*event_container = NULL;
*counter = 0;
}
2014-02-17 09:01:30 +08:00
int pop_id ( EventContainer **event_container, size_t *counter, int id )
{
if ( !*event_container || !*counter || !id )
return -1;
2014-02-17 09:01:30 +08:00
EventContainer *_it = *event_container;
int i;
2014-02-17 09:01:30 +08:00
for ( i = *counter; i; -- i ) {
if ( _it->id == id ) { /* Hit! */
break;
}
2014-02-17 09:01:30 +08:00
++_it;
}
2014-02-17 09:01:30 +08:00
if ( i ) {
2014-02-17 09:01:30 +08:00
for ( ; i; -- i ) {
*_it = *(_it + 1);
++_it;
}
-- (*counter );
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
if ( !(*counter)) { /* Free and set to NULL */
free(*event_container);
*event_container = NULL;
2014-02-17 09:01:30 +08:00
} else {
void *_result = realloc(*event_container, sizeof(EventContainer) * (*counter )); /* resize */
if ( _result != NULL ) {
*event_container = _result;
return 0;
} else {
2014-02-01 19:52:48 +08:00
/* Not sure what would happen next so abort execution.
*/
fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
abort();
2014-02-17 09:01:30 +08:00
return -1;
2014-02-01 19:52:48 +08:00
}
}
}
2014-02-17 09:01:30 +08:00
/* not found here */
2014-02-17 09:01:30 +08:00
return -1;
}
2014-06-15 22:36:57 +08:00
void push_event ( EventContainer **container, size_t *counter, void *(func)(void *), void *arg )
{
2014-02-17 09:01:30 +08:00
EventContainer *_new = realloc((*container ), sizeof(EventContainer) * ((*counter ) + 1));
if ( _new == NULL ) {
2014-02-01 19:52:48 +08:00
/* Not sure what would happen next so abort execution.
* TODO: This could notice the calling function
* about realloc failing.
*/
fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
abort();
}
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
_new[*counter].func = func;
_new[*counter].func_args = arg;
_new[*counter].timeout = 0;
_new[*counter].id = 0;
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
(*container) = _new;
2014-02-17 09:01:30 +08:00
(*counter )++;
}
2014-02-17 09:01:30 +08:00
void reorder_events ( size_t counter, EventContainer *container, unsigned timeout )
{
if ( counter > 1 ) {
2014-02-17 09:01:30 +08:00
int i = counter - 1;
2014-02-17 09:01:30 +08:00
/* start from behind excluding last added member */
2014-02-17 09:01:30 +08:00
EventContainer *_it = &container[i - 1];
EventContainer _last_added = container[i];
2014-02-17 09:01:30 +08:00
for ( ; i; --i ) {
2014-02-17 09:01:30 +08:00
if ( _it->timeout > timeout ) {
*(_it + 1) = *_it;
2014-02-17 09:01:30 +08:00
*_it = _last_added;
-- _it;
}
}
2014-02-17 09:01:30 +08:00
}
}
/* ============================================= */
/* main poll for event execution */
2014-02-17 09:01:30 +08:00
void *event_poll( void *arg )
{
2014-02-17 09:01:30 +08:00
EventHandler *_event_handler = arg;
while ( _event_handler->running ) {
2014-02-01 19:52:48 +08:00
LOCK( _event_handler );
2014-02-17 09:01:30 +08:00
if ( _event_handler->timed_events ) {
2014-05-05 08:33:08 +08:00
uint32_t _time = ((uint32_t)current_time_monotonic());
2014-02-17 09:01:30 +08:00
if ( _event_handler->timed_events[0].timeout < _time ) {
2014-02-17 09:01:30 +08:00
RUN_IN_THREAD ( _event_handler->timed_events[0].func,
_event_handler->timed_events[0].func_args );
2014-02-17 09:01:30 +08:00
pop_id(&_event_handler->timed_events,
&_event_handler->timed_events_count,
2014-02-01 19:52:48 +08:00
_event_handler->timed_events[0].id);
2014-02-17 09:01:30 +08:00
}
2014-02-17 09:01:30 +08:00
}
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
UNLOCK( _event_handler );
2014-02-17 09:01:30 +08:00
usleep(FREQUENCY);
}
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
LOCK( _event_handler );
2014-02-17 09:01:30 +08:00
clear_events(&_event_handler->timed_events, &_event_handler->timed_events_count);
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
UNLOCK( _event_handler );
2014-02-17 09:01:30 +08:00
_event_handler->running = -1;
pthread_exit(NULL);
}
2014-06-15 22:36:57 +08:00
int throw_event( void *(func)(void *), void *arg )
{
pthread_t _tid;
int _rc =
2014-02-17 09:01:30 +08:00
pthread_create(&_tid, NULL, func, arg );
return (0 != _rc ) ? _rc : pthread_detach(_tid);
}
EventHandler event_handler;
/* Place and order array of timers */
2014-06-15 22:36:57 +08:00
int throw_timer_event ( void *(func)(void *), void *arg, unsigned timeout)
{
static int _unique_id = 1;
2014-02-17 09:01:30 +08:00
push_event(&event_handler.timed_events, &(event_handler.timed_events_count), func, arg );
2014-02-17 09:01:30 +08:00
size_t _counter = event_handler.timed_events_count;
2014-02-17 09:01:30 +08:00
2014-05-05 08:33:08 +08:00
event_handler.timed_events[_counter - 1].timeout = timeout + ((uint32_t)current_time_monotonic());
2014-02-17 09:01:30 +08:00
event_handler.timed_events[_counter - 1].id = _unique_id;
++_unique_id;
/* reorder */
2014-02-17 09:01:30 +08:00
reorder_events(_counter, event_handler.timed_events, timeout );
2014-02-17 09:01:30 +08:00
return _unique_id - 1;
}
int execute_timer_event ( int id )
{
int _status;
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
LOCK((&event_handler));
2014-02-17 09:01:30 +08:00
EventContainer *_it = event_handler.timed_events;
int _i = event_handler.timed_events_count;
2014-02-17 09:01:30 +08:00
/* Find it and execute */
for ( ; _i; _i-- ) {
if ( _it->id == id ) {
RUN_IN_THREAD ( _it->func, _it->func_args );
break;
}
2014-02-17 09:01:30 +08:00
++_it;
}
2014-02-17 09:01:30 +08:00
/* Now remove it from the queue */
2014-02-17 09:01:30 +08:00
if ( _i ) {
2014-02-17 09:01:30 +08:00
for ( ; _i; -- _i ) {
*_it = *(_it + 1);
++_it;
}
-- event_handler.timed_events_count;
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
if ( !event_handler.timed_events_count ) { /* Free and set to null */
free(event_handler.timed_events);
event_handler.timed_events = NULL;
2014-02-17 09:01:30 +08:00
} else {
void *_result = realloc(event_handler.timed_events,
sizeof(EventContainer) * event_handler.timed_events_count); /* resize */
if ( _result != NULL ) {
event_handler.timed_events = _result;
} else {
2014-02-01 19:52:48 +08:00
/* Not sure what would happen next so abort execution.
*/
fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
abort();
2014-02-17 09:01:30 +08:00
return -1;
}
2014-02-01 19:52:48 +08:00
}
2014-02-17 09:01:30 +08:00
_status = 0;
2014-02-17 09:01:30 +08:00
} else _status = -1;
2014-02-01 19:52:48 +08:00
UNLOCK((&event_handler));
2014-02-17 09:01:30 +08:00
return _status;
}
int reset_timer_event ( int id, uint32_t timeout )
{
int _status;
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
LOCK((&event_handler));
2014-02-17 09:01:30 +08:00
EventContainer *_it = event_handler.timed_events;
int _i = event_handler.timed_events_count;
2014-02-17 09:01:30 +08:00
/* Find it and change */
for ( ; _i; _i-- ) {
if ( _it->id == id ) {
2014-05-05 08:33:08 +08:00
_it->timeout = timeout + ((uint32_t)current_time_monotonic());
break;
}
2014-02-17 09:01:30 +08:00
++_it;
}
2014-02-17 09:01:30 +08:00
_status = _i ? -1 : 0;
2014-02-17 09:01:30 +08:00
2014-02-01 19:52:48 +08:00
UNLOCK((&event_handler));
2014-02-17 09:01:30 +08:00
return _status;
}
/* Remove timer from array */
inline__ int cancel_timer_event ( int id )
{
return pop_id (&event_handler.timed_events, &event_handler.timed_events_count, id );
}
/* Initialization and termination of event polls
* This will be run at the beginning and the end of the program execution.
* I think that's the best way to do it.
*/
void __attribute__((constructor)) init_event_poll ()
{
event_handler.timed_events = NULL;
event_handler.timed_events_count = 0;
2014-02-17 09:01:30 +08:00
event_handler.running = 1;
2014-02-17 09:01:30 +08:00
pthread_mutex_init(&event_handler.mutex, NULL);
2014-02-17 09:01:30 +08:00
RUN_IN_THREAD(event_poll, &event_handler);
}
2014-02-07 07:10:55 +08:00
/* NOTE: Do we need this? */
void __attribute__((destructor)) terminate_event_poll()
{
/* Exit thread */
event_handler.running = 0;
2014-02-17 09:01:30 +08:00
2014-02-07 07:10:55 +08:00
/* Give it enought time to exit */
2014-02-17 09:01:30 +08:00
usleep(FREQUENCY * 2);
pthread_mutex_destroy( &event_handler.mutex );
2014-02-01 19:52:48 +08:00
}