summaryrefslogtreecommitdiff
path: root/src/libio/mpu-lock.c
blob: d59f5654efe97f37da499b6f6a055eb8a43ac22c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/***************************************************************
  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 <config.h>
#endif

#include <libmpuio.h>
#include <mpu-symbols.h>
#include <mpu-libio.h>


/***************************************************************
  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() */