change interface name for rw_lock

This commit is contained in:
mutouyun 2018-12-12 16:14:04 +08:00
parent eccded45c2
commit bc25cd6912
4 changed files with 45 additions and 37 deletions

View File

@ -1,9 +1,11 @@
TEMPLATE = lib TEMPLATE = lib
TARGET = ipc TARGET = ipc
CONFIG += c++14
CONFIG -= qt CONFIG -= qt
msvc:QMAKE_CXXFLAGS += /std:c++17
else:CONFIG += c++1z
DEFINES += __IPC_LIBRARY__ DEFINES += __IPC_LIBRARY__
DESTDIR = ../output DESTDIR = ../output

View File

@ -2,9 +2,12 @@ TEMPLATE = app
QT += core testlib QT += core testlib
QT -= gui QT -= gui
CONFIG += console c++14 CONFIG += console
CONFIG -= app_bundle CONFIG -= app_bundle
msvc:QMAKE_CXXFLAGS += /std:c++17
else:CONFIG += c++1z
DESTDIR = ../output DESTDIR = ../output
INCLUDEPATH += \ INCLUDEPATH += \

View File

@ -14,7 +14,21 @@ class rw_lock {
}; };
public: public:
void r_lock(void) { void lock(void) {
while (1) {
std::size_t expected = 0;
if (lc_.compare_exchange_weak(expected, w_flag, std::memory_order_acq_rel)) {
break;
}
std::this_thread::yield();
}
}
void unlock(void) {
lc_.store(0, std::memory_order_release);
}
void lock_shared(void) {
while(1) { while(1) {
std::size_t old = lc_.load(std::memory_order_relaxed); std::size_t old = lc_.load(std::memory_order_relaxed);
std::size_t unlocked = old + 1; std::size_t unlocked = old + 1;
@ -27,23 +41,9 @@ public:
} }
} }
void r_unlock(void) { void unlock_shared(void) {
lc_.fetch_sub(1, std::memory_order_release); lc_.fetch_sub(1, std::memory_order_release);
} }
void w_lock(void) {
while (1) {
std::size_t expected = 0;
if (lc_.compare_exchange_weak(expected, w_flag, std::memory_order_acq_rel)) {
break;
}
std::this_thread::yield();
}
}
void w_unlock(void) {
lc_.store(0, std::memory_order_release);
}
}; };
} // namespace ipc } // namespace ipc

View File

@ -2,6 +2,8 @@
#include <vector> #include <vector>
#include <type_traits> #include <type_traits>
#include <iostream> #include <iostream>
#include <shared_mutex>
#include <mutex>
#include "ipc.h" #include "ipc.h"
#include "rw_lock.h" #include "rw_lock.h"
@ -17,22 +19,12 @@ class Unit : public TestSuite {
} }
private slots: private slots:
void test_send_recv();
void test_rw_lock(); void test_rw_lock();
void test_send_recv();
} unit__; } unit__;
#include "test_ipc.moc" #include "test_ipc.moc"
void Unit::test_send_recv() {
// auto h = ipc::connect("my-ipc");
// QVERIFY(h != nullptr);
// char data[] = "hello ipc!";
// QVERIFY(ipc::send(h, data, sizeof(data)));
// auto got = ipc::recv(h);
// QCOMPARE((char*)got.data(), data);
// ipc::disconnect(h);
}
void Unit::test_rw_lock() { void Unit::test_rw_lock() {
std::thread r_trd[4]; std::thread r_trd[4];
std::thread w_trd[4]; std::thread w_trd[4];
@ -46,11 +38,12 @@ void Unit::test_rw_lock() {
std::size_t cnt = 0; std::size_t cnt = 0;
while (1) { while (1) {
int x = -1; int x = -1;
lc.r_lock(); {
if (cnt < datas.size()) { [[maybe_unused]] std::shared_lock<ipc::rw_lock> guard { lc };
x = datas[cnt]; if (cnt < datas.size()) {
x = datas[cnt];
}
} }
lc.r_unlock();
if (x == 0) break; // quit if (x == 0) break; // quit
if (x != -1) { if (x != -1) {
seq.push_back(x); seq.push_back(x);
@ -70,18 +63,18 @@ void Unit::test_rw_lock() {
for (auto& t : w_trd) { for (auto& t : w_trd) {
t = std::thread([&] { t = std::thread([&] {
for (int i = 1; i <= 100; ++i) { for (int i = 1; i <= 100; ++i) {
lc.w_lock(); lc.lock();
datas.push_back(i); datas.push_back(i);
lc.w_unlock(); lc.unlock();
std::this_thread::yield(); std::this_thread::yield();
} }
}); });
} }
for (auto& t : w_trd) t.join(); for (auto& t : w_trd) t.join();
lc.w_lock(); lc.lock();
datas.push_back(0); datas.push_back(0);
lc.w_unlock(); lc.unlock();
for (auto& t : r_trd) t.join(); for (auto& t : r_trd) t.join();
@ -91,4 +84,14 @@ void Unit::test_rw_lock() {
std::cout << std::endl; std::cout << std::endl;
} }
void Unit::test_send_recv() {
// auto h = ipc::connect("my-ipc");
// QVERIFY(h != nullptr);
// char data[] = "hello ipc!";
// QVERIFY(ipc::send(h, data, sizeof(data)));
// auto got = ipc::recv(h);
// QCOMPARE((char*)got.data(), data);
// ipc::disconnect(h);
}
} // internal-linkage } // internal-linkage