mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
crash may cause deadlock
This commit is contained in:
parent
05e481290e
commit
6cc2913f6b
@ -6,4 +6,5 @@ if(NOT MSVC)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
|
||||
endif()
|
||||
|
||||
add_subdirectory(test)
|
||||
add_subdirectory(test)
|
||||
add_subdirectory(chat)
|
||||
14
build/chat/CMakeLists.txt
Normal file
14
build/chat/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
project(chat)
|
||||
|
||||
include_directories(../../include)
|
||||
file(GLOB SRC_FILES ../../demo/chat/*.cpp)
|
||||
file(GLOB HEAD_FILES ../../demo/chat/*.h)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/../output)
|
||||
|
||||
link_directories(${EXECUTABLE_OUTPUT_PATH})
|
||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ipc)
|
||||
if(NOT MSVC)
|
||||
target_link_libraries(${PROJECT_NAME} pthread rt)
|
||||
endif()
|
||||
@ -27,10 +27,10 @@ using uint_t = typename uint<N>::type;
|
||||
// constants
|
||||
|
||||
enum : std::size_t {
|
||||
invalid_value = (std::numeric_limits<std::size_t>::max)(),
|
||||
data_length = 64,
|
||||
name_length = 64,
|
||||
send_wait = 100 // ms
|
||||
invalid_value = (std::numeric_limits<std::size_t>::max)(),
|
||||
data_length = 64,
|
||||
name_length = 64,
|
||||
default_timeut = 100 // ms
|
||||
};
|
||||
|
||||
enum class relat { // multiplicity of the relationship
|
||||
|
||||
@ -230,7 +230,7 @@ static bool send(ipc::handle_t h, void const * data, std::size_t size) {
|
||||
return [info, que, msg_id](int remain, void const * data, std::size_t size) {
|
||||
if (!wait_for(info->wt_waiter_, [&] {
|
||||
return !que->push(que, msg_id, remain, data, size);
|
||||
}, send_wait)) {
|
||||
}, default_timeut)) {
|
||||
if (!que->force_push(que, msg_id, remain, data, size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -113,8 +113,10 @@ public:
|
||||
calc_wait_time(ts, tm);
|
||||
int eno;
|
||||
if ((eno = ::pthread_cond_timedwait(&cond_, &mtx.native(), &ts)) != 0) {
|
||||
ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||
eno, tm, ts.tv_sec, ts.tv_nsec);
|
||||
if (eno != ETIMEDOUT) {
|
||||
ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||
eno, tm, ts.tv_sec, ts.tv_nsec);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -181,8 +183,10 @@ public:
|
||||
timespec ts;
|
||||
calc_wait_time(ts, tm);
|
||||
if (::sem_timedwait(h, &ts) != 0) {
|
||||
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||
errno, tm, ts.tv_sec, ts.tv_nsec);
|
||||
if (errno != ETIMEDOUT) {
|
||||
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||
errno, tm, ts.tv_sec, ts.tv_nsec);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -260,7 +264,7 @@ public:
|
||||
if (counter_ > 0) {
|
||||
ret = sem_helper::post(std::get<1>(h));
|
||||
-- counter_;
|
||||
ret = ret && sem_helper::wait(std::get<2>(h));
|
||||
ret = ret && sem_helper::wait(std::get<2>(h), default_timeut);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -278,7 +282,7 @@ public:
|
||||
}
|
||||
do {
|
||||
-- counter_;
|
||||
ret = ret && sem_helper::wait(std::get<2>(h));
|
||||
ret = ret && sem_helper::wait(std::get<2>(h), default_timeut);
|
||||
} while (counter_ > 0);
|
||||
}
|
||||
return ret;
|
||||
|
||||
@ -39,7 +39,6 @@ public:
|
||||
return true;
|
||||
}
|
||||
if (ret == WAIT_TIMEOUT) {
|
||||
ipc::log("WaitForSingleObject is timeout.\n");
|
||||
return false;
|
||||
}
|
||||
ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret);
|
||||
@ -127,7 +126,7 @@ public:
|
||||
if (*counter_ > 0) {
|
||||
ret = sema_.post();
|
||||
-- *counter_;
|
||||
ret = ret && handshake_.wait();
|
||||
ret = ret && handshake_.wait(default_timeut);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -143,7 +142,7 @@ public:
|
||||
ret = sema_.post(*counter_);
|
||||
do {
|
||||
-- *counter_;
|
||||
ret = ret && handshake_.wait();
|
||||
ret = ret && handshake_.wait(default_timeut);
|
||||
} while (*counter_ > 0);
|
||||
}
|
||||
return ret;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user