From edd5ccf24c146915ee475bd223e2ad695520a241 Mon Sep 17 00:00:00 2001 From: zilio nicolas Date: Fri, 10 Jul 2015 10:52:06 +0200 Subject: last modification+comments update --- pcilib/lock.c | 32 ++++++++++++++------------------ pcilib/lock.h | 25 +++++++++++++++++-------- pcilib/locking.c | 15 +++++++++++---- pcilib/locking.h | 7 ++++++- protocols/software.c | 2 +- 5 files changed, 49 insertions(+), 32 deletions(-) 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) && (ilocks_handle,i); for(j=0;jlocks_handle,PCILIB_KMEM_FLAG_REUSE); @@ -38,8 +42,9 @@ int pcilib_init_locking(pcilib_t* ctx, ...){ int err; pcilib_kmem_reuse_state_t reused; - + /* we flock() to make sure to not have two initialization in the same time (possible long time to init)*/ if((err=flock(ctx->handle,LOCK_EX))==-1) pcilib_warning("can't get flock on /dev/fpga0"); + handle=pcilib_alloc_kernel_memory(ctx,PCILIB_KMEM_TYPE_PAGE,PCILIB_NUMBER_OF_LOCK_PAGES,PCILIB_KMEM_PAGE_SIZE,0,PCILIB_KMEM_USE(PCILIB_KMEM_USE_MUTEXES,0),PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_PERSISTENT); if (!handle) { @@ -49,10 +54,11 @@ int pcilib_init_locking(pcilib_t* ctx, ...){ ctx->locks_handle=handle; reused = pcilib_kmem_is_reused(ctx, handle); - #define DEBUG_REUSE +//#define DEBUG_REUSE #ifdef DEBUG_REUSE reused=0; #endif +/* verification about the handling memory got, first use or not, and integrity*/ if ((reused & PCILIB_KMEM_REUSE_REUSED) == 0) { pcilib_register_t i; @@ -61,10 +67,11 @@ reused=0; pcilib_clean_all_locks(ctx); return 1; } + /* if we get here so this is the first initialization (after some free or new), we so set kernel pages to 0 and initialize then the first lock that will be used when we create other locks*/ for(i=0;iname,"software_register"); pcilib_lock(semId,MUTEX_LOCK); handle = pcilib_alloc_kernel_memory(ctx, PCILIB_KMEM_TYPE_PAGE, 1, PCILIB_KMEM_PAGE_SIZE, 0, PCILIB_KMEM_USE(PCILIB_KMEM_USE_SOFTWARE_REGISTERS, bank), PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_PERSISTENT); /**< get the kernel memory*/ -- cgit v1.2.3