summaryrefslogtreecommitdiffstats
path: root/pcilib
diff options
context:
space:
mode:
authorzilio nicolas <nicolas.zilio@kit.edu>2015-07-07 15:38:14 +0200
committerzilio nicolas <nicolas.zilio@kit.edu>2015-07-07 15:38:14 +0200
commitfe821c4f1b85d2a2d358da098f85327d41212dc3 (patch)
tree3f40b2667595b4c7869d455ebaef4e9040ef3865 /pcilib
parentf923add0a9b6bd17d43f72c21eec4e01e19240cf (diff)
downloadpcitool-fe821c4f1b85d2a2d358da098f85327d41212dc3.tar.gz
pcitool-fe821c4f1b85d2a2d358da098f85327d41212dc3.tar.bz2
pcitool-fe821c4f1b85d2a2d358da098f85327d41212dc3.tar.xz
pcitool-fe821c4f1b85d2a2d358da098f85327d41212dc3.zip
modified for last remarks
Diffstat (limited to 'pcilib')
-rw-r--r--pcilib/lock.c40
-rw-r--r--pcilib/locking.c4
2 files changed, 24 insertions, 20 deletions
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 <stdio.h>
/*
* 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;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(lock_id,(char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t)))==0){
+ if(strcmp(temp,(char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t)))==0){
return (pcilib_lock_t*)(locks_addr+i*PCILIB_LOCK_SIZE);}
i++;
}
@@ -93,15 +97,15 @@ pcilib_lock_t* pcilib_init_lock(pcilib_t *ctx, char* lock_id, ...){
}
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 semaphore attribute\n");
- if((err = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))!=0) pcilib_error("can't set a shared mutex\n");
- if((err= pthread_mutexattr_setrobust(&attr,PTHREAD_MUTEX_ROBUST))!=0) pcilib_error("can't set a robust mutex\n");
- if((err=pthread_mutex_init((pcilib_lock_t*)(locks_addr+i*PCILIB_LOCK_SIZE),&attr))!=0) pcilib_error("can't set attributes to semaphore\n");
+ 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)),lock_id);
+ strcpy((char*)(locks_addr+i*PCILIB_LOCK_SIZE+sizeof(pcilib_lock_t)),temp);
- if(flag & LOCK_INIT) pcilib_unlock((pcilib_lock_t*)addr);
+ if((flag & LOCK_INIT)==0) pcilib_unlock((pcilib_lock_t*)addr);
return (pcilib_lock_t*)(locks_addr+i*PCILIB_LOCK_SIZE);
}
@@ -111,6 +115,6 @@ pcilib_lock_t* pcilib_init_lock(pcilib_t *ctx, char* lock_id, ...){
*/
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\n");
+ 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));
}
diff --git a/pcilib/locking.c b/pcilib/locking.c
index 93e6fdd..1d6b51e 100644
--- a/pcilib/locking.c
+++ b/pcilib/locking.c
@@ -49,7 +49,7 @@ 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
@@ -57,7 +57,7 @@ reused=0;
pcilib_register_t i;
if (reused & PCILIB_KMEM_REUSE_PARTIAL) {
- pcilib_error("Inconsistent software registers are found (only part of required buffers is available)");
+ pcilib_error("Inconsistent memory for locks was found (only part of required buffers is available)");
pcilib_clean_all_locks(ctx);
return 1;
}