All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
nonBlockDelete.cc
Go to the documentation of this file.
1 /* nonBlockDelete.cc
2  */
5 #include "osl/oslConfig.h"
6 #include <boost/thread.hpp>
7 #include <iostream>
8 
9 static volatile bool finish = false;
10 
12 {
13 public:
14  Queue() : thread(0)
15  {
16  }
17  boost::thread *thread;
18 };
19 
21 {
22  boost::shared_ptr<Queue> queue;
23  Runner(const boost::shared_ptr<Queue>& q) : queue(q)
24  {
25  }
26  void operator()()
27  {
28  while (! finish)
29  {
30  boost::shared_ptr<void> ptr;
31  ptr = queue->pop_front();
32  const int count = ptr.use_count();
33  if (count > 1)
34  std::cerr << "NonBlockDelete " << count << " > 1 "
35  << ptr.get() << std::endl;
36  // release ptr
37  }
38  }
39 };
40 
41 
44 {
45  static NonBlockDelete the_instance;
46  return the_instance;
47 }
48 
51  : queue(new Queue())
52 {
53  queue->thread = new boost::thread(Runner(queue));
54 }
55 
58 {
59  finish = true;
60  queue->quit(1);
61  queue->thread->join();
62  delete queue->thread;
63 }
64 
65 int osl::misc::
67 {
68  NonBlockDelete& the_instance = instance();
69  return the_instance.queue->size();
70 }
71 
72 void osl::misc::NonBlockDelete::resetAny(boost::shared_ptr<void>& ptr)
73 {
74  if (finish || OslConfig::memoryUseRatio() > 0.9) {
75  ptr.reset();
76  return;
77  }
78  instance().push_back(ptr);
79 }
80 
81 void osl::misc::NonBlockDelete::push_back(boost::shared_ptr<void>& ptr)
82 {
83  if (finish) {
84  ptr.reset();
85  return;
86  }
87  queue->push_back(ptr);
88 }
89 
90 void osl::misc::
92 {
93  NonBlockDelete& the_instance = instance();
94  while (the_instance.queue->size())
95  {
96  boost::shared_ptr<void> ptr
97  = the_instance.queue->pop_front_non_block();
98  }
99  boost::thread::yield();
100 }
101 
102 bool osl::misc::
104 {
105  NonBlockDelete& the_instance = instance();
106  if (the_instance.queue->size() == 0)
107  return false;
108  boost::shared_ptr<void> ptr
109  = the_instance.queue->pop_front_non_block();
110  return true;
111 }
112 
113 
114 /* ------------------------------------------------------------------------- */
115 // ;;; Local Variables:
116 // ;;; mode:c++
117 // ;;; c-basic-offset:2
118 // ;;; End: