wangzhengquan
2021-02-20 e0aea3742aed09a0a9ed384ccd7db203b6efc650
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * encapsulate lock_free_queue, populate in userspace
 */
 
#ifndef __SHM_QUEUE_H__
#define __SHM_QUEUE_H__
 
#include "hashtable.h"
 
#include "logger_factory.h"
#include "sem_util.h"
#include "shm_allocator.h"
#include "usg_common.h"
#include "array_lock_free_sem_queue.h"
#include "lock_free_queue.h"
#include "bus_error.h"
 
template <typename ELEM_T> class SHMQueue {
 
private:
  const int mkey;
  hashtable_t * hashtable;
  // 是否是key对应的共享队列的真正拥有者,也就是说是bind到key上的,不是attach到key上的对象
  bool owner;
  size_t mqsize;
 
public:
  /// @brief constructor of the class
  SHMQueue(size_t qsize = 16);
 
  ~SHMQueue();
 
  bool bind(int key, bool force) ;
  bool attach(int key);
 
  int get_key();
 
  uint32_t size();
 
  bool full();
  bool empty();
 
  int push(const ELEM_T &a_data, const struct timespec *timeout=NULL, int flag=0);
  int pop(ELEM_T &a_data, const struct timespec *timeout=NULL, int flag=0);
 
  ELEM_T &operator[](unsigned i);
 
 
private:
protected:
  /// @brief the actual queue-> methods are forwarded into the real
  ///        implementation
 
  LockFreeQueue<ELEM_T, SHM_Allocator> *queue;
 
private:
  /// @brief disable copy constructor declaring it private
  SHMQueue<ELEM_T>(const SHMQueue<ELEM_T> &a_src);
};
 
 
 
 
 
template <typename ELEM_T>
SHMQueue<ELEM_T>::SHMQueue(size_t qsize): mqsize(qsize) {
 
  hashtable = mm_get_hashtable();
  owner = false;
  mkey = 0;
  // queue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, key);
  // if (queue == NULL || (void *)queue == (void *)1) {
  //   queue = new LockFreeQueue<ELEM_T, SHM_Allocator>(qsize);
  //   hashtable_put(hashtable, key, (void *)queue);
  // }
  // queue->reference++;
  // LoggerFactory::getLogger()->debug("SHMQueue constructor reference===%d", queue->reference.load());
}
 
template <typename ELEM_T> SHMQueue<ELEM_T>::~SHMQueue() {
  LoggerFactory::getLogger()->debug("SHMQueue destroy");
  if(owner) {
    delete queue;
    hashtable_remove(hashtable, mkey);
  }
 
  
}
 
template <typename ELEM_T>
bool SHMQueue<ELEM_T>::bind(int key, bool force)  {
 
 
  hashtable_lock(hashtable);
  void *tmp_ptr = hashtable_get(hashtable, key);
  if (tmp_ptr == NULL || tmp_ptr == (void *)1 || force) {
    queue = new LockFreeQueue<ELEM_T, SHM_Allocator>(mqsize);
    hashtable_put(hashtable, key, (void *)queue);
    mkey = key;
    owner = true;
    hashtable_unlock(hashtable);
    return true;
  }
  
  hashtable_unlock(hashtable);
  return  false;
}
 
template <typename ELEM_T>
bool SHMQueue<ELEM_T>::attach(int key) {
  void *tmp_ptr = hashtable_get(hashtable, key);
  if (tmp_ptr == NULL || tmp_ptr == (void *)1) {
    return false;
  }
  mkey = key;
  queue =  (LockFreeQueue<ELEM_T, SHM_Allocator> *)tmp_ptr;
  return true;
}
 
template <typename ELEM_T> int SHMQueue<ELEM_T>::get_key() {
  return mkey;
}
 
 
template <typename ELEM_T> uint32_t SHMQueue<ELEM_T>::size() {
  return queue->size();
}
 
template <typename ELEM_T> bool SHMQueue<ELEM_T>::full() {
  return queue->full();
}
 
template <typename ELEM_T> bool SHMQueue<ELEM_T>::empty() {
  return queue->empty();
}
 
 
 
 
template <typename ELEM_T>
int SHMQueue<ELEM_T>::push(const ELEM_T &a_data, const struct timespec *timeout, int flag) {
 
 
  int rv = queue->push(a_data, timeout, flag);
  if(rv == 0) {
    return 0;
  }
  if(rv == ETIMEDOUT)
    return EBUS_TIMEOUT;
  else {
    LoggerFactory::getLogger()->error("LockFreeQueue push_timeout: %s", bus_strerror(rv));
    return rv;
  }
}
 
 
template <typename ELEM_T>
int SHMQueue<ELEM_T>::pop(ELEM_T &a_data, const struct timespec *timeout, int flag) {
 
  int rv = queue->pop(a_data, timeout, flag);
  if(rv == 0) {
    return 0;
  }
 
 
  if(rv == ETIMEDOUT)
    return EBUS_TIMEOUT;
  else {
    LoggerFactory::getLogger()->error("LockFreeQueue pop_timeout: %s", bus_strerror(rv));
    return rv;
  }
  return rv;
  
}
 
template <typename ELEM_T>
ELEM_T &SHMQueue<ELEM_T>::operator[](unsigned i) {
  return queue->operator[](i);
}
 
 
 
 
#endif