From 3710ce88088c00599c5b108456f6dde9a4d981bc Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 08 七月 2020 18:09:58 +0800
Subject: [PATCH] commmit
---
test/single_productor.c | 28 +++++---
test/test_queue | 0
squeue/include/shm_queue.h | 6 +
test/test.h | 5 +
squeue/include/shm_allocator.h | 74 ++++++++++++++++++++++++
test/multiple_queue_productor.c | 4
test/single_consumer | 0
test/test.c | 0
test/single_productor | 0
test/single_consumer.c | 8 +-
test/test_queue.c | 53 +++++++++++++++++
11 files changed, 157 insertions(+), 21 deletions(-)
diff --git a/squeue/include/shm_allocator.h b/squeue/include/shm_allocator.h
new file mode 100644
index 0000000..f9e838b
--- /dev/null
+++ b/squeue/include/shm_allocator.h
@@ -0,0 +1,74 @@
+#ifndef __SHM_ALLOCATOR_H__
+#define __SHM_ALLOCATOR_H__
+#include "usg_common.h"
+#include "mm.h"
+#include <new>
+#include <cstdlib> // for exit()
+#include <climits> // for UNIX_MAX
+#include <cstddef>
+
+
+
+template<class T> class SHMAllocator
+{
+public:
+ typedef T value_type;
+ typedef T* pointer;
+ typedef const T* const_pointer;
+ typedef T& reference;
+ typedef const T& const_reference;
+ typedef size_t size_type;
+ typedef ptrdiff_t difference_type;
+
+
+ SHMAllocator() {};
+ ~SHMAllocator() {};
+ template<class U> SHMAllocator(const SHMAllocator<U>& t) { };
+ template<class U> struct rebind { typedef SHMAllocator<U> other; };
+
+ pointer allocate(size_type n, const void* hint=0) {
+// fprintf(stderr, "allocate n=%u, hint= %p\n",n, hint);
+ return((T*) (mm_malloc(n * sizeof(T))));
+ }
+
+ void deallocate(pointer p, size_type n) {
+// fprintf(stderr, "dealocate n=%u" ,n);
+ mm_free((void*)p);
+ }
+
+ void construct(pointer p, const T& value) {
+ ::new(p) T(value);
+ }
+
+ void construct(pointer p)
+ {
+ ::new(p) T();
+ }
+
+ void destroy(pointer p) {
+ p->~T();
+ }
+
+ pointer address(reference x) {
+ return (pointer)&x;
+ }
+
+ const_pointer address(const_reference x) {
+ return (const_pointer)&x;
+ }
+
+ size_type max_size() const {
+ return size_type(UINT_MAX/sizeof(T));
+ }
+};
+
+
+// template<class charT, class traits = char _traits<charT>,
+// class Allocator = allocator<charT> >
+
+
+
+
+typedef std::basic_string<char, std::char_traits<char>, SHMAllocator<char> > shmstring;
+
+#endif
\ No newline at end of file
diff --git a/squeue/include/shm_queue.h b/squeue/include/shm_queue.h
index 8177e1a..0bcc3db 100644
--- a/squeue/include/shm_queue.h
+++ b/squeue/include/shm_queue.h
@@ -1,7 +1,7 @@
#ifndef __SHM_QUEUE_H__
#define __SHM_QUEUE_H__
-#include <usg_common.h>
+#include "usg_common.h"
#include "mm.h"
#include "hashtable.h"
#include "lock_free_queue.h"
@@ -181,4 +181,6 @@
return queue->operator[](i);
}
-#endif // _LOCK_FREE_QUEUE_H__
+
+
+#endif
diff --git a/test/multiple_queue_productor.c b/test/multiple_queue_productor.c
index f01652e..c3024fb 100644
--- a/test/multiple_queue_productor.c
+++ b/test/multiple_queue_productor.c
@@ -18,8 +18,8 @@
// cerr << "productor key="<<targ->key << endl;
err_msg(0, "productor key = %d\n", targ->key );
- // LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (targ->key, qsize);
- SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(targ->key, qsize);
+ // LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (targ->key, qsize);
+ SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(targ->key, qsize);
/* Transfer blocks of data from stdin to shared memory */
int end = targ->end;
struct Item item;
diff --git a/test/single_consumer b/test/single_consumer
index 9e60a3e..eb83102 100755
--- a/test/single_consumer
+++ b/test/single_consumer
Binary files differ
diff --git a/test/single_consumer.c b/test/single_consumer.c
index 29ae0bc..8a39c24 100644
--- a/test/single_consumer.c
+++ b/test/single_consumer.c
@@ -18,15 +18,15 @@
signal(SIGINT, sigint_handler);
// SHMQueue<struct Item, 3> *queue = new SHMQueue<struct Item, 3>(qsize);
- SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(key, qsize);
+ SHMQueue<item_t> *queue = new SHMQueue<item_t>(key, qsize);
//LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (key, qsize);
/* Transfer blocks of data from shared memory to stdout */
struct timespec timeout = {10, 0};
- struct Item item;
- while(!stop && queue->pop(item)) {
- cout << "鍑洪槦锛�" << item.pic << ", " << item.info << endl;
+ item_t item;
+ while(!stop && queue->pop_timeout(item, &timeout)) {
+ cout << "鍑洪槦锛�" << item << endl;
//cout << item.pic << endl;
//sleep(1);
}
diff --git a/test/single_productor b/test/single_productor
index 020041c..7dcfc82 100755
--- a/test/single_productor
+++ b/test/single_productor
Binary files differ
diff --git a/test/single_productor.c b/test/single_productor.c
index af6b3b4..71c7d9f 100644
--- a/test/single_productor.c
+++ b/test/single_productor.c
@@ -15,6 +15,7 @@
signal(SIGINT, sigint_handler);
int qsize = 16;
+
/* Create set containing two semaphores; initialize so that
writer has first access to shared memory. */
int start = 0;
@@ -24,22 +25,27 @@
start = atoi(argv[1]);
end = atoi(argv[2]);
}
- //LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (key, qsize);
- SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(key, qsize);
+ //LockFreeQueue<item_t> *queue = QueueFactory::createQueue<item_t> (key, qsize);
+ SHMQueue<item_t> *queue = new SHMQueue<item_t>(key, qsize);
/* Transfer blocks of data from stdin to shared memory */
- struct Item item;
- struct timespec timeout = {5, 0};
+ //item_t item;
+ char item[1024];
+ struct timespec timeout = {10, 0};
int i = start;
- item.pic = i;
- item.info = i;
+ // item.pic = i;
+ // item.info = i;
//while((end == -1 || (i < end) ) && (queue->add(item)) ) {
- while(!stop && (queue->push(item)) ) {
- item.pic = i;
- item.info = i;
-
- cout << "鍏ラ槦锛�" << item.pic << ", " << item.info << endl;
+ while(!stop ) {
+ // item.pic = i;
+ // item.info = i;
+ //item = i + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+ sprintf(item, "(%d)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", i);
+ if(queue->push_timeout(item, &timeout))
+ cout << "鍏ラ槦锛�" << item << endl;
+ else
+ break;
// cout << item.pic << endl;
i++;
diff --git a/test/test.c b/test/test.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/test.c
diff --git a/test/test.h b/test/test.h
index e2309bc..664eb0f 100644
--- a/test/test.h
+++ b/test/test.h
@@ -1,6 +1,8 @@
#include "usg_common.h"
#include "usg_typedef.h"
#include "shm_queue.h"
+#include "shm_allocator.h"
+#include <sstream>
//#include "queue_factory.h"
#include <pthread.h>
@@ -12,6 +14,9 @@
};
+
+typedef shmstring item_t;
+
struct Targ {
int key;
int start;
diff --git a/test/test_queue b/test/test_queue
index c321a31..ca75da2 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 fbc6e45..cb6ade7 100644
--- a/test/test_queue.c
+++ b/test/test_queue.c
@@ -1,7 +1,8 @@
#include "test.h"
+
using namespace std;
-int main () {
+void testStruct() {
unsigned int i = 0;
int key = 2;
@@ -39,6 +40,54 @@
}
delete queue;
- mm_destroy();
+}
+void testString() {
+ unsigned int i = 0;
+ std::ostringstream outstr;
+ int key = 2;
+
+ shmstring item;
+
+ size_t qsize = 16;
+ //LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (key, qsize);
+ SHMQueue<shmstring> *queue = new SHMQueue<shmstring>(key, 16);
+ // LockFreeQueue<struct Item> queue(16);
+ for(i = 0; i < qsize; i++) {
+ outstr.seekp(0);
+ outstr << "hello " << i ;
+ if(queue->push(outstr.str().c_str())) {
+ cout << i << " push锛�" << outstr.str() << endl;
+ }
+ }
+
+ // for(i = 0; i < qsize; i++) {
+
+ // //queue.dequeue(item);
+
+ // item = (*queue)[i];
+ // cout << "i=" << i << ":" << item << endl;
+ // }
+
+
+
+ struct timespec timeout = {1, 0};
+
+ i = 0;
+ while((queue->pop_timeout(item, &timeout)) ) {
+ cout << i << " pop锛�" << item << endl;
+ // cout << item.pic << endl;
+ i++;
+ }
+
+ delete queue;
+}
+
+
+int main () {
+ testString();
+
+
+ mm_destroy();
+ return 0;
}
\ No newline at end of file
--
Gitblit v1.8.0