~ubuntu-branches/ubuntu/hardy/openmpi/hardy-updates

« back to all changes in this revision

Viewing changes to opal/event/rtsig.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2006-10-15 00:46:11 UTC
  • Revision ID: james.westby@ubuntu.com-20061015004611-uuhxnaxyjmuxfd5h
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "opal_config.h"
 
2
 
 
3
/* Enable F_SETSIG and F_SETOWN */
 
4
#define _GNU_SOURCE
 
5
 
 
6
#ifdef HAVE_SYS_TYPES_H
 
7
#include <sys/types.h>
 
8
#endif
 
9
#ifdef HAVE_SYS_TIME_H
 
10
#include <sys/time.h>
 
11
#else
 
12
#include <sys/_time.h>
 
13
#endif
 
14
#include <assert.h>
 
15
#include <errno.h>
 
16
#include <fcntl.h>
 
17
#include <signal.h>
 
18
#include <stdio.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
 
21
#include <sys/poll.h>
 
22
#include <sys/queue.h>
 
23
#ifndef HAVE_WORKING_RTSIG
 
24
#include <sys/stat.h>
 
25
#endif
 
26
#ifdef HAVE_UNISTD_H
 
27
#include <unistd.h>
 
28
#endif
 
29
 
 
30
#define EVLIST_X_NORT   0x1000  /* Skip RT signals (internal) */
 
31
 
 
32
#include "event.h"
 
33
extern struct event_list eventqueue;
 
34
extern struct event_list signalqueue;
 
35
 
 
36
struct rtsigop {
 
37
    sigset_t sigs;
 
38
    struct pollfd *poll;
 
39
    struct event **toev;
 
40
    int cur, max, total;
 
41
#ifndef HAVE_WORKING_RTSIG
 
42
    int pollmode;
 
43
#endif
 
44
};
 
45
 
 
46
#define INIT_MAX 16
 
47
 
 
48
static int
 
49
poll_add(struct rtsigop *op, struct event *ev)
 
50
{
 
51
    struct pollfd *pfd;
 
52
 
 
53
    if (op->poll == NULL) return 0;
 
54
 
 
55
    if (op->cur == op->max) {
 
56
        void *p;
 
57
 
 
58
        p = realloc(op->poll, sizeof(*op->poll) * (op->max << 1));
 
59
        if (!p) {
 
60
            errno = ENOMEM;
 
61
            return -1;
 
62
        }
 
63
        op->poll = p;
 
64
        p = realloc(op->toev, sizeof(*op->toev) * (op->max << 1));
 
65
        if (!p) {
 
66
            op->poll = realloc(op->poll, sizeof(*op->poll) * op->max);
 
67
            errno = ENOMEM;
 
68
            return -1;
 
69
        }
 
70
        op->toev = p;
 
71
        op->max <<= 1;
 
72
    }
 
73
 
 
74
    pfd = &op->poll[op->cur];
 
75
    pfd->fd = ev->ev_fd;
 
76
    pfd->events = 0;
 
77
    if (ev->ev_events & EV_READ) pfd->events |= POLLIN;
 
78
    if (ev->ev_events & EV_WRITE) pfd->events |= POLLOUT;
 
79
    pfd->revents = 0;
 
80
 
 
81
    op->toev[op->cur] = ev;
 
82
    op->cur++;
 
83
 
 
84
    return 0;
 
85
}
 
86
 
 
87
static void
 
88
poll_free(struct rtsigop *op, int n)
 
89
{
 
90
    if (op->poll == NULL) return;
 
91
 
 
92
    op->cur--;
 
93
    if (n < op->cur) {
 
94
        memcpy(&op->poll[n], &op->poll[op->cur], sizeof(*op->poll));
 
95
        op->toev[n] = op->toev[op->cur];
 
96
    }
 
97
    if (op->max > INIT_MAX && op->cur < op->max >> 1) {
 
98
        op->max >>= 1;
 
99
        op->poll = realloc(op->poll, sizeof(*op->poll) * op->max);
 
100
        op->toev = realloc(op->toev, sizeof(*op->toev) * op->max);
 
101
    }
 
102
}
 
103
 
 
104
static void
 
105
poll_remove(struct rtsigop *op, struct event *ev)
 
