nonBlockDelete.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
nonBlockDelete.cc
Go to the documentation of this file.
1 /* nonBlockDelete.cc
2 */
3 #include "osl/misc/nonBlockDelete.h"
4 #include "osl/misc/pointerQueue.h"
5 #include "osl/oslConfig.h"
6 #include <boost/thread.hpp>
7 #include <iostream>
8
9 static volatile bool finish = false;
10
11 class osl::misc::NonBlockDelete::Queue : public PointerQueue<void>
12 {
13 public:
14 Queue() : thread(0)
15 {
16 }
17 boost::thread *thread;
18 };
19
20 struct osl::misc::NonBlockDelete::Runner
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
42 osl::misc::NonBlockDelete& osl::misc::
43 NonBlockDelete::instance()
44 {
45 static NonBlockDelete the_instance;
46 return the_instance;
47 }
48
49 osl::misc::
50 NonBlockDelete::NonBlockDelete()
51 : queue(new Queue())
52 {
53 queue->thread = new boost::thread(Runner(queue));
54 }
55
56 osl::misc::
57 NonBlockDelete::~NonBlockDelete()
58 {
59 finish = true;
60 queue->quit(1);
61 queue->thread->join();
62 delete queue->thread;
63 }
64
65 int osl::misc::
66 NonBlockDelete::waiting()
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::
91 NonBlockDelete::deleteAll()
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::
103 NonBlockDelete::deleteOne()
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: