xref: /onnv-gate/usr/src/lib/libast/common/sfio/sfmutex.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #include	"sfhdr.h"
234887Schin 
244887Schin /*	Obtain/release exclusive use of a stream.
254887Schin **
264887Schin **	Written by Kiem-Phong Vo.
274887Schin */
284887Schin 
294887Schin /* the main locking/unlocking interface */
304887Schin #if __STD_C
sfmutex(Sfio_t * f,int type)314887Schin int sfmutex(Sfio_t* f, int type)
324887Schin #else
334887Schin int sfmutex(f, type)
344887Schin Sfio_t*	f;
354887Schin int	type;
364887Schin #endif
374887Schin {
384887Schin #if !vt_threaded
394887Schin 	NOTUSED(f); NOTUSED(type);
404887Schin 	return 0;
414887Schin #else
424887Schin 
434887Schin 	SFONCE();
444887Schin 
454887Schin 	if(!f)
464887Schin 		return -1;
474887Schin 
484887Schin 	if(!f->mutex)
494887Schin 	{	if(f->bits&SF_PRIVATE)
504887Schin 			return 0;
514887Schin 
524887Schin 		vtmtxlock(_Sfmutex);
534887Schin 		f->mutex = vtmtxopen(NIL(Vtmutex_t*), VT_INIT);
544887Schin 		vtmtxunlock(_Sfmutex);
554887Schin 		if(!f->mutex)
564887Schin 			return -1;
574887Schin 	}
584887Schin 
594887Schin 	if(type == SFMTX_LOCK)
604887Schin 		return vtmtxlock(f->mutex);
614887Schin 	else if(type == SFMTX_TRYLOCK)
624887Schin 		return vtmtxtrylock(f->mutex);
634887Schin 	else if(type == SFMTX_UNLOCK)
644887Schin 		return vtmtxunlock(f->mutex);
654887Schin 	else if(type == SFMTX_CLRLOCK)
664887Schin 		return vtmtxclrlock(f->mutex);
674887Schin 	else	return -1;
684887Schin #endif /*vt_threaded*/
694887Schin }
70