1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gio.Cancellable; 26 27 private import glib.ConstructionException; 28 private import glib.ErrorG; 29 private import glib.GException; 30 private import glib.Source; 31 private import gobject.ObjectG; 32 private import gobject.Signals; 33 private import gtkc.gio; 34 public import gtkc.giotypes; 35 private import std.algorithm; 36 37 38 /** 39 * GCancellable is a thread-safe operation cancellation stack used 40 * throughout GIO to allow for cancellation of synchronous and 41 * asynchronous operations. 42 */ 43 public class Cancellable : ObjectG 44 { 45 /** the main Gtk struct */ 46 protected GCancellable* gCancellable; 47 48 /** Get the main Gtk struct */ 49 public GCancellable* getCancellableStruct(bool transferOwnership = false) 50 { 51 if (transferOwnership) 52 ownedRef = false; 53 return gCancellable; 54 } 55 56 /** the main Gtk struct as a void* */ 57 protected override void* getStruct() 58 { 59 return cast(void*)gCancellable; 60 } 61 62 protected override void setStruct(GObject* obj) 63 { 64 gCancellable = cast(GCancellable*)obj; 65 super.setStruct(obj); 66 } 67 68 /** 69 * Sets our main struct and passes it to the parent class. 70 */ 71 public this (GCancellable* gCancellable, bool ownedRef = false) 72 { 73 this.gCancellable = gCancellable; 74 super(cast(GObject*)gCancellable, ownedRef); 75 } 76 77 78 /** */ 79 public static GType getType() 80 { 81 return g_cancellable_get_type(); 82 } 83 84 /** 85 * Creates a new #GCancellable object. 86 * 87 * Applications that want to start one or more operations 88 * that should be cancellable should create a #GCancellable 89 * and pass it to the operations. 90 * 91 * One #GCancellable can be used in multiple consecutive 92 * operations or in multiple concurrent operations. 93 * 94 * Returns: a #GCancellable. 95 * 96 * Throws: ConstructionException GTK+ fails to create the object. 97 */ 98 public this() 99 { 100 auto p = g_cancellable_new(); 101 102 if(p is null) 103 { 104 throw new ConstructionException("null returned by new"); 105 } 106 107 this(cast(GCancellable*) p, true); 108 } 109 110 /** 111 * Gets the top cancellable from the stack. 112 * 113 * Returns: a #GCancellable from the top 114 * of the stack, or %NULL if the stack is empty. 115 */ 116 public static Cancellable getCurrent() 117 { 118 auto p = g_cancellable_get_current(); 119 120 if(p is null) 121 { 122 return null; 123 } 124 125 return ObjectG.getDObject!(Cancellable)(cast(GCancellable*) p); 126 } 127 128 /** 129 * Will set @cancellable to cancelled, and will emit the 130 * #GCancellable::cancelled signal. (However, see the warning about 131 * race conditions in the documentation for that signal if you are 132 * planning to connect to it.) 133 * 134 * This function is thread-safe. In other words, you can safely call 135 * it from a thread other than the one running the operation that was 136 * passed the @cancellable. 137 * 138 * If @cancellable is %NULL, this function returns immediately for convenience. 139 * 140 * The convention within GIO is that cancelling an asynchronous 141 * operation causes it to complete asynchronously. That is, if you 142 * cancel the operation from the same thread in which it is running, 143 * then the operation's #GAsyncReadyCallback will not be invoked until 144 * the application returns to the main loop. 145 */ 146 public void cancel() 147 { 148 g_cancellable_cancel(gCancellable); 149 } 150 151 /** 152 * Convenience function to connect to the #GCancellable::cancelled 153 * signal. Also handles the race condition that may happen 154 * if the cancellable is cancelled right before connecting. 155 * 156 * @callback is called at most once, either directly at the 157 * time of the connect if @cancellable is already cancelled, 158 * or when @cancellable is cancelled in some thread. 159 * 160 * @data_destroy_func will be called when the handler is 161 * disconnected, or immediately if the cancellable is already 162 * cancelled. 163 * 164 * See #GCancellable::cancelled for details on how to use this. 165 * 166 * Since GLib 2.40, the lock protecting @cancellable is not held when 167 * @callback is invoked. This lifts a restriction in place for 168 * earlier GLib versions which now makes it easier to write cleanup 169 * code that unconditionally invokes e.g. g_cancellable_cancel(). 170 * 171 * Params: 172 * callback = The #GCallback to connect. 173 * data = Data to pass to @callback. 174 * dataDestroyFunc = Free function for @data or %NULL. 175 * 176 * Returns: The id of the signal handler or 0 if @cancellable has already 177 * been cancelled. 178 * 179 * Since: 2.22 180 */ 181 public gulong connect(GCallback callback, void* data, GDestroyNotify dataDestroyFunc) 182 { 183 return g_cancellable_connect(gCancellable, callback, data, dataDestroyFunc); 184 } 185 186 /** 187 * Disconnects a handler from a cancellable instance similar to 188 * g_signal_handler_disconnect(). Additionally, in the event that a 189 * signal handler is currently running, this call will block until the 190 * handler has finished. Calling this function from a 191 * #GCancellable::cancelled signal handler will therefore result in a 192 * deadlock. 193 * 194 * This avoids a race condition where a thread cancels at the 195 * same time as the cancellable operation is finished and the 196 * signal handler is removed. See #GCancellable::cancelled for 197 * details on how to use this. 198 * 199 * If @cancellable is %NULL or @handler_id is %0 this function does 200 * nothing. 201 * 202 * Params: 203 * handlerId = Handler id of the handler to be disconnected, or %0. 204 * 205 * Since: 2.22 206 */ 207 public void disconnect(gulong handlerId) 208 { 209 g_cancellable_disconnect(gCancellable, handlerId); 210 } 211 212 /** 213 * Gets the file descriptor for a cancellable job. This can be used to 214 * implement cancellable operations on Unix systems. The returned fd will 215 * turn readable when @cancellable is cancelled. 216 * 217 * You are not supposed to read from the fd yourself, just check for 218 * readable status. Reading to unset the readable status is done 219 * with g_cancellable_reset(). 220 * 221 * After a successful return from this function, you should use 222 * g_cancellable_release_fd() to free up resources allocated for 223 * the returned file descriptor. 224 * 225 * See also g_cancellable_make_pollfd(). 226 * 227 * Returns: A valid file descriptor. %-1 if the file descriptor 228 * is not supported, or on errors. 229 */ 230 public int getFd() 231 { 232 return g_cancellable_get_fd(gCancellable); 233 } 234 235 /** 236 * Checks if a cancellable job has been cancelled. 237 * 238 * Returns: %TRUE if @cancellable is cancelled, 239 * FALSE if called with %NULL or if item is not cancelled. 240 */ 241 public bool isCancelled() 242 { 243 return g_cancellable_is_cancelled(gCancellable) != 0; 244 } 245 246 /** 247 * Creates a #GPollFD corresponding to @cancellable; this can be passed 248 * to g_poll() and used to poll for cancellation. This is useful both 249 * for unix systems without a native poll and for portability to 250 * windows. 251 * 252 * When this function returns %TRUE, you should use 253 * g_cancellable_release_fd() to free up resources allocated for the 254 * @pollfd. After a %FALSE return, do not call g_cancellable_release_fd(). 255 * 256 * If this function returns %FALSE, either no @cancellable was given or 257 * resource limits prevent this function from allocating the necessary 258 * structures for polling. (On Linux, you will likely have reached 259 * the maximum number of file descriptors.) The suggested way to handle 260 * these cases is to ignore the @cancellable. 261 * 262 * You are not supposed to read from the fd yourself, just check for 263 * readable status. Reading to unset the readable status is done 264 * with g_cancellable_reset(). 265 * 266 * Params: 267 * pollfd = a pointer to a #GPollFD 268 * 269 * Returns: %TRUE if @pollfd was successfully initialized, %FALSE on 270 * failure to prepare the cancellable. 271 * 272 * Since: 2.22 273 */ 274 public bool makePollfd(GPollFD* pollfd) 275 { 276 return g_cancellable_make_pollfd(gCancellable, pollfd) != 0; 277 } 278 279 /** 280 * Pops @cancellable off the cancellable stack (verifying that @cancellable 281 * is on the top of the stack). 282 */ 283 public void popCurrent() 284 { 285 g_cancellable_pop_current(gCancellable); 286 } 287 288 /** 289 * Pushes @cancellable onto the cancellable stack. The current 290 * cancellable can then be received using g_cancellable_get_current(). 291 * 292 * This is useful when implementing cancellable operations in 293 * code that does not allow you to pass down the cancellable object. 294 * 295 * This is typically called automatically by e.g. #GFile operations, 296 * so you rarely have to call this yourself. 297 */ 298 public void pushCurrent() 299 { 300 g_cancellable_push_current(gCancellable); 301 } 302 303 /** 304 * Releases a resources previously allocated by g_cancellable_get_fd() 305 * or g_cancellable_make_pollfd(). 306 * 307 * For compatibility reasons with older releases, calling this function 308 * is not strictly required, the resources will be automatically freed 309 * when the @cancellable is finalized. However, the @cancellable will 310 * block scarce file descriptors until it is finalized if this function 311 * is not called. This can cause the application to run out of file 312 * descriptors when many #GCancellables are used at the same time. 313 * 314 * Since: 2.22 315 */ 316 public void releaseFd() 317 { 318 g_cancellable_release_fd(gCancellable); 319 } 320 321 /** 322 * Resets @cancellable to its uncancelled state. 323 * 324 * If cancellable is currently in use by any cancellable operation 325 * then the behavior of this function is undefined. 326 * 327 * Note that it is generally not a good idea to reuse an existing 328 * cancellable for more operations after it has been cancelled once, 329 * as this function might tempt you to do. The recommended practice 330 * is to drop the reference to a cancellable after cancelling it, 331 * and let it die with the outstanding async operations. You should 332 * create a fresh cancellable for further async operations. 333 */ 334 public void reset() 335 { 336 g_cancellable_reset(gCancellable); 337 } 338 339 /** 340 * If the @cancellable is cancelled, sets the error to notify 341 * that the operation was cancelled. 342 * 343 * Returns: %TRUE if @cancellable was cancelled, %FALSE if it was not 344 * 345 * Throws: GException on failure. 346 */ 347 public bool setErrorIfCancelled() 348 { 349 GError* err = null; 350 351 auto p = g_cancellable_set_error_if_cancelled(gCancellable, &err) != 0; 352 353 if (err !is null) 354 { 355 throw new GException( new ErrorG(err) ); 356 } 357 358 return p; 359 } 360 361 /** 362 * Creates a source that triggers if @cancellable is cancelled and 363 * calls its callback of type #GCancellableSourceFunc. This is 364 * primarily useful for attaching to another (non-cancellable) source 365 * with g_source_add_child_source() to add cancellability to it. 366 * 367 * For convenience, you can call this with a %NULL #GCancellable, 368 * in which case the source will never trigger. 369 * 370 * The new #GSource will hold a reference to the #GCancellable. 371 * 372 * Returns: the new #GSource. 373 * 374 * Since: 2.28 375 */ 376 public Source sourceNew() 377 { 378 auto p = g_cancellable_source_new(gCancellable); 379 380 if(p is null) 381 { 382 return null; 383 } 384 385 return new Source(cast(GSource*) p, true); 386 } 387 388 protected class OnCancelledDelegateWrapper 389 { 390 static OnCancelledDelegateWrapper[] listeners; 391 void delegate(Cancellable) dlg; 392 gulong handlerId; 393 394 this(void delegate(Cancellable) dlg) 395 { 396 this.dlg = dlg; 397 this.listeners ~= this; 398 } 399 400 void remove(OnCancelledDelegateWrapper source) 401 { 402 foreach(index, wrapper; listeners) 403 { 404 if (wrapper.handlerId == source.handlerId) 405 { 406 listeners[index] = null; 407 listeners = std.algorithm.remove(listeners, index); 408 break; 409 } 410 } 411 } 412 } 413 414 /** 415 * Emitted when the operation has been cancelled. 416 * 417 * Can be used by implementations of cancellable operations. If the 418 * operation is cancelled from another thread, the signal will be 419 * emitted in the thread that cancelled the operation, not the 420 * thread that is running the operation. 421 * 422 * Note that disconnecting from this signal (or any signal) in a 423 * multi-threaded program is prone to race conditions. For instance 424 * it is possible that a signal handler may be invoked even after 425 * a call to g_signal_handler_disconnect() for that handler has 426 * already returned. 427 * 428 * There is also a problem when cancellation happens right before 429 * connecting to the signal. If this happens the signal will 430 * unexpectedly not be emitted, and checking before connecting to 431 * the signal leaves a race condition where this is still happening. 432 * 433 * In order to make it safe and easy to connect handlers there 434 * are two helper functions: g_cancellable_connect() and 435 * g_cancellable_disconnect() which protect against problems 436 * like this. 437 * 438 * An example of how to us this: 439 * |[<!-- language="C" --> 440 * // Make sure we don't do unnecessary work if already cancelled 441 * if (g_cancellable_set_error_if_cancelled (cancellable, error)) 442 * return; 443 * 444 * // Set up all the data needed to be able to handle cancellation 445 * // of the operation 446 * my_data = my_data_new (...); 447 * 448 * id = 0; 449 * if (cancellable) 450 * id = g_cancellable_connect (cancellable, 451 * G_CALLBACK (cancelled_handler) 452 * data, NULL); 453 * 454 * // cancellable operation here... 455 * 456 * g_cancellable_disconnect (cancellable, id); 457 * 458 * // cancelled_handler is never called after this, it is now safe 459 * // to free the data 460 * my_data_free (my_data); 461 * ]| 462 * 463 * Note that the cancelled signal is emitted in the thread that 464 * the user cancelled from, which may be the main thread. So, the 465 * cancellable signal should not do something that can block. 466 */ 467 gulong addOnCancelled(void delegate(Cancellable) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 468 { 469 auto wrapper = new OnCancelledDelegateWrapper(dlg); 470 wrapper.handlerId = Signals.connectData( 471 this, 472 "cancelled", 473 cast(GCallback)&callBackCancelled, 474 cast(void*)wrapper, 475 cast(GClosureNotify)&callBackCancelledDestroy, 476 connectFlags); 477 return wrapper.handlerId; 478 } 479 480 extern(C) static void callBackCancelled(GCancellable* cancellableStruct, OnCancelledDelegateWrapper wrapper) 481 { 482 wrapper.dlg(wrapper.outer); 483 } 484 485 extern(C) static void callBackCancelledDestroy(OnCancelledDelegateWrapper wrapper, GClosure* closure) 486 { 487 wrapper.remove(wrapper); 488 } 489 }