From 379f42982b8c57ee6511cb8e498019f454323977 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 07 七月 2020 11:04:39 +0800
Subject: [PATCH] update

---
 squeue/include/queue_factory.h                          |   46 ++++++++++-----
 squeue/include/SLinkedLockFreeQueue.h                   |   34 +++++-----
 squeue/mm.c                                             |   27 ++++----
 squeue/sem_util.h                                       |    4 
 squeue/include/lock_free_queue_impl_multiple_producer.h |    3 
 squeue/include/lock_free_queue_impl.h                   |   10 +++
 squeue/mm.h                                             |    4 
 test/test_queue.c                                       |   12 +--
 test/test_queue                                         |    0 
 squeue/include/lock_free_queue.h                        |    4 +
 test/test.h                                             |   12 ++-
 squeue/include/SAbstractQueue.h                         |    2 
 squeue/sem_util.c                                       |   14 ++--
 13 files changed, 99 insertions(+), 73 deletions(-)

diff --git a/squeue/include/SAbstractQueue.h b/squeue/include/SAbstractQueue.h
index 19c3d58..a72439f 100644
--- a/squeue/include/SAbstractQueue.h
+++ b/squeue/include/SAbstractQueue.h
@@ -1,8 +1,6 @@
 // queue.h -- interface for a queue
 #ifndef SAbstractQueue_H_
 #define SAbstractQueue_H_
-#include "mm.h" 
-#include "pcsem.h"
 
 template <typename T>
 class SAbstractQueue
diff --git a/squeue/include/SLinkedLockFreeQueue.h b/squeue/include/SLinkedLockFreeQueue.h
index 66596f2..8225920 100644
--- a/squeue/include/SLinkedLockFreeQueue.h
+++ b/squeue/include/SLinkedLockFreeQueue.h
@@ -2,7 +2,7 @@
 #ifndef SLinkedLockFreeQueue_H_
 #define SLinkedLockFreeQueue_H_
 #include "mm.h" 
-#include "pcsem.h"
+#include "sem_util.h"
 #include "SAbstractQueue.h"
 
 
@@ -102,8 +102,8 @@
     Head.store(pointer, std::memory_order_relaxed);
     Tail.store(pointer, std::memory_order_relaxed);
 
-    slots = pcsem::init(IPC_PRIVATE, qsize);
-    items = pcsem::init(IPC_PRIVATE, 0);
+    slots = SemUtil::get(IPC_PRIVATE, qsize);
+    items = SemUtil::get(IPC_PRIVATE, 0);
    
 }
 
