wangzhengquan
2021-01-20 a4d18d6ab3216ce1bf8052f0fdc4ea34bc6385e8
update
11个文件已修改
108 ■■■■ 已修改文件
CMakeLists.txt 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
build.sh 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
include/usgcommon/logger.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/libusgcommon.a 补丁 | 查看 | 原始文档 | blame | 历史
lib/libusgcommon.so 补丁 | 查看 | 原始文档 | blame | 历史
src/CMakeLists.txt 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/shm/hashtable.cpp 16 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/CMakeLists.txt 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/futex_test.cpp 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test_net_socket/heart_beat.cpp 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test_net_socket/heart_beat.sh 17 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CMakeLists.txt
@@ -14,16 +14,18 @@
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
option(BUILD_DOC "Build doc" OFF)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/include/usgcommon")
list(APPEND EXTRA_LIBS ${PROJECT_SOURCE_DIR}/lib/libusgcommon.a pthread rt)
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
add_subdirectory(${PROJECT_SOURCE_DIR}/test)
add_subdirectory(${PROJECT_SOURCE_DIR}/test_net_socket)
# build api doc
if (CMAKE_BUILD_TYPE MATCHES "^[Rr]elease")
if (BUILD_DOC)
    # build the docs
    add_subdirectory(${PROJECT_SOURCE_DIR}/doc)
else()
    add_subdirectory(${PROJECT_SOURCE_DIR}/src)
    add_subdirectory(${PROJECT_SOURCE_DIR}/test)
    add_subdirectory(${PROJECT_SOURCE_DIR}/test_net_socket)
endif()
build.sh
@@ -1,9 +1,10 @@
#! /bin/bash
BUILD_TYPE="Debug"
BUILD_DOC="OFF"
function usage() {
    echo "build.sh [release | debug]"
    echo "build.sh [release | debug | doc]"
}
case ${1} in
@@ -15,6 +16,15 @@
  BUILD_TYPE="Debug"
  ;;
  "doc")
  BUILD_TYPE="Release"
  BUILD_DOC="ON"
  ;;
  "help")
  usage
  ;;
  "")
  BUILD_TYPE="Debug"
  ;;
@@ -23,6 +33,7 @@
  echo "Invalid Argument."
     usage
  ;;
esac
@@ -33,13 +44,14 @@
# -DCMAKE_BUILD_TYPE=Debug  | Release
# -DCMAKE_BUILD_TYPE=Debug | Release
# -DBUILD_SHARED_LIBS=ON
# -DCMAKE_INSTALL_PREFIX=$(pwd/../dest)
# -DQCA_MAN_INSTALL_DIR:PATH=/usr/share/man 
cmake -DCMAKE_INSTALL_PREFIX="$(pwd)/../dest" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_SHARED_LIBS=ON -DSUPPORT_RDMA=OFF ..
cmake -DCMAKE_INSTALL_PREFIX="$(pwd)/../dest" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_SHARED_LIBS=ON \
  -DBUILD_DOC=${BUILD_DOC} -DSUPPORT_RDMA=OFF ..
cmake --build .
cmake --build . --target install
# cmake --build . --target install
 
include/usgcommon/logger.h
@@ -66,7 +66,7 @@
  void error(const char *fmt, ...);
  void error(int err, const char *fmt, ...);
  void fatal(const char *fmt, ...);
  void fatal(int err, const char *fmt, ...) ;
  static const char * strlevel(int level);
};
lib/libusgcommon.a
Binary files differ
lib/libusgcommon.so
Binary files differ
src/CMakeLists.txt
@@ -1,7 +1,7 @@
# should we use our own math functions
option(SUPPORT_RDMA "If support rdma" ON)
option(SUPPORT_RDMA "If support rdma" OFF)
# configure a header file to pass some of the CMake settings
# to the source code
@@ -25,6 +25,7 @@
        shm/mm.cpp
        shm/hashtable.cpp
        px_sem_util.cpp
    svsem_util.cpp
    )
src/shm/hashtable.cpp
@@ -34,7 +34,7 @@
  hashtable->wlock = SemUtil::get(IPC_PRIVATE, 1);
  hashtable->cond = SemUtil::get(IPC_PRIVATE, 1);
  hashtable->readcnt = 0;
printf("hashtable->mutex=%d\n", hashtable->mutex);
}
void hashtable_destroy(hashtable_t *hashtable) {
@@ -206,19 +206,19 @@
  int rv;
  rv = SemUtil::dec(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
    LoggerFactory::getLogger()->error(rv, "hashtable_get 1");
  }
  hashtable->readcnt++;
  if (hashtable->readcnt == 1) {
    //获取读写锁
    rv = SemUtil::dec(hashtable->wlock);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
      LoggerFactory::getLogger()->error(rv, "hashtable_get 2");
    }
  }
  rv = SemUtil::inc(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
    LoggerFactory::getLogger()->error(rv, "hashtable_get 3");
  }
  // ================
