libbluray | branch: master | hpi1 <[email protected]> | Thu Mar 10 13:22:31 2016 +0200| [2e823519b5f79f30813c9ef73608b8cf78c0f635] | committer: hpi1
Split event queue to separate file > http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=2e823519b5f79f30813c9ef73608b8cf78c0f635 --- Makefile.am | 2 + src/libbluray/bluray.c | 89 +++++++---------------------------------- src/util/event_queue.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ src/util/event_queue.h | 35 ++++++++++++++++ 4 files changed, 156 insertions(+), 74 deletions(-) diff --git a/Makefile.am b/Makefile.am index 5c77a05..3a54bfc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,6 +112,8 @@ libbluray_la_SOURCES = \ src/util/attributes.h \ src/util/bits.h \ src/util/bits.c \ + src/util/event_queue.h \ + src/util/event_queue.c \ src/util/logging.h \ src/util/logging.c \ src/util/log_control.h \ diff --git a/src/libbluray/bluray.c b/src/libbluray/bluray.c index 9aba36d..f19d850 100644 --- a/src/libbluray/bluray.c +++ b/src/libbluray/bluray.c @@ -28,6 +28,7 @@ #include "bluray_internal.h" #include "register.h" #include "util/array.h" +#include "util/event_queue.h" #include "util/macro.h" #include "util/logging.h" #include "util/strutl.h" @@ -59,14 +60,6 @@ #include <string.h> -#define MAX_EVENTS 31 /* 2^n - 1 */ -typedef struct bd_event_queue_s { - BD_MUTEX mutex; - unsigned in; /* next free slot */ - unsigned out; /* next event */ - BD_EVENT ev[MAX_EVENTS+1]; -} BD_EVENT_QUEUE; - typedef enum { title_undef = 0, title_hdmv, @@ -200,78 +193,26 @@ void bd_get_version(int *major, int *minor, int *micro) * Navigation mode event queue */ -static void _init_event_queue(BLURAY *bd) -{ - if (!bd->event_queue) { - bd->event_queue = calloc(1, sizeof(struct bd_event_queue_s)); - if (bd->event_queue) { - bd_mutex_init(&bd->event_queue->mutex); - } - } else { - bd_mutex_lock(&bd->event_queue->mutex); - bd->event_queue->in = 0; - bd->event_queue->out = 0; - memset(bd->event_queue->ev, 0, sizeof(bd->event_queue->ev)); - bd_mutex_unlock(&bd->event_queue->mutex); - } -} - -static void _free_event_queue(BLURAY *bd) -{ - if (bd->event_queue) { - bd_mutex_destroy(&bd->event_queue->mutex); - X_FREE(bd->event_queue); - } -} - static int _get_event(BLURAY *bd, BD_EVENT *ev) { - struct bd_event_queue_s *eq = bd->event_queue; - - if (eq) { - bd_mutex_lock(&eq->mutex); - - if (eq->in != eq->out) { - - *ev = eq->ev[eq->out]; - eq->out = (eq->out + 1) & MAX_EVENTS; - - bd_mutex_unlock(&eq->mutex); - return 1; - } - - bd_mutex_unlock(&eq->mutex); + int result = event_queue_get(bd->event_queue, ev); + if (!result) { + ev->event = BD_EVENT_NONE; } - - ev->event = BD_EVENT_NONE; - - return 0; + return result; } static int _queue_event(BLURAY *bd, uint32_t event, uint32_t param) { - struct bd_event_queue_s *eq = bd->event_queue; - - if (eq) { - bd_mutex_lock(&eq->mutex); - - unsigned new_in = (eq->in + 1) & MAX_EVENTS; - - if (new_in != eq->out) { - eq->ev[eq->in].event = event; - eq->ev[eq->in].param = param; - eq->in = new_in; - - bd_mutex_unlock(&eq->mutex); - return 1; + int result = 0; + if (bd->event_queue) { + BD_EVENT ev = { event, param }; + result = event_queue_put(bd->event_queue, &ev); + if (!result) { + BD_DEBUG(DBG_BLURAY|DBG_CRIT, "_queue_event(%d, %d): queue overflow !\n", event, param); } - - bd_mutex_unlock(&eq->mutex); - - BD_DEBUG(DBG_BLURAY|DBG_CRIT, "_queue_event(%d, %d): queue overflow !\n", event, param); } - - return 0; + return result; } /* @@ -1526,7 +1467,7 @@ void bd_close(BLURAY *bd) sound_free(&bd->sound_effects); bd_registers_free(bd->regs); - _free_event_queue(bd); + event_queue_destroy(&bd->event_queue); array_free((void**)&bd->titles); _storage_free(bd); @@ -3239,7 +3180,7 @@ int bd_play(BLURAY *bd) } if (!bd->event_queue) { - _init_event_queue(bd); + bd->event_queue = event_queue_new(sizeof(BD_EVENT)); bd_psr_lock(bd->regs); bd_psr_register_cb(bd->regs, _process_psr_event, bd); @@ -3505,7 +3446,7 @@ int bd_read_ext(BLURAY *bd, unsigned char *buf, int len, BD_EVENT *event) int bd_get_event(BLURAY *bd, BD_EVENT *event) { if (!bd->event_queue) { - _init_event_queue(bd); + bd->event_queue = event_queue_new(sizeof(BD_EVENT)); bd_psr_register_cb(bd->regs, _process_psr_event, bd); _queue_initial_psr_events(bd); diff --git a/src/util/event_queue.c b/src/util/event_queue.c new file mode 100644 index 0000000..976201d --- /dev/null +++ b/src/util/event_queue.c @@ -0,0 +1,104 @@ +/* + * This file is part of libbluray + * Copyright (C) 2010-2016 Petri Hintukainen <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include "event_queue.h" + +#include "util/macro.h" +#include "util/mutex.h" + +#include <string.h> + + +#define MAX_EVENTS 31 /* 2^n - 1 */ + +struct bd_event_queue { + BD_MUTEX mutex; + size_t event_size; + unsigned in; /* next free slot */ + unsigned out; /* next event */ + + unsigned char ev[1]; +}; + + +void event_queue_destroy(BD_EVENT_QUEUE **pp) +{ + if (pp && *pp) { + BD_EVENT_QUEUE *eq = *pp; + bd_mutex_destroy(&eq->mutex); + X_FREE(*pp); + } +} + +BD_EVENT_QUEUE *event_queue_new(size_t event_size) +{ + BD_EVENT_QUEUE *eq = calloc(1, sizeof(BD_EVENT_QUEUE) + event_size * (MAX_EVENTS + 1)); + if (eq) { + bd_mutex_init(&eq->mutex); + eq->event_size = event_size; + } + return eq; +} + +int event_queue_get(BD_EVENT_QUEUE *eq, void *ev) +{ + int result = 0; + + if (eq) { + bd_mutex_lock(&eq->mutex); + + if (eq->in != eq->out) { + + memcpy(ev, &eq->ev[eq->out * eq->event_size], eq->event_size); + eq->out = (eq->out + 1) & MAX_EVENTS; + + result = 1; + } + + bd_mutex_unlock(&eq->mutex); + } + + return result; +} + +int event_queue_put(BD_EVENT_QUEUE *eq, const void *ev) +{ + int result = 0; + + if (eq) { + bd_mutex_lock(&eq->mutex); + + unsigned new_in = (eq->in + 1) & MAX_EVENTS; + + if (new_in != eq->out) { + memcpy(&eq->ev[eq->in * eq->event_size], ev, eq->event_size); + eq->in = new_in; + + result = 1; + } + + bd_mutex_unlock(&eq->mutex); + } + + return result; +} diff --git a/src/util/event_queue.h b/src/util/event_queue.h new file mode 100644 index 0000000..6331b7d --- /dev/null +++ b/src/util/event_queue.h @@ -0,0 +1,35 @@ +/* + * This file is part of libbluray + * Copyright (C) 2010-2016 Petri Hintukainen <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#if !defined(BD_EVENT_QUEUE_H_) +#define BD_EVENT_QUEUE_H_ + +#include "util/attributes.h" + +#include <stddef.h> + +typedef struct bd_event_queue BD_EVENT_QUEUE; + +BD_PRIVATE BD_EVENT_QUEUE *event_queue_new(size_t event_size); +BD_PRIVATE void event_queue_destroy(BD_EVENT_QUEUE **); + +BD_PRIVATE int event_queue_get(BD_EVENT_QUEUE *eq, void *ev); +BD_PRIVATE int event_queue_put(BD_EVENT_QUEUE *eq, const void *ev); + +#endif /* BD_EVENT_QUEUE_H_ */ _______________________________________________ libbluray-devel mailing list [email protected] https://mailman.videolan.org/listinfo/libbluray-devel
