From f923add0a9b6bd17d43f72c21eec4e01e19240cf Mon Sep 17 00:00:00 2001 From: zilio nicolas Date: Tue, 7 Jul 2015 11:12:52 +0200 Subject: clean version for locks --- pcilib/lock.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 pcilib/lock.c (limited to 'pcilib/lock.c') diff --git a/pcilib/lock.c b/pcilib/lock.c new file mode 100644 index 0000000..8715d21 --- /dev/null +++ b/pcilib/lock.c @@ -0,0 +1,116 @@ +#define _GNU_SOURCE +#define _XOPEN_SOURCE 600 + +#include +#include +#include "error.h" +#include "lock.h" +#include "pci.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, error %i\n",(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){ + pthread_mutex_consistent(lock_ctx); + pthread_mutex_lock(lock_ctx); + if(err!=0) pcilib_error("can't acquire lock %s, error %i\n",(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, error %i\n",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno); + else if(errno==EOWNERDEAD){ + pthread_mutex_consistent(lock_ctx); + pthread_mutex_lock(lock_ctx); + if(err!=0) pcilib_error("can't acquire lock %s, error %i\n",(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, error %i\n",(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, error %i\n",(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 semaphore\n"); +} + +/** + * pcilib_init_lock + * 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 + * @param[out] lock_ctx the pointer that will points to the semaphore for other functions + * @param[in] keysem the integer that permits to define to what the semaphore is attached + */ +pcilib_lock_t* pcilib_init_lock(pcilib_t *ctx, char* lock_id, ...){ + int err; + pthread_mutexattr_t attr; + int i,j; + void* addr,*locks_addr; + va_list pa; + va_start(pa,lock_id); + + pcilib_lock_init_flags_t flag; + flag=va_arg(pa,pcilib_lock_init_flags_t); + va_end(pa); + + if(strlen(lock_id)>PCILIB_LOCK_SIZE-sizeof(pcilib_lock_t)) pcilib_error("the entered protocol name is too long"); + 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 & LOCK_INIT) pcilib_lock((pcilib_lock_t*)addr,MUTEX_LOCK); + /* we search for the given lock if it was already initialized*/ + for(j=0;jlocks_handle,j); + while((*(char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t))!=0) && (i Date: Tue, 7 Jul 2015 15:38:14 +0200 Subject: modified for last remarks --- pcilib/lock.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'pcilib/lock.c') diff --git a/pcilib/lock.c b/pcilib/lock.c index 8715d21..b99ecfc 100644 --- a/pcilib/lock.c +++ b/pcilib/lock.c @@ -6,7 +6,7 @@ #include "error.h" #include "lock.h" #include "pci.h" - +#include /* * this function will take the lock for the semaphore pointed by semId */ @@ -18,32 +18,32 @@ void pcilib_lock(pcilib_lock_t *lock_ctx, pcilib_lock_flags_t 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, error %i\n",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno); + 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){ pthread_mutex_consistent(lock_ctx); pthread_mutex_lock(lock_ctx); - if(err!=0) pcilib_error("can't acquire lock %s, error %i\n",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno); + 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, error %i\n",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno); + 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_lock(lock_ctx); - if(err!=0) pcilib_error("can't acquire lock %s, error %i\n",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno); + 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, error %i\n",(char*)(lock_ctx+sizeof(pcilib_lock_t)),errno); + 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, error %i\n",(char*)(lock_ctx+sizeof(pcilib_lock_t)), errno); + 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"); @@ -54,7 +54,7 @@ void pcilib_lock(pcilib_lock_t *lock_ctx, pcilib_lock_flags_t flags, ...){ */ void pcilib_unlock(pcilib_lock_t* lock_ctx){ int err; - if((err=pthread_mutex_unlock(lock_ctx))!=0) pcilib_error("can't unlock semaphore\n"); + if((err=pthread_mutex_unlock(lock_ctx))!=0) pcilib_error("can't unlock mutex: errno %i",errno); } /** @@ -71,21 +71,25 @@ pcilib_lock_t* pcilib_init_lock(pcilib_t *ctx, char* lock_id, ...){ va_list pa; va_start(pa,lock_id); + char* temp; + temp=malloc((strlen(lock_id)+strlen("bank_register_"))*sizeof(char)); + sprintf(temp,"bank_register_%s",lock_id); + pcilib_lock_init_flags_t flag; flag=va_arg(pa,pcilib_lock_init_flags_t); va_end(pa); - if(strlen(lock_id)>PCILIB_LOCK_SIZE-sizeof(pcilib_lock_t)) pcilib_error("the entered protocol name is too long"); + if(strlen(temp)>PCILIB_LOCK_SIZE-sizeof(pcilib_lock_t)) pcilib_error("the entered protocol name is too long"); 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 & LOCK_INIT) pcilib_lock((pcilib_lock_t*)addr,MUTEX_LOCK); + if((flag & LOCK_INIT)==0) pcilib_lock((pcilib_lock_t*)addr,MUTEX_LOCK); /* we search for the given lock if it was already initialized*/ for(j=0;jlocks_handle,j); while((*(char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t))!=0) && (i Date: Fri, 10 Jul 2015 10:52:06 +0200 Subject: last modification+comments update --- pcilib/lock.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'pcilib/lock.c') diff --git a/pcilib/lock.c b/pcilib/lock.c index b99ecfc..b92b11d 100644 --- a/pcilib/lock.c +++ b/pcilib/lock.c @@ -7,6 +7,7 @@ #include "lock.h" #include "pci.h" #include + /* * this function will take the lock for the semaphore pointed by semId */ @@ -21,6 +22,7 @@ void pcilib_lock(pcilib_lock_t *lock_ctx, pcilib_lock_flags_t flags, ...){ 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); @@ -31,7 +33,7 @@ void pcilib_lock(pcilib_lock_t *lock_ctx, pcilib_lock_flags_t flags, ...){ 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_lock(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); } } @@ -58,43 +60,37 @@ void pcilib_unlock(pcilib_lock_t* lock_ctx){ } /** - * pcilib_init_lock * 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 - * @param[out] lock_ctx the pointer that will points to the semaphore for other functions - * @param[in] keysem the integer that permits to define to what the semaphore is attached */ -pcilib_lock_t* pcilib_init_lock(pcilib_t *ctx, char* lock_id, ...){ +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); - - char* temp; - temp=malloc((strlen(lock_id)+strlen("bank_register_"))*sizeof(char)); - sprintf(temp,"bank_register_%s",lock_id); - - pcilib_lock_init_flags_t flag; - flag=va_arg(pa,pcilib_lock_init_flags_t); + err=vsprintf(buffer,lock_id,pa); va_end(pa); - if(strlen(temp)>PCILIB_LOCK_SIZE-sizeof(pcilib_lock_t)) pcilib_error("the entered protocol name is too long"); + 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 & LOCK_INIT)==0) pcilib_lock((pcilib_lock_t*)addr,MUTEX_LOCK); + 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;jlocks_handle,j); while((*(char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t))!=0) && (i Date: Wed, 5 Aug 2015 18:11:59 +0200 Subject: Use global locks to protect kmem allocation to prevent race while allocating simmultaneously locking kmem pages and any other type of kmem --- pcilib/lock.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'pcilib/lock.c') diff --git a/pcilib/lock.c b/pcilib/lock.c index f1cbc56..13e363a 100644 --- a/pcilib/lock.c +++ b/pcilib/lock.c @@ -140,7 +140,10 @@ const char *pcilib_lock_get_name(pcilib_lock_t *lock) { int pcilib_lock_custom(pcilib_lock_t *lock, pcilib_lock_flags_t flags, pcilib_timeout_t timeout) { int err; - if (!lock) return 0; + if (!lock) { + pcilib_error("The null lock pointer is passed to lock function"); + return PCILIB_ERROR_INVALID_ARGUMENT; + } struct timespec tm; -- cgit v1.2.3 From 63bc897c9444b364b0364bfd04a0c87d9fe29307 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Thu, 6 Aug 2015 01:13:17 +0200 Subject: Provide pcilib_try_lock call --- pcilib/lock.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'pcilib/lock.c') diff --git a/pcilib/lock.c b/pcilib/lock.c index 13e363a..9045ffa 100644 --- a/pcilib/lock.c +++ b/pcilib/lock.c @@ -190,6 +190,10 @@ int pcilib_lock(pcilib_lock_t* lock) { return pcilib_lock_custom(lock, PCILIB_LOCK_FLAGS_DEFAULT, PCILIB_TIMEOUT_INFINITE); } +int pcilib_try_lock(pcilib_lock_t* lock) { + return pcilib_lock_custom(lock, PCILIB_LOCK_FLAGS_DEFAULT, PCILIB_TIMEOUT_IMMEDIATE); +} + /** * this function will unlock the semaphore pointed by lock_ctx. */ -- cgit v1.2.3 From 8b8a2bee0bce0bc9cc78d43f236c6a3d8a0bd2e4 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Thu, 6 Aug 2015 02:27:54 +0200 Subject: Fix handling of inconsistent mutexes --- pcilib/lock.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'pcilib/lock.c') diff --git a/pcilib/lock.c b/pcilib/lock.c index 9045ffa..03a2377 100644 --- a/pcilib/lock.c +++ b/pcilib/lock.c @@ -174,8 +174,9 @@ int pcilib_lock_custom(pcilib_lock_t *lock, pcilib_lock_flags_t flags, pcilib_ti err = pthread_mutex_consistent(&lock->mutex); if (err) { pcilib_error("Failed to mark mutex as consistent, errno %i", err); + break; } - break; + return 0; case ETIMEDOUT: case EBUSY: return PCILIB_ERROR_TIMEOUT; @@ -203,6 +204,13 @@ void pcilib_unlock(pcilib_lock_t *lock) { if (!lock) return; - if ((err = pthread_mutex_unlock(&lock->mutex)) != 0) - pcilib_error("Can't unlock mutex, errno %i", err); + if ((err = pthread_mutex_unlock(&lock->mutex)) != 0) { + switch (err) { + case EPERM: + pcilib_error("Trying to unlock not locked mutex (%s) or the mutex which was locked by a different thread", lock->name); + break; + default: + pcilib_error("Can't unlock mutex, errno %i", err); + } + } } -- cgit v1.2.3