File: dyn-metrics-cache.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (102 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2023-2024 Attila Szakacs <attila.szakacs@axoflow.com>
 * Copyright (c) 2024 Balazs Scheidler <balazs.scheidler@axoflow.com>
 * Copyright (c) 2024 László Várady
 * Copyright (c) 2024 Axoflow
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "dyn-metrics-cache.h"
#include "syslog-ng.h"
#include "apphook.h"
#include "tls-support.h"

TLS_BLOCK_START
{
  DynMetricsStore *metrics_cache;
}
TLS_BLOCK_END;

#define metrics_cache __tls_deref(metrics_cache)

static DynMetricsStore *global_metrics_cache;
static GMutex global_metrics_cache_lock;

static void
_sync_with_global_cache(DynMetricsStore *store)
{
  g_mutex_lock(&global_metrics_cache_lock);
  dyn_metrics_store_merge(global_metrics_cache, store);
  g_mutex_unlock(&global_metrics_cache_lock);
}

static void
_purge_global_cache(gint type, gpointer c)
{
  g_mutex_lock(&global_metrics_cache_lock);
  dyn_metrics_store_reset(global_metrics_cache);
  g_mutex_unlock(&global_metrics_cache_lock);
}

static void
_init_tls_cache(gpointer user_data)
{
  g_assert(!metrics_cache);

  metrics_cache = dyn_metrics_store_new();
}

static void
_deinit_tls_cache(gpointer user_data)
{
  _sync_with_global_cache(metrics_cache);
  dyn_metrics_store_free(metrics_cache);
}

DynMetricsStore *
dyn_metrics_cache(void)
{
  return metrics_cache;
}

void
dyn_metrics_cache_global_init(void)
{
  g_assert(!global_metrics_cache);
  g_mutex_init(&global_metrics_cache_lock);
  global_metrics_cache = dyn_metrics_store_new();

  register_application_thread_init_hook(_init_tls_cache, NULL);
  register_application_thread_deinit_hook(_deinit_tls_cache, NULL);

  register_application_hook(AH_CONFIG_CHANGED, _purge_global_cache, NULL, AHM_RUN_REPEAT);

  _init_tls_cache(NULL);
}

void
dyn_metrics_cache_global_deinit(void)
{
  _deinit_tls_cache(NULL);

  dyn_metrics_store_free(global_metrics_cache);
  g_mutex_clear(&global_metrics_cache_lock);
}