1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * User-level event queue management routines
7 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * Copyright (c) 2001-2002 Sandia National Laboratories
10 * This file is part of Lustre, http://www.sf.net/projects/lustre/
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <portals/api-support.h>
30 /* Nothing to do anymore... */
34 void ptl_eq_fini(void)
36 /* Nothing to do anymore... */
39 int ptl_eq_ni_init(nal_t * nal)
41 /* Nothing to do anymore... */
45 void ptl_eq_ni_fini(nal_t * nal)
47 /* Nothing to do anymore... */
50 int PtlEQGet(ptl_handle_eq_t eventq, ptl_event_t * ev)
55 ptl_event_t *new_event;
62 nal = ptl_hndl2nal(&eventq);
66 eq = ptl_handle2usereq(&eventq);
67 nal->lock(nal, &flags);
69 /* size must be a power of 2 to handle a wrapped sequence # */
70 LASSERT (eq->size != 0 &&
71 eq->size == LOWEST_BIT_SET (eq->size));
73 new_index = eq->sequence & (eq->size - 1);
74 new_event = &eq->base[new_index];
75 CDEBUG(D_INFO, "new_event: %p, sequence: %lu, eq->size: %u\n",
76 new_event, eq->sequence, eq->size);
77 if (PTL_SEQ_GT (eq->sequence, new_event->sequence)) {
78 nal->unlock(nal, &flags);
84 /* ensure event is delivered correctly despite possible
85 races with lib_finalize */
86 if (eq->sequence != new_event->sequence) {
87 CERROR("DROPPING EVENT: eq seq %lu ev seq %lu\n",
88 eq->sequence, new_event->sequence);
94 eq->sequence = new_event->sequence + 1;
95 nal->unlock(nal, &flags);
100 int PtlEQWait(ptl_handle_eq_t eventq_in, ptl_event_t *event_out)
104 /* PtlEQGet does the handle checking */
105 while ((rc = PtlEQGet(eventq_in, event_out)) == PTL_EQ_EMPTY) {
106 nal_t *nal = ptl_hndl2nal(&eventq_in);
117 static jmp_buf eq_jumpbuf;
119 static void eq_timeout(int signal)
123 /* signal will be automatically disabled in sig handler,
124 * must enable it before long jump
127 sigaddset(&set, SIGALRM);
128 sigprocmask(SIG_UNBLOCK, &set, NULL);
130 longjmp(eq_jumpbuf, -1);
133 int PtlEQWait_timeout(ptl_handle_eq_t eventq_in, ptl_event_t * event_out,
136 static void (*prev) (int) = NULL;
137 static int left_over;
138 time_t time_at_start;
141 if (setjmp(eq_jumpbuf)) {
142 signal(SIGALRM, prev);
143 alarm(left_over - timeout);
147 left_over = alarm(timeout);
148 prev = signal(SIGALRM, eq_timeout);
149 time_at_start = time(NULL);
150 if (left_over && left_over < timeout)
153 rc = PtlEQWait(eventq_in, event_out);
155 signal(SIGALRM, prev);
156 alarm(left_over); /* Should compute how long we waited */
164 * Here timeout need a trick with tcpnal, definitely unclean but OK for
168 /* global variables defined by tcpnal */
169 extern int __tcpnal_eqwait_timeout_value;
170 extern int __tcpnal_eqwait_timedout;
172 int PtlEQWait_timeout(ptl_handle_eq_t eventq_in, ptl_event_t * event_out,
178 return PtlEQWait(eventq_in, event_out);
180 __tcpnal_eqwait_timeout_value = timeout;
182 while ((rc = PtlEQGet(eventq_in, event_out)) == PTL_EQ_EMPTY) {
183 nal_t *nal = ptl_hndl2nal(&eventq_in);
188 if (__tcpnal_eqwait_timedout) {
189 if (__tcpnal_eqwait_timedout != ETIMEDOUT)
190 printf("Warning: yield return error %d\n",
191 __tcpnal_eqwait_timedout);
197 __tcpnal_eqwait_timeout_value = 0;
202 #endif /* __KERNEL__ */