Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #include <iostream> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <boost/shared_ptr.hpp> |
| 24 | #include <boost/test/unit_test.hpp> |
| 25 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 26 | #include "thrift/concurrency/Mutex.h" |
| 27 | #include "thrift/concurrency/PosixThreadFactory.h" |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 28 | |
| 29 | using boost::shared_ptr; |
| 30 | using boost::unit_test::test_suite; |
| 31 | using boost::unit_test::framework::master_test_suite; |
| 32 | |
| 33 | using namespace apache::thrift::concurrency; |
| 34 | using namespace std; |
| 35 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 36 | class Locker : public Runnable { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 37 | protected: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 38 | Locker(boost::shared_ptr<ReadWriteMutex> rwlock, bool writer) |
| 39 | : rwlock_(rwlock), writer_(writer), started_(false), gotLock_(false), signaled_(false) {} |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 40 | |
| 41 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 42 | virtual void run() { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 43 | started_ = true; |
| 44 | if (writer_) { |
| 45 | rwlock_->acquireWrite(); |
| 46 | } else { |
| 47 | rwlock_->acquireRead(); |
| 48 | } |
| 49 | gotLock_ = true; |
| 50 | while (!signaled_) { |
| 51 | usleep(5000); |
| 52 | } |
| 53 | rwlock_->release(); |
| 54 | } |
| 55 | |
| 56 | bool started() const { return started_; } |
| 57 | bool gotLock() const { return gotLock_; } |
| 58 | void signal() { signaled_ = true; } |
| 59 | |
| 60 | protected: |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 61 | boost::shared_ptr<ReadWriteMutex> rwlock_; |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 62 | bool writer_; |
| 63 | volatile bool started_; |
| 64 | volatile bool gotLock_; |
| 65 | volatile bool signaled_; |
| 66 | }; |
| 67 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 68 | class Reader : public Locker { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 69 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 70 | Reader(boost::shared_ptr<ReadWriteMutex> rwlock) : Locker(rwlock, false) {} |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 73 | class Writer : public Locker { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 74 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | Writer(boost::shared_ptr<ReadWriteMutex> rwlock) : Locker(rwlock, true) {} |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 78 | void test_starve(PosixThreadFactory::POLICY policy) { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 79 | // the man pages for pthread_wrlock_rdlock suggest that any OS guarantee about |
| 80 | // writer starvation may be influenced by the scheduling policy, so let's try |
| 81 | // all 3 policies to see if any of them work. |
| 82 | PosixThreadFactory factory(policy); |
| 83 | factory.setDetached(false); |
| 84 | |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 85 | boost::shared_ptr<ReadWriteMutex> rwlock(new NoStarveReadWriteMutex()); |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 86 | |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 87 | boost::shared_ptr<Reader> reader1(new Reader(rwlock)); |
| 88 | boost::shared_ptr<Reader> reader2(new Reader(rwlock)); |
| 89 | boost::shared_ptr<Writer> writer(new Writer(rwlock)); |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 90 | |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 91 | boost::shared_ptr<Thread> treader1 = factory.newThread(reader1); |
| 92 | boost::shared_ptr<Thread> treader2 = factory.newThread(reader2); |
| 93 | boost::shared_ptr<Thread> twriter = factory.newThread(writer); |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 94 | |
| 95 | // launch a reader and make sure he has the lock |
| 96 | treader1->start(); |
| 97 | while (!reader1->gotLock()) { |
| 98 | usleep(2000); |
| 99 | } |
| 100 | |
| 101 | // launch a writer and make sure he's blocked on the lock |
| 102 | twriter->start(); |
| 103 | while (!writer->started()) { |
| 104 | usleep(2000); |
| 105 | } |
| 106 | // tricky part... we can never be 100% sure that the writer is actually |
| 107 | // blocked on the lock, but we can pretty reasonably sure because we know |
| 108 | // he just executed the line immediately before getting the lock, and |
| 109 | // we'll wait a full second for him to get on it. |
| 110 | sleep(1); |
| 111 | |
| 112 | // launch a second reader... if the RWMutex guarantees that writers won't |
| 113 | // starve, this reader should not be able to acquire the lock until the writer |
| 114 | // has acquired and released it. |
| 115 | treader2->start(); |
| 116 | while (!reader2->started()) { |
| 117 | usleep(2000); |
| 118 | } |
| 119 | // again... can't be 100% sure the reader is waiting on (or has) the lock |
| 120 | // but we can be close. |
| 121 | sleep(1); |
| 122 | |
| 123 | // tell reader 1 to let go of the lock |
| 124 | reader1->signal(); |
| 125 | |
| 126 | // wait for someone to get the lock |
| 127 | while (!reader2->gotLock() && !writer->gotLock()) { |
| 128 | usleep(2000); |
| 129 | } |
| 130 | |
| 131 | // the test succeeded if the WRITER got the lock. |
| 132 | bool success = writer->gotLock(); |
| 133 | |
| 134 | // tell everyone we're done and wait for them to finish |
| 135 | reader2->signal(); |
| 136 | writer->signal(); |
| 137 | treader1->join(); |
| 138 | treader2->join(); |
| 139 | twriter->join(); |
| 140 | |
| 141 | // make sure it worked. |
| 142 | BOOST_CHECK_MESSAGE(success, "writer is starving"); |
| 143 | } |
| 144 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 145 | BOOST_AUTO_TEST_SUITE(RWMutexStarveTest) |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 146 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 147 | BOOST_AUTO_TEST_CASE(test_starve_other) { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 148 | test_starve(PosixThreadFactory::OTHER); |
| 149 | } |
| 150 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 151 | BOOST_AUTO_TEST_CASE(test_starve_rr) { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 152 | test_starve(PosixThreadFactory::ROUND_ROBIN); |
| 153 | } |
| 154 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 155 | BOOST_AUTO_TEST_CASE(test_starve_fifo) { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 156 | test_starve(PosixThreadFactory::FIFO); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_SUITE_END() |