wangzhengquan
2020-07-08 3710ce88088c00599c5b108456f6dde9a4d981bc
commmit
2个文件已添加
9个文件已修改
178 ■■■■ 已修改文件
squeue/include/shm_allocator.h 74 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
squeue/include/shm_queue.h 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/multiple_queue_productor.c 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/single_consumer 补丁 | 查看 | 原始文档 | blame | 历史
test/single_consumer.c 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/single_productor 补丁 | 查看 | 原始文档 | blame | 历史
test/single_productor.c 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/test.c 补丁 | 查看 | 原始文档 | blame | 历史
test/test.h 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/test_queue 补丁 | 查看 | 原始文档 | blame | 历史
test/test_queue.c 53 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
squeue/include/shm_allocator.h
New file
@@ -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
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
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;
test/single_consumer
Binary files differ
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);
  }
test/single_productor
Binary files differ
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++;
test/test.c
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;
test/test_queue
Binary files differ
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;
}