2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
13 #include "../rx/rx_kernel.h"
14 #include "../rx/rx_multi.h"
17 # include "rx_multi.h"
18 # include "rx_internal.h"
21 /* multi.c and multi.h, together with some rxgen hooks, provide a way of making multiple, but similar, rx calls to multiple hosts simultaneously */
23 struct multi_handle *multi_Init(conns, nConns)
24 struct rx_connection **conns;
28 register struct rx_call **calls;
29 register short *ready;
30 register struct multi_handle *mh;
32 /* Note: all structures that are possibly referenced by other processes must be allocated. In some kernels variables allocated on a process stack will not be accessible to other processes */
33 calls = (struct rx_call **)osi_Alloc(sizeof (struct rx_call *) * nConns);
34 ready = (short *) osi_Alloc(sizeof(short *) * nConns);
35 mh = (struct multi_handle *) osi_Alloc(sizeof (struct multi_handle));
36 if (!calls || !ready || !mh) osi_Panic("multi_Rx: no mem\n");
38 mh->nextReady = mh->firstNotReady = mh->ready = ready;
41 #ifdef RX_ENABLE_LOCKS
42 MUTEX_INIT(&mh->lock, "rx_multi_lock", MUTEX_DEFAULT, 0);
43 CV_INIT(&mh->cv, "rx_multi_cv", CV_DEFAULT, 0);
44 #endif /* RX_ENABLE_LOCKS */
45 for (i=0; i<nConns; i++) {
46 register struct rx_call *call;
47 call = mh->calls[i] = rx_NewCall(conns[i]);
48 rx_SetArrivalProc(call, multi_Ready, (VOID *)mh, (VOID *)i);
53 /* Return the user's connection index of the most recently ready call; that is, a call that has received at least one reply packet */
55 register struct multi_handle *mh;
60 #ifdef RX_ENABLE_LOCKS
61 MUTEX_ENTER(&mh->lock);
62 #endif /* RX_ENABLE_LOCKS */
63 while (mh->nextReady == mh->firstNotReady) {
64 if (mh->nReady == mh->nConns) {
65 #ifdef RX_ENABLE_LOCKS
66 MUTEX_EXIT(&mh->lock);
67 #endif /* RX_ENABLE_LOCKS */
71 #ifdef RX_ENABLE_LOCKS
72 CV_WAIT(&mh->cv, &mh->lock);
73 #else /* RX_ENABLE_LOCKS */
75 #endif /* RX_ENABLE_LOCKS */
77 index = *(mh->nextReady);
79 #ifdef RX_ENABLE_LOCKS
80 MUTEX_EXIT(&mh->lock);
81 #endif /* RX_ENABLE_LOCKS */
86 /* Called by Rx when the first reply packet of a call is received, or the call is aborted. */
87 void multi_Ready(call, mh, index)
88 register struct rx_call *call;
89 register struct multi_handle *mh;
92 #ifdef RX_ENABLE_LOCKS
93 MUTEX_ENTER(&mh->lock);
94 #endif /* RX_ENABLE_LOCKS */
95 *mh->firstNotReady++ = index;
97 #ifdef RX_ENABLE_LOCKS
99 MUTEX_EXIT(&mh->lock);
100 #else /* RX_ENABLE_LOCKS */
102 #endif /* RX_ENABLE_LOCKS */
105 /* Called when the multi rx call is over, or when the user aborts it (by using the macro multi_Abort) */
106 void multi_Finalize(mh)
107 register struct multi_handle *mh;
110 register int nCalls = mh->nConns;
111 for (i=0; i<nCalls; i++) {
112 register struct rx_call *call = mh->calls[i];
113 if (call) rx_EndCall(call, RX_USER_ABORT);
115 #ifdef RX_ENABLE_LOCKS
116 MUTEX_DESTROY(&mh->lock);
118 #endif /* RX_ENABLE_LOCKS */
119 osi_Free(mh->calls, sizeof (struct rx_call *) * nCalls);
120 osi_Free(mh->ready, sizeof(short *) * nCalls);
121 osi_Free(mh, sizeof(struct multi_handle));
124 /* ignores all remaining multiRx calls */
125 void multi_Finalize_Ignore(mh)
126 register struct multi_handle *mh;
129 register int nCalls = mh->nConns;
130 for (i=0; i<nCalls; i++) {
131 register struct rx_call *call = mh->calls[i];
132 if (call) rx_EndCall(call, 0);
134 #ifdef RX_ENABLE_LOCKS
135 MUTEX_DESTROY(&mh->lock);
137 #endif /* RX_ENABLE_LOCKS */
138 osi_Free(mh->calls, sizeof (struct rx_call *) * nCalls);
139 osi_Free(mh->ready, sizeof(short *) * nCalls);
140 osi_Free(mh, sizeof(struct multi_handle));