New file |
| | |
| | | #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 |
| | |
| | | #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" |
| | |
| | | return queue->operator[](i); |
| | | } |
| | | |
| | | #endif // _LOCK_FREE_QUEUE_H__ |
| | | |
| | | |
| | | #endif |
| | |
| | | // 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; |
| | |
| | | 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); |
| | | } |
| | |
| | | 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; |
| | |
| | | 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++; |
| | |
| | | #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> |
| | | |
| | |
| | | |
| | | }; |
| | | |
| | | |
| | | typedef shmstring item_t; |
| | | |
| | | struct Targ { |
| | | int key; |
| | | int start; |
| | |
| | | #include "test.h" |
| | | |
| | | using namespace std; |
| | | |
| | | int main () { |
| | | void testStruct() { |
| | | unsigned int i = 0; |
| | | |
| | | int key = 2; |
| | |
| | | } |
| | | |
| | | 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; |
| | | } |