From 7e7cd233550e34e1943755ae6cd4713094142a1e Mon Sep 17 00:00:00 2001 From: unixliang Date: Sat, 9 Jun 2018 21:24:47 +0800 Subject: [PATCH] add PHXRPC_ASSERT --- codegen/server_template.cpp | 11 ++----- phxrpc/Makefile | 4 ++- phxrpc/comm.h | 24 +++++++++++++++ phxrpc/comm/assert.cpp | 33 ++++++++++++++++++++ phxrpc/comm/assert.h | 40 +++++++++++++++++++++++++ phxrpc/http/test_http_client.cpp | 7 ++--- phxrpc/network/uthread_context_util.cpp | 21 +++++-------- phxrpc/network/uthread_epoll.cpp | 4 +-- 8 files changed, 115 insertions(+), 29 deletions(-) create mode 100644 phxrpc/comm.h create mode 100644 phxrpc/comm/assert.cpp create mode 100644 phxrpc/comm/assert.h diff --git a/codegen/server_template.cpp b/codegen/server_template.cpp index 192e56d..3b77e05 100644 --- a/codegen/server_template.cpp +++ b/codegen/server_template.cpp @@ -33,6 +33,7 @@ const char * PHXRPC_EPOLL_SERVER_MAIN_TEMPLATE = #include "phxrpc/rpc.h" #include "phxrpc/http.h" #include "phxrpc/file.h" +#include "phxrpc/comm.h" using namespace std; @@ -78,10 +79,7 @@ int main( int argc, char * argv[] ) { if( daemonize ) phxrpc::ServerUtils::Daemonize(); - { - bool chk = (signal(SIGPIPE, SIG_IGN) != SIG_ERR); - if (!chk) assert(chk); - } + PHXRPC_ASSERT(signal(SIGPIPE, SIG_IGN) != SIG_ERR); //set customize log/monitor //phxrpc::setlog(openlog, closelog, vlog); @@ -171,10 +169,7 @@ int main( int argc, char * argv[] ) { if( daemonize ) phxrpc::ServerUtils::Daemonize(); - { - bool chk = (signal(SIGPIPE, SIG_IGN) != SIG_ERR); - if (!chk) assert(chk); - } + PHXRPC_ASSERT(signal(SIGPIPE, SIG_IGN) != SIG_ERR); //set customize log/monitor //phxrpc::setlog(openlog, closelog, vlog); diff --git a/phxrpc/Makefile b/phxrpc/Makefile index 7185e22..79a2561 100644 --- a/phxrpc/Makefile +++ b/phxrpc/Makefile @@ -14,12 +14,14 @@ LIB_RPC_OBJS = rpc/phxrpc.pb.o rpc/http_caller.o rpc/server_config.o rpc/client_ rpc/socket_stream_phxrpc.o rpc/uthread_caller.o rpc/client_monitor.o rpc/server_monitor.o \ rpc/monitor_factory.o rpc/hsha_server.o rpc/server_base.o +LIB_COMM_OBJS = comm/assert.o + ifeq ($(OS),Darwin) CFLAGS += -I$(PHXRPC_ROOT)/plugin_darwin/network LIB_NETWORK_OBJS += ../plugin_darwin/network/epoll-darwin.o endif -LIB_OBJS = $(LIB_RPC_OBJS) $(LIB_HTTP_OBJS) $(LIB_NETWORK_OBJS) $(LIB_FILE_OBJS) \ +LIB_OBJS = $(LIB_RPC_OBJS) $(LIB_HTTP_OBJS) $(LIB_NETWORK_OBJS) $(LIB_FILE_OBJS) $(LIB_COMM_OBJS) TARGETS = libphxrpc.a diff --git a/phxrpc/comm.h b/phxrpc/comm.h new file mode 100644 index 0000000..a2ef976 --- /dev/null +++ b/phxrpc/comm.h @@ -0,0 +1,24 @@ +/* +Tencent is pleased to support the open source community by making +PhxRPC available. +Copyright (C) 2016 THL A29 Limited, a Tencent company. +All rights reserved. + +Licensed under the BSD 3-Clause License (the "License"); you may +not use this file except in compliance with the License. You may +obtain a copy of the License at + +https://opensource.org/licenses/BSD-3-Clause + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied. See the License for the specific language governing +permissions and limitations under the License. + +See the AUTHORS file for names of contributors. +*/ + +#pragma once + +#include "comm/assert.h" diff --git a/phxrpc/comm/assert.cpp b/phxrpc/comm/assert.cpp new file mode 100644 index 0000000..e6d96d7 --- /dev/null +++ b/phxrpc/comm/assert.cpp @@ -0,0 +1,33 @@ +/* +Tencent is pleased to support the open source community by making +PhxRPC available. +Copyright (C) 2016 THL A29 Limited, a Tencent company. +All rights reserved. + +Licensed under the BSD 3-Clause License (the "License"); you may +not use this file except in compliance with the License. You may +obtain a copy of the License at + +https://opensource.org/licenses/BSD-3-Clause + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied. See the License for the specific language governing +permissions and limitations under the License. + +See the AUTHORS file for names of contributors. +*/ + +#include +#include + +namespace phxrpc { + +void __bad_assertion(const char *mess) { + fputs(mess, stderr); + abort(); +} + +} + diff --git a/phxrpc/comm/assert.h b/phxrpc/comm/assert.h new file mode 100644 index 0000000..172e4a4 --- /dev/null +++ b/phxrpc/comm/assert.h @@ -0,0 +1,40 @@ +/* +Tencent is pleased to support the open source community by making +PhxRPC available. +Copyright (C) 2016 THL A29 Limited, a Tencent company. +All rights reserved. + +Licensed under the BSD 3-Clause License (the "License"); you may +not use this file except in compliance with the License. You may +obtain a copy of the License at + +https://opensource.org/licenses/BSD-3-Clause + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied. See the License for the specific language governing +permissions and limitations under the License. + +See the AUTHORS file for names of contributors. +*/ + +#pragma once + +namespace phxrpc { + +void __bad_assertion(const char *mess); + +} + +#undef __PHXRPC_STR +#undef __PHXRPC_XSTR +#undef PHXRPC_ASSERT + +#define __PHXRPC_STR(x) # x +#define __PHXRPC_XSTR(x) __PHXRPC_STR(x) +#define PHXRPC_ASSERT(expr) \ + ((expr) ? (void)0 : \ + phxrpc::__bad_assertion("Assertion \"" #expr \ + "\" failed, file " __PHXRPC_XSTR(__FILE__) \ + ", line " __PHXRPC_XSTR(__LINE__) "\n")) diff --git a/phxrpc/http/test_http_client.cpp b/phxrpc/http/test_http_client.cpp index 2b202ca..a9e0150 100644 --- a/phxrpc/http/test_http_client.cpp +++ b/phxrpc/http/test_http_client.cpp @@ -29,6 +29,7 @@ See the AUTHORS file for names of contributors. #include "http_msg.h" #include "http_client.h" +#include "phxrpc/comm.h" #include "phxrpc/file/file_utils.h" #include "phxrpc/file/opt_map.h" #include "phxrpc/network/socket_stream_block.h" @@ -50,10 +51,8 @@ void ShowUsage(const char *program) { } int main(int argc, char **argv) { - { - bool chk = (sigset(SIGPIPE, SIG_IGN) != SIG_ERR); - if (!chk) assert(chk); - } + + PHXRPC_ASSERT(signal(SIGPIPE, SIG_IGN) != SIG_ERR); OptMap optMap("h:p:r:u:f:ov"); diff --git a/phxrpc/network/uthread_context_util.cpp b/phxrpc/network/uthread_context_util.cpp index 41cf00c..e7d6e57 100644 --- a/phxrpc/network/uthread_context_util.cpp +++ b/phxrpc/network/uthread_context_util.cpp @@ -25,6 +25,7 @@ See the AUTHORS file for names of contributors. #include #include +#include "phxrpc/comm.h" namespace phxrpc { @@ -48,11 +49,8 @@ UThreadStackMemory :: UThreadStackMemory(const size_t stack_size, const bool nee PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); assert(raw_stack_ != nullptr); - bool chk; - chk = (mprotect(raw_stack_, page_size, PROT_NONE) == 0); - if (!chk) assert(chk); - chk = (mprotect((void *)((char *)raw_stack_ + stack_size_ + page_size), page_size, PROT_NONE) == 0); - if (!chk) assert(chk); + PHXRPC_ASSERT(mprotect(raw_stack_, page_size, PROT_NONE) == 0); + PHXRPC_ASSERT(mprotect((void *)((char *)raw_stack_ + stack_size_ + page_size), page_size, PROT_NONE) == 0); stack_ = (void *)((char *)raw_stack_ + page_size); } else { @@ -63,18 +61,13 @@ UThreadStackMemory :: UThreadStackMemory(const size_t stack_size, const bool nee } UThreadStackMemory :: ~UThreadStackMemory() { - bool chk; int page_size = getpagesize(); if (need_protect_) { - chk = (mprotect(raw_stack_, page_size, PROT_READ | PROT_WRITE) == 0); - if (!chk) assert(chk); - chk = (mprotect((void *)((char *)raw_stack_ + stack_size_ + page_size), page_size, PROT_READ | PROT_WRITE) == 0); - if (!chk) assert(chk); - chk = (munmap(raw_stack_, stack_size_ + page_size * 2) == 0); - if (!chk) assert(chk); + PHXRPC_ASSERT(mprotect(raw_stack_, page_size, PROT_READ | PROT_WRITE) == 0); + PHXRPC_ASSERT(mprotect((void *)((char *)raw_stack_ + stack_size_ + page_size), page_size, PROT_READ | PROT_WRITE) == 0); + PHXRPC_ASSERT(munmap(raw_stack_, stack_size_ + page_size * 2) == 0); } else { - chk = (munmap(raw_stack_, stack_size_) == 0); - if (!chk) assert(chk); + PHXRPC_ASSERT(munmap(raw_stack_, stack_size_) == 0); } } diff --git a/phxrpc/network/uthread_epoll.cpp b/phxrpc/network/uthread_epoll.cpp index 935f428..7b33e01 100644 --- a/phxrpc/network/uthread_epoll.cpp +++ b/phxrpc/network/uthread_epoll.cpp @@ -42,6 +42,7 @@ See the AUTHORS file for names of contributors. #include #endif +#include "phxrpc/comm.h" #include "phxrpc/file/log_utils.h" #include "phxrpc/network/socket_stream_base.h" @@ -80,8 +81,7 @@ EpollNotifier::~EpollNotifier() { } void EpollNotifier::Run() { - bool chk = (pipe(pipe_fds_) == 0); - if (!chk) assert(chk); + PHXRPC_ASSERT(pipe(pipe_fds_) == 0); fcntl(pipe_fds_[1], F_SETFL, O_NONBLOCK); scheduler_->AddTask(std::bind(&EpollNotifier::Func, this), nullptr); }