@@ -228,25 +228,25 @@
  rv = SemUtil::dec(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
    LoggerFactory::getLogger()->error(rv, "hashtable_get 4");
  }
  hashtable->readcnt--;
  if(hashtable->readcnt == 0) {
    //释放读写锁
    rv = SemUtil::inc(hashtable->wlock);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
      LoggerFactory::getLogger()->error(rv, "hashtable_get 5");
    }
    //通知写
    rv = SemUtil::set(hashtable->cond, 1);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
      LoggerFactory::getLogger()->error(rv, "hashtable_get 6");
    }
  }
  rv = SemUtil::inc(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
    LoggerFactory::getLogger()->error(rv, "hashtable_get 7");
  }
  return res;
}
test/CMakeLists.txt
@@ -23,5 +23,20 @@
                            )
# add the install targets
install(TARGETS test1 DESTINATION bin)
add_executable(svsem_test svsem_test.cpp )
target_link_libraries(svsem_test PRIVATE  ${EXTRA_LIBS} )
target_include_directories(svsem_test PRIVATE
                            "${PROJECT_BINARY_DIR}"
                             ${EXTRA_INCLUDES}
                            )
add_executable(svsem_mon svsem_mon.cpp )
target_link_libraries(svsem_mon PRIVATE  ${EXTRA_LIBS} )
target_include_directories(svsem_mon PRIVATE
                            "${PROJECT_BINARY_DIR}"
                             ${EXTRA_INCLUDES}
                            )
test/futex_test.cpp
@@ -46,6 +46,8 @@
{
  int s;
  sigset_t mask_all, pre;
  sigfillset(&mask_all);
  /* __sync_bool_compare_and_swap(ptr, oldval, newval) is a gcc
     built-in function.  It atomically performs the equivalent of:
@@ -60,11 +62,12 @@
    /* Is the futex available? */
    if (__sync_bool_compare_and_swap(futexp, 1, 0))
    if (__sync_bool_compare_and_swap(futexp, 1, 0)) {
      sigprocmask(SIG_BLOCK, &mask_all, &pre);
      break;      /* Yes */
    }
    /* Futex is not available; wait */
    s = futex(futexp, FUTEX_WAIT, 0, NULL, NULL, 0);
    if (s == -1 && errno != EAGAIN)
      errExit("futex-FUTEX_WAIT");
@@ -79,12 +82,14 @@
fpost(int *futexp)
{
  int s;
  sigset_t mask;
  sigemptyset(&mask);
  /* __sync_bool_compare_and_swap() was described in comments above */
  if (__sync_bool_compare_and_swap(futexp, 0, 1)) {
    s = futex(futexp, FUTEX_WAKE, 1, NULL, NULL, 0);
    sigprocmask(SIG_SETMASK, &mask, NULL);
    if (s  == -1)
      errExit("futex-FUTEX_WAKE");
  }
test_net_socket/heart_beat.cpp
@@ -15,9 +15,9 @@
void sigint_handler(int sig) {
   // net_mod_socket_close(server_socket);
  printf("===Catch sigint======================\n");
  shm_mm_wrapper_destroy();
  exit(0);
  // printf("===Catch sigint======================\n");
  // shm_mm_wrapper_destroy();
  // exit(0);
}
void server(int port) {
@@ -65,7 +65,7 @@
void *runclient(void *arg) {
  signal(SIGINT,  sigint_handler);
  // signal(SIGINT,  sigint_handler);
  Targ *targ = (Targ *)arg;
  int port = targ->port;
  void *client = net_mod_socket_open();
test_net_socket/heart_beat.sh
@@ -1,9 +1,10 @@
#! /bin/bash
PROCESSES=100
PROCESSES=10
function clean() {
     ps -ef | grep "heart_beat" | awk  '{print $2}' | xargs -i kill -9 {}
     ipcrm -a
    ipcrm -a
    ps -ef | grep "heart_beat" | awk  '{print $2}' | xargs -i kill -9 {}
}
function start_server() {
@@ -28,7 +29,6 @@
        kill -9 ${pid_arr[$i]}
        #./heart_beat client 101 & ${pid_arr[$i]}=$!
    done
    ipcrm -a
}
@@ -36,11 +36,18 @@
  "server")
  start_server
  ;;
  "clients")
  start_clients
  sleep 5
    close_clients
  ;;
  "clean")
  clean
  ;;
  "")
    start_server
    sleep 1
@@ -48,6 +55,8 @@
    sleep 5
    close_clients
  ;;
  *)
  echo "error input"
  exit 1