blob: a6186ff952d7f903718aa55f3d4eb89fc0b44ef1 [file] [log] [blame]
// Copyright (c) 2013, Kenton Varda <[email protected]>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "mutex.h"
#include "debug.h"
#include "thread.h"
#include <pthread.h>
#include <unistd.h>
#include <gtest/gtest.h>
namespace kj {
namespace {
inline void delay() { usleep(10000); }
#if KJ_NO_EXCEPTIONS
#undef EXPECT_ANY_THROW
#define EXPECT_ANY_THROW(code) EXPECT_DEATH(code, ".")
#define EXPECT_NONFATAL_FAILURE(code) code
#else
#define EXPECT_NONFATAL_FAILURE EXPECT_ANY_THROW
#endif
#ifdef KJ_DEBUG
#define EXPECT_DEBUG_ANY_THROW EXPECT_ANY_THROW
#else
#define EXPECT_DEBUG_ANY_THROW(EXP)
#endif
TEST(Mutex, MutexGuarded) {
MutexGuarded<uint> value(123);
{
Locked<uint> lock = value.lockExclusive();
EXPECT_EQ(123u, *lock);
EXPECT_EQ(123u, value.getAlreadyLockedExclusive());
Thread thread([&]() {
Locked<uint> threadLock = value.lockExclusive();
EXPECT_EQ(456u, *threadLock);
*threadLock = 789;
});
delay();
EXPECT_EQ(123u, *lock);
*lock = 456;
auto earlyRelease = kj::mv(lock);
}
EXPECT_EQ(789u, *value.lockExclusive());
{
auto rlock1 = value.lockShared();
EXPECT_EQ(789u, *rlock1);
EXPECT_EQ(789u, value.getAlreadyLockedShared());
{
auto rlock2 = value.lockShared();
EXPECT_EQ(789u, *rlock2);
auto rlock3 = value.lockShared();
EXPECT_EQ(789u, *rlock3);
auto rlock4 = value.lockShared();
EXPECT_EQ(789u, *rlock4);
}
Thread thread2([&]() {
Locked<uint> threadLock = value.lockExclusive();
*threadLock = 321;
});
#if KJ_USE_FUTEX
// So, it turns out that pthread_rwlock on BSD "prioritizes" readers over writers. The result
// is that if one thread tries to take multiple read locks, but another thread happens to
// request a write lock it between, you get a deadlock. This seems to contradict the man pages
// and common sense, but this is how it is. The futex-based implementation doesn't currently
// have this problem because it does not prioritize writers. Perhaps it will in the future,
// but we'll leave this test here until then to make sure we notice the change.
delay();
EXPECT_EQ(789u, *rlock1);
{
auto rlock2 = value.lockShared();
EXPECT_EQ(789u, *rlock2);
auto rlock3 = value.lockShared();
EXPECT_EQ(789u, *rlock3);
auto rlock4 = value.lockShared();
EXPECT_EQ(789u, *rlock4);
}
#endif
delay();
EXPECT_EQ(789u, *rlock1);
auto earlyRelease = kj::mv(rlock1);
}
EXPECT_EQ(321u, *value.lockExclusive());
EXPECT_DEBUG_ANY_THROW(value.getAlreadyLockedExclusive());
EXPECT_DEBUG_ANY_THROW(value.getAlreadyLockedShared());
EXPECT_EQ(321u, value.getWithoutLock());
}
TEST(Mutex, Lazy) {
Lazy<uint> lazy;
bool initStarted = false;
Thread thread([&]() {
EXPECT_EQ(123u, lazy.get([&](SpaceFor<uint>& space) -> Own<uint> {
__atomic_store_n(&initStarted, true, __ATOMIC_RELAXED);
delay();
return space.construct(123);
}));
});
// Spin until the initializer has been entered in the thread.
while (!__atomic_load_n(&initStarted, __ATOMIC_RELAXED)) {
sched_yield();
}
EXPECT_EQ(123u, lazy.get([](SpaceFor<uint>& space) { return space.construct(456); }));
EXPECT_EQ(123u, lazy.get([](SpaceFor<uint>& space) { return space.construct(789); }));
}
} // namespace
} // namespace kj