GApplication: add a "resource base path"
[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, see <http://www.gnu.org/licenses/>.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  *          Alexander Larsson <alexl@redhat.com>
21  */
22
23 /**
24  * SECTION:gthreadedsocketservice
25  * @title: GThreadedSocketService
26  * @short_description: A threaded GSocketService
27  * @include: gio/gio.h
28  * @see_also: #GSocketService.
29  *
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.
34  *
35  * The signal handler may perform blocking IO and need not return
36  * until the connection is closed.
37  *
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.
42  *
43  * As with #GSocketService, you may connect to #GThreadedSocketService::run,
44  * or subclass and override the default handler.
45  */
46
47 #include "config.h"
48 #include "gsocketconnection.h"
49 #include "gthreadedsocketservice.h"
50 #include "glibintl.h"
51
52 struct _GThreadedSocketServicePrivate
53 {
54   GThreadPool *thread_pool;
55   int max_threads;
56   gint job_count;
57 };
58
59 static guint g_threaded_socket_service_run_signal;
60
61 G_DEFINE_TYPE_WITH_PRIVATE (GThreadedSocketService,
62                             g_threaded_socket_service,
63                             G_TYPE_SOCKET_SERVICE)
64
65 enum
66 {
67   PROP_0,
68   PROP_MAX_THREADS
69 };
70
71 G_LOCK_DEFINE_STATIC(job_count);
72
73 typedef struct
74 {
75   GThreadedSocketService *service;
76   GSocketConnection *connection;
77   GObject *source_object;
78 } GThreadedSocketServiceData;
79
80 static void
81 g_threaded_socket_service_func (gpointer _data,
82                                 gpointer user_data)
83 {
84   GThreadedSocketService *threaded = user_data;
85   GThreadedSocketServiceData *data = _data;
86   gboolean result;
87
88   g_signal_emit (data->service, g_threaded_socket_service_run_signal,
89                  0, data->connection, data->source_object, &result);
90
91   g_object_unref (data->service);
92   g_object_unref (data->connection);
93   if (data->source_object)
94     g_object_unref (data->source_object);
95   g_slice_free (GThreadedSocketServiceData, data);
96
97   G_LOCK (job_count);
98   if (threaded->priv->job_count-- == threaded->priv->max_threads)
99     g_socket_service_start (G_SOCKET_SERVICE (threaded));
100   G_UNLOCK (job_count);
101 }
102
103 static gboolean
104 g_threaded_socket_service_incoming (GSocketService    *service,
105                                     GSocketConnection *connection,
106                                     GObject           *source_object)
107 {
108   GThreadedSocketService *threaded;
109   GThreadedSocketServiceData *data;
110
111   threaded = G_THREADED_SOCKET_SERVICE (service);
112
113   data = g_slice_new (GThreadedSocketServiceData);
114   data->service = g_object_ref (service);
115   data->connection = g_object_ref (connection);
116   if (source_object)
117     data->source_object = g_object_ref (source_object);
118   else
119     data->source_object = NULL;
120
121   G_LOCK (job_count);
122   if (++threaded->priv->job_count == threaded->priv->max_threads)
123     g_socket_service_stop (service);
124   G_UNLOCK (job_count);
125
126   g_thread_pool_push (threaded->priv->thread_pool, data, NULL);
127
128
129
130   return FALSE;
131 }
132
133 static void
134 g_threaded_socket_service_init (GThreadedSocketService *service)
135 {
136   service->priv = g_threaded_socket_service_get_instance_private (service);
137   service->priv->max_threads = 10;
138 }
139
140 static void
141 g_threaded_socket_service_constructed (GObject *object)
142 {
143   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
144
145   service->priv->thread_pool =
146     g_thread_pool_new  (g_threaded_socket_service_func,
147                         service,
148                         service->priv->max_threads,
149                         FALSE,
150                         NULL);
151 }
152
153
154 static void
155 g_threaded_socket_service_finalize (GObject *object)
156 {
157   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
158
159   g_thread_pool_free (service->priv->thread_pool, FALSE, TRUE);
160
161   G_OBJECT_CLASS (g_threaded_socket_service_parent_class)
162     ->finalize (object);
163 }
164
165 static void
166 g_threaded_socket_service_get_property (GObject    *object,
167                                         guint       prop_id,
168                                         GValue     *value,
169                                         GParamSpec *pspec)
170 {
171   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
172
173   switch (prop_id)
174     {
175       case PROP_MAX_THREADS:
176         g_value_set_int (value, service->priv->max_threads);
177         break;
178
179       default:
180         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181     }
182 }
183
184 static void
185 g_threaded_socket_service_set_property (GObject      *object,
186                                         guint         prop_id,
187                                         const GValue *value,
188                                         GParamSpec   *pspec)
189 {
190   GThreadedSocketService *service = G_THREADED_SOCKET_SERVICE (object);
191
192   switch (prop_id)
193     {
194       case PROP_MAX_THREADS:
195         service->priv->max_threads = g_value_get_int (value);
196         break;
197
198       default:
199         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200     }
201 }
202
203
204 static void
205 g_threaded_socket_service_class_init (GThreadedSocketServiceClass *class)
206 {
207   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
208   GSocketServiceClass *ss_class = &class->parent_class;
209
210   gobject_class->constructed = g_threaded_socket_service_constructed;
211   gobject_class->finalize = g_threaded_socket_service_finalize;
212   gobject_class->set_property = g_threaded_socket_service_set_property;
213   gobject_class->get_property = g_threaded_socket_service_get_property;
214
215   ss_class->incoming = g_threaded_socket_service_incoming;
216
217   /**
218    * GThreadedSocketService::run:
219    * @service: the #GThreadedSocketService.
220    * @connection: a new #GSocketConnection object.
221    * @source_object: the source_object passed to g_socket_listener_add_address().
222    *
223    * The ::run signal is emitted in a worker thread in response to an
224    * incoming connection. This thread is dedicated to handling
225    * @connection and may perform blocking IO. The signal handler need
226    * not return until the connection is closed.
227    *
228    * Returns: %TRUE to stop further signal handlers from being called
229    */
230   g_threaded_socket_service_run_signal =
231     g_signal_new ("run", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
232                   G_STRUCT_OFFSET (GThreadedSocketServiceClass, run),
233                   g_signal_accumulator_true_handled, NULL,
234                   NULL, G_TYPE_BOOLEAN,
235                   2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
236
237   g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
238                                    g_param_spec_int ("max-threads",
239                                                      P_("Max threads"),
240                                                      P_("The max number of threads handling clients for this service"),
241                                                      -1,
242                                                      G_MAXINT,
243                                                      10,
244                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 }
246
247 /**
248  * g_threaded_socket_service_new:
249  * @max_threads: the maximal number of threads to execute concurrently
250  *   handling incoming clients, -1 means no limit
251  *
252  * Creates a new #GThreadedSocketService with no listeners. Listeners
253  * must be added with one of the #GSocketListener "add" methods.
254  *
255  * Returns: a new #GSocketService.
256  *
257  * Since: 2.22
258  */
259 GSocketService *
260 g_threaded_socket_service_new (int max_threads)
261 {
262   return g_object_new (G_TYPE_THREADED_SOCKET_SERVICE,
263                        "max-threads", max_threads,
264                        NULL);
265 }