]> git.saurik.com Git - apple/hfs.git/blob - livefiles_hfs_plugin/lf_hfs_locks.c
b39ffad53bfb0c4b46e3334aa7b479dcf16be19e
[apple/hfs.git] / livefiles_hfs_plugin / lf_hfs_locks.c
1 /* Copyright © 2017-2018 Apple Inc. All rights reserved.
2 *
3 * lf_hfs_locks.c
4 * livefiles_hfs
5 *
6 * Created by Or Haimovich on 18/3/18.
7 */
8
9 #include "lf_hfs_locks.h"
10 #include <errno.h>
11 #include <assert.h>
12
13 void lf_lck_rw_init( pthread_rwlock_t* lck )
14 {
15 errno_t err = pthread_rwlock_init( lck, NULL );
16 assert( err == 0 );
17 }
18
19 void lf_lck_rw_destroy( pthread_rwlock_t* lck )
20 {
21 errno_t err = pthread_rwlock_destroy( lck );
22 assert( err == 0 );
23 }
24
25 void lf_lck_rw_unlock_shared( pthread_rwlock_t* lck )
26 {
27 errno_t err = pthread_rwlock_unlock( lck );
28 assert( err == 0 );
29 }
30
31 void lf_lck_rw_lock_shared( pthread_rwlock_t* lck )
32 {
33 errno_t err = pthread_rwlock_rdlock( lck );
34 assert( err == 0 );
35 }
36
37 void lf_lck_rw_lock_exclusive( pthread_rwlock_t* lck )
38 {
39 errno_t err = pthread_rwlock_wrlock( lck );
40 assert( err == 0 );
41 }
42
43 void lf_lck_rw_unlock_exclusive( pthread_rwlock_t* lck )
44 {
45 errno_t err = pthread_rwlock_unlock( lck );
46 assert( err == 0 );
47 }
48
49 bool lf_lck_rw_try_lock( pthread_rwlock_t* lck, lck_rwlock_type_e which )
50 {
51 bool trylock;
52
53 if ( which == LCK_RW_TYPE_SHARED )
54 {
55 trylock = pthread_rwlock_tryrdlock( lck );
56 }
57 else if ( which == LCK_RW_TYPE_EXCLUSIVE )
58 {
59 trylock = pthread_rwlock_trywrlock( lck );
60 }
61 else
62 {
63 assert(0);
64 }
65
66 return trylock;
67 }
68
69 void lf_lck_rw_lock_exclusive_to_shared( pthread_rwlock_t* lck)
70 {
71 lf_lck_rw_unlock_exclusive( lck );
72 lf_lck_rw_lock_shared( lck );
73 }
74
75 bool lf_lck_rw_lock_shared_to_exclusive( pthread_rwlock_t* lck)
76 {
77 lf_lck_rw_unlock_shared( lck );
78 lf_lck_rw_lock_exclusive( lck );
79
80 return true;
81 }
82
83 void lf_cond_init( pthread_cond_t* cond )
84 {
85 errno_t err = pthread_cond_init( cond, NULL );
86 assert( err == 0 );
87 }
88
89 void lf_cond_destroy( pthread_cond_t* cond )
90 {
91 errno_t err = pthread_cond_destroy( cond );
92 assert( err == 0 );
93 }
94
95 int lf_cond_wait_relative(pthread_cond_t *pCond, pthread_mutex_t *pMutex, struct timespec *pTime) {
96
97 int iErr = pthread_cond_timedwait_relative_np(pCond, pMutex, pTime);
98 assert((iErr == 0) || (iErr == ETIMEDOUT));
99 return(iErr);
100 }
101
102 void lf_cond_wakeup(pthread_cond_t *pCond) {
103
104 int iErr = pthread_cond_signal(pCond);
105 assert(iErr == 0);
106 }
107
108 void lf_lck_mtx_init( pthread_mutex_t* lck )
109 {
110 errno_t err = pthread_mutex_init( lck, NULL );
111 assert( err == 0 );
112 }
113
114 void lf_lck_mtx_destroy( pthread_mutex_t *lck )
115 {
116 errno_t err = pthread_mutex_destroy( lck );
117 assert( err == 0 );
118 }
119
120 void lf_lck_mtx_lock( pthread_mutex_t* lck )
121 {
122 errno_t err = pthread_mutex_lock( lck );
123 assert( err == 0 );
124 }
125
126 void lf_lck_mtx_unlock( pthread_mutex_t* lck )
127 {
128 errno_t err = pthread_mutex_unlock( lck );
129 assert( err == 0 );
130 }
131
132 void lf_lck_mtx_lock_spin( pthread_mutex_t *lck )
133 {
134 // No real spin lock
135 lf_lck_mtx_lock( lck );
136 }
137
138 int lf_lck_mtx_try_lock(pthread_mutex_t *lck) {
139 errno_t err = pthread_mutex_trylock(lck);
140 return err;
141 }
142
143 //void lf_lck_mtx_convert_spin( pthread_mutex_t *lck )
144 //{
145 // // No real spin lock
146 //}
147
148 void lf_lck_spin_init( pthread_mutex_t *lck )
149 {
150 errno_t err = pthread_mutex_init( lck, NULL );
151 assert( err == 0 );
152 }
153
154 void lf_lck_spin_destroy( pthread_mutex_t *lck )
155 {
156 errno_t err = pthread_mutex_destroy( lck );
157 assert( err == 0 );
158 }
159
160 void lf_lck_spin_lock( pthread_mutex_t *lck )
161 {
162 errno_t err = pthread_mutex_lock( lck );
163 assert( err == 0 );
164 }
165
166 void lf_lck_spin_unlock( pthread_mutex_t *lck )
167 {
168 errno_t err = pthread_mutex_unlock( lck );
169 assert( err == 0 );
170 }
171
172 lck_attr_t *lf_lck_attr_alloc_init( void )
173 {
174 static lck_attr_t attr = {0};
175 return &attr;
176 }
177 lck_grp_attr_t *lf_lck_grp_attr_alloc_init( void )
178 {
179 static lck_grp_attr_t group_attr = {0};
180 return &group_attr;
181 }
182 lck_grp_t *lf_lck_grp_alloc_init( void )
183 {
184 static lck_grp_t group = {0};
185 return &group;
186 }