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 <afsconfig.h>
11 #include <afs/param.h>
16 #include "../rx/rx_kernel.h"
17 #include "../rx/rx_multi.h"
20 # include "rx_multi.h"
23 /* multi.c and multi.h, together with some rxgen hooks, provide a way of making multiple, but similar, rx calls to multiple hosts simultaneously */
25 struct multi_handle *multi_Init(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 */
54 int multi_Select(register struct multi_handle *mh)
59 #ifdef RX_ENABLE_LOCKS
60 MUTEX_ENTER(&mh->lock);
61 #endif /* RX_ENABLE_LOCKS */
62 while (mh->nextReady == mh->firstNotReady) {
63 if (mh->nReady == mh->nConns) {
64 #ifdef RX_ENABLE_LOCKS
65 MUTEX_EXIT(&mh->lock);
66 #endif /* RX_ENABLE_LOCKS */
70 #ifdef RX_ENABLE_LOCKS
71 CV_WAIT(&mh->cv, &mh->lock);
72 #else /* RX_ENABLE_LOCKS */
74 #endif /* RX_ENABLE_LOCKS */
76 index = *(mh->nextReady);
78 #ifdef RX_ENABLE_LOCKS
79 MUTEX_EXIT(&mh->lock);
80 #endif /* RX_ENABLE_LOCKS */
85 /* Called by Rx when the first reply packet of a call is received, or the call is aborted. */
86 void multi_Ready(register struct rx_call *call,
87 register struct multi_handle *mh, register int index)
89 #ifdef RX_ENABLE_LOCKS
90 MUTEX_ENTER(&mh->lock);
91 #endif /* RX_ENABLE_LOCKS */
92 *mh->firstNotReady++ = index;
94 #ifdef RX_ENABLE_LOCKS
96 MUTEX_EXIT(&mh->lock);
97 #else /* RX_ENABLE_LOCKS */
99 #endif /* RX_ENABLE_LOCKS */
102 /* Called when the multi rx call is over, or when the user aborts it (by using the macro multi_Abort) */
103 void multi_Finalize(register struct multi_handle *mh)
106 register int nCalls = mh->nConns;
107 for (i=0; i<nCalls; i++) {
108 register struct rx_call *call = mh->calls[i];
109 if (call) rx_EndCall(call, RX_USER_ABORT);
111 #ifdef RX_ENABLE_LOCKS
112 MUTEX_DESTROY(&mh->lock);
114 #endif /* RX_ENABLE_LOCKS */
115 osi_Free(mh->calls, sizeof (struct rx_call *) * nCalls);
116 osi_Free(mh->ready, sizeof(short *) * nCalls);
117 osi_Free(mh, sizeof(struct multi_handle));
120 /* ignores all remaining multiRx calls */
121 void multi_Finalize_Ignore(register struct multi_handle *mh)
124 register int nCalls = mh->nConns;
125 for (i=0; i<nCalls; i++) {
126 register struct rx_call *call = mh->calls[i];
127 if (call) rx_EndCall(call, 0);
129 #ifdef RX_ENABLE_LOCKS
130 MUTEX_DESTROY(&mh->lock);
132 #endif /* RX_ENABLE_LOCKS */
133 osi_Free(mh->calls, sizeof (struct rx_call *) * nCalls);
134 osi_Free(mh->ready, sizeof(short *) * nCalls);
135 osi_Free(mh, sizeof(struct multi_handle));