Add infrastructure to run a function asynchronously.
This adds start_async() and finish_async(), which runs a function asynchronously. Communication with the caller happens only via pipes. For this reason, this implementation forks off a child process that runs the function. [sp: Style nit fixed by removing unnecessary block on if condition inside of start_async()] Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Johannes Sixt committed
Oct 19, 2007 at 21:48 UTC
2d22c208304156892fd6674e0055a3212c1e2d2e
2 files changed
+65
-8
run-command.c
+43
-8
@@ -127,16 +127,11 @@ int start_command(struct child_process *cmd)
127
return 0;
128
}
129
130
-int finish_command(struct child_process *cmd)
130
+static int wait_or_whine(pid_t pid)
131
{
132
- if (cmd->close_in)
133
- close(cmd->in);
134
- if (cmd->close_out)
135
- close(cmd->out);
136
-
132
for (;;) {
133
int status, code;
139
- pid_t waiting = waitpid(cmd->pid, &status, 0);
134
+ pid_t waiting = waitpid(pid, &status, 0);
135
136
if (waiting < 0) {
137
if (errno == EINTR)
@@ -144,7 +139,7 @@ int finish_command(struct child_process *cmd)
139
error("waitpid failed (%s)", strerror(errno));
140
return -ERR_RUN_COMMAND_WAITPID;
141
}
147
- if (waiting != cmd->pid)
142
+ if (waiting != pid)
143
return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
144
if (WIFSIGNALED(status))
145
return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
@@ -158,6 +153,15 @@ int finish_command(struct child_process *cmd)
153
}
154
}
155
156
+int finish_command(struct child_process *cmd)
157
+{
158
+ if (cmd->close_in)
159
+ close(cmd->in);
160
+ if (cmd->close_out)
161
+ close(cmd->out);
162
+ return wait_or_whine(cmd->pid);
163
+}
164
+
165
int run_command(struct child_process *cmd)
166
{
167
int code = start_command(cmd);
@@ -200,3 +204,34 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const
204
cmd.env = env;
205
return run_command(&cmd);
206
}
207
+
208
+int start_async(struct async *async)
209
+{
210
+ int pipe_out[2];
211
+
212
+ if (pipe(pipe_out) < 0)
213
+ return error("cannot create pipe: %s", strerror(errno));
214
+
215
+ async->pid = fork();
216
+ if (async->pid < 0) {
217
+ error("fork (async) failed: %s", strerror(errno));
218
+ close_pair(pipe_out);
219
+ return -1;
220
+ }
221
+ if (!async->pid) {
222
+ close(pipe_out[0]);
223
+ exit(!!async->proc(pipe_out[1], async->data));
224
+ }
225
+ async->out = pipe_out[0];
226
+ close(pipe_out[1]);
227
+ return 0;
228
+}
229
+
230
+int finish_async(struct async *async)
231
+{
232
+ int ret = 0;
233
+
234
+ if (wait_or_whine(async->pid))
235
+ ret = error("waitpid (async) failed");
236
+ return ret;
237
+}
run-command.h
+22
@@ -43,4 +43,26 @@ int run_command_v_opt_cd(const char **argv, int opt, const char *dir);
43
*/
44
int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env);
45
46
+/*
47
+ * The purpose of the following functions is to feed a pipe by running
48
+ * a function asynchronously and providing output that the caller reads.
49
+ *
50
+ * It is expected that no synchronization and mutual exclusion between
51
+ * the caller and the feed function is necessary so that the function
52
+ * can run in a thread without interfering with the caller.
53
+ */
54
+struct async {
55
+ /*
56
+ * proc writes to fd and closes it;
57
+ * returns 0 on success, non-zero on failure
58
+ */
59
+ int (*proc)(int fd, void *data);
60
+ void *data;
61
+ int out; /* caller reads from here and closes it */
62
+ pid_t pid;
63
+};
64
+
65
+int start_async(struct async *async);
66
+int finish_async(struct async *async);
67
+
68
#endif