File: mutex_owner_dead_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (151 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download | duplicates (6)
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
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/interprocess/detail/workaround.hpp>

//Robust mutex handling is only supported in conforming POSIX systems
#if !defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) || !defined(BOOST_INTERPROCESS_POSIX_ROBUST_MUTEXES)

int main()
{
   return 0;
}

#else

#include <boost/interprocess/detail/os_thread_functions.hpp>

#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
#include "mutex_test_template.hpp"
#include <iostream>


namespace boost {
namespace interprocess {
namespace test {

template<typename M>
void lock_and_exit(void *, M &sm)
{
   sm.lock();
}

enum EOwnerDeadLockType
{
   OwnerDeadLock,
   OwnerDeadTryLock,
   OwnerDeadTimedLock
};

template<class M>
void test_owner_dead_mutex_do_lock(M &mtx, EOwnerDeadLockType lock_type)
{
   switch (lock_type)
   {
      case OwnerDeadLock:
      mtx.lock();
      break;

      case OwnerDeadTryLock:
      mtx.try_lock();
      break;

      case OwnerDeadTimedLock:
      mtx.timed_lock(boost_systemclock_delay(10));
      break;
   }
}

template<class M>
int test_owner_dead_mutex_impl(EOwnerDeadLockType lock_type)
{
   using namespace boost::interprocess;
   M mtx;

   // Locker thread launches and exits without releasing the mutex
   ipcdetail::OS_thread_t tm1;
   ipcdetail::thread_launch(tm1, thread_adapter<M>(&lock_and_exit, 0, mtx));
   ipcdetail::thread_join(tm1);
   BOOST_TRY {
      test_owner_dead_mutex_do_lock(mtx, lock_type);
   }
   BOOST_CATCH(lock_exception& e) {
      #ifndef BOOST_NO_EXCEPTIONS
      if (e.get_error_code() == not_recoverable){
         //Now try once again to lock it, to make sure it's not recoverable
         BOOST_TRY {
            test_owner_dead_mutex_do_lock(mtx, lock_type);
         }
         BOOST_CATCH(lock_exception& e) {
            if (e.get_error_code() == not_recoverable)
               return 0;
            else{
               std::cerr << "e.get_error_code() != not_recoverable! (2)";
               return 3;
            }
         }
         BOOST_CATCH(...) {
            std::cerr << "lock_exception not thrown! (2)";
            return 4;
         } BOOST_CATCH_END
         std::cerr << "Exception not thrown (2)!";
         return 5;
      }
      else{
         std::cerr << "e.get_error_code() != not_recoverable!";
         return 1;
      }
      #endif   //BOOST_NO_EXCEPTIONS
   }
   BOOST_CATCH(...) {
      std::cerr << "lock_exception not thrown!";
      return 2;
   } BOOST_CATCH_END
   std::cerr << "Exception not thrown!";
   return 3;
}

template<class M>
int test_owner_dead_mutex()
{
   int ret;
   ret = test_owner_dead_mutex_impl<M>(OwnerDeadLock);
   if (ret) return ret;
   ret = test_owner_dead_mutex_impl<M>(OwnerDeadTryLock);
   if (ret) return ret;
   ret = test_owner_dead_mutex_impl<M>(OwnerDeadTimedLock);
   return ret;
}

}}}   //boost::interprocess::test


int main ()
{
   using namespace boost::interprocess;
   int ret;
   std::cerr << "Testing interprocess_mutex";
   ret = test::test_owner_dead_mutex<interprocess_mutex>();
   if (ret)
      return ret;

   #ifdef BOOST_INTERPROCESS_POSIX_RECURSIVE_MUTEXES
   std::cerr << "Testing interprocess_recursive_mutex";
   ret = test::test_owner_dead_mutex<interprocess_recursive_mutex>();
   if (ret)
      return ret;
   #endif

   return ret;
}

#endif