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