-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockref.c
More file actions
executable file
·85 lines (74 loc) · 1.77 KB
/
Copy pathlockref.c
File metadata and controls
executable file
·85 lines (74 loc) · 1.77 KB
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
82
83
84
#include "lockref.h"
#include "assert.h"
#include <errno.h>
void
util_lockref_init(struct lockref *lockref, int c)
{
pthread_mutex_init(&lockref->lock, NULL);
lockref->count = c;
}
void
util_lockref_fini(struct lockref *lockref)
{
pthread_mutex_destroy(&lockref->lock);
}
void
util_lockref_get(struct lockref *lockref)
{
pthread_mutex_lock(&lockref->lock);
lockref->count++;
pthread_mutex_unlock(&lockref->lock);
}
int
util_lockref_get_not_dead(struct lockref *lockref)
{
int retval;
pthread_mutex_lock(&lockref->lock);
retval = 0;
if (lockref->count >= 0) {
lockref->count++;
retval = 1;
}
pthread_mutex_unlock(&lockref->lock);
return retval;
}
void
util_lockref_mark_dead(struct lockref *lockref)
{
// assert mutex is locked
always_assert(pthread_mutex_trylock(&lockref->lock) == EBUSY);
lockref->count = -128;
}
/**
* lockref_put_or_lock - decrements count unless count <= 1 before decrement
* @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
*/
int
util_lockref_put_or_lock(struct lockref *lockref)
{
pthread_mutex_lock(&lockref->lock);
if (lockref->count <= 1)
return 0;
lockref->count--;
pthread_mutex_unlock(&lockref->lock);
return 1;
}
/**
* lockref_put_not_zero - Decrements count unless count <= 1 before decrement
* @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if count would become zero
*/
int
util_lockref_put_not_zero(struct lockref *lockref)
{
int retval;
pthread_mutex_lock(&lockref->lock);
retval = 0;
if (lockref->count > 1) {
lockref->count--;
retval = 1;
}
pthread_mutex_unlock(&lockref->lock);
return retval;
}