#include <glib.h>
Go to the source code of this file.
Enumerations | |
enum | lock_state_t { lock_none, lock_read, lock_write } |
Functions | |
gboolean | erRegRWLockInit (void) |
void | erRegRWLockDestroy (void) |
gboolean | erRegReadLock (void) |
gboolean | erRegWriteLock (void) |
gboolean | erRegUnlock (void) |
lock_state_t | erRegGetLockState (void) |
Copyright (C) 2007 iRex Technologies B.V. All rights reserved.
Definition in file erregrwlock.h.
enum lock_state_t |
Definition at line 41 of file erregrwlock.h.
00042 { 00043 lock_none, 00044 lock_read, 00045 lock_write 00046 } lock_state_t;
lock_state_t erRegGetLockState | ( | void | ) |
inquire current lock state on registry
Definition at line 273 of file erregrwlock.c.
00274 { 00275 return g_lock_state; 00276 }
gboolean erRegReadLock | ( | void | ) |
put read lock on registry, call it when reading registry notes: don't forget using erRegUnlock() to release the lock afterwards
Definition at line 156 of file erregrwlock.c.
00157 { 00158 ERREG_RWLOCK_LOGPRINTF("entry"); 00159 g_assert(g_lock_fd >= 0); 00160 00161 gboolean bRet = FALSE; // FALSE = error 00162 int rc; 00163 00164 // get mutex to block other threads 00165 rc = pthread_mutex_lock(&g_mutex); 00166 if (rc != 0) 00167 { 00168 // mutex error 00169 ERREG_RWLOCK_ERRORPRINTF("Could not lock mutex, error [%d] [%s]", rc, strerror(rc)); 00170 g_assert_not_reached(); 00171 } 00172 else 00173 { 00174 // get file lock 00175 rc = readw_lock(); 00176 if (rc == -1) 00177 { 00178 // file lock error 00179 ERREG_RWLOCK_ERRORPRINTF("Could not lock file, error [%d] [%s]", errno, strerror(errno)); 00180 g_assert_not_reached(); 00181 } 00182 else 00183 { 00184 // file lock obtained 00185 ERREG_RWLOCK_LOGPRINTF("Obtained RLock."); 00186 g_lock_state = lock_read; 00187 bRet = TRUE; // TRUE = ok 00188 } 00189 } 00190 00191 return bRet; 00192 }
void erRegRWLockDestroy | ( | void | ) |
destroy resource for rwlock when exit an application which calls erRegRWLockInit() before
Definition at line 126 of file erregrwlock.c.
00127 { 00128 ERREG_RWLOCK_LOGPRINTF("entry"); 00129 g_assert(g_lock_fd >= 0); 00130 00131 int rc; 00132 00133 // close file for inter-process locking, this implicitly releases the lock 00134 rc = close(g_lock_fd); 00135 if (rc != 0) 00136 { 00137 // file error 00138 ERREG_RWLOCK_ERRORPRINTF("Could not close file, error [%d] [%s]", errno, strerror(errno)); 00139 g_assert_not_reached(); 00140 } 00141 g_lock_fd = -1; 00142 00143 // destroy mutex for inter-thread locking 00144 rc = pthread_mutex_destroy(&(g_mutex)); 00145 if (rc != 0) 00146 { 00147 // mutex error 00148 ERREG_RWLOCK_ERRORPRINTF("Could not destroy mutex, error [%d] [%s]", rc, strerror(rc)); 00149 g_assert_not_reached(); 00150 } 00151 00152 g_lock_state = lock_none; 00153 }
gboolean erRegRWLockInit | ( | void | ) |
Initilize resource for rwlock when launching an application which wants to access registry
Definition at line 88 of file erregrwlock.c.
00089 { 00090 ERREG_RWLOCK_LOGPRINTF("entry"); 00091 g_assert(g_lock_fd < 0); 00092 00093 gboolean bRet = FALSE; // FALSE = error 00094 int rc; 00095 00096 // initialise mutex for inter-thread locking 00097 rc = pthread_mutex_init(&(g_mutex), NULL); 00098 if (rc != 0) 00099 { 00100 // mutex error 00101 ERREG_RWLOCK_ERRORPRINTF("Could not initialize mutex, error [%d] [%s]", rc, strerror(rc)); 00102 g_assert_not_reached(); 00103 } 00104 else 00105 { 00106 // open file for inter-process locking 00107 g_lock_fd = open(DATADIR LOCK_FILE, O_RDWR | O_CREAT); 00108 if (g_lock_fd < 0) 00109 { 00110 // file error 00111 ERREG_RWLOCK_ERRORPRINTF("Could not open file [%s], error [%d] [%s]", DATADIR LOCK_FILE, errno, strerror(errno)); 00112 g_assert_not_reached(); 00113 } 00114 else 00115 { 00116 // filed opened ok 00117 bRet = TRUE; 00118 } 00119 } 00120 00121 g_lock_state = lock_none; 00122 return bRet; 00123 }
gboolean erRegUnlock | ( | void | ) |
release lock(read or write) on registry
Definition at line 234 of file erregrwlock.c.
00235 { 00236 ERREG_RWLOCK_LOGPRINTF("entry"); 00237 g_assert(g_lock_fd >= 0); 00238 00239 gboolean bRet = FALSE; // FALSE = error 00240 int rc; 00241 00242 // release file lock 00243 rc = un_lock(); 00244 if (rc == -1) 00245 { 00246 // file unlock error 00247 ERREG_RWLOCK_ERRORPRINTF("Could not release file lock, error [%d] [%s]", errno, strerror(errno)); 00248 g_assert_not_reached(); 00249 } 00250 else 00251 { 00252 // release mutex 00253 rc = pthread_mutex_unlock(&g_mutex); 00254 if (rc != 0) 00255 { 00256 // mutex error 00257 ERREG_RWLOCK_ERRORPRINTF("Could not release mutex, error [%d] [%s]", rc, strerror(rc)); 00258 g_assert_not_reached(); 00259 } 00260 else 00261 { 00262 // locks released ok 00263 ERREG_RWLOCK_LOGPRINTF("Released Lock"); 00264 g_lock_state = lock_none; 00265 bRet = TRUE; // TRUE = ok 00266 } 00267 } 00268 00269 return bRet; 00270 }
gboolean erRegWriteLock | ( | void | ) |
put write lock on registry, call it when writing registry notes: don't forget using erRegUnlock() to release the lock afterwards
Definition at line 195 of file erregrwlock.c.
00196 { 00197 ERREG_RWLOCK_LOGPRINTF("entry"); 00198 g_assert(g_lock_fd >= 0); 00199 00200 gboolean bRet = FALSE; // FALSE = error 00201 int rc; 00202 00203 // get mutex to block other threads 00204 rc = pthread_mutex_lock(&g_mutex); 00205 if (rc != 0) 00206 { 00207 // mutex error 00208 ERREG_RWLOCK_ERRORPRINTF("Could not lock mutex, error [%d] [%s]", rc, strerror(rc)); 00209 g_assert_not_reached(); 00210 } 00211 else 00212 { 00213 // get file lock 00214 rc = writew_lock(); 00215 if (rc == -1) 00216 { 00217 // file lock error 00218 ERREG_RWLOCK_ERRORPRINTF("Could not lock file, error [%d] [%s]", errno, strerror(errno)); 00219 g_assert_not_reached(); 00220 } 00221 else 00222 { 00223 // file lock obtained 00224 ERREG_RWLOCK_LOGPRINTF("Obtained WLock"); 00225 g_lock_state = lock_write; 00226 bRet = TRUE; // TRUE = ok 00227 } 00228 } 00229 00230 return bRet; 00231 }