106
{
 
107
    int i;
 
108
 
 
109
    for (i = 0; i < op->cur; i++) {
 
110
        if (op->toev[i] == ev) {
 
111
            poll_free(op, i);
 
112
            break;
 
113
        }
 
114
    }
 
115
}
 
116
 
 
117
static void
 
118
activate(struct event *ev, int flags)
 
119
{
 
120
    if (!(ev->ev_events & EV_PERSIST)) event_del(ev);
 
121
    event_active(ev, flags, 1);
 
122
}
 
123
 
 
124
static void *rtsig_init(void);
 
125
static int rtsig_add(void *, struct event *);
 
126
static int rtsig_del(void *, struct event *);
 
127
static int rtsig_recalc(void *, int);
 
128
static int rtsig_dispatch(void *, struct timeval *);
 
129
 
 
130
struct opal_eventop rtsigops = {
 
131
    "rtsig",
 
132
    rtsig_init,
 
133
    rtsig_add,
 
134
    rtsig_del,
 
135
    rtsig_recalc,
 
136
    rtsig_dispatch
 
137
};
 
138
 
 
139
static void *
 
140
rtsig_init(void)
 
141
{
 
142
        struct rtsigop *op;
 
143
 
 
144
        if (getenv("EVENT_NORTSIG"))
 
145
                return (NULL);
 
146
 
 
147
        op = malloc(sizeof(*op));
 
148
        if (op == NULL) return (NULL);
 
149
 
 
150
        memset(op, 0, sizeof(*op));
 
151
 
 
152
        op->max = INIT_MAX;
 
153
        op->poll = malloc(sizeof(*op->poll) * op->max);
 
154
        if (op->poll == NULL) {
 
155
                free(op);
 
156
                return (NULL);
 
157
        }
 
158
        op->toev = malloc(sizeof(*op->toev) * op->max);
 
159
        if (op->toev == NULL) {
 
160
                free(op->poll);
 
161
                free(op);
 
162
                return (NULL);
 
163
        }
 
164
 
 
165
        sigemptyset(&op->sigs);
 
166
        sigaddset(&op->sigs, SIGIO);
 
167
        sigaddset(&op->sigs, SIGRTMIN);
 
168
        sigprocmask(SIG_BLOCK, &op->sigs, NULL);
 
169
 
 
170
        return (op);
 
171
}
 
172
 
 
173
static int
 
174
rtsig_add(void *arg, struct event *ev)
 
175
{
 
176
        struct rtsigop *op = (struct rtsigop *) arg;
 
177
        int flags, i;
 
178
#ifndef HAVE_WORKING_RTSIG
 
179
        struct stat st;
 
180
#endif
 
181
 
 
182
        if (ev->ev_events & EV_SIGNAL) {
 
183
                sigaddset(&op->sigs, EVENT_SIGNAL(ev));
 
184
                return sigprocmask(SIG_BLOCK, &op->sigs, NULL);
 
185
        }
 
186
 
 
187
        if (!(ev->ev_events & (EV_READ | EV_WRITE))) return 0;
 
188
 
 
189
#ifndef HAVE_WORKING_RTSIG
 
190
        if (fstat(ev->ev_fd, &st) == -1) return -1;
 
191
        if (S_ISFIFO(st.st_mode)) {
 
192
                ev->ev_flags |= EVLIST_X_NORT;
 
193
                op->pollmode++;
 
194
        }
 
195
#endif
 
196
 
 
197
        flags = fcntl(ev->ev_fd, F_GETFL);
 
198
        if (flags == -1)
 
199
                return (-1);
 
200
 
 
201
        if (!(flags & O_ASYNC)) {
 
202
                if (fcntl(ev->ev_fd, F_SETSIG, SIGRTMIN) == -1
 
203
                    || fcntl(ev->ev_fd, F_SETOWN, (int) getpid()) == -1)
 
204
                        return (-1);
 
205
 
 
206
                if (fcntl(ev->ev_fd, F_SETFL, flags | O_ASYNC))
 
207
                        return (-1);
 
208
        }
 
209
 
 
210
#ifdef O_ONESIGFD
 
211
        fcntl(ev->ev_fd, F_SETAUXFL, O_ONESIGFD);
 
212
#endif
 
213
 
 
214
        op->total++;
 
215
        if (poll_add(op, ev) == -1)
 
216
                goto err;
 
217
 
 
218
        return (0);
 
219
 
 
220
 err:
 
221
        i = errno;
 
222
        fcntl(ev->ev_fd, F_SETFL, flags);
 
223
        errno = i;
 
224
        return (-1);
 
225
}
 
