summaryrefslogtreecommitdiffstats
path: root/pcilib/lock.c
diff options
context:
space:
mode:
Diffstat (limited to 'pcilib/lock.c')
-rw-r--r--pcilib/lock.c277
1 files changed, 181 insertions, 96 deletions
diff --git a/pcilib/lock.c b/pcilib/lock.c
index b92b11d..f1cbc56 100644
--- a/pcilib/lock.c
+++ b/pcilib/lock.c
@@ -1,116 +1,201 @@
#define _GNU_SOURCE
#define _XOPEN_SOURCE 600
+#include "config.h"
+
+#include <stdio.h>
#include <string.h>
#include <stdint.h>
+#include <assert.h>
+#include <pthread.h>
+#include <stdint.h>
+
+#ifdef HAVE_STDATOMIC_H
+# include <stdatomic.h>
+#endif /* HAVE_STDATOMIC_H */
+
#include "error.h"
#include "lock.h"
#include "pci.h"
-#include <stdio.h>
-/*
- * this function will take the lock for the semaphore pointed by semId
- */
-void pcilib_lock(pcilib_lock_t *lock_ctx, pcilib_lock_flags_t flags, ...){
- int err;
- struct timespec *time;
- va_list pa;
- va_start(pa,flags);
-
- if(flags & MUTEX_LOCK){
- err=pthread_mutex_lock(lock_ctx);/**< we try to lock here*/
- if(errno!=EOWNERDEAD && err!=0) pcilib_error("can't acquire lock %s, errno %i",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno);
- /** if the lock haven't been acquired and errno==EOWNERDEAD, it means the previous application that got the lock crashed, we have to remake the lock "consistent" so*/
- else if(errno==EOWNERDEAD){
- /* one question is "is pthread_mutex_consistent protected in case we call twice it?", it seems to not make any importance in fact regarding man pages, but we have to survey it in future applications*/
- pthread_mutex_consistent(lock_ctx);
- pthread_mutex_lock(lock_ctx);
- if(err!=0) pcilib_error("can't acquire lock %s, errno %i",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno);
- }
- }
- else if(flags & MUTEX_TRYLOCK){
- err=pthread_mutex_trylock(lock_ctx);/**< we try to lock here*/
- if(errno!=EOWNERDEAD && err!=0) pcilib_error("can't acquire lock %s, errno %i",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno);
- else if(errno==EOWNERDEAD){
- pthread_mutex_consistent(lock_ctx);
- pthread_mutex_trylock(lock_ctx);
- if(err!=0) pcilib_error("can't acquire lock %s, errno %i",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno);
- }
- }
- else if(flags & MUTEX_TIMEDLOCK){
- time=va_arg(pa,struct timespec*);
- va_end(pa);
- err=pthread_mutex_timedlock(lock_ctx, time);/**< we try to lock here*/
- if(errno!=EOWNERDEAD && err!=0) pcilib_error("can't acquire lock %s, errni %i",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno);
- else if(errno==EOWNERDEAD){
- pthread_mutex_consistent(lock_ctx);
- pthread_mutex_timedlock(lock_ctx, time);
- if(err!=0) pcilib_error("can't acquire lock %s, errno %i",(char*)(lock_ctx+sizeof(pcilib_lock_t)), errno);
- }
- }
- else pcilib_error("wrong flag for pcilib_lock");
-}
-/**
- * this function will unlock the semaphore pointed by lock_ctx.
- */
-void pcilib_unlock(pcilib_lock_t* lock_ctx){
- int err;
- if((err=pthread_mutex_unlock(lock_ctx))!=0) pcilib_error("can't unlock mutex: errno %i",errno);
-}
+struct pcilib_lock_s {
+ pthread_mutex_t mutex;
+ pcilib_lock_flags_t flags;
+#ifdef HAVE_STDATOMIC_H
+ volatile atomic_uint refs;
+#else /* HAVE_STDATOMIC_H */
+ volatile uint32_t refs;
+#endif /* HAVE_STDATOMIC_H */
+ char name[];
+};
+
/**
* this function initialize a new semaphore in the kernel if it's not already initialized given the key that permits to differentiate semaphores, and then return the integer that points to the semaphore that have been initialized or to a previously already initialized semaphore
*/
-pcilib_lock_t* pcilib_init_lock(pcilib_t *ctx, pcilib_lock_init_flags_t flag, char* lock_id, ...){
- int err;
- pthread_mutexattr_t attr;
- int i,j;
- void* addr,*locks_addr;
- char buffer[PCILIB_LOCK_SIZE-sizeof(pcilib_lock_t)];
- /* here lock_id is the format string for vsprintf, the lock name will so be put in adding arguments*/
- va_list pa;
- va_start(pa,lock_id);
- err=vsprintf(buffer,lock_id,pa);
- va_end(pa);
-
- if(err<0) pcilib_error("error in obtaining the lock name");
- if(((PCILIB_MAX_NUMBER_LOCKS*PCILIB_LOCK_SIZE)%PCILIB_KMEM_PAGE_SIZE)!=0) pcilib_error("PCILIB_MAX_NUMBER_LOCKS*PCILIB_LOCK_SIZE should be a multiple of kmem page size");
-
- addr=pcilib_kmem_get_block_ua(ctx,ctx->locks_handle,0);
- if((flag & PCILIB_NO_LOCK)==0) pcilib_lock((pcilib_lock_t*)addr,MUTEX_LOCK);
- /* we search for the given lock if it was already initialized*/
- for(j=0;j<PCILIB_NUMBER_OF_LOCK_PAGES;j++){
- i=0;
- locks_addr=pcilib_kmem_get_block_ua(ctx,ctx->locks_handle,j);
- while((*(char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t))!=0) && (i<PCILIB_LOCKS_PER_PAGE)){
- if(strcmp(buffer,(char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t)))==0){
- return (pcilib_lock_t*)(locks_addr+i*PCILIB_LOCK_SIZE);}
- i++;
- }
- if(i<PCILIB_LOCKS_PER_PAGE) break;
+int pcilib_init_lock(pcilib_lock_t *lock, pcilib_lock_flags_t flags, const char *lock_id) {
+ int err;
+ pthread_mutexattr_t attr;
+
+ assert(lock);
+ assert(lock_id);
+
+ memset(lock, 0, PCILIB_LOCK_SIZE);
+
+ if (strlen(lock_id) >= (PCILIB_LOCK_SIZE - offsetof(struct pcilib_lock_s, name))) {
+ pcilib_error("The supplied lock id (%s) is too long...", lock_id);
+ return PCILIB_ERROR_INVALID_ARGUMENT;
+ }
+
+ if ((err = pthread_mutexattr_init(&attr))!=0) {
+ pcilib_error("Can't initialize mutex attribute, errno %i", errno);
+ return PCILIB_ERROR_FAILED;
+ }
+
+ if ((err = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))!=0) {
+ pcilib_error("Can't configure a shared mutex attribute, errno %i", errno);
+ return PCILIB_ERROR_FAILED;
+ }
+
+ if ((flags&PCILIB_LOCK_FLAG_PERSISTENT)==0) {
+ if ((err = pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST))!=0) {
+ pcilib_error("Can't configure a robust mutex attribute, errno: %i", errno);
+ return PCILIB_ERROR_FAILED;
}
- /* the kernel space could be full*/
- if(i==PCILIB_LOCKS_PER_PAGE) pcilib_error("no more free space for a new lock\n");
- /* if not, we create a new one*/
- if((err= pthread_mutexattr_init(&attr))!=0) pcilib_error("can't initialize mutex attribute, errno %i",errno);
- if((err = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))!=0) pcilib_error("can't set a shared mutex, errno %i", errno);
- if((err= pthread_mutexattr_setrobust(&attr,PTHREAD_MUTEX_ROBUST))!=0) pcilib_error("can't set a robust mutex, errno: %i",errno);
- if((err=pthread_mutex_init((pcilib_lock_t*)(locks_addr+i*PCILIB_LOCK_SIZE),&attr))!=0) pcilib_error("can't set attributes to mutex, errno : %i",errno);
- pthread_mutexattr_destroy(&attr);
-
- strcpy((char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t)),buffer);
-
- if((flag & PCILIB_NO_LOCK)==0) pcilib_unlock((pcilib_lock_t*)addr);
- return (pcilib_lock_t*)(locks_addr+i*PCILIB_LOCK_SIZE);
-
+ }
+
+ if ((err = pthread_mutex_init(&lock->mutex, &attr))!=0) {
+ pcilib_error("Can't create mutex, errno : %i",errno);
+ return PCILIB_ERROR_FAILED;
+ }
+
+ strcpy(lock->name, lock_id);
+ lock->refs = 1;
+ lock->flags = flags;
+
+ return 0;
}
+
/*
* we uninitialize a mutex and set its name to 0 pointed by lock_ctx with this function. setting name to is the real destroying operation, but we need to unitialize the lock to initialize it again after
*/
-void pcilib_free_lock(pcilib_lock_t *lock_ctx){
- int err;
- if((err=pthread_mutex_destroy(lock_ctx))==-1) pcilib_warning("can't clean lock properly, errno %i",errno);
- memset((char*)(lock_ctx+sizeof(pcilib_lock_t)),0,PCILIB_LOCK_SIZE-sizeof(pcilib_lock_t));
- }
+void pcilib_free_lock(pcilib_lock_t *lock) {
+ int err;
+
+ assert(lock);
+
+// if (lock->refs)
+// pcilib_error("Forbidding to destroy the referenced mutex...");
+
+ if ((err = pthread_mutex_destroy(&lock->mutex))==-1)
+ pcilib_warning("Can't destroy POSIX mutex, errno %i",errno);
+}
+
+
+void pcilib_lock_ref(pcilib_lock_t *lock) {
+ assert(lock);
+
+#ifdef HAVE_STDATOMIC_H
+ atomic_fetch_add_explicit(&lock->refs, 1, memory_order_relaxed);
+#else /* HAVE_STDATOMIC_H */
+ lock->refs++;
+#endif /* HAVE_STDATOMIC_H */
+}
+
+void pcilib_lock_unref(pcilib_lock_t *lock) {
+ assert(lock);
+
+ if (!lock->refs) {
+ pcilib_warning("Lock is not referenced");
+ return;
+ }
+
+#ifdef HAVE_STDATOMIC_H
+ atomic_fetch_sub_explicit(&lock->refs, 1, memory_order_relaxed);
+#else /* HAVE_STDATOMIC_H */
+ lock->refs--;
+#endif /* HAVE_STDATOMIC_H */
+}
+
+size_t pcilib_lock_get_refs(pcilib_lock_t *lock) {
+ return lock->refs;
+}
+
+pcilib_lock_flags_t pcilib_lock_get_flags(pcilib_lock_t *lock) {
+ return lock->flags;
+}
+
+const char *pcilib_lock_get_name(pcilib_lock_t *lock) {
+ assert(lock);
+
+ if (lock->name[0]) return lock->name;
+ return NULL;
+}
+
+/*
+ * this function will take the lock for the semaphore pointed by semId
+ */
+int pcilib_lock_custom(pcilib_lock_t *lock, pcilib_lock_flags_t flags, pcilib_timeout_t timeout) {
+ int err;
+
+ if (!lock) return 0;
+
+ struct timespec tm;
+
+ switch (timeout) {
+ case PCILIB_TIMEOUT_INFINITE:
+ err = pthread_mutex_lock(&lock->mutex);
+ break;
+ case PCILIB_TIMEOUT_IMMEDIATE:
+ err = pthread_mutex_trylock(&lock->mutex);
+ break;
+ default:
+ clock_gettime(CLOCK_REALTIME, &tm);
+ tm.tv_nsec += 1000 * (timeout%1000000);
+ if (tm.tv_nsec < 1000000000)
+ tm.tv_sec += timeout/1000000;
+ else {
+ tm.tv_sec += 1 + timeout/1000000;
+ tm.tv_nsec -= 1000000000;
+ }
+ err = pthread_mutex_timedlock(&lock->mutex, &tm);
+ }
+
+ if (!err)
+ return 0;
+
+ switch (err) {
+ case EOWNERDEAD:
+ err = pthread_mutex_consistent(&lock->mutex);
+ if (err) {
+ pcilib_error("Failed to mark mutex as consistent, errno %i", err);
+ }
+ break;
+ case ETIMEDOUT:
+ case EBUSY:
+ return PCILIB_ERROR_TIMEOUT;
+ default:
+ pcilib_error("Failed to obtain mutex, errno %i", err);
+ }
+
+ return PCILIB_ERROR_FAILED;
+}
+
+int pcilib_lock(pcilib_lock_t* lock) {
+ return pcilib_lock_custom(lock, PCILIB_LOCK_FLAGS_DEFAULT, PCILIB_TIMEOUT_INFINITE);
+}
+
+/**
+ * this function will unlock the semaphore pointed by lock_ctx.
+ */
+void pcilib_unlock(pcilib_lock_t *lock) {
+ int err;
+
+ if (!lock)
+ return;
+
+ if ((err = pthread_mutex_unlock(&lock->mutex)) != 0)
+ pcilib_error("Can't unlock mutex, errno %i", err);
+}