2015-03-31 17:05:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Espen Jürgensen <espenjurgensen@gmail.com>
|
|
|
|
*
|
|
|
|
* This program 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2015-04-01 08:36:24 -04:00
|
|
|
#include <string.h>
|
2015-03-31 17:05:24 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
2016-05-05 13:39:15 -04:00
|
|
|
#ifdef HAVE_PTHREAD_NP_H
|
2016-03-05 07:19:55 -05:00
|
|
|
# include <pthread_np.h>
|
|
|
|
#endif
|
2015-03-31 17:05:24 -04:00
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
#include "db.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "worker.h"
|
2016-05-14 01:03:30 -04:00
|
|
|
#include "commands.h"
|
2015-03-31 17:05:24 -04:00
|
|
|
|
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
struct worker_arg
|
2015-03-31 17:05:24 -04:00
|
|
|
{
|
2016-05-14 01:03:30 -04:00
|
|
|
void (*cb)(void *);
|
|
|
|
void *cb_arg;
|
|
|
|
int delay;
|
|
|
|
struct event *timer;
|
2015-03-31 17:05:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* --- Globals --- */
|
|
|
|
// worker thread
|
|
|
|
static pthread_t tid_worker;
|
|
|
|
|
|
|
|
// Event base, pipes and events
|
|
|
|
struct event_base *evbase_worker;
|
|
|
|
static int g_initialized;
|
2016-05-14 01:03:30 -04:00
|
|
|
static struct commands_base *cmdbase;
|
|
|
|
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2015-04-01 08:36:24 -04:00
|
|
|
/* ---------------------------- CALLBACK EXECUTION ------------------------- */
|
|
|
|
/* Thread: worker */
|
2015-03-31 17:05:24 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
execute_cb(int fd, short what, void *arg)
|
|
|
|
{
|
2016-05-14 01:03:30 -04:00
|
|
|
struct worker_arg *cmdarg = arg;
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
cmdarg->cb(cmdarg->cb_arg);
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
event_free(cmdarg->timer);
|
|
|
|
free(cmdarg->cb_arg);
|
|
|
|
free(cmdarg);
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
static enum command_state
|
|
|
|
execute(void *arg, int *retval)
|
2015-03-31 17:05:24 -04:00
|
|
|
{
|
2016-05-14 01:03:30 -04:00
|
|
|
struct worker_arg *cmdarg = arg;
|
|
|
|
struct timeval tv = { cmdarg->delay, 0 };
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
if (cmdarg->delay)
|
2015-03-31 17:05:24 -04:00
|
|
|
{
|
2016-05-14 01:03:30 -04:00
|
|
|
cmdarg->timer = evtimer_new(evbase_worker, execute_cb, cmdarg);
|
|
|
|
evtimer_add(cmdarg->timer, &tv);
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
*retval = 0;
|
|
|
|
return COMMAND_PENDING; // Not done yet, ask caller not to free cmd
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
cmdarg->cb(cmdarg->cb_arg);
|
|
|
|
free(cmdarg->cb_arg);
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
*retval = 0;
|
|
|
|
return COMMAND_END;
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------- MAIN --------------------------------- */
|
|
|
|
/* Thread: worker */
|
|
|
|
|
|
|
|
static void *
|
|
|
|
worker(void *arg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = db_perthread_init();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-04-01 08:36:24 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Error: DB init failed (worker thread)\n");
|
2015-03-31 17:05:24 -04:00
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_initialized = 1;
|
|
|
|
|
|
|
|
event_base_dispatch(evbase_worker);
|
|
|
|
|
|
|
|
if (g_initialized)
|
|
|
|
{
|
2015-04-01 08:36:24 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Worker event loop terminated ahead of time!\n");
|
2015-03-31 17:05:24 -04:00
|
|
|
g_initialized = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
db_perthread_deinit();
|
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------- Our worker API --------------------------- */
|
|
|
|
|
|
|
|
/* Thread: player */
|
|
|
|
void
|
|
|
|
worker_execute(void (*cb)(void *), void *cb_arg, size_t arg_size, int delay)
|
|
|
|
{
|
2016-05-14 01:03:30 -04:00
|
|
|
struct worker_arg *cmdarg;
|
2015-03-31 17:05:24 -04:00
|
|
|
void *argcpy;
|
|
|
|
|
2015-04-01 08:36:24 -04:00
|
|
|
DPRINTF(E_DBG, L_MAIN, "Got worker execute request\n");
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
cmdarg = (struct worker_arg *)malloc(sizeof(struct worker_arg));
|
|
|
|
if (!cmdarg)
|
2015-03-31 17:05:24 -04:00
|
|
|
{
|
2016-05-14 01:03:30 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Could not allocate worker_arg\n");
|
2015-03-31 17:05:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
memset(cmdarg, 0, sizeof(struct worker_arg));
|
2015-03-31 17:05:24 -04:00
|
|
|
|
|
|
|
argcpy = malloc(arg_size);
|
|
|
|
if (!argcpy)
|
|
|
|
{
|
2015-04-01 08:36:24 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Out of memory\n");
|
2015-03-31 17:05:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(argcpy, cb_arg, arg_size);
|
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
cmdarg->cb = cb;
|
|
|
|
cmdarg->cb_arg = argcpy;
|
|
|
|
cmdarg->delay = delay;
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2016-05-14 01:03:30 -04:00
|
|
|
commands_exec_async(cmdbase, execute, cmdarg);
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
worker_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
evbase_worker = event_base_new();
|
|
|
|
if (!evbase_worker)
|
|
|
|
{
|
2015-04-01 08:36:24 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Could not create an event base\n");
|
2015-03-31 17:05:24 -04:00
|
|
|
goto evbase_fail;
|
|
|
|
}
|
|
|
|
|
2016-05-23 00:41:43 -04:00
|
|
|
cmdbase = commands_base_new(evbase_worker, NULL);
|
2015-03-31 17:05:24 -04:00
|
|
|
|
|
|
|
ret = pthread_create(&tid_worker, NULL, worker, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-04-01 08:36:24 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Could not spawn worker thread: %s\n", strerror(errno));
|
2015-03-31 17:05:24 -04:00
|
|
|
|
|
|
|
goto thread_fail;
|
|
|
|
}
|
|
|
|
|
2016-05-05 13:39:15 -04:00
|
|
|
#if defined(HAVE_PTHREAD_SETNAME_NP)
|
2016-03-05 07:19:55 -05:00
|
|
|
pthread_setname_np(tid_worker, "worker");
|
2016-05-05 13:39:15 -04:00
|
|
|
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
|
2016-03-05 07:19:55 -05:00
|
|
|
pthread_set_name_np(tid_worker, "worker");
|
|
|
|
#endif
|
|
|
|
|
2015-03-31 17:05:24 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
thread_fail:
|
2016-05-14 01:03:30 -04:00
|
|
|
commands_base_free(cmdbase);
|
2015-03-31 17:05:24 -04:00
|
|
|
event_base_free(evbase_worker);
|
|
|
|
evbase_worker = NULL;
|
|
|
|
|
|
|
|
evbase_fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
worker_deinit(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-05-23 00:41:43 -04:00
|
|
|
g_initialized = 0;
|
2016-06-11 04:00:23 -04:00
|
|
|
commands_base_destroy(cmdbase);
|
2015-03-31 17:05:24 -04:00
|
|
|
|
|
|
|
ret = pthread_join(tid_worker, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2015-04-01 08:36:24 -04:00
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Could not join worker thread: %s\n", strerror(errno));
|
2015-03-31 17:05:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free event base (should free events too)
|
|
|
|
event_base_free(evbase_worker);
|
|
|
|
}
|