acl  3.5.3.0
fiber_sem.hpp
浏览该文件的文档.
1 #pragma once
2 #include "fiber_cpp_define.hpp"
3 #include <list>
4 #include <assert.h>
5 
6 struct ACL_FIBER_SEM;
7 
8 namespace acl {
9 
11 {
12 public:
13  fiber_sem(int max);
14  ~fiber_sem(void);
15 
16  int wait(void);
17  int trywait(void);
18  int post(void);
19 
20 private:
21  ACL_FIBER_SEM* sem_;
22  fiber_sem(const fiber_sem&);
23  const fiber_sem& operator=(const fiber_sem&);
24 };
25 
27 {
28 public:
29  fiber_sem_guard(fiber_sem& sem) : sem_(sem)
30  {
31  (void) sem_.wait();
32  }
33 
35  {
36  sem_.post();
37  }
38 
39 private:
40  fiber_sem& sem_;
41 
43  void operator=(const fiber_sem_guard&);
44 };
45 
46 template<typename T>
48 {
49 public:
50  fiber_sbox(bool free_obj = true)
51  : sem_(0), size_(0), free_obj_(free_obj) {}
52 
53  ~fiber_sbox(void) { clear(free_obj_); }
54 
55  void clear(bool free_obj = false)
56  {
57  if (free_obj) {
58  for (typename std::list<T*>::iterator it =
59  sbox_.begin(); it != sbox_.end(); ++it) {
60 
61  delete *it;
62  }
63  }
64  sbox_.clear();
65  }
66 
67  void push(T* t)
68  {
69  sbox_.push_back(t);
70  sem_.post();
71  }
72 
73  T* pop(bool* found = NULL)
74  {
75  sem_.wait();
76  bool found_flag;
77  T* t = peek(found_flag);
78  assert(found_flag);
79  if (found) {
80  *found = true;
81  }
82  return t;
83  }
84 
85 private:
86  fiber_sem sem_;
87  std::list<T*> sbox_;
88  size_t size_;
89  bool free_obj_;
90 
91  fiber_sbox(const fiber_sbox&);
92  void operator=(const fiber_sbox&);
93 
94  T* peek(bool& found_flag)
95  {
96  typename std::list<T*>::iterator it = sbox_.begin();
97  if (it == sbox_.end()) {
98  found_flag = false;
99  return NULL;
100  }
101  found_flag = true;
102  size_--;
103  T* t = *it;
104  sbox_.erase(it);
105  return t;
106  }
107 };
108 
109 } // namespace acl
void push(T *t)
Definition: fiber_sem.hpp:67
#define FIBER_CPP_API
T * pop(bool *found=NULL)
Definition: fiber_sem.hpp:73
int post(void)
struct ACL_FIBER_SEM ACL_FIBER_SEM
Definition: fiber_sem.h:13
void clear(bool free_obj=false)
Definition: fiber_sem.hpp:55
fiber_sbox(bool free_obj=true)
Definition: fiber_sem.hpp:50
fiber_sem_guard(fiber_sem &sem)
Definition: fiber_sem.hpp:29
int wait(void)