g_unix_set_fd_nonblocking: New API to control file descriptor blocking state
[platform/upstream/glib.git] / gio / gthreadedsocketservice.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2009 Codethink Limited
4  * Copyright © 2009 Red Hat, Inc
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Ryan Lortie <desrt@desrt.ca>
22  *          Alexander Larsson <alexl@redhat.com>
23  */
24
25 /**
26  * SECTION:gthreadedsocketservice
27  * @title: GThreadedSocketService
28  * @short_description: A threaded GSocketService
29  * @see_also: #GSocketService.
30  *
31  * A #GThreadedSocketService is a simple subclass of #GSocketService
32  * that handles incoming connections by creating a worker thread and
33  * dispatching the connection to it by emitting the ::run signal in
34  * the new thread.
35  *
36  * The signal handler may perform blocking IO and need not return
37  * until the connection is closed.
38  *
39  * The service is implemented using a thread pool, so there is a
40  * limited amount of threads available to serve incoming requests.
41  * The service automatically stops the #GSocketService from accepting
42  * new connections when all threads are busy.
43  *
44  * As with #GSocketService, you may connect to #GThreadedSocketService:run,
45  * or subclass and override the default handler.
46  */
47
48 #include "config.h"
49 #include "gsocketconnection.h"
50 #include "gthreadedsocketservice.h"
51 #include "glibintl.h"
52
53 #include "gio-marshal.h"
54
55
56 static guint g_threaded_socket_service_run_signal;
57
58 G_DEFINE_TYPE (GThreadedSocketService,
59                g_threaded_socket_service,
60                G_TYPE_SOCKET_SERVICE);
61
62 enum
63 {
64   PROP_0,
65   PROP_MAX_THREADS
66 };
67
68
69 G_LOCK_DEFINE_STATIC(job_count);
70
71 struct _GThreadedSocketServicePrivate
72 {
73   GThreadPool *thread_pool;
74   int max_threads;
75   gint job_count;
76 };
77
78 typedef struct
79 {
80   GThreadedSocketService *service;
81   GSocketConnection *connection;
82   GObject *source_object;
83 } GThreadedSocketServiceData;
84
85 static void
86 g_threaded_socket_service_func (gpointer _data,
87                                 gpointer user_data)
88 {
89   GThreadedSocketService *threaded = user_data;
90   GThreadedSocketServiceData *data = _data;
91   gboolean result;
92
93   g_signal_emit (data->service, g_threaded_socket_service_run_signal,
94                  0, data->connection, data->source_object, &result);
95
96   g_object_unref (data->service);
97   g_object_unref (data->connection);
98   if (data->source_object)
99     g_object_unref (data->source_object);
100   g_slice_free (GThreadedSocketServiceData, data);
101
102   G_LOCK (job_count);
103   if (threaded->priv->job_count-- == threaded->priv->max_threads)
104     g_socket_service_start (G_SOCKET_SERVICE (threaded));
105   G_UNLOCK (job_count);
106 }
107
108 static gboolean
109 g_threaded_socket_service_incoming (GSocketService    *service,
110                                     GSocketConnection *connection,
111                                     GObject           *source_object)
112 {
113   GThreadedSocketService *threaded;
114   GThreadedSocketServiceData *data;
115
116   threaded = G_THREADED_SOCKET_SERVICE (service);
117
118   data = g_slice_new (GThreadedSocketServiceData);
119   data->service = g_object_ref (service);
120   data->connection = g_object_ref (connection);
121   if (source_object)
122     data->source_object = g_object_ref (source_object);
123   else
124     data->source_object = NULL;
125
126   G_LOCK (job_count);
127   if (++threaded->priv->job_count == threaded->priv->max_threads)
128     g_socket_service_stop (service);
129   G_UNLOCK (job_count);
130
131   g_thread_pool_push (threaded->priv->thread_pool, data, NULL);
132
133
134
135   return FALSE;
136 }
137
138 static void
139 g_threaded_socket_service_init (GThreadedSocketService *service)
140 {
141   service->priv = G_TYPE_INSTANCE_GET_PRIVATE (service,
142                                                G_TYPE_THREADED_SOCKET_SERVICE,
143                                                GThreadedSocketServicePrivate);
144   service->priv->max_threads = 10;
145 }
146
147 static void
148 g_threaded_socket_service_constructed (GObject *object)
149 {
150   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
151
152   service->priv->thread_pool =
153     g_thread_pool_new  (g_threaded_socket_service_func,
154                         service,
155                         service->priv->max_threads,
156                         FALSE,
157                         NULL);
158 }
159
160
161 static void
162 g_threaded_socket_service_finalize (GObject *object)
163 {
164   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
165
166   g_thread_pool_free (service->priv->thread_pool, FALSE, TRUE);
167
168   G_OBJECT_CLASS (g_threaded_socket_service_parent_class)
169     ->finalize (object);
170 }
171
172 static void
173 g_threaded_socket_service_get_property (GObject    *object,
174                                         guint       prop_id,
175                                         GValue     *value,
176                                         GParamSpec *pspec)
177 {
178   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
179
180   switch (prop_id)
181     {
182       case PROP_MAX_THREADS:
183         g_value_set_int (value, service->priv->max_threads);
184         break;
185
186       default:
187         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188     }
189 }
190
191 static void
192 g_threaded_socket_service_set_property (GObject      *object,
193                                         guint         prop_id,
194                                         const GValue *value,
195                                         GParamSpec   *pspec)
196 {
197   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
198
199   switch (prop_id)
200     {
201       case PROP_MAX_THREADS:
202         service->priv->max_threads = g_value_get_int (value);
203         break;
204
205       default:
206         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207     }
208 }
209
210
211 static void
212 g_threaded_socket_service_class_init (GThreadedSocketServiceClass *class)
213 {
214   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
215   GSocketServiceClass *ss_class = &class->parent_class;
216
217   g_type_class_add_private (class, sizeof (GThreadedSocketServicePrivate));
218
219   gobject_class->constructed = g_threaded_socket_service_constructed;
220   gobject_class->finalize = g_threaded_socket_service_finalize;
221   gobject_class->set_property = g_threaded_socket_service_set_property;
222   gobject_class->get_property = g_threaded_socket_service_get_property;
223
224   ss_class->incoming = g_threaded_socket_service_incoming;
225
226   /**
227    * GThreadedSocketService::run:
228    * @service: the #GThreadedSocketService.
229    * @connection: a new #GSocketConnection object.
230    * @source_object: the source_object passed to g_socket_listener_add_address().
231    *
232    * The ::run signal is emitted in a worker thread in response to an
233    * incoming connection. This thread is dedicated to handling
234    * @connection and may perform blocking IO. The signal handler need
235    * not return until the connection is closed.
236    *
237    * Returns: %TRUE to stop further signal handlers from being called
238    */
239   g_threaded_socket_service_run_signal =
240     g_signal_new ("run", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
241                   G_STRUCT_OFFSET (GThreadedSocketServiceClass, run),
242                   g_signal_accumulator_true_handled, NULL,
243                   _gio_marshal_BOOLEAN__OBJECT_OBJECT, G_TYPE_BOOLEAN,
244                   2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
245
246   g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
247                                    g_param_spec_int ("max-threads",
248                                                      P_("Max threads"),
249                                                      P_("The max number of threads handling clients for this service"),
250                                                      -1,
251                                                      G_MAXINT,
252                                                      10,
253                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 }
255
256 /**
257  * g_threaded_socket_service_new:
258  * @max_threads: the maximal number of threads to execute concurrently
259  *   handling incoming clients, -1 means no limit
260  *
261  * Creates a new #GThreadedSocketService with no listeners. Listeners
262  * must be added with g_socket_service_add_listeners().
263  *
264  * Returns: a new #GSocketService.
265  *
266  * Since: 2.22
267  */
268 GSocketService *
269 g_threaded_socket_service_new (int max_threads)
270 {
271   return g_object_new (G_TYPE_THREADED_SOCKET_SERVICE,
272                        "max-threads", max_threads,
273                        NULL);
274 }