Skip to content

Commit

Permalink
added support for threads
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomix committed Jul 9, 2012
1 parent da0a349 commit ed3c4b6
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/shogun/lib/Map.h
Expand Up @@ -19,6 +19,13 @@

#include <cstdio>

#include <shogun/io/SGIO.h>
#include <shogun/base/Parallel.h>

#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif

namespace shogun
{

Expand Down Expand Up @@ -75,11 +82,16 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject
}

array=new DynArray<CMapNode<K, T>*>(reserved, tracable);

PTHREAD_LOCK_INIT(&lock);
}

/** Default destructor */
virtual ~CMap()
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_DESTROY(&lock);
#endif
destroy_map();
}

Expand All @@ -97,8 +109,14 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject
int32_t index=hash(key);
if (chain_search(index, key)==NULL)
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&lock);
#endif
int32_t added_index=insert_key(index, key, data);
num_elements++;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&lock);
#endif

return added_index;
}
Expand Down Expand Up @@ -131,8 +149,14 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject

if (result!=NULL)
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&lock);
#endif
delete_key(index, result);
num_elements--;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&lock);
#endif
}
}

Expand Down Expand Up @@ -163,6 +187,9 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject
int32_t index=hash(key);
CMapNode<K, T>* result=chain_search(index, key);

#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&lock);
#endif
if (result!=NULL)
return result->data;
else
Expand All @@ -172,6 +199,10 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject

return result->data;
}

#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&lock);
#endif
}

/** Set element by key
Expand All @@ -183,13 +214,20 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject
{
int32_t index=hash(key);
CMapNode<K, T>* result=chain_search(index, key);


#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&lock);
#endif
if (result!=NULL)
result->data=data;
else
{
add(key, data);
}

#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&lock);
#endif
}

/** Get number of elements
Expand Down Expand Up @@ -390,7 +428,7 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject
node->left=NULL;
node->right=NULL;

free_index=temp;
free_index=temp;
}

/*cleans up map*/
Expand Down Expand Up @@ -439,6 +477,10 @@ IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject

/** array for index permission */
DynArray<CMapNode<K, T>*>* array;

#ifdef HAVE_PTHREAD
PTHREAD_LOCK_T lock;
#endif
};

}
Expand Down

0 comments on commit ed3c4b6

Please sign in to comment.