1012ba0a592bea88898da996438caf9452fed499
[platform/upstream/libzypp.git] / devel / devel.ma / Sat.cc
1 #include <iostream>
2
3 #include <boost/thread.hpp>
4 #include <boost/thread/thread_time.hpp>
5 namespace boost
6 {
7   namespace detail
8   {
9     inline std::ostream & operator<<( std::ostream & str, const thread_data_base & obj )
10     { return str << &obj; }
11   }
12 }
13
14 #include "zypp/ByteCount.h"
15 #include "zypp/Pathname.h"
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/LogControl.h"
18
19 using std::endl;
20 using namespace zypp;
21
22 #undef MIL
23 #define MIL MilSync( _BASEFILE, __FUNCTION__, __LINE__ )._str
24
25 #ifdef _REENTRANT
26 #warning _REENTRANT
27 #else
28 #warning NOT_REENTRANT
29 #endif
30
31 template <class Derived>
32 struct ClassLevelLockable
33 {
34   typedef boost::recursive_mutex Lockable;
35
36   typedef boost::lock_guard<Lockable> Lock;
37   struct Lock
38   {
39     Lock( const Derived & obj )
40     {}
41     ~Lock
42   };
43
44   Lockable _mutex;
45 };
46
47 template <class Derived>
48 struct ObjectLevelLockable
49 {
50   typedef boost::recursive_mutex Lockable;
51   typedef boost::lock_guard<Lockable> Lock;
52
53 };
54
55
56 struct MilSync
57 {
58   MilSync( const char * file_r, const char * func_r, const int line_r )
59     : _guard( _mutex )
60     , _str( zypp::base::logger::getStream( ZYPP_BASE_LOGGER_LOGGROUP, zypp::base::logger::E_MIL, file_r, func_r, line_r ) )
61   {}
62   typedef boost::recursive_mutex Lockable;
63   static Lockable             _mutex;
64   boost::lock_guard<Lockable> _guard;
65   std::ostream & _str;
66 };
67 MilSync::Lockable MilSync::_mutex;
68
69 struct ThreadExcl
70 {
71   ThreadExcl()
72   {
73     MIL << "+TE" << boost::this_thread::get_id() << endl;
74     boost::this_thread::sleep(  boost::posix_time::seconds(1) );
75   }
76
77   ~ThreadExcl()
78   {
79     MIL << "-TE" << boost::this_thread::get_id() << endl;
80   }
81 };
82
83 void t_exit()
84 {
85   MIL << "---" << boost::this_thread::get_id() << endl;
86 }
87
88 void t_main()
89 {
90   MIL << "+++" << boost::this_thread::get_id() << " " << boost::this_thread::interruption_enabled() << endl;
91   boost::this_thread::at_thread_exit( t_exit );
92   ThreadExcl a;
93   while( true )
94     boost::this_thread::sleep(  boost::posix_time::seconds(1) );
95 }
96
97 int main( int argc, char * argv[] )
98 {
99   //zypp::base::LogControl::instance().logfile( "log.restrict" );
100   INT << "===[START]==========================================" << endl;
101
102   MIL << "M+++" << boost::this_thread::get_id() << endl;
103   boost::thread_group mthreads;
104
105   mthreads.create_thread( t_main );
106   mthreads.create_thread( t_main );
107   mthreads.create_thread( t_main );
108   mthreads.create_thread( t_main );
109   mthreads.create_thread( t_main );
110
111   MIL << "M???" << boost::this_thread::get_id() << endl;
112   //boost::this_thread::sleep(  boost::posix_time::seconds(10) );
113   mthreads.interrupt_all();
114   mthreads.join_all();
115   MIL << "M---" << boost::this_thread::get_id() << endl;
116
117   ///////////////////////////////////////////////////////////////////
118   INT << "===[END]============================================" << endl << endl;
119   zypp::base::LogControl::instance().logNothing();
120   return 0;
121 }