226
 
 
227
static int
 
228
rtsig_del(void *arg, struct event *ev)
 
229
{
 
230
        struct rtsigop *op = (struct rtsigop *) arg;
 
231
 
 
232
        if (ev->ev_events & EV_SIGNAL) {
 
233
                sigset_t sigs;
 
234
 
 
235
                sigdelset(&op->sigs, EVENT_SIGNAL(ev));
 
236
 
 
237
                sigemptyset(&sigs);
 
238
                sigaddset(&sigs, EVENT_SIGNAL(ev));
 
239
                return (sigprocmask(SIG_UNBLOCK, &sigs, NULL));
 
240
        }
 
241
 
 
242
        if (!(ev->ev_events & (EV_READ | EV_WRITE)))
 
243
                return (0);
 
244
 
 
245
#ifndef HAVE_WORKING_RTSIG
 
246
        if (ev->ev_flags & EVLIST_X_NORT)
 
247
                op->pollmode--;
 
248
#endif
 
249
        poll_remove(op, ev);
 
250
        op->total--;
 
251
 
 
252
        return (0);
 
253
}
 
254
 
 
255
static int
 
256
rtsig_recalc(void *arg, int max)
 
257
{
 
258
    return (0);
 
259
}
 
260
 
 
261
static int
 
262
rtsig_dispatch(void *arg, struct timeval *tv)
 
263
{
 
264
        struct rtsigop *op = (struct rtsigop *) arg;
 
265
        struct timespec ts;
 
266
        int res, i;
 
267
 
 
268
        if (op->poll == NULL)
 
269
                goto retry_poll;
 
270
#ifndef HAVE_WORKING_RTSIG
 
271
        if (op->pollmode)
 
272
                goto poll_all;
 
273
#endif
 
274
 
 
275
        if (op->cur) {
 
276
                ts.tv_sec = ts.tv_nsec = 0;
 
277
        } else {
 
278
                ts.tv_sec = tv->tv_sec;
 
279
                ts.tv_nsec = tv->tv_usec * 1000;
 
280
        }
 
281
 
 
282
        for (;;) {
 
283
                siginfo_t info;
 
284
                struct event *ev;
 
285
                int signum;
 
286
 
 
287
                signum = sigtimedwait(&op->sigs, &info, &ts);
 
288
 
 
289
                if (signum == -1) {
 
290
                        if (errno == EAGAIN)
 
291
                                break;
 
292
                        return (errno == EINTR ? 0 : -1);
 
293
                }
 
294
 
 
295
                ts.tv_sec = ts.tv_nsec = 0;
 
296
 
 
297
                if (signum == SIGIO) {
 
298
#ifndef HAVE_WORKING_RTSIG
 
299
                poll_all:
 
300
#endif
 
301
                        free(op->poll);
 
302
                        free(op->toev);
 
303
                retry_poll:
 
304
                        op->cur = 0;
 
305
                        op->max = op->total;
 
306
                        op->poll = malloc(sizeof(*op->poll) * op->total);
 
307
                        if (op->poll == NULL)
 
308
                                return (-1);
 
309
                        op->toev = malloc(sizeof(*op->toev) * op->total);
 
310
                        if (op->toev == NULL) {
 
311
                                free(op->poll);
 
312
                                op->poll = NULL;
 
313
                                return (-1);
 
314
                        }
 
315
 
 
316
                        TAILQ_FOREACH(ev, &eventqueue, ev_next)
 
317
                            if (!(ev->ev_flags & EVLIST_X_NORT))
 
318
                                    poll_add(op, ev);
 
319
 
 
320
                        break;
 
321
                }
 
322
 
 
323
                if (signum == SIGRTMIN) {
 
324
                        int flags, i, sigok = 0;
 
325
 
 
326
                        if (info.si_band <= 0) { /* SI_SIGIO */
 
327
                                flags = EV_READ | EV_WRITE;
 
328
                        } else {
 
329
                                flags = 0;
 
330
                                if (info.si_band & POLLIN) flags |= EV_READ;
 
331
                                if (info.si_band & POLLOUT) flags |= EV_WRITE;
 
332
                                if (!flags) continue;
 
333
                        }
 
334
 
 
335
                        for (i = 0; flags && i < op->cur; i++) {
 
336
                                ev = op->toev[i];
 
337
 
 
338
                                if (ev->ev_fd == info.si_fd) {
 
339
                                        flags &= ~ev->ev_events;
 
340
                                        sigok = 1;
 
341
                                }
 
342
                        }
 
343
 
 
344
                        for (ev = TAILQ_FIRST(&eventqueue);
 
345
                            flags && ev != TAILQ_END(&eventqueue);
 
346
                            ev = TAILQ_NEXT(ev, ev_next)) {
 
347
                                if (ev->ev_fd == info.si_fd) {
 
348
                                        if (flags & ev->ev_events) {
 
349
                                                i = poll_add(op, ev);
 
350
                                                if (i == -1) return -1;
 
351
                                                flags &= ~ev->ev_events;
 
352
                                        }
 
353
                                        sigok = 1;
 
354
                                }
 
355
                        }
 
356
 
 
357
                        if (!sigok) {
 
358
                                flags = fcntl(info.si_fd, F_GETFL);
 
359
                                if (flags == -1) return -1;
 
360
                                fcntl(info.si_fd, F_SETFL, flags & ~O_ASYNC);
 
361
                        }
 
362
                } else {
 
363
                        TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) {
 
364
                                if (EVENT_SIGNAL(ev) == signum)
 
365
                                        activate(ev, EV_SIGNAL);
 
366
                        }
 
367
                }
 
368
        }
 
