Commit 2e823519 authored by Petri Hintukainen's avatar Petri Hintukainen

Split event queue to separate file

parent 3c5513d1
......@@ -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 \
......
......@@ -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);
......
/*
* This file is part of libbluray
* Copyright (C) 2010-2016 Petri Hintukainen <phintuka@users.sourceforge.net>
*
* 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;
}
/*
* This file is part of libbluray
* Copyright (C) 2010-2016 Petri Hintukainen <phintuka@users.sourceforge.net>
*
* 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_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment