File: mainloop-io-worker.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (135 lines) | stat: -rw-r--r-- 3,835 bytes parent folder | download
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
/*
 * Copyright (c) 2002-2013 Balabit
 * Copyright (c) 1998-2013 Balázs Scheidler
 *
 * 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 "mainloop-io-worker.h"
#include "mainloop-worker.h"
#include "mainloop-call.h"
#include "logqueue.h"

/************************************************************************************
 * I/O worker threads
 ************************************************************************************/

static struct iv_work_pool main_loop_io_workers;

static void
_release(MainLoopIOWorkerJob *self)
{
  if (self->release)
    self->release(self->user_data);
}

static void
_engage(MainLoopIOWorkerJob *self)
{
  if (self->engage)
    self->engage(self->user_data);
}

/* NOTE: runs in the main thread */
void
main_loop_io_worker_job_submit(MainLoopIOWorkerJob *self, GIOCondition cond)
{
  g_assert(self->working == FALSE);
  if (main_loop_workers_quit)
    return;

  _engage(self);
  main_loop_worker_job_start();
  self->working = TRUE;
  self->cond = cond;
  iv_work_pool_submit_work(&main_loop_io_workers, &self->work_item);
}

/* NOTE: runs in the actual worker thread spawned by the
 * main_loop_io_workers thread pool */
static void
_work(MainLoopIOWorkerJob *self)
{
  self->work(self->user_data, self->cond);
  main_loop_worker_invoke_batch_callbacks();
  main_loop_worker_run_gc();
}

/* NOTE: runs in the main thread */
static void
_complete(MainLoopIOWorkerJob *self)
{
  self->working = FALSE;
  self->completion(self->user_data);
  main_loop_worker_job_complete();
  _release(self);
}

void
main_loop_io_worker_job_init(MainLoopIOWorkerJob *self)
{
  IV_WORK_ITEM_INIT(&self->work_item);
  self->work_item.cookie = self;
  self->work_item.work = (void (*)(void *)) _work;
  self->work_item.completion = (void (*)(void *)) _complete;
}

static gint
get_processor_count(void)
{
#ifdef _SC_NPROCESSORS_ONLN
  return sysconf(_SC_NPROCESSORS_ONLN);
#else
  return -1;
#endif
}

void
main_loop_io_worker_init(void)
{
  if (main_loop_io_workers.max_threads == 0)
    {
      main_loop_io_workers.max_threads = MIN(MAX(MAIN_LOOP_MIN_WORKER_THREADS, get_processor_count()),
                                             MAIN_LOOP_MAX_WORKER_THREADS);
    }

  main_loop_io_workers.thread_start = (void (*)(void *)) main_loop_worker_thread_start;
  main_loop_io_workers.thread_stop = (void (*)(void *)) main_loop_worker_thread_stop;
  iv_work_pool_create(&main_loop_io_workers);

  log_queue_set_max_threads(MIN(main_loop_io_workers.max_threads, MAIN_LOOP_MAX_WORKER_THREADS));
}

void
main_loop_io_worker_deinit(void)
{
  iv_work_pool_put(&main_loop_io_workers);
}

static GOptionEntry main_loop_io_worker_options[] =
{
  { "worker-threads",      0,         0, G_OPTION_ARG_INT, &main_loop_io_workers.max_threads, "Set the number of I/O worker threads", "<max>" },
  { NULL },
};

void
main_loop_io_worker_add_options(GOptionContext *ctx)
{
  g_option_context_add_main_entries(ctx, main_loop_io_worker_options, NULL);
}