1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gnome-vfs-cancellation.c - Cancellation handling for the GNOME Virtual File
5 Copyright (C) 1999 Free Software Foundation
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Gnome Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Author: Ettore Perazzoli <ettore@gnu.org> */
25 #include "gnome-vfs-cancellation.h"
27 #include "gnome-vfs-utils.h"
30 /* WARNING: this code is not general-purpose. It is supposed to make the two
31 sides of the VFS (i.e. the main process/thread and its asynchronous slave)
32 talk in a simple way. For this reason, only the main process/thread should
33 be allowed to call `gnome_vfs_cancellation_cancel()'. *All* the code is
34 based on this assumption. */
37 struct GnomeVFSCancellation {
45 * gnome_vfs_cancellation_new:
47 * Create a new GnomeVFSCancellation object for reporting cancellation to a
50 * Return value: A pointer to the new GnomeVFSCancellation object.
52 GnomeVFSCancellation *
53 gnome_vfs_cancellation_new (void)
55 GnomeVFSCancellation *new;
57 new = g_new (GnomeVFSCancellation, 1);
58 new->cancelled = FALSE;
66 * gnome_vfs_cancellation_destroy:
67 * @cancellation: A GnomeVFSCancellation object
69 * Destroy @cancellation.
72 gnome_vfs_cancellation_destroy (GnomeVFSCancellation *cancellation)
74 g_return_if_fail (cancellation != NULL);
76 if (cancellation->pipe_in >= 0) {
77 close (cancellation->pipe_in);
78 close (cancellation->pipe_out);
80 g_free (cancellation);
84 * gnome_vfs_cancellation_cancel:
85 * @cancellation: A GnomeVFSCancellation object
87 * Send a cancellation request through @cancellation.
90 gnome_vfs_cancellation_cancel (GnomeVFSCancellation *cancellation)
92 g_return_if_fail (cancellation != NULL);
94 GNOME_VFS_ASSERT_PRIMARY_THREAD;
96 if (cancellation->cancelled)
99 if (cancellation->pipe_out >= 0)
100 write (cancellation->pipe_out, "c", 1);
102 cancellation->cancelled = TRUE;
106 * gnome_vfs_cancellation_check:
107 * @cancellation: A GnomeVFSCancellation object
109 * Check for pending cancellation.
111 * Return value: %TRUE if the operation should be interrupted.
114 gnome_vfs_cancellation_check (GnomeVFSCancellation *cancellation)
116 if (cancellation == NULL)
119 return cancellation->cancelled;
123 * gnome_vfs_cancellation_ack:
124 * @cancellation: A GnomeVFSCancellation object
126 * Acknowledge a cancellation. This should be called if
127 * `gnome_vfs_cancellation_check()' returns %TRUE or if `select()' reports that
128 * input is available on the file descriptor returned by
129 * `gnome_vfs_cancellation_get_fd()'.
132 gnome_vfs_cancellation_ack (GnomeVFSCancellation *cancellation)
136 if (cancellation == NULL)
139 if (cancellation->pipe_in >= 0)
140 read (cancellation->pipe_in, &c, 1);
142 cancellation->cancelled = FALSE;
146 * gnome_vfs_cancellation_get_fd:
147 * @cancellation: A GnomeVFSCancellation object
149 * Get a file descriptor -based notificator for @cancellation. When
150 * @cancellation receives a cancellation request, a character will be made
151 * available on the returned file descriptor for input.
153 * This is very useful for detecting cancellation during I/O operations: you
154 * can use the `select()' call to check for available input/output on the file
155 * you are reading/writing, and on the notificator's file descriptor at the
156 * same time. If a data is available on the notificator's file descriptor, you
157 * know you have to cancel the read/write operation.
159 * Return value: the notificator's file descriptor, or -1 if starved of
163 gnome_vfs_cancellation_get_fd (GnomeVFSCancellation *cancellation)
165 g_return_val_if_fail (cancellation != NULL, -1);
167 if (cancellation->pipe_in <= 0) {
170 if (pipe (pipefd) == -1)
173 cancellation->pipe_in = pipefd [0];
174 cancellation->pipe_out = pipefd [1];
177 return cancellation->pipe_in;