[go: up one dir, main page]

File: asyn_io.c

package info (click to toggle)
libforms 1.2.3-1.3
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 10,736 kB
  • ctags: 9,015
  • sloc: ansic: 97,669; sh: 11,156; makefile: 951
file content (322 lines) | stat: -rw-r--r-- 7,486 bytes parent folder | download | duplicates (4)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file asyn_io.c
 *.
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao
 *  All rights reserved.
 *
 *  Handle input other than the X event queue. Mostly maintanance
 *  here. Actual input/output handling is triggered in the main loop
 *  via fli_watch_io().
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include "flinternal.h"
#include <sys/types.h>

#ifndef FL_WIN32
#include <sys/time.h>
#endif

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#ifdef __sgi
#include <bstring.h>
#endif

#ifdef FL_WIN32
#include <X11/Xpoll.h>
#endif


static fd_set st_rfds,
              st_wfds,
              st_efds;

static void add_to_freelist( FLI_IO_REC * io );
static void clear_freelist( void );


/***************************************
 * Collect all the fd_sets so we don't do it inside the
 * critical select inner loop
 ***************************************/

static void
collect_fd( void )
{
    FLI_IO_REC *p;
    int nf = 0;

    /* Initialize the sets */

    FD_ZERO( &st_rfds );
    FD_ZERO( &st_wfds );
    FD_ZERO( &st_efds );

    /* Loop through all requested IOs */

    for ( p = fli_context->io_rec; p; p = p->next )
    {
        if ( p->source < 0 )
        {
            M_err( "collect_fd", "source < 0\n" );
            continue;
        }

        if ( p->mask & FL_READ )
            FD_SET( p->source, &st_rfds );
        if ( p->mask & FL_WRITE )
            FD_SET( p->source, &st_wfds );
        if ( p->mask & FL_EXCEPT )
            FD_SET( p->source, &st_efds );
        if ( nf < p->source + 1 )
            nf = p->source + 1;
    }

    fli_context->num_io = nf;
}


/***************************************
 * Register a callback function for file descriptor fd
 ***************************************/

void
fl_add_io_callback( int              fd,
                    unsigned int     mask,
                    FL_IO_CALLBACK   callback,
                    void           * data )
{
    FLI_IO_REC *io_rec;

    /* Create new record and make it the start of the list */

    io_rec = fl_malloc( sizeof *io_rec );

    io_rec->next     = fli_context->io_rec;
    io_rec->callback = callback;
    io_rec->data     = data;
    io_rec->source   = fd;
    io_rec->mask     = mask;

    fli_context->io_rec = io_rec;

    collect_fd( );
}


/***************************************
 ***************************************/

void
fl_remove_io_callback( int            fd,
                       unsigned       int mask,
                       FL_IO_CALLBACK cb )
{
    FLI_IO_REC *io,
               *previous_io = NULL;

    for ( io = fli_context->io_rec;
          io && ! ( io->source == fd && io->callback == cb && io->mask & mask );
          io = io->next )
        previous_io = io;

    if ( ! io )
    {
        M_err( "fl_remove_io_callback", "Non-existent handler for %d", fd );
        return;
    }

    /* If after removal the fd isn't to be checked anymore (i.e. mask == 0)
       remove it from global record */

    if ( ! ( io->mask &= ~ mask ) )
    {
        if ( previous_io )
            previous_io->next = io->next;
        else
            fli_context->io_rec = io->next;

        /* Caution: the following may look idiotic at first: simply getting
           rid of the structure for the callback would seem to be appropriate.
           But things get interesting if the callback gets removed from within
           the callback - then just removing it gets us into trouble since
           then fli_watch_io(), iterating over all IO callbacks still tries to
           to access the 'next' field and if the structure has been
           deallocated completely (instead of having been temporarily moved
           to somewhere else where it still can be accessed) stumbles badly.
           That's also why fli_watch_io() calls clear_freelist() - it's the
           only instance that knows when the structure isn't needed anymore. */

        add_to_freelist( io );
    }

    collect_fd( );
}


/***************************************
 * Watch for activities using select or poll. Timeout in milli-second
 ***************************************/

void
fli_watch_io( FLI_IO_REC * io_rec,
              long         msec )
{
    fd_set rfds,
           wfds,
           efds;
    struct timeval timeout;
    FLI_IO_REC *p;
    int nf;

    clear_freelist( );

    if ( ! io_rec )
    {
        if ( msec > 0 )
            fl_msleep( msec );

        return;
    }

    timeout.tv_usec = 1000 * ( msec % 1000 );
    timeout.tv_sec  = msec / 1000;

    /* Initialize the sets */

    rfds = st_rfds;
    wfds = st_wfds;
    efds = st_efds;

    /* Now watch it. HP defines rfds to be ints. Althought compiler will
       bark, it is harmless. */

    nf = select( fli_context->num_io, &rfds, &wfds, &efds, &timeout );

    if ( nf < 0 )     /* something is wrong. */
    {
        if ( errno == EINTR )
            M_warn( "fli_watch_io", "select interrupted by signal" );

        /* select() on some platforms returns -1 with errno == 0 */

        else if ( errno != 0 )
            M_err( "fli_watch_io", fli_get_syserror_msg( ) );

        return;
    }

    /* Timeout expired */

    if ( nf == 0 )
        return;

    /* Handle it */

    for ( p = io_rec; p; p = p->next )
    {
        if ( ! p->callback || p->source < 0 || p->mask == 0 )
            continue;

        if ( p->mask & FL_READ && FD_ISSET( p->source, &rfds ) )
            p->callback( p->source, p->data );

        if ( p->mask & FL_WRITE && FD_ISSET( p->source, &wfds ) )
            p->callback( p->source, p->data );

        if ( p->mask & FL_EXCEPT && FD_ISSET( p->source, &efds ) )
            p->callback( p->source, p->data );
    }

    clear_freelist( );
}


/***************************************
 ***************************************/

int
fli_is_watched_io( int fd )
{
    FLI_IO_REC *p;

    for ( p = fli_context->io_rec; p; p = p->next )
        if ( p->source == fd && p->mask )
            return 1;

    return 0;
}


/***************************************
 ***************************************/

static struct free_list
{
    struct free_list * next;
    FLI_IO_REC       * io;
} *fl = NULL;

static void
add_to_freelist( FLI_IO_REC * io )
{
    struct free_list *cur;

    cur = malloc( sizeof *cur );
    cur->next = fl;
    cur->io   = io;

    fl = cur;
}


/***************************************
 ***************************************/

static void
clear_freelist( void )
{
    struct free_list *cur;

    while ( fl )
    {
        fl_free( fl->io );
        cur = fl;
        fl = fl->next;
        fl_free( cur );
    }
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */