1*b30d1939SAndy Fiddaman /***********************************************************************
2*b30d1939SAndy Fiddaman * *
3*b30d1939SAndy Fiddaman * This software is part of the ast package *
4*b30d1939SAndy Fiddaman * Copyright (c) 1985-2011 AT&T Intellectual Property *
5*b30d1939SAndy Fiddaman * and is licensed under the *
6*b30d1939SAndy Fiddaman * Eclipse Public License, Version 1.0 *
7*b30d1939SAndy Fiddaman * by AT&T Intellectual Property *
8*b30d1939SAndy Fiddaman * *
9*b30d1939SAndy Fiddaman * A copy of the License is available at *
10*b30d1939SAndy Fiddaman * http://www.eclipse.org/org/documents/epl-v10.html *
11*b30d1939SAndy Fiddaman * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12*b30d1939SAndy Fiddaman * *
13*b30d1939SAndy Fiddaman * Information and Software Systems Research *
14*b30d1939SAndy Fiddaman * AT&T Research *
15*b30d1939SAndy Fiddaman * Florham Park NJ *
16*b30d1939SAndy Fiddaman * *
17*b30d1939SAndy Fiddaman * Glenn Fowler <gsf@research.att.com> *
18*b30d1939SAndy Fiddaman * David Korn <dgk@research.att.com> *
19*b30d1939SAndy Fiddaman * Phong Vo <kpv@research.att.com> *
20*b30d1939SAndy Fiddaman * *
21*b30d1939SAndy Fiddaman ***********************************************************************/
22*b30d1939SAndy Fiddaman #include "sfhdr.h"
23*b30d1939SAndy Fiddaman
24*b30d1939SAndy Fiddaman
25*b30d1939SAndy Fiddaman /* Push/pop streams
26*b30d1939SAndy Fiddaman **
27*b30d1939SAndy Fiddaman ** Written by Kiem-Phong Vo.
28*b30d1939SAndy Fiddaman */
29*b30d1939SAndy Fiddaman
30*b30d1939SAndy Fiddaman #define STKMTXLOCK(f1,f2) \
31*b30d1939SAndy Fiddaman { if(f1) SFMTXLOCK(f1); \
32*b30d1939SAndy Fiddaman if(f2) SFMTXLOCK(f2); \
33*b30d1939SAndy Fiddaman }
34*b30d1939SAndy Fiddaman #define STKMTXRETURN(f1,f2,rv) \
35*b30d1939SAndy Fiddaman { if(f1) SFMTXUNLOCK(f1); \
36*b30d1939SAndy Fiddaman if(f2) SFMTXUNLOCK(f2); \
37*b30d1939SAndy Fiddaman return(rv); \
38*b30d1939SAndy Fiddaman }
39*b30d1939SAndy Fiddaman
40*b30d1939SAndy Fiddaman #if __STD_C
sfstack(Sfio_t * f1,Sfio_t * f2)41*b30d1939SAndy Fiddaman Sfio_t* sfstack(Sfio_t* f1, Sfio_t* f2)
42*b30d1939SAndy Fiddaman #else
43*b30d1939SAndy Fiddaman Sfio_t* sfstack(f1,f2)
44*b30d1939SAndy Fiddaman Sfio_t* f1; /* base of stack */
45*b30d1939SAndy Fiddaman Sfio_t* f2; /* top of stack */
46*b30d1939SAndy Fiddaman #endif
47*b30d1939SAndy Fiddaman {
48*b30d1939SAndy Fiddaman reg int n;
49*b30d1939SAndy Fiddaman reg Sfio_t* rf;
50*b30d1939SAndy Fiddaman reg Sfrsrv_t* rsrv;
51*b30d1939SAndy Fiddaman reg Void_t* mtx;
52*b30d1939SAndy Fiddaman
53*b30d1939SAndy Fiddaman STKMTXLOCK(f1,f2);
54*b30d1939SAndy Fiddaman
55*b30d1939SAndy Fiddaman if(f1 && (f1->mode&SF_RDWR) != f1->mode && _sfmode(f1,0,0) < 0)
56*b30d1939SAndy Fiddaman STKMTXRETURN(f1,f2, NIL(Sfio_t*));
57*b30d1939SAndy Fiddaman if(f2 && (f2->mode&SF_RDWR) != f2->mode && _sfmode(f2,0,0) < 0)
58*b30d1939SAndy Fiddaman STKMTXRETURN(f1,f2, NIL(Sfio_t*));
59*b30d1939SAndy Fiddaman if(!f1)
60*b30d1939SAndy Fiddaman STKMTXRETURN(f1,f2, f2);
61*b30d1939SAndy Fiddaman
62*b30d1939SAndy Fiddaman /* give access to other internal functions */
63*b30d1939SAndy Fiddaman _Sfstack = sfstack;
64*b30d1939SAndy Fiddaman
65*b30d1939SAndy Fiddaman if(f2 == SF_POPSTACK)
66*b30d1939SAndy Fiddaman { if(!(f2 = f1->push))
67*b30d1939SAndy Fiddaman STKMTXRETURN(f1,f2, NIL(Sfio_t*));
68*b30d1939SAndy Fiddaman f2->mode &= ~SF_PUSH;
69*b30d1939SAndy Fiddaman }
70*b30d1939SAndy Fiddaman else
71*b30d1939SAndy Fiddaman { if(f2->push)
72*b30d1939SAndy Fiddaman STKMTXRETURN(f1,f2, NIL(Sfio_t*));
73*b30d1939SAndy Fiddaman if(f1->pool && f1->pool != &_Sfpool && f1->pool != f2->pool &&
74*b30d1939SAndy Fiddaman f1 == f1->pool->sf[0])
75*b30d1939SAndy Fiddaman { /* get something else to pool front since f1 will be locked */
76*b30d1939SAndy Fiddaman for(n = 1; n < f1->pool->n_sf; ++n)
77*b30d1939SAndy Fiddaman { if(SFFROZEN(f1->pool->sf[n]) )
78*b30d1939SAndy Fiddaman continue;
79*b30d1939SAndy Fiddaman (*_Sfpmove)(f1->pool->sf[n],0);
80*b30d1939SAndy Fiddaman break;
81*b30d1939SAndy Fiddaman }
82*b30d1939SAndy Fiddaman }
83*b30d1939SAndy Fiddaman }
84*b30d1939SAndy Fiddaman
85*b30d1939SAndy Fiddaman if(f2->pool && f2->pool != &_Sfpool && f2 != f2->pool->sf[0])
86*b30d1939SAndy Fiddaman (*_Sfpmove)(f2,0);
87*b30d1939SAndy Fiddaman
88*b30d1939SAndy Fiddaman /* swap streams */
89*b30d1939SAndy Fiddaman sfswap(f1,f2);
90*b30d1939SAndy Fiddaman
91*b30d1939SAndy Fiddaman /* but the reserved buffer and mutex must remain the same */
92*b30d1939SAndy Fiddaman rsrv = f1->rsrv; f1->rsrv = f2->rsrv; f2->rsrv = rsrv;
93*b30d1939SAndy Fiddaman mtx = f1->mutex; f1->mutex = f2->mutex; f2->mutex = mtx;
94*b30d1939SAndy Fiddaman
95*b30d1939SAndy Fiddaman SFLOCK(f1,0);
96*b30d1939SAndy Fiddaman SFLOCK(f2,0);
97*b30d1939SAndy Fiddaman
98*b30d1939SAndy Fiddaman if(f2->push != f2)
99*b30d1939SAndy Fiddaman { /* freeze the pushed stream */
100*b30d1939SAndy Fiddaman f2->mode |= SF_PUSH;
101*b30d1939SAndy Fiddaman f1->push = f2;
102*b30d1939SAndy Fiddaman rf = f1;
103*b30d1939SAndy Fiddaman }
104*b30d1939SAndy Fiddaman else
105*b30d1939SAndy Fiddaman { /* unfreeze the just exposed stream */
106*b30d1939SAndy Fiddaman f1->mode &= ~SF_PUSH;
107*b30d1939SAndy Fiddaman f2->push = NIL(Sfio_t*);
108*b30d1939SAndy Fiddaman rf = f2;
109*b30d1939SAndy Fiddaman }
110*b30d1939SAndy Fiddaman
111*b30d1939SAndy Fiddaman SFOPEN(f1,0);
112*b30d1939SAndy Fiddaman SFOPEN(f2,0);
113*b30d1939SAndy Fiddaman
114*b30d1939SAndy Fiddaman STKMTXRETURN(f1,f2, rf);
115*b30d1939SAndy Fiddaman }
116