File: defer.c

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (209 lines) | stat: -rw-r--r-- 3,805 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * ion/libmainloop/defer.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2007.
 *
 * See the included file LICENSE for details.
 */

/* This file contains routines for deferred execution of potentially
 * dangerous actions. They're called upon returning to the main
 * loop.
 */

#include <libtu/obj.h>
#include <libtu/objp.h>
#include <libtu/types.h>
#include <libtu/misc.h>
#include <libtu/dlist.h>
#include <libtu/output.h>
#include <libtu/locale.h>
#include "defer.h"


DECLSTRUCT(WDeferred){
    Watch watch;
    WDeferredAction *action;
    ExtlFn fn;
    WDeferred *next, *prev;
    WDeferred **list;
};


static WDeferred *deferred=NULL;


#define N_DBUF 16

/* To avoid allocating memory all the time, we use a small
 * buffer that should be able to contain the small expected
 * number of simultaneous deferred actions.
 */
static WDeferred dbuf[N_DBUF];
static int dbuf_used=0;


static WDeferred *alloc_defer()
{
    int i;

    /* Keeping it simple -- this naive loop should do it
     * as N_DBUF is small.
     */
    for(i=0; i<N_DBUF; i++){
        if(!(dbuf_used&(1<<i))){
            dbuf_used|=(1<<i);
            return dbuf+i;
        }
    }
    return ALLOC(WDeferred);
}


static void free_defer(WDeferred *d)
{
    if(d>=dbuf && d<dbuf+N_DBUF){
        dbuf_used&=~(1<<(d-dbuf));
        return;
    }
    FREE(d);
}


static void defer_watch_handler(Watch *w, Obj *UNUSED(obj))
{
    WDeferred *d=(WDeferred*)w;

    UNLINK_ITEM(*(WDeferred**)(d->list), d, next, prev);

    free_defer(d);

    warn(TR("Object destroyed while deferred actions are still pending."));
}


static bool already_deferred(Obj *obj, WDeferredAction *action,
                             WDeferred *list)
{
    WDeferred *d;

    for(d=list; d!=NULL; d=d->next){
        if(d->action==action && d->watch.obj==obj)
            return TRUE;
    }

    return FALSE;
}


bool mainloop_defer_action_on_list(Obj *obj, WDeferredAction *action,
                                   WDeferred **list)
{
    WDeferred *d;

    if(already_deferred(obj, action, *list))
        return TRUE;

    d=alloc_defer();

    if(d==NULL){
        warn_err();
        return FALSE;
    }

    d->action=action;
    d->list=list;
    d->fn=extl_fn_none();

    if(obj!=NULL)
        watch_setup(&(d->watch), obj, defer_watch_handler);

    LINK_ITEM(*list, d, next, prev);

    return TRUE;
}


bool mainloop_defer_action(Obj *obj, WDeferredAction *action)
{
    return mainloop_defer_action_on_list(obj, action, &deferred);
}


bool mainloop_defer_destroy(Obj *obj)
{
    if(OBJ_IS_BEING_DESTROYED(obj))
        return FALSE;

    return mainloop_defer_action(obj, destroy_obj);
}


bool mainloop_defer_extl_on_list(ExtlFn fn, WDeferred **list)
{
    WDeferred *d;

    d=alloc_defer();

    if(d==NULL){
        warn_err();
        return FALSE;
    }

    d->action=NULL;
    d->list=list;
    d->fn=extl_ref_fn(fn);

    watch_init(&(d->watch));

    LINK_ITEM(*list, d, next, prev);

    return TRUE;
}


/*EXTL_DOC
 * Defer execution of \var{fn} until the main loop.
 */
EXTL_SAFE
EXTL_EXPORT_AS(mainloop, defer)
bool mainloop_defer_extl(ExtlFn fn)
{
    return mainloop_defer_extl_on_list(fn, &deferred);
}


static void do_execute(WDeferred *d)
{
    Obj *obj=d->watch.obj;
    WDeferredAction *a=d->action;
    ExtlFn fn=d->fn;

    watch_reset(&(d->watch));
    free_defer(d);

    if(a!=NULL){
        if(obj!=NULL)
            a(obj);
    }else if(fn!=extl_fn_none()){
        extl_call(fn, NULL, NULL);
        extl_unref_fn(fn);
    }
}


void mainloop_execute_deferred_on_list(WDeferred **list)
{
    while(*list!=NULL){
        WDeferred *d=*list;
        UNLINK_ITEM(*list, d, next, prev);
        do_execute(d);
    }
}


void mainloop_execute_deferred()
{
    mainloop_execute_deferred_on_list(&deferred);
}