1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
4 * Copyright © 2009 Red Hat, Inc
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Authors: Ryan Lortie <desrt@desrt.ca>
22 * Alexander Larsson <alexl@redhat.com>
26 * SECTION:gthreadedsocketservice
27 * @title: GThreadedSocketService
28 * @short_description: A threaded GSocketService
30 * @see_also: #GSocketService.
32 * A #GThreadedSocketService is a simple subclass of #GSocketService
33 * that handles incoming connections by creating a worker thread and
34 * dispatching the connection to it by emitting the
35 * #GThreadedSocketService::run signal in the new thread.
37 * The signal handler may perform blocking IO and need not return
38 * until the connection is closed.
40 * The service is implemented using a thread pool, so there is a
41 * limited amount of threads available to serve incoming requests.
42 * The service automatically stops the #GSocketService from accepting
43 * new connections when all threads are busy.
45 * As with #GSocketService, you may connect to #GThreadedSocketService::run,
46 * or subclass and override the default handler.
50 #include "gsocketconnection.h"
51 #include "gthreadedsocketservice.h"
53 #include "gmarshal-internal.h"
55 struct _GThreadedSocketServicePrivate
57 GThreadPool *thread_pool;
62 static guint g_threaded_socket_service_run_signal;
64 G_DEFINE_TYPE_WITH_PRIVATE (GThreadedSocketService,
65 g_threaded_socket_service,
66 G_TYPE_SOCKET_SERVICE)
71 } GThreadedSocketServiceProperty;
73 G_LOCK_DEFINE_STATIC(job_count);
77 GThreadedSocketService *service; /* (owned) */
78 GSocketConnection *connection; /* (owned) */
79 GObject *source_object; /* (owned) (nullable) */
80 } GThreadedSocketServiceData;
83 g_threaded_socket_service_data_free (GThreadedSocketServiceData *data)
85 g_clear_object (&data->service);
86 g_clear_object (&data->connection);
87 g_clear_object (&data->source_object);
88 g_slice_free (GThreadedSocketServiceData, data);
92 g_threaded_socket_service_func (gpointer job_data,
95 GThreadedSocketServiceData *data = job_data;
98 g_signal_emit (data->service, g_threaded_socket_service_run_signal,
99 0, data->connection, data->source_object, &result);
102 if (data->service->priv->job_count-- == data->service->priv->max_threads)
103 g_socket_service_start (G_SOCKET_SERVICE (data->service));
104 G_UNLOCK (job_count);
106 g_threaded_socket_service_data_free (data);
110 g_threaded_socket_service_incoming (GSocketService *service,
111 GSocketConnection *connection,
112 GObject *source_object)
114 GThreadedSocketService *threaded;
115 GThreadedSocketServiceData *data;
116 GError *local_error = NULL;
118 threaded = G_THREADED_SOCKET_SERVICE (service);
120 data = g_slice_new0 (GThreadedSocketServiceData);
121 data->service = g_object_ref (threaded);
122 data->connection = g_object_ref (connection);
123 data->source_object = (source_object != NULL) ? g_object_ref (source_object) : NULL;
126 if (++threaded->priv->job_count == threaded->priv->max_threads)
127 g_socket_service_stop (service);
128 G_UNLOCK (job_count);
130 if (!g_thread_pool_push (threaded->priv->thread_pool, data, &local_error))
132 g_warning ("Error handling incoming socket: %s", local_error->message);
133 g_threaded_socket_service_data_free (data);
136 g_clear_error (&local_error);
142 g_threaded_socket_service_init (GThreadedSocketService *service)
144 service->priv = g_threaded_socket_service_get_instance_private (service);
145 service->priv->max_threads = 10;
149 g_threaded_socket_service_constructed (GObject *object)
151 GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
153 service->priv->thread_pool =
154 g_thread_pool_new (g_threaded_socket_service_func,
156 service->priv->max_threads,
163 g_threaded_socket_service_finalize (GObject *object)
165 GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
167 /* All jobs in the pool hold a reference to this #GThreadedSocketService, so
168 * this should only be called once the pool is empty: */
169 g_thread_pool_free (service->priv->thread_pool, FALSE, FALSE);
171 G_OBJECT_CLASS (g_threaded_socket_service_parent_class)
176 g_threaded_socket_service_get_property (GObject *object,
181 GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
183 switch ((GThreadedSocketServiceProperty) prop_id)
185 case PROP_MAX_THREADS:
186 g_value_set_int (value, service->priv->max_threads);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195 g_threaded_socket_service_set_property (GObject *object,
200 GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
202 switch ((GThreadedSocketServiceProperty) prop_id)
204 case PROP_MAX_THREADS:
205 service->priv->max_threads = g_value_get_int (value);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 g_threaded_socket_service_class_init (GThreadedSocketServiceClass *class)
217 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
218 GSocketServiceClass *ss_class = &class->parent_class;
220 gobject_class->constructed = g_threaded_socket_service_constructed;
221 gobject_class->finalize = g_threaded_socket_service_finalize;
222 gobject_class->set_property = g_threaded_socket_service_set_property;
223 gobject_class->get_property = g_threaded_socket_service_get_property;
225 ss_class->incoming = g_threaded_socket_service_incoming;
228 * GThreadedSocketService::run:
229 * @service: the #GThreadedSocketService.
230 * @connection: a new #GSocketConnection object.
231 * @source_object: (nullable): the source_object passed to g_socket_listener_add_address().
233 * The ::run signal is emitted in a worker thread in response to an
234 * incoming connection. This thread is dedicated to handling
235 * @connection and may perform blocking IO. The signal handler need
236 * not return until the connection is closed.
238 * Returns: %TRUE to stop further signal handlers from being called
240 g_threaded_socket_service_run_signal =
241 g_signal_new (I_("run"), G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
242 G_STRUCT_OFFSET (GThreadedSocketServiceClass, run),
243 g_signal_accumulator_true_handled, NULL,
244 _g_cclosure_marshal_BOOLEAN__OBJECT_OBJECT,
246 2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
247 g_signal_set_va_marshaller (g_threaded_socket_service_run_signal,
248 G_TYPE_FROM_CLASS (class),
249 _g_cclosure_marshal_BOOLEAN__OBJECT_OBJECTv);
251 g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
252 g_param_spec_int ("max-threads",
254 P_("The max number of threads handling clients for this service"),
258 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 * g_threaded_socket_service_new:
263 * @max_threads: the maximal number of threads to execute concurrently
264 * handling incoming clients, -1 means no limit
266 * Creates a new #GThreadedSocketService with no listeners. Listeners
267 * must be added with one of the #GSocketListener "add" methods.
269 * Returns: a new #GSocketService.
274 g_threaded_socket_service_new (int max_threads)
276 return g_object_new (G_TYPE_THREADED_SOCKET_SERVICE,
277 "max-threads", max_threads,