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/locking.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 pcilib/locking.c (limited to 'pcilib/locking.c') diff --git a/pcilib/locking.c b/pcilib/locking.c new file mode 100644 index 0000000..93e6fdd --- /dev/null +++ b/pcilib/locking.c @@ -0,0 +1,74 @@ +#define _XOPEN_SOURCE 700 + +#include "error.h" +#include "pci.h" +#include +#include +#include "lock.h" + +/* + * this function clean all locks created by the pcitool program + */ +void pcilib_clean_all_locks(pcilib_t* ctx){ +int i,j; +void* addr; + i=0; + while(ilocks_handle,i); +for(j=0;jlocks_handle,PCILIB_KMEM_FLAG_REUSE); +} + +/* + * this function allocates the kernel memory for the locks for software registers + */ +int pcilib_init_locking(pcilib_t* ctx, ...){ +/*for future possible more args + va_list pa; + va_start(pa,ctx);*/ + pcilib_kmem_handle_t *handle; + int err; + pcilib_kmem_reuse_state_t reused; + + + 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) { + pcilib_error("Allocation of kernel memory for mutexes has failed"); + return 1; + } + + ctx->locks_handle=handle; + reused = pcilib_kmem_is_reused(ctx, handle); +//#define DEBUG_REUSE +#ifdef DEBUG_REUSE +reused=0; +#endif + if ((reused & PCILIB_KMEM_REUSE_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_clean_all_locks(ctx); + return 1; + } + for(i=0;ihandle,LOCK_UN))==-1) pcilib_warning("could not remove lock correctly on /dev/fpga0"); + + return 0; +} -- cgit v1.2.3 From fe821c4f1b85d2a2d358da098f85327d41212dc3 Mon Sep 17 00:00:00 2001 From: zilio nicolas Date: Tue, 7 Jul 2015 15:38:14 +0200 Subject: modified for last remarks --- pcilib/locking.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pcilib/locking.c') 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; } -- cgit v1.2.3 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/locking.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'pcilib/locking.c') diff --git a/pcilib/locking.c b/pcilib/locking.c index 1d6b51e..cbe9533 100644 --- a/pcilib/locking.c +++ b/pcilib/locking.c @@ -7,12 +7,13 @@ #include "lock.h" /* - * this function clean all locks created by the pcitool program + * this function destroys all locks without touching at kernel memory directly */ void pcilib_clean_all_locks(pcilib_t* ctx){ int i,j; void* addr; i=0; + /* iteration on all locks*/ while(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;i