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>
|
2023-01-10 10:22:06 -05:00
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-03-31 17:05:24 -04:00
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
#include "db.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "worker.h"
|
2023-01-21 18:33:54 -05:00
|
|
|
#include "evthr.h"
|
2021-07-05 15:40:31 -04:00
|
|
|
#include "misc.h"
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
#define THREADPOOL_NTHREADS 4
|
2023-01-10 10:22:06 -05:00
|
|
|
|
|
|
|
static struct evthr_pool *worker_threadpool;
|
2023-01-27 17:13:46 -05:00
|
|
|
static __thread struct evthr *worker_thr;
|
2023-01-10 10:22:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------- CALLBACK EXECUTION ------------------------- */
|
|
|
|
/* Worker threads */
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
static void
|
|
|
|
execute(struct evthr *thr, void *arg, void *shared)
|
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 };
|
2023-01-10 10:22:06 -05:00
|
|
|
struct event_base *evbase;
|
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
|
|
|
{
|
2023-01-10 10:22:06 -05:00
|
|
|
evbase = evthr_get_base(thr);
|
|
|
|
cmdarg->timer = evtimer_new(evbase, execute_cb, cmdarg);
|
2016-05-14 01:03:30 -04:00
|
|
|
evtimer_add(cmdarg->timer, &tv);
|
2023-01-10 10:22:06 -05:00
|
|
|
return;
|
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);
|
2023-01-10 10:22:06 -05:00
|
|
|
free(cmdarg);
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
static void
|
|
|
|
init_cb(struct evthr *thr, void *shared)
|
2015-03-31 17:05:24 -04:00
|
|
|
{
|
2023-01-21 18:33:54 -05:00
|
|
|
CHECK_ERR(L_MAIN, db_perthread_init());
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
worker_thr = thr;
|
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
thread_setname(pthread_self(), "worker");
|
|
|
|
}
|
2015-03-31 17:05:24 -04:00
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
static void
|
|
|
|
exit_cb(struct evthr *thr, void *shared)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
worker_thr = NULL;
|
|
|
|
|
2015-03-31 17:05:24 -04:00
|
|
|
db_perthread_deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------- Our worker API --------------------------- */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-11-19 17:08:50 -05:00
|
|
|
cmdarg = calloc(1, sizeof(struct worker_arg));
|
2016-05-14 01:03:30 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-16 15:44:51 -05:00
|
|
|
if (arg_size > 0)
|
2015-03-31 17:05:24 -04:00
|
|
|
{
|
2017-01-16 15:44:51 -05:00
|
|
|
argcpy = malloc(arg_size);
|
|
|
|
if (!argcpy)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Out of memory\n");
|
|
|
|
free(cmdarg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(argcpy, cb_arg, arg_size);
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
2017-01-16 15:44:51 -05:00
|
|
|
else
|
|
|
|
argcpy = NULL;
|
2015-03-31 17:05:24 -04:00
|
|
|
|
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
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
evthr_pool_defer(worker_threadpool, execute, cmdarg);
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
struct event_base *
|
|
|
|
worker_evbase_get(void)
|
|
|
|
{
|
|
|
|
return evthr_get_base(worker_thr);
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:05:24 -04:00
|
|
|
int
|
|
|
|
worker_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
worker_threadpool = evthr_pool_wexit_new(THREADPOOL_NTHREADS, init_cb, exit_cb, NULL);
|
|
|
|
if (!worker_threadpool)
|
2015-03-31 17:05:24 -04:00
|
|
|
{
|
2023-01-10 10:22:06 -05:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Could not create worker thread pool\n");
|
|
|
|
goto error;
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
ret = evthr_pool_start(worker_threadpool);
|
2015-03-31 17:05:24 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-10 10:22:06 -05:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Could not spawn worker threads\n");
|
|
|
|
goto error;
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2023-01-10 10:22:06 -05:00
|
|
|
error:
|
|
|
|
worker_deinit();
|
2015-03-31 17:05:24 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
worker_deinit(void)
|
|
|
|
{
|
2023-01-10 10:22:06 -05:00
|
|
|
evthr_pool_stop(worker_threadpool);
|
|
|
|
evthr_pool_free(worker_threadpool);
|
2015-03-31 17:05:24 -04:00
|
|
|
}
|