1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
#include "helpers.h"
#include <string.h>
#include <stdio.h>
#include <glib.h>
#include <pcre2.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/resource.h>
#include <errno.h>
#include <unistd.h>
#include "log.h"
#include "main.h"
#include "bufferpool.h"
#include "media_socket.h"
#include "uring.h"
#include "poller.h"
#if 0
#define BSDB(x...) fprintf(stderr, x)
#else
#define BSDB(x...) ((void)0)
#endif
struct detach_thread {
void (*func)(void *);
void *data;
const char *scheduler;
int priority;
};
struct scheduler {
const char *name;
int num;
int nice;
};
struct looper_thread {
enum thread_looper_action (*f)(void);
const char *name;
int64_t interval_us;
};
static mutex_t threads_lists_lock = MUTEX_STATIC_INIT;
static GList *threads_to_join;
static GList *threads_running;
static cond_t threads_cond = COND_STATIC_INIT;
static mutex_t thread_wakers_lock = MUTEX_STATIC_INIT;
static GList *thread_wakers;
static const struct scheduler schedulers[] = {
{ "default", -1, 1 },
{ "none", -1, 1 },
#ifdef SCHED_FIFO
{ "fifo", SCHED_FIFO, 0 },
#endif
#ifdef SCHED_RR
{ "rr", SCHED_RR, 0 },
#endif
//#ifdef SCHED_DEADLINE
// { "deadline", SCHED_DEADLINE, 0 },
//#endif
#ifdef SCHED_OTHER
{ "other", SCHED_OTHER, 1 },
#endif
#ifdef SCHED_BATCH
{ "batch", SCHED_BATCH, 1 },
#endif
#ifdef SCHED_IDLE
{ "idle", SCHED_IDLE, 0 },
#endif
};
int pcre2_multi_match(pcre2_code *re, const char *s, unsigned int num, parse_func f,
void *p, GQueue *q)
{
size_t start, len, next;
char **el;
unsigned int i;
void *ins;
el = malloc(sizeof(*el) * num);
pcre2_match_data *md = pcre2_match_data_create(num, NULL);
for (start = 0, len = strlen(s);
pcre2_match(re, (PCRE2_SPTR8) s + start, len - start, 0, 0, md, NULL) >= 0;
start += next)
{
PCRE2_SIZE *ovec = pcre2_get_ovector_pointer(md);
uint32_t count = pcre2_get_ovector_count(md);
next = ovec[1];
for (i = 0; i < num; i++) {
size_t *ov = ovec + 2 + i*2;
el[i] = (i + 1 >= count) ? NULL : g_strndup(s + start + ov[0], ov[1] - ov[0]);
}
if (f(el, &ins, p))
g_queue_push_tail(q, ins);
for (i = 0; i < num; i++)
g_free(el[i]);
}
free(el);
pcre2_match_data_free(md);
return q ? q->length : 0;
}
static void thread_join_me(void) {
pthread_t *me;
me = g_new(__typeof(*me), 1);
*me = pthread_self();
mutex_lock(&threads_lists_lock);
threads_to_join = g_list_prepend(threads_to_join, me);
cond_broadcast(&threads_cond);
mutex_unlock(&threads_lists_lock);
}
static gint thread_equal(gconstpointer a, gconstpointer b) {
const pthread_t *x = a, *y = b;
return !pthread_equal(*x, *y);
}
void threads_join_all(bool cancel) {
pthread_t *t;
GList *l;
while (1) {
if (cancel) {
mutex_lock(&thread_wakers_lock);
for (l = thread_wakers; l; l = l->next) {
struct thread_waker *wk = l->data;
wk->func(wk);
}
mutex_unlock(&thread_wakers_lock);
mutex_lock(&threads_lists_lock);
for (l = threads_running; l; l = l->next) {
t = l->data;
pthread_cancel(*t);
}
mutex_unlock(&threads_lists_lock);
}
mutex_lock(&threads_lists_lock);
while (threads_to_join) {
t = threads_to_join->data;
pthread_join(*t, NULL);
threads_to_join = g_list_delete_link(threads_to_join, threads_to_join);
l = g_list_find_custom(threads_running, t, thread_equal);
if (l) {
g_free(l->data);
threads_running = g_list_delete_link(threads_running, l);
}
else
abort();
g_free(t);
}
if ((!cancel && rtpe_shutdown) || (cancel && !threads_running)) {
mutex_unlock(&threads_lists_lock);
break;
}
cond_wait(&threads_cond, &threads_lists_lock);
mutex_unlock(&threads_lists_lock);
}
}
static void thread_waker_wake_cond(struct thread_waker *wk) {
mutex_lock(wk->lock);
cond_broadcast(wk->cond);
mutex_unlock(wk->lock);
}
void thread_waker_add(struct thread_waker *wk) {
wk->func = thread_waker_wake_cond;
thread_waker_add_generic(wk);
}
void thread_waker_add_generic(struct thread_waker *wk) {
mutex_lock(&thread_wakers_lock);
thread_wakers = g_list_prepend(thread_wakers, wk);
mutex_unlock(&thread_wakers_lock);
}
void thread_waker_del(struct thread_waker *wk) {
mutex_lock(&thread_wakers_lock);
thread_wakers = g_list_remove(thread_wakers, wk);
mutex_unlock(&thread_wakers_lock);
}
static void thread_detach_cleanup(void *dtp) {
struct detach_thread *dt = dtp;
g_free(dt);
bufferpool_destroy(media_bufferpool);
#ifdef HAVE_LIBURING
if (rtpe_config.common.io_uring)
uring_thread_cleanup();
#endif
thread_join_me();
}
static void *thread_detach_func(void *d) {
struct detach_thread *dt = d;
pthread_t *t;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
t = g_new(__typeof(*t), 1);
*t = pthread_self();
mutex_lock(&threads_lists_lock);
threads_running = g_list_prepend(threads_running, t);
mutex_unlock(&threads_lists_lock);
const struct scheduler *scheduler = NULL;
if (dt->scheduler) {
for (int i = 0; i < G_N_ELEMENTS(schedulers); i++) {
if (!strcmp(dt->scheduler, schedulers[i].name)) {
scheduler = &schedulers[i];
break;
}
}
if (!scheduler)
ilog(LOG_ERR, "Specified scheduler policy '%s' not found", dt->scheduler);
else if (scheduler->num != -1) {
struct sched_param param = { 0 };
if (!scheduler->nice)
param.sched_priority = dt->priority;
ilog(LOG_DEBUG, "Setting thread scheduling parameters to '%s' (%i) / %i",
dt->scheduler, scheduler->num, param.sched_priority);
if (pthread_setschedparam(*t, scheduler->num, ¶m))
ilog(LOG_ERR, "Failed to set thread scheduling parameters to '%s' (%i) / %i: %s",
dt->scheduler, scheduler->num, param.sched_priority,
strerror(errno));
}
}
if ((!scheduler && dt->priority) || (scheduler && scheduler->nice)) {
ilog(LOG_DEBUG, "Setting thread nice value to %i", dt->priority);
if (setpriority(PRIO_PROCESS, 0, dt->priority))
ilog(LOG_ERR, "Failed to set thread nice value to %i: %s",
dt->priority, strerror(errno));
}
media_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free);
#ifdef HAVE_LIBURING
if (rtpe_config.common.io_uring)
uring_thread_init();
#endif
thread_cleanup_push(thread_detach_cleanup, dt);
dt->func(dt->data);
thread_cleanup_pop(true);
return NULL;
}
void thread_create_detach_prio(void (*f)(void *), void *d, const char *scheduler, int priority,
const char *name)
{
struct detach_thread *dt;
dt = g_new(__typeof(*dt), 1);
dt->func = f;
dt->data = d;
dt->scheduler = scheduler;
dt->priority = priority;
if (thread_create(thread_detach_func, dt, true, NULL, name))
abort();
}
static void thread_looper_helper(void *fp) {
// move object to stack and free it, so we can be cancelled without having a leak
struct looper_thread *lhp = fp;
struct looper_thread lh = *lhp;
g_free(lhp);
int64_t interval_us = lh.interval_us;
#ifdef ASAN_BUILD
interval_us = MIN(interval_us, 100000);
#endif
static const int64_t warn_limit_pct = 20; // 20%
int64_t warn_limit_us = interval_us * warn_limit_pct / 100;
struct timespec interval_ts = {
.tv_sec = interval_us / 1000000,
.tv_nsec = (interval_us % 1000000) * 1000,
};
while (!rtpe_shutdown) {
rtpe_now = now_us();
enum thread_looper_action ret = lh.f();
uring_methods.thread_loop();
if (ret == TLA_BREAK)
break;
int64_t stop = now_us();
int64_t duration_us = stop - rtpe_now;
if (duration_us > warn_limit_us)
ilog(LOG_WARN, "Run time of timer \"%s\": %" PRId64 ".%06" PRId64 " sec, "
"exceeding limit of %" PRId64 "%% (%" PRId64 ".%06" PRId64 " sec)",
lh.name,
duration_us / 1000000, duration_us % 1000000,
warn_limit_pct,
warn_limit_us / 1000000, warn_limit_us % 1000000);
struct timespec sleeptime = interval_ts;
struct timespec remtime;
while (true) {
thread_cancel_enable();
int res = nanosleep(&sleeptime, &remtime);
thread_cancel_disable();
if (res == -1 && errno == EINTR) {
sleeptime = remtime;
continue;
}
break;
}
}
}
void thread_create_looper(enum thread_looper_action (*f)(void), const char *scheduler, int priority,
const char *name,
int64_t interval_us)
{
struct looper_thread *lh = g_new(__typeof(*lh), 1);
*lh = (__typeof__(*lh)) {
.f = f,
.name = name,
.interval_us = interval_us,
};
thread_create_detach_prio(thread_looper_helper, lh, scheduler, priority, name);
}
|