2009-05-15 21:26:24 +02:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Codethink Limited
|
|
|
|
* Copyright © 2009 Red Hat, Inc
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2017-05-27 18:21:30 +02:00
|
|
|
* 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.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* 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
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Authors: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
* Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-10-25 16:08:40 +02:00
|
|
|
* GThreadedSocketService:
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2023-10-25 16:08:40 +02:00
|
|
|
* A `GThreadedSocketService` is a simple subclass of [class@Gio.SocketService]
|
2009-05-15 21:26:24 +02:00
|
|
|
* that handles incoming connections by creating a worker thread and
|
2011-06-05 00:44:44 +02:00
|
|
|
* dispatching the connection to it by emitting the
|
2023-10-25 16:08:40 +02:00
|
|
|
* [signal@Gio.ThreadedSocketService::run signal] in the new thread.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2023-10-25 16:08:40 +02:00
|
|
|
* The signal handler may perform blocking I/O and need not return
|
2009-05-15 21:26:24 +02:00
|
|
|
* until the connection is closed.
|
|
|
|
*
|
|
|
|
* The service is implemented using a thread pool, so there is a
|
2011-04-20 19:08:06 +02:00
|
|
|
* limited amount of threads available to serve incoming requests.
|
2023-10-25 16:08:40 +02:00
|
|
|
* The service automatically stops the [class@Gio.SocketService] from accepting
|
2009-05-15 21:26:24 +02:00
|
|
|
* new connections when all threads are busy.
|
|
|
|
*
|
2023-10-25 16:08:40 +02:00
|
|
|
* As with [class@Gio.SocketService], you may connect to
|
|
|
|
* [signal@Gio.ThreadedSocketService::run], or subclass and override the default
|
|
|
|
* handler.
|
2023-11-15 12:55:34 +01:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-15 21:26:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "gsocketconnection.h"
|
|
|
|
#include "gthreadedsocketservice.h"
|
2009-05-20 11:28:27 +02:00
|
|
|
#include "glibintl.h"
|
2019-05-31 04:29:18 +02:00
|
|
|
#include "gmarshal-internal.h"
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
struct _GThreadedSocketServicePrivate
|
|
|
|
{
|
|
|
|
GThreadPool *thread_pool;
|
|
|
|
int max_threads;
|
|
|
|
gint job_count;
|
|
|
|
};
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
static guint g_threaded_socket_service_run_signal;
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GThreadedSocketService,
|
|
|
|
g_threaded_socket_service,
|
|
|
|
G_TYPE_SOCKET_SERVICE)
|
2009-05-20 11:28:27 +02:00
|
|
|
|
2019-04-12 16:17:35 +02:00
|
|
|
typedef enum
|
2009-05-20 11:28:27 +02:00
|
|
|
{
|
2019-04-12 16:17:35 +02:00
|
|
|
PROP_MAX_THREADS = 1,
|
|
|
|
} GThreadedSocketServiceProperty;
|
2009-05-20 11:28:27 +02:00
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
G_LOCK_DEFINE_STATIC(job_count);
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2019-04-12 16:22:19 +02:00
|
|
|
GThreadedSocketService *service; /* (owned) */
|
|
|
|
GSocketConnection *connection; /* (owned) */
|
|
|
|
GObject *source_object; /* (owned) (nullable) */
|
2009-05-15 21:26:24 +02:00
|
|
|
} GThreadedSocketServiceData;
|
|
|
|
|
2019-04-12 16:21:10 +02:00
|
|
|
static void
|
|
|
|
g_threaded_socket_service_data_free (GThreadedSocketServiceData *data)
|
|
|
|
{
|
2019-04-12 16:22:19 +02:00
|
|
|
g_clear_object (&data->service);
|
2019-04-12 16:21:10 +02:00
|
|
|
g_clear_object (&data->connection);
|
|
|
|
g_clear_object (&data->source_object);
|
|
|
|
g_slice_free (GThreadedSocketServiceData, data);
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
static void
|
2019-04-12 16:22:19 +02:00
|
|
|
g_threaded_socket_service_func (gpointer job_data,
|
|
|
|
gpointer user_data)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
2019-04-12 16:22:19 +02:00
|
|
|
GThreadedSocketServiceData *data = job_data;
|
2009-05-15 21:26:24 +02:00
|
|
|
gboolean result;
|
|
|
|
|
2019-04-12 16:22:19 +02:00
|
|
|
g_signal_emit (data->service, g_threaded_socket_service_run_signal,
|
2009-05-15 21:26:24 +02:00
|
|
|
0, data->connection, data->source_object, &result);
|
|
|
|
|
|
|
|
G_LOCK (job_count);
|
2019-04-12 16:22:19 +02:00
|
|
|
if (data->service->priv->job_count-- == data->service->priv->max_threads)
|
|
|
|
g_socket_service_start (G_SOCKET_SERVICE (data->service));
|
2009-05-15 21:26:24 +02:00
|
|
|
G_UNLOCK (job_count);
|
2014-10-28 22:16:50 +01:00
|
|
|
|
2019-04-12 16:21:10 +02:00
|
|
|
g_threaded_socket_service_data_free (data);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_threaded_socket_service_incoming (GSocketService *service,
|
|
|
|
GSocketConnection *connection,
|
|
|
|
GObject *source_object)
|
|
|
|
{
|
|
|
|
GThreadedSocketService *threaded;
|
|
|
|
GThreadedSocketServiceData *data;
|
2019-04-12 16:24:05 +02:00
|
|
|
GError *local_error = NULL;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
threaded = G_THREADED_SOCKET_SERVICE (service);
|
|
|
|
|
2019-04-12 16:22:19 +02:00
|
|
|
data = g_slice_new0 (GThreadedSocketServiceData);
|
|
|
|
data->service = g_object_ref (threaded);
|
2009-05-15 21:26:24 +02:00
|
|
|
data->connection = g_object_ref (connection);
|
2019-04-12 16:22:19 +02:00
|
|
|
data->source_object = (source_object != NULL) ? g_object_ref (source_object) : NULL;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
G_LOCK (job_count);
|
|
|
|
if (++threaded->priv->job_count == threaded->priv->max_threads)
|
|
|
|
g_socket_service_stop (service);
|
|
|
|
G_UNLOCK (job_count);
|
|
|
|
|
2019-04-12 16:24:05 +02:00
|
|
|
if (!g_thread_pool_push (threaded->priv->thread_pool, data, &local_error))
|
|
|
|
{
|
|
|
|
g_warning ("Error handling incoming socket: %s", local_error->message);
|
|
|
|
g_threaded_socket_service_data_free (data);
|
|
|
|
}
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2019-04-12 16:24:05 +02:00
|
|
|
g_clear_error (&local_error);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_threaded_socket_service_init (GThreadedSocketService *service)
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
service->priv = g_threaded_socket_service_get_instance_private (service);
|
2009-05-15 21:26:24 +02:00
|
|
|
service->priv->max_threads = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_threaded_socket_service_constructed (GObject *object)
|
|
|
|
{
|
|
|
|
GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
|
|
|
|
|
|
|
|
service->priv->thread_pool =
|
|
|
|
g_thread_pool_new (g_threaded_socket_service_func,
|
2019-04-12 16:22:19 +02:00
|
|
|
NULL,
|
2009-05-15 21:26:24 +02:00
|
|
|
service->priv->max_threads,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_threaded_socket_service_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
|
|
|
|
|
2019-04-12 16:22:19 +02:00
|
|
|
/* All jobs in the pool hold a reference to this #GThreadedSocketService, so
|
|
|
|
* this should only be called once the pool is empty: */
|
2014-10-28 22:16:50 +01:00
|
|
|
g_thread_pool_free (service->priv->thread_pool, FALSE, FALSE);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_threaded_socket_service_parent_class)
|
|
|
|
->finalize (object);
|
|
|
|
}
|
|
|
|
|
2009-05-20 11:28:27 +02:00
|
|
|
static void
|
|
|
|
g_threaded_socket_service_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
|
|
|
|
|
2019-04-12 16:17:35 +02:00
|
|
|
switch ((GThreadedSocketServiceProperty) prop_id)
|
2009-05-20 11:28:27 +02:00
|
|
|
{
|
|
|
|
case PROP_MAX_THREADS:
|
|
|
|
g_value_set_int (value, service->priv->max_threads);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_threaded_socket_service_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
|
|
|
|
|
2019-04-12 16:17:35 +02:00
|
|
|
switch ((GThreadedSocketServiceProperty) prop_id)
|
2009-05-20 11:28:27 +02:00
|
|
|
{
|
|
|
|
case PROP_MAX_THREADS:
|
|
|
|
service->priv->max_threads = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
g_threaded_socket_service_class_init (GThreadedSocketServiceClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
GSocketServiceClass *ss_class = &class->parent_class;
|
|
|
|
|
|
|
|
gobject_class->constructed = g_threaded_socket_service_constructed;
|
|
|
|
gobject_class->finalize = g_threaded_socket_service_finalize;
|
2009-05-20 11:28:27 +02:00
|
|
|
gobject_class->set_property = g_threaded_socket_service_set_property;
|
|
|
|
gobject_class->get_property = g_threaded_socket_service_get_property;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
ss_class->incoming = g_threaded_socket_service_incoming;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GThreadedSocketService::run:
|
|
|
|
* @service: the #GThreadedSocketService.
|
|
|
|
* @connection: a new #GSocketConnection object.
|
2020-06-02 12:40:54 +02:00
|
|
|
* @source_object: (nullable): the source_object passed to g_socket_listener_add_address().
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* The ::run signal is emitted in a worker thread in response to an
|
2009-05-28 00:20:08 +02:00
|
|
|
* incoming connection. This thread is dedicated to handling
|
|
|
|
* @connection and may perform blocking IO. The signal handler need
|
2009-05-15 21:26:24 +02:00
|
|
|
* not return until the connection is closed.
|
|
|
|
*
|
2011-04-20 19:08:06 +02:00
|
|
|
* Returns: %TRUE to stop further signal handlers from being called
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
g_threaded_socket_service_run_signal =
|
2015-09-12 06:00:40 +02:00
|
|
|
g_signal_new (I_("run"), G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
|
2009-05-20 11:28:27 +02:00
|
|
|
G_STRUCT_OFFSET (GThreadedSocketServiceClass, run),
|
|
|
|
g_signal_accumulator_true_handled, NULL,
|
2019-05-31 04:29:18 +02:00
|
|
|
_g_cclosure_marshal_BOOLEAN__OBJECT_OBJECT,
|
|
|
|
G_TYPE_BOOLEAN,
|
2009-05-20 11:28:27 +02:00
|
|
|
2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
|
2019-05-31 04:29:18 +02:00
|
|
|
g_signal_set_va_marshaller (g_threaded_socket_service_run_signal,
|
|
|
|
G_TYPE_FROM_CLASS (class),
|
|
|
|
_g_cclosure_marshal_BOOLEAN__OBJECT_OBJECTv);
|
2009-05-20 11:28:27 +02:00
|
|
|
|
2023-11-29 14:23:09 +01:00
|
|
|
/**
|
|
|
|
* GThreadedSocketService:max-threads:
|
|
|
|
*
|
|
|
|
* The maximum number of threads handling clients for this service.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
|
|
|
*/
|
2009-05-20 11:28:27 +02:00
|
|
|
g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_int ("max-threads", NULL, NULL,
|
2009-05-20 11:28:27 +02:00
|
|
|
-1,
|
|
|
|
G_MAXINT,
|
|
|
|
10,
|
|
|
|
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_threaded_socket_service_new:
|
2009-05-18 08:47:10 +02:00
|
|
|
* @max_threads: the maximal number of threads to execute concurrently
|
2009-05-28 00:20:08 +02:00
|
|
|
* handling incoming clients, -1 means no limit
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
* Creates a new #GThreadedSocketService with no listeners. Listeners
|
2011-06-06 16:22:40 +02:00
|
|
|
* must be added with one of the #GSocketListener "add" methods.
|
2009-05-28 00:20:08 +02:00
|
|
|
*
|
|
|
|
* Returns: a new #GSocketService.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketService *
|
2009-05-18 08:47:10 +02:00
|
|
|
g_threaded_socket_service_new (int max_threads)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
2009-05-20 11:28:27 +02:00
|
|
|
return g_object_new (G_TYPE_THREADED_SOCKET_SERVICE,
|
|
|
|
"max-threads", max_threads,
|
|
|
|
NULL);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|