1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost 4 // Software License, Version 1.0. (See accompanying file 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // See http://www.boost.org/libs/interprocess for documentation. 8 // 9 ////////////////////////////////////////////////////////////////////////////// 10 11 #include <boost/interprocess/sync/interprocess_semaphore.hpp> 12 #include <boost/interprocess/exceptions.hpp> 13 #include <boost/date_time/posix_time/posix_time_types.hpp> 14 #include "named_creation_template.hpp" 15 #include "mutex_test_template.hpp" 16 17 static const std::size_t SemCount = 1; 18 static const std::size_t RecSemCount = 100; 19 20 //This wrapper is necessary to plug this class 21 //in named creation tests and interprocess_mutex tests 22 class semaphore_test_wrapper 23 : public boost::interprocess::interprocess_semaphore 24 { 25 public: semaphore_test_wrapper()26 semaphore_test_wrapper() 27 : boost::interprocess::interprocess_semaphore(SemCount) 28 {} 29 lock()30 void lock() 31 { this->wait(); } 32 try_lock()33 bool try_lock() 34 { return this->try_wait(); } 35 timed_lock(const boost::posix_time::ptime & pt)36 bool timed_lock(const boost::posix_time::ptime &pt) 37 { return this->timed_wait(pt); } 38 unlock()39 void unlock() 40 { this->post(); } 41 42 protected: semaphore_test_wrapper(int initial_count)43 semaphore_test_wrapper(int initial_count) 44 : boost::interprocess::interprocess_semaphore(initial_count) 45 {} 46 }; 47 48 //This wrapper is necessary to plug this class 49 //in recursive tests 50 class recursive_semaphore_test_wrapper 51 : public semaphore_test_wrapper 52 { 53 public: recursive_semaphore_test_wrapper()54 recursive_semaphore_test_wrapper() 55 : semaphore_test_wrapper(RecSemCount) 56 {} 57 }; 58 main()59int main () 60 { 61 using namespace boost::interprocess; 62 63 test::test_all_lock<semaphore_test_wrapper>(); 64 test::test_all_recursive_lock<recursive_semaphore_test_wrapper>(); 65 test::test_all_mutex<semaphore_test_wrapper>(); 66 return 0; 67 } 68