libcvd-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libcvd-members] libcvd/cvd/internal aligned_mem.h


From: Edward Rosten
Subject: [libcvd-members] libcvd/cvd/internal aligned_mem.h
Date: Wed, 19 Jul 2006 00:12:10 +0000

CVSROOT:        /cvsroot/libcvd
Module name:    libcvd
Changes by:     Edward Rosten <edrosten>        06/07/19 00:12:10

Modified files:
        cvd/internal   : aligned_mem.h 

Log message:
        Make aligned mem thread safe (provided the code is compiled with 
-pthread)

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/internal/aligned_mem.h?cvsroot=libcvd&r1=1.6&r2=1.7

Patches:
Index: aligned_mem.h
===================================================================
RCS file: /cvsroot/libcvd/libcvd/cvd/internal/aligned_mem.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- aligned_mem.h       16 May 2006 13:20:32 -0000      1.6
+++ aligned_mem.h       19 Jul 2006 00:12:10 -0000      1.7
@@ -2,11 +2,22 @@
 #define CVD_ALIGNED_MEM_H
 #include <map>
 #include <cassert>
+#include <cvd/config.h>
+
+#ifdef _REENTRANT
+    #ifndef CVD_HAVE_PTHREAD
+       #error "CVD is not compiled with thread support. This code is not 
thread safe."
+    #else 
+       #include <pthread.h>
+    #endif
+#endif
 
 namespace CVD {
   namespace Internal
   {
     
+    extern pthread_mutex_t aligned_mem_mutex;
+
     template <class T, int N=20> struct placement_delete
     {
       enum { Size = (1<<N) };
@@ -36,31 +47,59 @@
       static inline void free(T*, size_t ) {}
     };
 
+
     template <class T, int N> struct aligned_mem {
       struct entry {
        char* buffer;
        size_t count;
       };
       static std::map<T*, entry> buffers;
+
+       static pthread_mutex_t mutex;
+
+
       static T* alloc(size_t count)
       {
        char* start = new char[count*sizeof(T) + N];
        size_t val = (size_t)start;
        T* astart = new (start + (N-(val % N))) T[count];
        entry e = {start, count};
+
+
+           #if defined(CVD_HAVE_PTHREAD) && defined(_REENTRANT)
+           pthread_mutex_lock(&mutex);
+           #endif
+
        buffers[astart] = e;
+
+           #if defined(CVD_HAVE_PTHREAD) && defined(_REENTRANT)
+           pthread_mutex_unlock(&mutex);
+           #endif
+
        return astart;
       }
       static void release(T* ptr) 
       {
+           #if defined(CVD_HAVE_PTHREAD) && defined(_REENTRANT)
+           pthread_mutex_lock(&mutex);
+           #endif
+
        typename std::map<T*,entry>::iterator it = buffers.find(ptr);
        assert(it != buffers.end());
        placement_delete<T>::free(ptr, it->second.count);
        delete[] it->second.buffer;
+
+
        buffers.erase(it);
+
+           #if defined(CVD_HAVE_PTHREAD) && defined(_REENTRANT)
+           pthread_mutex_unlock(&mutex);
+           #endif
       }
     };
 
+    template<class T, int N> pthread_mutex_t aligned_mem<T,N>::mutex = 
PTHREAD_MUTEX_INITIALIZER;
+
     template <class T, int N> std::map<T*,typename aligned_mem<T,N>::entry> 
aligned_mem<T,N>::buffers;
 
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]