File: mainloop-io-worker.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 (172 lines) | stat: -rw-r--r-- 4,627 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
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
/*
 * 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"
#include "apphook.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);
}

gboolean
main_loop_io_worker_job_submit(MainLoopIOWorkerJob *self, gpointer arg)
{
  main_loop_assert_main_thread();

  g_assert(self->working == FALSE);
  if (main_loop_workers_quit)
    return FALSE;

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

#if SYSLOG_NG_HAVE_IV_WORK_POOL_SUBMIT_CONTINUATION
void
main_loop_io_worker_job_submit_continuation(MainLoopIOWorkerJob *self, gpointer arg)
{
  main_loop_assert_worker_thread();
  g_assert(self->working == FALSE);

  _engage(self);
  main_loop_worker_job_start();
  self->working = TRUE;
  self->arg = arg;

  iv_work_pool_submit_continuation(&main_loop_io_workers, &self->work_item);
}
#endif

/* 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->arg);
  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;
  if (self->completion)
    self->completion(self->user_data, self->arg);
  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
}

static void
main_loop_io_worker_thread_start(void *cookie)
{
  main_loop_worker_thread_start(MLW_ASYNC_WORKER);
}

static void
main_loop_io_worker_thread_stop(void *cookie)
{
  main_loop_worker_thread_stop();
}

static void
__pre_pre_init_hook(gint type, gpointer user_data)
{
  main_loop_worker_allocate_thread_space(main_loop_io_workers.max_threads);
}

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 = main_loop_io_worker_thread_start;
  main_loop_io_workers.thread_stop = main_loop_io_worker_thread_stop;
  iv_work_pool_create(&main_loop_io_workers);
  register_application_hook(AH_CONFIG_PRE_PRE_INIT, __pre_pre_init_hook, NULL, AHM_RUN_REPEAT);
}

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);
}