* plugins/build-basic-autotools/configuration-list.c,
[anjuta-git-plugin.git] / libanjuta / anjuta-async-command.c
blob6dcc2b599b8621223982b5c011c6e7ddfaca916e
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2007 <jrliggett@cox.net>
6 * Portions based on the original Subversion plugin
7 * Copyright (C) Johannes Schmid 2005
8 *
9 * anjuta is free software.
11 * You may redistribute it and/or modify it under the terms of the
12 * GNU General Public License, as published by the Free Software
13 * Foundation; either version 2 of the License, or (at your option)
14 * any later version.
16 * anjuta is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with anjuta. If not, write to:
23 * The Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor
25 * Boston, MA 02110-1301, USA.
28 #include "anjuta-async-command.h"
30 /**
31 * SECTION: anjuta-async-command
32 * @short_description: #AnjutaCommand subclass that serves as the base for
33 * commands that need to run in another thread.
34 * @include: libanjuta/anjuta-async-command.h
36 * #AnjutaAsyncCommand provides a simple way for plugins to run tasks that
37 * are synchronous and usually take several seconds or longer to execute in
38 * another thread so that such tasks do no block Anjuta's user interface.
40 * #AnjutaAsyncCommand automatically runs and manages the thread when the
41 * command starts, and destroys it when the command finishes. Aside from
42 * locking protected data with anjuta_async_command_lock/unlock, clients, and
43 * even commands themselves need not even be concerned that their tasks are
44 * rnning on another thread.
46 * For an example of how #AnjutaAsyncCommand is used, see the Subversion plugin.
49 struct _AnjutaAsyncCommandPriv
51 GMutex *mutex;
52 guint return_code;
53 gboolean complete;
54 gboolean new_data_arrived;
57 G_DEFINE_TYPE (AnjutaAsyncCommand, anjuta_async_command, ANJUTA_TYPE_COMMAND);
59 static void
60 anjuta_async_command_init (AnjutaAsyncCommand *self)
62 self->priv = g_new0 (AnjutaAsyncCommandPriv, 1);
64 self->priv->mutex = g_mutex_new ();
67 static void
68 anjuta_async_command_finalize (GObject *object)
70 AnjutaAsyncCommand *self;
72 self = ANJUTA_ASYNC_COMMAND (object);
74 g_mutex_free (self->priv->mutex);
75 g_idle_remove_by_data (self);
77 g_free (self->priv);
79 G_OBJECT_CLASS (anjuta_async_command_parent_class)->finalize (object);
82 static gboolean
83 anjuta_async_command_notification_poll (AnjutaCommand *command)
85 AnjutaAsyncCommand *self;
87 self = ANJUTA_ASYNC_COMMAND (command);
89 if (self->priv->new_data_arrived &&
90 g_mutex_trylock (self->priv->mutex))
92 g_signal_emit_by_name (command, "data-arrived");
93 g_mutex_unlock (self->priv->mutex);
94 self->priv->new_data_arrived = FALSE;
97 if (self->priv->complete)
99 g_signal_emit_by_name (command, "command-finished",
100 self->priv->return_code);
101 return FALSE;
103 else
104 return TRUE;
108 static gpointer
109 anjuta_async_command_thread (AnjutaCommand *command)
111 guint return_code;
113 return_code = ANJUTA_COMMAND_GET_CLASS (command)->run (command);
114 anjuta_command_notify_complete (command, return_code);
115 return NULL;
118 static void
119 start_command (AnjutaCommand *command)
121 g_idle_add ((GSourceFunc) anjuta_async_command_notification_poll,
122 command);
123 g_thread_create ((GThreadFunc) anjuta_async_command_thread,
124 command, FALSE, NULL);
127 static void
128 notify_data_arrived (AnjutaCommand *command)
130 AnjutaAsyncCommand *self;
132 self = ANJUTA_ASYNC_COMMAND (command);
134 self->priv->new_data_arrived = TRUE;
137 static void
138 notify_complete (AnjutaCommand *command, guint return_code)
140 AnjutaAsyncCommand *self;
142 self = ANJUTA_ASYNC_COMMAND (command);
144 self->priv->complete = TRUE;
145 self->priv->return_code = return_code;
148 static void
149 anjuta_async_command_class_init (AnjutaAsyncCommandClass *klass)
151 GObjectClass* object_class = G_OBJECT_CLASS (klass);
152 AnjutaCommandClass* parent_class = ANJUTA_COMMAND_CLASS (klass);
154 object_class->finalize = anjuta_async_command_finalize;
156 parent_class->start = start_command;
157 parent_class->notify_data_arrived = notify_data_arrived;
158 parent_class->notify_complete = notify_complete;
161 void
162 anjuta_async_command_set_error_message (AnjutaCommand *command,
163 gchar *error_message)
165 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
166 ANJUTA_COMMAND_GET_CLASS (command)->set_error_message (command,
167 error_message);
168 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
171 gchar *
172 anjuta_async_command_get_error_message (AnjutaCommand *command)
174 gchar *error_message;
176 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
177 error_message = ANJUTA_COMMAND_GET_CLASS (command)->get_error_message (command);
178 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
180 return error_message;
184 * anjuta_async_command_lock:
185 * @self: AnjutaAsyncCommand object.
187 * Locks the command's built-in mutex.
189 void
190 anjuta_async_command_lock (AnjutaAsyncCommand *self)
192 g_mutex_lock (self->priv->mutex);
196 * anjuta_async_command_unlock:
197 * @self: AnjutaAsyncCommand object.
199 * Unlocks the command's built-in mutex.
201 void
202 anjuta_async_command_unlock (AnjutaAsyncCommand *self)
204 g_mutex_unlock (self->priv->mutex);