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
@ -7,3 +7,4 @@ if(NOT MSVC)
|
|||||||
endif()
|
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()
|
||||||
@ -30,7 +30,7 @@ enum : std::size_t {
|
|||||||
invalid_value = (std::numeric_limits<std::size_t>::max)(),
|
invalid_value = (std::numeric_limits<std::size_t>::max)(),
|
||||||
data_length = 64,
|
data_length = 64,
|
||||||
name_length = 64,
|
name_length = 64,
|
||||||
send_wait = 100 // ms
|
default_timeut = 100 // ms
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class relat { // multiplicity of the relationship
|
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) {
|
return [info, que, msg_id](int remain, void const * data, std::size_t size) {
|
||||||
if (!wait_for(info->wt_waiter_, [&] {
|
if (!wait_for(info->wt_waiter_, [&] {
|
||||||
return !que->push(que, msg_id, remain, data, size);
|
return !que->push(que, msg_id, remain, data, size);
|
||||||
}, send_wait)) {
|
}, default_timeut)) {
|
||||||
if (!que->force_push(que, msg_id, remain, data, size)) {
|
if (!que->force_push(que, msg_id, remain, data, size)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -113,8 +113,10 @@ public:
|
|||||||
calc_wait_time(ts, tm);
|
calc_wait_time(ts, tm);
|
||||||
int eno;
|
int eno;
|
||||||
if ((eno = ::pthread_cond_timedwait(&cond_, &mtx.native(), &ts)) != 0) {
|
if ((eno = ::pthread_cond_timedwait(&cond_, &mtx.native(), &ts)) != 0) {
|
||||||
|
if (eno != ETIMEDOUT) {
|
||||||
ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||||
eno, tm, ts.tv_sec, ts.tv_nsec);
|
eno, tm, ts.tv_sec, ts.tv_nsec);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -181,8 +183,10 @@ public:
|
|||||||
timespec ts;
|
timespec ts;
|
||||||
calc_wait_time(ts, tm);
|
calc_wait_time(ts, tm);
|
||||||
if (::sem_timedwait(h, &ts) != 0) {
|
if (::sem_timedwait(h, &ts) != 0) {
|
||||||
|
if (errno != ETIMEDOUT) {
|
||||||
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||||
errno, tm, ts.tv_sec, ts.tv_nsec);
|
errno, tm, ts.tv_sec, ts.tv_nsec);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -260,7 +264,7 @@ public:
|
|||||||
if (counter_ > 0) {
|
if (counter_ > 0) {
|
||||||
ret = sem_helper::post(std::get<1>(h));
|
ret = sem_helper::post(std::get<1>(h));
|
||||||
-- counter_;
|
-- counter_;
|
||||||
ret = ret && sem_helper::wait(std::get<2>(h));
|
ret = ret && sem_helper::wait(std::get<2>(h), default_timeut);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -278,7 +282,7 @@ public:
|
|||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
-- counter_;
|
-- counter_;
|
||||||
ret = ret && sem_helper::wait(std::get<2>(h));
|
ret = ret && sem_helper::wait(std::get<2>(h), default_timeut);
|
||||||
} while (counter_ > 0);
|
} while (counter_ > 0);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@ -39,7 +39,6 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (ret == WAIT_TIMEOUT) {
|
if (ret == WAIT_TIMEOUT) {
|
||||||
ipc::log("WaitForSingleObject is timeout.\n");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret);
|
ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret);
|
||||||
@ -127,7 +126,7 @@ public:
|
|||||||
if (*counter_ > 0) {
|
if (*counter_ > 0) {
|
||||||
ret = sema_.post();
|
ret = sema_.post();
|
||||||
-- *counter_;
|
-- *counter_;
|
||||||
ret = ret && handshake_.wait();
|
ret = ret && handshake_.wait(default_timeut);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -143,7 +142,7 @@ public:
|
|||||||
ret = sema_.post(*counter_);
|
ret = sema_.post(*counter_);
|
||||||
do {
|
do {
|
||||||
-- *counter_;
|
-- *counter_;
|
||||||
ret = ret && handshake_.wait();
|
ret = ret && handshake_.wait(default_timeut);
|
||||||
} while (*counter_ > 0);
|
} while (*counter_ > 0);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user