1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
4 * Copyright © 2009 Red Hat, Inc
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 2 of the licence or (at
9 * your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
20 * Alexander Larsson <alexl@redhat.com>
24 * SECTION:gthreadedsocketservice
25 * @title: GThreadedSocketService
26 * @short_description: A threaded GSocketService
28 * @see_also: #GSocketService.
30 * A #GThreadedSocketService is a simple subclass of #GSocketService
31 * that handles incoming connections by creating a worker thread and
32 * dispatching the connection to it by emitting the
33 * #GThreadedSocketService::run signal in the new thread.
35 * The signal handler may perform blocking IO and need not return
36 * until the connection is closed.
38 * The service is implemented using a thread pool, so there is a
39 * limited amount of threads available to serve incoming requests.
40 * The service automatically stops the #GSocketService from accepting
41 * new connections when all threads are busy.
43 * As with #GSocketService, you may connect to #GThreadedSocketService::run,
44 * or subclass and override the default handler.
48 #include "gsocketconnection.h"
49 #include "gthreadedsocketservice.h"
52 struct _GThreadedSocketServicePrivate
54 GThreadPool
*thread_pool
;
59 static guint g_threaded_socket_service_run_signal
;
61 G_DEFINE_TYPE_WITH_PRIVATE (GThreadedSocketService
,
62 g_threaded_socket_service
,
63 G_TYPE_SOCKET_SERVICE
)
71 G_LOCK_DEFINE_STATIC(job_count
);
75 GSocketConnection
*connection
;
76 GObject
*source_object
;
77 } GThreadedSocketServiceData
;
80 g_threaded_socket_service_func (gpointer _data
,
83 GThreadedSocketService
*threaded
= user_data
;
84 GThreadedSocketServiceData
*data
= _data
;
87 g_signal_emit (threaded
, g_threaded_socket_service_run_signal
,
88 0, data
->connection
, data
->source_object
, &result
);
90 g_object_unref (data
->connection
);
91 if (data
->source_object
)
92 g_object_unref (data
->source_object
);
93 g_slice_free (GThreadedSocketServiceData
, data
);
96 if (threaded
->priv
->job_count
-- == threaded
->priv
->max_threads
)
97 g_socket_service_start (G_SOCKET_SERVICE (threaded
));
100 g_object_unref (threaded
);
104 g_threaded_socket_service_incoming (GSocketService
*service
,
105 GSocketConnection
*connection
,
106 GObject
*source_object
)
108 GThreadedSocketService
*threaded
;
109 GThreadedSocketServiceData
*data
;
111 threaded
= G_THREADED_SOCKET_SERVICE (service
);
113 data
= g_slice_new (GThreadedSocketServiceData
);
115 /* Ref the socket service for the thread */
116 g_object_ref (service
);
118 data
->connection
= g_object_ref (connection
);
120 data
->source_object
= g_object_ref (source_object
);
122 data
->source_object
= NULL
;
125 if (++threaded
->priv
->job_count
== threaded
->priv
->max_threads
)
126 g_socket_service_stop (service
);
127 G_UNLOCK (job_count
);
129 g_thread_pool_push (threaded
->priv
->thread_pool
, data
, NULL
);
137 g_threaded_socket_service_init (GThreadedSocketService
*service
)
139 service
->priv
= g_threaded_socket_service_get_instance_private (service
);
140 service
->priv
->max_threads
= 10;
144 g_threaded_socket_service_constructed (GObject
*object
)
146 GThreadedSocketService
*service
= G_THREADED_SOCKET_SERVICE (object
);
148 service
->priv
->thread_pool
=
149 g_thread_pool_new (g_threaded_socket_service_func
,
151 service
->priv
->max_threads
,
158 g_threaded_socket_service_finalize (GObject
*object
)
160 GThreadedSocketService
*service
= G_THREADED_SOCKET_SERVICE (object
);
162 g_thread_pool_free (service
->priv
->thread_pool
, FALSE
, FALSE
);
164 G_OBJECT_CLASS (g_threaded_socket_service_parent_class
)
169 g_threaded_socket_service_get_property (GObject
*object
,
174 GThreadedSocketService
*service
= G_THREADED_SOCKET_SERVICE (object
);
178 case PROP_MAX_THREADS
:
179 g_value_set_int (value
, service
->priv
->max_threads
);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
188 g_threaded_socket_service_set_property (GObject
*object
,
193 GThreadedSocketService
*service
= G_THREADED_SOCKET_SERVICE (object
);
197 case PROP_MAX_THREADS
:
198 service
->priv
->max_threads
= g_value_get_int (value
);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
208 g_threaded_socket_service_class_init (GThreadedSocketServiceClass
*class)
210 GObjectClass
*gobject_class
= G_OBJECT_CLASS (class);
211 GSocketServiceClass
*ss_class
= &class->parent_class
;
213 gobject_class
->constructed
= g_threaded_socket_service_constructed
;
214 gobject_class
->finalize
= g_threaded_socket_service_finalize
;
215 gobject_class
->set_property
= g_threaded_socket_service_set_property
;
216 gobject_class
->get_property
= g_threaded_socket_service_get_property
;
218 ss_class
->incoming
= g_threaded_socket_service_incoming
;
221 * GThreadedSocketService::run:
222 * @service: the #GThreadedSocketService.
223 * @connection: a new #GSocketConnection object.
224 * @source_object: the source_object passed to g_socket_listener_add_address().
226 * The ::run signal is emitted in a worker thread in response to an
227 * incoming connection. This thread is dedicated to handling
228 * @connection and may perform blocking IO. The signal handler need
229 * not return until the connection is closed.
231 * Returns: %TRUE to stop further signal handlers from being called
233 g_threaded_socket_service_run_signal
=
234 g_signal_new (I_("run"), G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST
,
235 G_STRUCT_OFFSET (GThreadedSocketServiceClass
, run
),
236 g_signal_accumulator_true_handled
, NULL
,
237 NULL
, G_TYPE_BOOLEAN
,
238 2, G_TYPE_SOCKET_CONNECTION
, G_TYPE_OBJECT
);
240 g_object_class_install_property (gobject_class
, PROP_MAX_THREADS
,
241 g_param_spec_int ("max-threads",
243 P_("The max number of threads handling clients for this service"),
247 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
251 * g_threaded_socket_service_new:
252 * @max_threads: the maximal number of threads to execute concurrently
253 * handling incoming clients, -1 means no limit
255 * Creates a new #GThreadedSocketService with no listeners. Listeners
256 * must be added with one of the #GSocketListener "add" methods.
258 * Returns: a new #GSocketService.
263 g_threaded_socket_service_new (int max_threads
)
265 return g_object_new (G_TYPE_THREADED_SOCKET_SERVICE
,
266 "max-threads", max_threads
,