1 /*
2 
3   silcasync.c
4 
5   Author: Pekka Riikonen <priikone@silcnet.org>
6 
7   Copyright (C) 2005, 2006 Pekka Riikonen
8 
9   The contents of this file are subject to one of the Licenses specified
10   in the COPYING file;  You may not use this file except in compliance
11   with the License.
12 
13   The software distributed under the License is distributed on an "AS IS"
14   basis, in the hope that it will be useful, but WITHOUT WARRANTY OF ANY
15   KIND, either expressed or implied.  See the COPYING file for more
16   information.
17 
18 */
19 
20 #include "silc.h"
21 
22 /* Halts async operation */
23 
silc_async_halt(SilcAsyncOperation op)24 SilcBool silc_async_halt(SilcAsyncOperation op)
25 {
26   SILC_LOG_DEBUG(("Halting async operation"));
27 
28   if (op->pause_cb)
29     return op->pause_cb(op, TRUE, op->context);
30 
31   return FALSE;
32 }
33 
34 /* Resumes async operation */
35 
silc_async_resume(SilcAsyncOperation op)36 SilcBool silc_async_resume(SilcAsyncOperation op)
37 {
38   SILC_LOG_DEBUG(("Resuming async operation"));
39 
40   if (op->pause_cb)
41     return op->pause_cb(op, FALSE, op->context);
42 
43   return FALSE;
44 }
45 
46 /* Aborts async operation */
47 
silc_async_abort(SilcAsyncOperation op,SilcAsyncOperationAbort abort_cb,void * context)48 void silc_async_abort(SilcAsyncOperation op,
49                       SilcAsyncOperationAbort abort_cb, void *context)
50 {
51   SILC_LOG_DEBUG(("Aborting async operation"));
52 
53   if (op->abort_cb)
54     op->abort_cb(op, op->context);
55 
56   if (abort_cb)
57     abort_cb(op, context);
58 
59   silc_async_free(op);
60 }
61 
62 /* Creates new async operation */
63 
silc_async_alloc(SilcAsyncOperationAbort abort_cb,SilcAsyncOperationPause pause_cb,void * context)64 SilcAsyncOperation silc_async_alloc(SilcAsyncOperationAbort abort_cb,
65 				    SilcAsyncOperationPause pause_cb,
66 				    void *context)
67 {
68   SilcAsyncOperation op;
69 
70   SILC_LOG_DEBUG(("Creating new async operation"));
71 
72   op = silc_calloc(1, sizeof(*op));
73   if (!op)
74     return NULL;
75 
76   silc_async_init(op, abort_cb, pause_cb, context);
77 
78   op->allocated = TRUE;
79 
80   return op;
81 }
82 
83 /* Creates new async operation */
84 
silc_async_init(SilcAsyncOperation op,SilcAsyncOperationAbort abort_cb,SilcAsyncOperationPause pause_cb,void * context)85 SilcBool silc_async_init(SilcAsyncOperation op,
86 			 SilcAsyncOperationAbort abort_cb,
87 			 SilcAsyncOperationPause pause_cb,
88 			 void *context)
89 {
90   SILC_ASSERT(abort_cb);
91   op->abort_cb = abort_cb;
92   op->pause_cb = pause_cb;
93   op->context = context;
94   op->allocated = FALSE;
95   return TRUE;
96 }
97 
98 /* Stops async operation */
99 
silc_async_free(SilcAsyncOperation op)100 void silc_async_free(SilcAsyncOperation op)
101 {
102   if (op->allocated) {
103     SILC_LOG_DEBUG(("Stopping async operation"));
104     silc_free(op);
105   }
106 }
107 
108 /* Return context */
109 
silc_async_get_context(SilcAsyncOperation op)110 void *silc_async_get_context(SilcAsyncOperation op)
111 {
112   return op->context;
113 }
114