[15496] | 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 |
---|
| 3 | System access methods. |
---|
| 4 | |
---|
| 5 | Copyright (C) 1999 Free Software Foundation |
---|
| 6 | |
---|
| 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. |
---|
| 11 | |
---|
| 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. |
---|
| 16 | |
---|
| 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. |
---|
| 21 | |
---|
| 22 | Author: Ettore Perazzoli <ettore@gnu.org> */ |
---|
| 23 | |
---|
| 24 | #include <config.h> |
---|
[17127] | 25 | #include "gnome-vfs-cancellation.h" |
---|
[15496] | 26 | |
---|
[17127] | 27 | #include "gnome-vfs-private.h" |
---|
| 28 | #include "gnome-vfs.h" |
---|
[15496] | 29 | #include <unistd.h> |
---|
| 30 | |
---|
| 31 | /* WARNING: this code is not general-purpose. It is supposed to make the two |
---|
| 32 | sides of the VFS (i.e. the main process/thread and its asynchronous slave) |
---|
| 33 | talk in a simple way. For this reason, only the main process/thread should |
---|
| 34 | be allowed to call `gnome_vfs_cancellation_cancel()'. *All* the code is |
---|
| 35 | based on this assumption. */ |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | struct GnomeVFSCancellation { |
---|
| 39 | gboolean cancelled; |
---|
| 40 | gint pipe_in; |
---|
| 41 | gint pipe_out; |
---|
| 42 | }; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * gnome_vfs_cancellation_new: |
---|
| 47 | * |
---|
| 48 | * Create a new GnomeVFSCancellation object for reporting cancellation to a |
---|
| 49 | * GNOME VFS module. |
---|
| 50 | * |
---|
| 51 | * Return value: A pointer to the new GnomeVFSCancellation object. |
---|
| 52 | **/ |
---|
| 53 | GnomeVFSCancellation * |
---|
| 54 | gnome_vfs_cancellation_new (void) |
---|
| 55 | { |
---|
| 56 | GnomeVFSCancellation *new; |
---|
| 57 | gint pipefd[2]; |
---|
| 58 | |
---|
| 59 | if (pipe (pipefd) == -1) |
---|
| 60 | return NULL; |
---|
| 61 | |
---|
| 62 | new = g_new (GnomeVFSCancellation, 1); |
---|
| 63 | new->cancelled = FALSE; |
---|
| 64 | new->pipe_in = pipefd[0]; |
---|
| 65 | new->pipe_out = pipefd[1]; |
---|
| 66 | |
---|
| 67 | return new; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * gnome_vfs_cancellation_destroy: |
---|
| 72 | * @cancellation: A GnomeVFSCancellation object |
---|
| 73 | * |
---|
| 74 | * Destroy @cancellation. |
---|
| 75 | **/ |
---|
| 76 | void |
---|
| 77 | gnome_vfs_cancellation_destroy (GnomeVFSCancellation *cancellation) |
---|
| 78 | { |
---|
| 79 | g_return_if_fail (cancellation != NULL); |
---|
| 80 | |
---|
| 81 | close (cancellation->pipe_in); |
---|
| 82 | close (cancellation->pipe_out); |
---|
| 83 | g_free (cancellation); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * gnome_vfs_cancellation_cancel: |
---|
| 88 | * @cancellation: A GnomeVFSCancellation object |
---|
| 89 | * |
---|
| 90 | * Send a cancellation request through @cancellation. |
---|
| 91 | **/ |
---|
| 92 | void |
---|
| 93 | gnome_vfs_cancellation_cancel (GnomeVFSCancellation *cancellation) |
---|
| 94 | { |
---|
| 95 | g_return_if_fail (cancellation != NULL); |
---|
| 96 | |
---|
[17127] | 97 | GNOME_VFS_ASSERT_PRIMARY_THREAD; |
---|
| 98 | |
---|
[15496] | 99 | if (cancellation->cancelled) |
---|
| 100 | return; |
---|
| 101 | |
---|
| 102 | write (cancellation->pipe_out, "c", 1); |
---|
| 103 | cancellation->cancelled = TRUE; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * gnome_vfs_cancellation_check: |
---|
| 108 | * @cancellation: A GnomeVFSCancellation object |
---|
| 109 | * |
---|
| 110 | * Check for pending cancellation. |
---|
| 111 | * |
---|
| 112 | * Return value: %TRUE if the operation should be interrupted. |
---|
| 113 | **/ |
---|
| 114 | gboolean |
---|
| 115 | gnome_vfs_cancellation_check (GnomeVFSCancellation *cancellation) |
---|
| 116 | { |
---|
| 117 | if (cancellation == NULL) |
---|
| 118 | return FALSE; |
---|
| 119 | |
---|
| 120 | return cancellation->cancelled; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * gnome_vfs_cancellation_ack: |
---|
| 125 | * @cancellation: A GnomeVFSCancellation object |
---|
| 126 | * |
---|
| 127 | * Acknowledge a cancellation. This should be called if |
---|
| 128 | * `gnome_vfs_cancellation_check()' returns %TRUE or if `select()' reports that |
---|
| 129 | * input is available on the file descriptor returned by |
---|
| 130 | * `gnome_vfs_cancellation_get_fd()'. |
---|
| 131 | **/ |
---|
| 132 | void |
---|
| 133 | gnome_vfs_cancellation_ack (GnomeVFSCancellation *cancellation) |
---|
| 134 | { |
---|
| 135 | gchar c; |
---|
| 136 | |
---|
| 137 | if (cancellation == NULL) |
---|
| 138 | return; |
---|
| 139 | |
---|
| 140 | read (cancellation->pipe_in, &c, 1); |
---|
| 141 | cancellation->cancelled = FALSE; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * gnome_vfs_cancellation_get_fd: |
---|
| 146 | * @cancellation: A GnomeVFSCancellation object |
---|
| 147 | * |
---|
| 148 | * Get a file descriptor -based notificator for @cancellation. When |
---|
| 149 | * @cancellation receives a cancellation request, a character will be made |
---|
| 150 | * available on the returned file descriptor for input. |
---|
| 151 | * |
---|
| 152 | * This is very useful for detecting cancellation during I/O operations: you |
---|
| 153 | * can use the `select()' call to check for available input/output on the file |
---|
| 154 | * you are reading/writing, and on the notificator's file descriptor at the |
---|
| 155 | * same time. If a data is available on the notificator's file descriptor, you |
---|
| 156 | * know you have to cancel the read/write operation. |
---|
| 157 | * |
---|
| 158 | * Return value: the notificator's file descriptor. |
---|
| 159 | **/ |
---|
| 160 | gint |
---|
| 161 | gnome_vfs_cancellation_get_fd (GnomeVFSCancellation *cancellation) |
---|
| 162 | { |
---|
| 163 | g_return_val_if_fail (cancellation != NULL, -1); |
---|
| 164 | |
---|
| 165 | return cancellation->pipe_in; |
---|
| 166 | } |
---|