Skip to content

Commit 0cccdac

Browse files
committed
merge issue-61; add condition for linux
1 parent 4ca300b commit 0cccdac

File tree

13 files changed

+191
-33
lines changed

13 files changed

+191
-33
lines changed

include/libipc/condition.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include <cstdint> // std::uint64_t
4+
5+
#include "libipc/export.h"
6+
#include "libipc/def.h"
7+
#include "libipc/mutex.h"
8+
9+
namespace ipc {
10+
namespace sync {
11+
12+
class IPC_EXPORT condition {
13+
condition(condition const &) = delete;
14+
condition &operator=(condition const &) = delete;
15+
16+
public:
17+
condition();
18+
explicit condition(char const *name);
19+
~condition();
20+
21+
void const *native() const noexcept;
22+
void *native() noexcept;
23+
24+
bool valid() const noexcept;
25+
26+
bool open(char const *name) noexcept;
27+
void close() noexcept;
28+
29+
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm = ipc::invalid_value) noexcept;
30+
bool notify() noexcept;
31+
bool broadcast() noexcept;
32+
33+
private:
34+
class condition_;
35+
condition_* p_;
36+
};
37+
38+
} // namespace sync
39+
} // namespace ipc

src/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ if(UNIX)
55
else()
66
file(GLOB SRC_FILES ${LIBIPC_PROJECT_DIR}/src/libipc/platform/*_win.cpp)
77
endif()
8-
aux_source_directory(${LIBIPC_PROJECT_DIR}/src SRC_FILES)
8+
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc SRC_FILES)
9+
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc/sync SRC_FILES)
910

1011
file(GLOB HEAD_FILES
1112
${LIBIPC_PROJECT_DIR}/include/libipc/*.h
@@ -37,8 +38,8 @@ set_target_properties(${PROJECT_NAME}
3738
# set version
3839
set_target_properties(${PROJECT_NAME}
3940
PROPERTIES
40-
VERSION 1.0.0
41-
SOVERSION 1)
41+
VERSION 1.1.0
42+
SOVERSION 2)
4243

4344
target_include_directories(${PROJECT_NAME}
4445
PUBLIC ${LIBIPC_PROJECT_DIR}/include
File renamed without changes.
File renamed without changes.

src/libipc/platform/condition_linux.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <cstring>
45

56
#include <pthread.h>
67

7-
#include "libipc/utility/log.h"
88
#include "libipc/platform/get_wait_time.h"
9+
#include "libipc/utility/log.h"
10+
#include "libipc/utility/scope_guard.h"
911
#include "libipc/mutex.h"
12+
#include "libipc/shm.h"
1013

1114
namespace ipc {
1215
namespace detail {
@@ -89,7 +92,7 @@ class condition {
8992
return true;
9093
case invalid_value: {
9194
int eno;
92-
if ((eno = ::pthread_cond_wait(cond_, mtx.native())) != 0) {
95+
if ((eno = ::pthread_cond_wait(cond_, static_cast<pthread_mutex_t *>(mtx.native()))) != 0) {
9396
ipc::error("fail pthread_cond_wait[%d]\n", eno);
9497
return false;
9598
}
@@ -98,7 +101,7 @@ class condition {
98101
default: {
99102
auto ts = detail::make_timespec(tm);
100103
int eno;
101-
if ((eno = ::pthread_cond_timedwait(cond_, mtx.native(), &ts)) != 0) {
104+
if ((eno = ::pthread_cond_timedwait(cond_, static_cast<pthread_mutex_t *>(mtx.native()), &ts)) != 0) {
102105
if (eno != ETIMEDOUT) {
103106
ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
104107
eno, tm, ts.tv_sec, ts.tv_nsec);
File renamed without changes.

src/libipc/sync/condition.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
#include "libipc/condition.h"
3+
4+
#include "libipc/utility/pimpl.h"
5+
#include "libipc/memory/resource.h"
6+
#include "libipc/platform/detail.h"
7+
#if defined(IPC_OS_WINDOWS_)
8+
#include "libipc/platform/condition_win.h"
9+
#elif defined(IPC_OS_LINUX_)
10+
#include "libipc/platform/condition_linux.h"
11+
#else/*linux*/
12+
# error "Unsupported platform."
13+
#endif
14+
15+
namespace ipc {
16+
namespace sync {
17+
18+
class condition::condition_ : public ipc::pimpl<condition_> {
19+
public:
20+
ipc::detail::sync::condition cond_;
21+
};
22+
23+
condition::condition()
24+
: p_(p_->make()) {
25+
}
26+
27+
condition::condition(char const * name)
28+
: condition() {
29+
open(name);
30+
}
31+
32+
condition::~condition() {
33+
close();
34+
p_->clear();
35+
}
36+
37+
void const *condition::native() const noexcept {
38+
return impl(p_)->cond_.native();
39+
}
40+
41+
void *condition::native() noexcept {
42+
return impl(p_)->cond_.native();
43+
}
44+
45+
bool condition::valid() const noexcept {
46+
return impl(p_)->cond_.valid();
47+
}
48+
49+
bool condition::open(char const *name) noexcept {
50+
return impl(p_)->cond_.open(name);
51+
}
52+
53+
void condition::close() noexcept {
54+
impl(p_)->cond_.close();
55+
}
56+
57+
bool condition::wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
58+
return impl(p_)->cond_.wait(mtx, tm);
59+
}
60+
61+
bool condition::notify() noexcept {
62+
return impl(p_)->cond_.notify();
63+
}
64+
65+
bool condition::broadcast() noexcept {
66+
return impl(p_)->cond_.broadcast();
67+
}
68+
69+
} // namespace sync
70+
} // namespace ipc
File renamed without changes.

0 commit comments

Comments
 (0)