Blame src/lib/libast/sfio/sfmutex.c

Packit 992a25
/***********************************************************************
Packit 992a25
*                                                                      *
Packit 992a25
*               This software is part of the ast package               *
Packit 992a25
*          Copyright (c) 1985-2011 AT&T Intellectual Property          *
Packit 992a25
*                      and is licensed under the                       *
Packit 992a25
*                 Eclipse Public License, Version 1.0                  *
Packit 992a25
*                    by AT&T Intellectual Property                     *
Packit 992a25
*                                                                      *
Packit 992a25
*                A copy of the License is available at                 *
Packit 992a25
*          http://www.eclipse.org/org/documents/epl-v10.html           *
Packit 992a25
*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
Packit 992a25
*                                                                      *
Packit 992a25
*              Information and Software Systems Research               *
Packit 992a25
*                            AT&T Research                             *
Packit 992a25
*                           Florham Park NJ                            *
Packit 992a25
*                                                                      *
Packit 992a25
*                 Glenn Fowler <gsf@research.att.com>                  *
Packit 992a25
*                  David Korn <dgk@research.att.com>                   *
Packit 992a25
*                   Phong Vo <kpv@research.att.com>                    *
Packit 992a25
*                                                                      *
Packit 992a25
***********************************************************************/
Packit 992a25
#include	"sfhdr.h"
Packit 992a25
Packit 992a25
/*	Obtain/release exclusive use of a stream.
Packit 992a25
**
Packit 992a25
**	Written by Kiem-Phong Vo.
Packit 992a25
*/
Packit 992a25
Packit 992a25
/* the main locking/unlocking interface */
Packit 992a25
#if __STD_C
Packit 992a25
int sfmutex(Sfio_t* f, int type)
Packit 992a25
#else
Packit 992a25
int sfmutex(f, type)
Packit 992a25
Sfio_t*	f;
Packit 992a25
int	type;
Packit 992a25
#endif
Packit 992a25
{
Packit 992a25
#if !vt_threaded
Packit 992a25
	NOTUSED(f); NOTUSED(type);
Packit 992a25
	return 0;
Packit 992a25
#else
Packit 992a25
Packit 992a25
	SFONCE();
Packit 992a25
Packit 992a25
	if(!f)
Packit 992a25
		return -1;
Packit 992a25
Packit 992a25
	if(!f->mutex)
Packit 992a25
	{	if(f->bits&SF_PRIVATE)
Packit 992a25
			return 0;
Packit 992a25
Packit 992a25
		vtmtxlock(_Sfmutex);
Packit 992a25
		f->mutex = vtmtxopen(NIL(Vtmutex_t*), VT_INIT);
Packit 992a25
		vtmtxunlock(_Sfmutex);
Packit 992a25
		if(!f->mutex)
Packit 992a25
			return -1;
Packit 992a25
	}
Packit 992a25
Packit 992a25
	if(type == SFMTX_LOCK)
Packit 992a25
		return vtmtxlock(f->mutex);
Packit 992a25
	else if(type == SFMTX_TRYLOCK)
Packit 992a25
		return vtmtxtrylock(f->mutex);
Packit 992a25
	else if(type == SFMTX_UNLOCK)
Packit 992a25
		return vtmtxunlock(f->mutex);
Packit 992a25
	else if(type == SFMTX_CLRLOCK)
Packit 992a25
		return vtmtxclrlock(f->mutex);
Packit 992a25
	else	return -1;
Packit 992a25
#endif /*vt_threaded*/
Packit 992a25
}