Added SILC Server library.
[silc.git] / lib / silcutil / silcasync.c
1 /*
2
3   silcasync.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21
22 /* Halts async operation */
23
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
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
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
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
85 SilcBool silc_async_init(SilcAsyncOperation op,
86                          SilcAsyncOperationAbort abort_cb,
87                          SilcAsyncOperationPause pause_cb,
88                          void *context)
89 {
90   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
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
110 void *silc_async_get_context(SilcAsyncOperation op)
111 {
112   return op->context;
113 }