/*************************************************************** MPU-LOCK.C This file contains public stream locking functions. PART OF : MPUIO - library . USAGE : External only . NOTE : Stream locks are recursive, matching stdio flockfile() ownership semantics. Copyright (C) 2000 - 2026 by Andrey V.Kosteltsev. All Rights Reserved. ***************************************************************/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include /*************************************************************** Lock STREAM for exclusive use by the calling thread. ***************************************************************/ void mpu_flockfile( mpu_FILE *stream ) { int ret; if( !__mpu_IO_valid( stream ) ) return; ret = pthread_mutex_lock( &stream->_lock ); if( ret != 0 ) errno = ret; return; } /* End of mpu_flockfile() */ /*************************************************************** Try to lock STREAM without waiting; return zero on success. ***************************************************************/ int mpu_ftrylockfile( mpu_FILE *stream ) { int ret; if( !__mpu_IO_valid( stream ) ) return( EINVAL ); ret = pthread_mutex_trylock( &stream->_lock ); if( ret != 0 ) errno = ret; return( ret ); } /* End of mpu_ftrylockfile() */ /*************************************************************** Release one STREAM lock held by the calling thread. ***************************************************************/ void mpu_funlockfile( mpu_FILE *stream ) { int ret; if( !__mpu_IO_valid( stream ) ) return; ret = pthread_mutex_unlock( &stream->_lock ); if( ret != 0 ) errno = ret; return; } /* End of mpu_funlockfile() */