@@ -111,8 +111,8 @@
 SLinkedLockFreeQueue<T>::~SLinkedLockFreeQueue()
 {
     std::cerr << "SLinkedLockFreeQueue destory" << std::endl;
-    pcsem::remove(slots);
-    pcsem::remove(items);
+    SemUtil::remove(slots);
+    SemUtil::remove(items);
 
     
     Node<T> * nodeptr;
@@ -189,12 +189,12 @@
 template <typename T>
 bool SLinkedLockFreeQueue<T>::add(const T & item)
 {
-    if (pcsem::dec(slots) == -1) {
+    if (SemUtil::dec(slots) == -1) {
         err_exit(errno, "add");
     }
 
     if (SLinkedLockFreeQueue<T>::_add(item)) {
-        pcsem::inc(items);      
+        SemUtil::inc(items);      
         return true;
     }
     return false;
@@ -204,7 +204,7 @@
 template <typename T>
 bool SLinkedLockFreeQueue<T>::add_nowait(const T & item)
 {
-    if (pcsem::dec_nowait(slots) == -1) {
+    if (SemUtil::dec_nowait(slots) == -1) {
         if (errno == EAGAIN)
             return false;
         else
@@ -212,7 +212,7 @@
     }
 
     if (SLinkedLockFreeQueue<T>::_add(item)) {
-        pcsem::inc(items);     
+        SemUtil::inc(items);     
         return true;
     }
     return false;
@@ -222,7 +222,7 @@
 template <typename T>
 bool SLinkedLockFreeQueue<T>::add_timeout(const T & item, struct timespec * timeout)
 {
-    if (pcsem::dec_timeout(slots, timeout) == -1) {
+    if (SemUtil::dec_timeout(slots, timeout) == -1) {
         if (errno == EAGAIN)
             return false;
         else 
@@ -230,7 +230,7 @@
     }
 
     if (SLinkedLockFreeQueue<T>::_add(item)){
-        pcsem::inc(items);       
+        SemUtil::inc(items);       
         return true;
     }
     return false;
@@ -285,12 +285,12 @@
 template <typename T>
 bool SLinkedLockFreeQueue<T>::remove(T & item)
 {
-    if (pcsem::dec(items) == -1) {
+    if (SemUtil::dec(items) == -1) {
         err_exit(errno, "remove");
     }
 
     if (SLinkedLockFreeQueue<T>::_remove(item)) {
-        pcsem::inc(slots);      
+        SemUtil::inc(slots);      
         return true;
     }
     return false;
@@ -300,7 +300,7 @@
 template <typename T>
 bool SLinkedLockFreeQueue<T>::remove_nowait(T & item)
 {
-    if (pcsem::dec_nowait(items) == -1) {
+    if (SemUtil::dec_nowait(items) == -1) {
         if (errno == EAGAIN)
             return false;
         else
@@ -308,7 +308,7 @@
     }
 
     if (SLinkedLockFreeQueue<T>::_remove(item)) {
-        pcsem::inc(slots);     
+        SemUtil::inc(slots);     
         return true;
     }
     return false;
@@ -318,7 +318,7 @@
 template <typename T>
 bool SLinkedLockFreeQueue<T>::remove_timeout(T & item, struct timespec * timeout)
 {
-    if (pcsem::dec_timeout(items, timeout) == -1) {
+    if (SemUtil::dec_timeout(items, timeout) == -1) {
         if (errno == EAGAIN)
             return false;
         else 
@@ -326,7 +326,7 @@
     }
 
     if (SLinkedLockFreeQueue<T>::_remove(item)) {
-        pcsem::inc(slots);       
+        SemUtil::inc(slots);       
         return true;
     }
     return false;
diff --git a/squeue/include/lock_free_queue.h b/squeue/include/lock_free_queue.h
index a800228..a8f7801 100644
--- a/squeue/include/lock_free_queue.h
+++ b/squeue/include/lock_free_queue.h
@@ -88,6 +88,8 @@
     /// environments this function might return bogus values. See help in method
     /// LockFreeQueue::size
     inline bool full();
+    
+    inline bool empty();
 
     /// @brief push an element at the tail of the queue
     /// @param the element to insert in the queue
@@ -139,7 +141,7 @@
     inline uint32_t size();
     
     inline bool full();
-    
+
     inline bool empty();
     
     bool push(const ELEM_T &a_data);   
diff --git a/squeue/include/lock_free_queue_impl.h b/squeue/include/lock_free_queue_impl.h
index f11a796..aa182a5 100644
--- a/squeue/include/lock_free_queue_impl.h
+++ b/squeue/include/lock_free_queue_impl.h
@@ -2,6 +2,8 @@
 #define __LOCK_FREE_QUEUE_IMPL_H__
 
 #include <assert.h> // assert()
+#include "mm.h" 
+#include "sem_util.h"
 
 template <
     typename ELEM_T, 
@@ -32,6 +34,14 @@
 inline bool LockFreeQueue<ELEM_T, Q_TYPE>::full()
 {
     return m_qImpl.full();
+}
+
+template <
+    typename ELEM_T, 
+    template <typename T> class Q_TYPE>
+inline bool LockFreeQueue<ELEM_T, Q_TYPE>::empty()
+{
+    return m_qImpl.empty();
 }  
 
 template <
diff --git a/squeue/include/lock_free_queue_impl_multiple_producer.h b/squeue/include/lock_free_queue_impl_multiple_producer.h
index e324fcb..959cca8 100644
--- a/squeue/include/lock_free_queue_impl_multiple_producer.h
+++ b/squeue/include/lock_free_queue_impl_multiple_producer.h
@@ -3,8 +3,7 @@
 
 #include <assert.h> // assert()
 #include <sched.h>  // sched_yield()
-#include "mm.h" 
-#include "pcsem.h"
+
 template <typename ELEM_T>
 int ArrayLockFreeQueueMultipleProducers<ELEM_T>::m_reference = 0;
 
diff --git a/squeue/include/queue_factory.h b/squeue/include/queue_factory.h
index 6c71d14..b898a86 100644
--- a/squeue/include/queue_factory.h
+++ b/squeue/include/queue_factory.h
@@ -7,17 +7,26 @@
 #include "SLinkedLockFreeQueue.h"
 
 namespace QueueFactory{
+	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 <typename T>
 	SLinkedLockFreeQueue<T>* createLinkedLockFreeQueue(int key, size_t size) {
-		hashtable_t *hashtable;
+		
+
 		SLinkedLockFreeQueue<T> *queue;
-		int first;
-		  
-		first = mm_init(sizeof(hashtable_t), (void **)&hashtable);
-		  
-		if (first)
-		   hashtable_init(hashtable);
+		hashtable_t *hashtable = getHashTable();
+		
 		
 		if ((queue = (SLinkedLockFreeQueue<T> *)hashtable_get(hashtable, key)) == NULL ) {
 			queue = new SLinkedLockFreeQueue<T>(size);
@@ -30,14 +39,9 @@
 
 	template <typename T>
 	LockFreeQueue<T>* createArrayLockFreeQueue(int key, size_t size=16) {
-		hashtable_t *hashtable;
+		 
 		LockFreeQueue<T> *queue;
-		int first;
-		  
-		first = mm_init(sizeof(hashtable_t), (void **)&hashtable);
-		  
-		if (first)
-		   hashtable_init(hashtable);;
+		hashtable_t *hashtable = getHashTable();
 		//LockFreeQueue<int, 10000> q;
 		if ((queue = (LockFreeQueue<T> *)hashtable_get(hashtable, key)) == NULL ) {
 			queue = new LockFreeQueue<T>(size);
@@ -49,9 +53,21 @@
 
 
 	template <typename T>
-	LockFreeQueue<T>* createQueue(int key, size_t size) {
+	LockFreeQueue<T>* createQueue(int key, size_t size = 16) {
 		return QueueFactory::createArrayLockFreeQueue<T>(key, size);
 	}
 
+	/**
+	 * destroy queue
+	*/
+	template <typename T>
+	void dropQueue(int key) {
+
+		LockFreeQueue<T> *queue = QueueFactory::createQueue<T> (key);
+		delete queue;
+		hashtable_t *hashtable = getHashTable();
+		hashtable_remove(hashtable, key);
+	}
+
 }
 #endif
diff --git a/squeue/mm.c b/squeue/mm.c
index 4135964..7b39391 100644
--- a/squeue/mm.c
+++ b/squeue/mm.c
@@ -2,7 +2,7 @@
  * 绠$悊鍏变韩鍐呭瓨鐨勫垎閰嶏紝涓庨噴鏀�
  */
 #include "mm.h"
-#include "pcsem.h"
+#include "sem_util.h"
 
 
 /* $begin mallocmacros */
@@ -28,7 +28,7 @@
 #define MIN_BLOCK_SIZE (ALIGN( (SIZE_T_SIZE << 1) + SIZE_T_SIZE + (PTR_SIZE << 1) ))
 
 
-#define MAX(x, y) ((x) > (y)? (x) : (y))
+//#define MAX(x, y) ((x) > (y)? (x) : (y))
 
 /* Pack a size and allocated bit into a word */
 #define PACK(size, alloc)  ((size) | (alloc)) //line:vm:mm:pack
@@ -77,7 +77,8 @@
 
 static  int shmid = -1;
 static  void *shmp;
-static int mutex = pcsem::init(8899, 1);
+//static int mutex = SemUtil::get(8899, 1);
+static int mutex = SemUtil::get(IPC_PRIVATE, 1);
 
 static void *mem_start_brk;  /* points to first byte of heap */
 static void *mem_brk;        /* points to last byte of heap */
@@ -99,11 +100,11 @@
 
     //fprintf(stderr, "mm_malloc : size=%u\n", size);
   /* Search the free list for a fit */
-  pcsem::dec(mutex);
+  SemUtil::dec(mutex);
   if ((ptr = find_fit(newsize)) != NULL)
   {
     aptr = place(ptr, newsize);
-    pcsem::inc(mutex);
+    SemUtil::inc(mutex);
     return aptr;
   } else {
     fprintf(stderr, "mm_malloc : out of memery\n");
@@ -129,11 +130,11 @@
 
   
   size_t size = GET_SIZE(HDRP(ptr));
-  pcsem::dec(mutex);
+  SemUtil::dec(mutex);
   PUT(HDRP(ptr), PACK(size, 0));
   PUT(FTRP(ptr), PACK(size, 0));
   coalesce(ptr);
-  pcsem::inc(mutex);
+  SemUtil::inc(mutex);
 }
 
 
@@ -214,10 +215,10 @@
 {
   
   //宸茬粡鍒濆鍖栬繃浜�
-  pcsem::dec(mutex);
+  SemUtil::dec(mutex);
   if (shmid != -1){
     *addr = shmp;
-    pcsem::inc(mutex);
+    SemUtil::inc(mutex);
     return false;
   }
 
@@ -246,7 +247,7 @@
 
   if(!first) {
     *addr = shmp;
-    pcsem::inc(mutex);
+    SemUtil::inc(mutex);
     return first;
   }
 
@@ -271,19 +272,19 @@
     err_exit(errno, "mm_init extend_heap");
 
   *addr = shmp;
-  pcsem::inc(mutex);
+  SemUtil::inc(mutex);
   return first;
 }
 
  
 
-void mm_deinit(void) {
+void mm_destroy(void) {
 
   if (shmdt(shmp) == -1)
     err_exit(errno, "mm_init shmdt");
   if (shmctl(shmid, IPC_RMID, 0) == -1)
     err_exit(errno, "mm_init shmctl IPC_RMID");
-  pcsem::remove(mutex);
+  SemUtil::remove(mutex);
 }
 /*
  * extend_heap - Extend heap with free block and return its block pointer
diff --git a/squeue/mm.h b/squeue/mm.h
index 728099f..0cb9729 100644
--- a/squeue/mm.h
+++ b/squeue/mm.h
@@ -6,7 +6,7 @@
 #include "usg_typedef.h"
 #include <sys/sem.h>
 #include <sys/shm.h>
-#define MAX_HEAP (20*(1<<20))  /* 20 MB */
+#define MAX_HEAP (512*(1<<20))  /* 20 MB */
 /* Hard-coded keys for IPC objects */
 
 #define SHM_KEY 0x1234          /* Key for shared memory segment */
@@ -14,7 +14,7 @@
 #define OBJ_PERMS (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
 
 extern bool mm_init(size_t offset, void **addr);
-extern void mm_deinit(void);
+extern void mm_destroy(void);
 extern void *mm_malloc (size_t size);
 extern void mm_free (void *ptr);
 extern void *mm_realloc(void *ptr, size_t size);
diff --git a/squeue/pcsem.c b/squeue/sem_util.c
similarity index 92%
rename from squeue/pcsem.c
rename to squeue/sem_util.c
index a95f10d..d7819a2 100644
--- a/squeue/pcsem.c
+++ b/squeue/sem_util.c
@@ -1,7 +1,7 @@
-#include "pcsem.h"
+#include "sem_util.h"
 
 
-int pcsem::init(key_t key, unsigned int value) {
+int SemUtil::get(key_t key, unsigned int value) {
     int semid, perms;
      
     perms = S_IRUSR | S_IWUSR;
@@ -70,7 +70,7 @@
    set to EINTR if operation was interrupted by a signal handler */
 
 /* Reserve semaphore - decrement it by 1 */
-int pcsem::dec(int semId)
+int SemUtil::dec(int semId)
 {
     struct sembuf sops;
 
@@ -85,7 +85,7 @@
     return 0;
 }
 
-int pcsem::dec_nowait(int semId)
+int SemUtil::dec_nowait(int semId)
 {
     struct sembuf sops;
 
@@ -100,7 +100,7 @@
     return 0;
 }
 
-int pcsem::dec_timeout(int semId, struct timespec * timeout)
+int SemUtil::dec_timeout(int semId, struct timespec * timeout)
 {
     struct sembuf sops;
 
@@ -118,7 +118,7 @@
 
 
 /* Release semaphore - increment it by 1 */
-int pcsem::inc(int semId)
+int SemUtil::inc(int semId)
 {
     struct sembuf sops;
 
@@ -129,7 +129,7 @@
     return semop(semId, &sops, 1);
 }
 
-void pcsem::remove(int semid) {
+void SemUtil::remove(int semid) {
     union semun dummy;
     if (semctl(semid, 0, IPC_RMID, dummy) == -1)
         err_exit(errno, "semctl");
diff --git a/squeue/pcsem.h b/squeue/sem_util.h
similarity index 80%
rename from squeue/pcsem.h
rename to squeue/sem_util.h
index 6b994b5..c4af638 100644
--- a/squeue/pcsem.h
+++ b/squeue/sem_util.h
@@ -4,9 +4,9 @@
 #include "usg_common.h"
 #include "usg_typedef.h"
 
-namespace pcsem {
+namespace SemUtil {
 
-	int init(key_t key, unsigned int value);
+	int get(key_t key, unsigned int value);
 	int dec(int semId);
 	int dec_nowait(int semId);
 	int dec_timeout(int semId, struct timespec * timeout);
diff --git a/test/test.h b/test/test.h
index 76aa358..c80ad70 100644
--- a/test/test.h
+++ b/test/test.h
@@ -21,12 +21,14 @@
 };
 
 // 閿�姣佸叡浜唴瀛樺拰淇″彿
-void destroy() {
+void destroy(int key) {
 	
-	LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (1, 16);
-   //queue->~LockFreeQueue();
-	delete queue;
-    mm_deinit();
+	// LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (1, 16);
+ //   //queue->~LockFreeQueue();
+	// delete queue;
+
+	QueueFactory::dropQueue<struct Item>(key);
+    mm_destroy();
     
 
 }
diff --git a/test/test_queue b/test/test_queue
index bcd2dd9..51f9055 100755
--- a/test/test_queue
+++ b/test/test_queue
Binary files differ
diff --git a/test/test_queue.c b/test/test_queue.c
index 6046902..db760fd 100644
--- a/test/test_queue.c
+++ b/test/test_queue.c
@@ -4,12 +4,12 @@
 int main () {
 	unsigned int i = 0;
 	
-	 
+	int key = 2;
 
 	struct Item item;
 
-	size_t qsize = 1;
-  	LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (2, qsize);
+	size_t qsize = 16;
+  	LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (key, qsize);
 
 	// LockFreeQueue<struct Item> queue(16);
 	for(i = 0; i < qsize; i++) {
@@ -33,14 +33,12 @@
 
 	i = 0;
 	while((queue->pop(item)) ) {
-	    cout << i << " 鍑洪槦锛�" << item.pic << ", " << item.info << endl;
+	    cout << i << " pop锛�" << item.pic << ", " << item.info << endl;
 	   // cout <<  item.pic << endl;
-
 	    i++;
-	    
 	}
 
 	
-	destroy();
+	destroy(key);
 
 }
\ No newline at end of file

--
Gitblit v1.8.0