#ifndef QFACTORY_H #define QFACTORY_H #include "usg_common.h" #include "mm.h" #include "hashtable.h" #include "lock_free_queue.h" class QueueFactory{ private: static hashtable_t * getHashTable() { static hashtable_t *hashtable = NULL; int first; if(hashtable == NULL) { first = mm_init(sizeof(hashtable_t), (void **)&hashtable); if (first) hashtable_init(hashtable); } return hashtable; } template static LockFreeQueue* createArrayLockFreeQueue(int key, size_t size=16) { LockFreeQueue *queue; hashtable_t *hashtable = getHashTable(); //LockFreeQueue q; if ((queue = (LockFreeQueue *)hashtable_get(hashtable, key)) == NULL ) { queue = new LockFreeQueue(size); hashtable_put(hashtable, key, (void *)queue); } return queue; } public: template static LockFreeQueue* createQueue(int key, size_t size = 16) { return QueueFactory::createArrayLockFreeQueue(key, size); } /** * destroy queue */ template static void dropQueue(int key) { LockFreeQueue *queue = QueueFactory::createQueue (key); delete queue; hashtable_t *hashtable = getHashTable(); hashtable_remove(hashtable, key); } }; #endif