Skip to content

Commit

Permalink
Merge pull request #43 from unixliang/master
Browse files Browse the repository at this point in the history
add PHXRPC_ASSERT
  • Loading branch information
unixliang authored Jun 26, 2018
2 parents 5d7b83c + 7e7cd23 commit 76548c2
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 29 deletions.
11 changes: 3 additions & 8 deletions codegen/server_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion phxrpc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions phxrpc/comm.h
Original file line number Diff line number Diff line change
@@ -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"
33 changes: 33 additions & 0 deletions phxrpc/comm/assert.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <cstdlib>

namespace phxrpc {

void __bad_assertion(const char *mess) {
fputs(mess, stderr);
abort();
}

}

40 changes: 40 additions & 0 deletions phxrpc/comm/assert.h
Original file line number Diff line number Diff line change
@@ -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"))
7 changes: 3 additions & 4 deletions phxrpc/http/test_http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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");

Expand Down
21 changes: 7 additions & 14 deletions phxrpc/network/uthread_context_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ See the AUTHORS file for names of contributors.
#include <unistd.h>
#include <sys/mman.h>

#include "phxrpc/comm.h"

namespace phxrpc {

Expand All @@ -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 {
Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions phxrpc/network/uthread_epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ See the AUTHORS file for names of contributors.
#include <sys/epoll.h>
#endif

#include "phxrpc/comm.h"
#include "phxrpc/file/log_utils.h"
#include "phxrpc/network/socket_stream_base.h"

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 76548c2

Please sign in to comment.