369
 
 
370
        if (!op->cur)
 
371
                return (0);
 
372
 
 
373
        res = poll(op->poll, op->cur, tv->tv_sec * 1000 + tv->tv_usec / 1000);
 
374
        if (res < 0)
 
375
                return (-1);
 
376
 
 
377
        i = 0;
 
378
#ifdef HAVE_WORKING_RTSIG
 
379
        while (i < res) {
 
380
#else
 
381
        while (i < op->cur) {
 
382
#endif
 
383
                if (op->poll[i].revents) {
 
384
                        int flags = 0;
 
385
                        struct event *ev = op->toev[i];
 
386
 
 
387
                        if (op->poll[i].revents & POLLIN)
 
388
                                flags |= EV_READ;
 
389
                        if (op->poll[i].revents & POLLOUT)
 
390
                                flags |= EV_WRITE;
 
391
 
 
392
                        if (!(ev->ev_events & EV_PERSIST)) {
 
393
                                event_del(ev);
 
394
                                res--;
 
395
                        } else {
 
396
                                i++;
 
397
                        }
 
398
                        event_active(ev, flags, 1);
 
399
                } else {
 
400
#ifndef HAVE_WORKING_RTSIG
 
401
                        if (op->toev[i]->ev_flags & EVLIST_X_NORT) {
 
402
                                i++;
 
403
                                res++;
 
404
                                continue;
 
405
                        }
 
406
#endif
 
407
                        for (;;) {
 
408
                                op->cur--;
 
409
                                if (i == op->cur)
 
410
                                        break;
 
411
                                if (op->poll[op->cur].revents) {
 
412
                                        memcpy(&op->poll[i], &op->poll[op->cur], sizeof(*op->poll));
 
413
                                        op->toev[i] = op->toev[op->cur];
 
414
                                        break;
 
415
                                }
 
416
                        }
 
417
                }
 
418
        }
 
419
#ifdef HAVE_WORKING_RTSIG
 
420
        op->cur = res;
 
421
#endif
 
422
 
 
423
        if (!op->cur) {
 
424
                op->max = INIT_MAX;
 
425
                free(op->poll);
 
426
                free(op->toev);
 
427
                /* We just freed it, we shouldn't have a problem getting it back. */
 
428
                op->poll = malloc(sizeof(*op->poll) * op->max);
 
429
                op->toev = malloc(sizeof(*op->toev) * op->max);
 
430
 
 
431
                if (op->poll == NULL || op->toev == NULL)
 
432
                        err(1, "%s: malloc");
 
433
        }
 
434
 
 
435
        return (0);
 
436
}