Skip to content

Commit

Permalink
Optimize cross-platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
lilucpp committed Dec 24, 2020
1 parent 1e6e4f3 commit 484d35a
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 114 deletions.
138 changes: 78 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,94 @@

[muduo](https://github.com/chenshuo/muduo)影响,出于学习目的,尝试移植一部分基础库到windows上,后来又”copy”了[limonp](https://github.com/yanyiwu/limonp)库的部分功能,作为平时开发基础库。

## 编译
## 代码风格

1. windows
google风格。修改了三处,如下,详见:.clang-format

安装包管理工具vcpkg,使用vcpkg安装依赖库:
```
BasedOnStyle: Google
DerivePointerAlignment: false
PointerAlignment: Right
ColumnLimit: 120
```

**提交代码前请使用格式化脚本格式化代码。**

```shell
python .\build_support\run_clang_format.py .\test
python .\build_support\run_clang_format.py .\src
```

格式化脚本可以设置跳过文件夹和文件。

```python
...
skippath = ['zib.h', 'zipconf.h']
for val in skippath:
if filename.endswith(val):
return True
...

...
skipdir = ['baselib','lib','.git','.vscode']
for val in skipdir:
if dir.endswith(val):
return True
...

```

## 编译说明

1. linux

依赖安装

```shell
sudo apt install libgtest-dev
cd /usr/src/gtest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo make install

sudo apt install gtest zlib1g-dev libboost-dev
```

- boost
- boost-utility
- boost-circular-buffer
- boost-stacktrace
- zlib
- gtest。
编译

```bat
python .\build_support\run_clang_format.py .\test
python .\build_support\run_clang_format.py .\src
```shell
git clone https://github.com/lilucpp/base.git
cd base
mkdir build
cd build
cmake .. -G "Visual Studio 14 2015" -DCMAKE_TOOLCHAIN_FILE=P:/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build . --config Release
cmake --install .
cmake ..
make
make install
```

最终目录如下:
2. windows

依赖安装,使用包管理工具vcpkg。

```bat
~base/build/PeanutBase$ tree
.
├── bin
│   ├── blockingqueue_bench.exe
│   ├── blockingqueue_test.exe
│   ├── boundedblockingqueue_test.exe
│   ├── gtest_main.exe
│   ├── gzipfile_test.exe
│   ├── singleton_test.exe
│   ├── testdata
│   │   ├── 1.conf
│   │   ├── dict.gbk
│   │   └── dict.utf8
│   └── threadpool_test.exe
├── include
│   ├── Atomic.h
│   ├── BlockingQueue.h
│   ├── BoundedBlockingQueue.h
│   ├── Colors.h
│   ├── Condition.h
│   ├── Config.h
│   ├── Copyable.h
│   ├── CountDownLatch.h
│   ├── CurrentThread.h
│   ├── Date.h
│   ├── Exception.h
│   ├── GzipFile.h
│   ├── Mutex.h
│   ├── Noncopyable.h
│   ├── Singleton.h
│   ├── StdExtension.h
│   ├── StringPiece.h
│   ├── StringUtil.h
│   ├── Thread.h
│   ├── ThreadPool.h
│   ├── Timestamp.h
│   ├── Types.h
│   └── portable_endian.h
└── lib
└── PeanutBase.lib
./vcpkg.exe install boost-utility boost-circular-buffer boost-stacktrace zlib gtest
```



主要功能
编译

```bat
git clone https://github.com/lilucpp/base.git
mkdir build
cd build
cmake .. -G "Visual Studio 14 2015" -DCMAKE_TOOLCHAIN_FILE=P:/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build . --config Release
cmake --install .
```


## 主要功能

1. Atomic
2. BlockingQueue
Expand Down Expand Up @@ -103,5 +121,5 @@
## todo

1. 减少boost依赖
2. 跨平台
2. ~~跨平台~~
3. 增加其他常用功能
21 changes: 18 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
set(base_SRCS
Condition.cpp
CountDownLatch.cpp
CurrentThread.cpp
Date.cpp
Exception.cpp
Timestamp.cpp
Thread.cpp
ThreadPool.cpp
)


if(UNIX)
set(base_SRCS
${base_SRCS}
CurrentThread_Linux.cpp
Thread_Linux.cpp
)

else()
set(base_SRCS
${base_SRCS}
CurrentThread_Win32.cpp
Thread_Win32.cpp
)

endif()

add_library(PeanutBase STATIC ${base_SRCS})
if(UNIX)
target_link_libraries(PeanutBase pthread)
endif()

############################################################
# Install
############################################################
Expand Down
17 changes: 3 additions & 14 deletions src/CurrentThread.cpp → src/CurrentThread_Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@
//
// Author:Lu Li (lilucpp at gmail dot com)

#include "CurrentThread.h"
#include <stdlib.h>

#if defined _WIN32
#include "boost/stacktrace.hpp"
#else
#include <cxxabi.h>
#include <execinfo.h>
#endif
#include <stdlib.h>
#include "CurrentThread.h"

namespace peanut {
namespace CurrentThread {
__thread pid_t t_cachedTid = 0;
__thread char t_tidString[32];
__thread int t_tidStringLength = 6;
__thread const char *t_threadName = "unknown";

#if defined _WIN32
static_assert(std::is_same<unsigned long, pid_t>::value, "pid_t should be unsigned long");
string stackTrace(bool demangle) { return boost::stacktrace::to_string(boost::stacktrace::stacktrace()); }
#else
static_assert(std::is_same<int, pid_t>::value, "pid_t should be int");

string stackTrace(bool demangle) {
string stack;
const int max_frames = 200;
Expand Down Expand Up @@ -73,7 +64,5 @@ string stackTrace(bool demangle) {
return stack;
}

#endif

} // namespace CurrentThread
} // namespace peanut
22 changes: 22 additions & 0 deletions src/CurrentThread_Win32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author:Lu Li (lilucpp at gmail dot com)

#include <stdlib.h>
#include "CurrentThread.h"

#include "boost/stacktrace.hpp"

namespace peanut {
namespace CurrentThread {
__thread pid_t t_cachedTid = 0;
__thread char t_tidString[32];
__thread int t_tidStringLength = 6;
__thread const char *t_threadName = "unknown";

static_assert(std::is_same<unsigned long, pid_t>::value, "pid_t should be unsigned long");
string stackTrace(bool demangle) { return boost::stacktrace::to_string(boost::stacktrace::stacktrace()); }

} // namespace CurrentThread
} // namespace peanut
5 changes: 1 addition & 4 deletions src/Date.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ namespace peanut {
/// This class is immutable.
/// It's recommended to pass it by value, since it's passed in register on x64.
///
class Date : public peanut::copyable
// public boost::less_than_comparable<Date>,
// public boost::equality_comparable<Date>
{
class Date : public peanut::copyable {
public:
struct YearMonthDay {
int year; // [1900..2500]
Expand Down
2 changes: 1 addition & 1 deletion src/StringUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ inline void GetTime(const std::string &format, std::string &timeStr) {
localtime_s(&tm_now, &timeNow);
#else
localtime_r(&timeNow, &tm_now);
#endif
#endif // _WIN32
size_t len = strftime((char *)timeStr.c_str(), timeStr.size(), format.c_str(), &tm_now);
timeStr.resize(len);
}
Expand Down
1 change: 0 additions & 1 deletion src/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#if defined _WIN32
#define pthread_t size_t

#endif // _WIN32

namespace peanut {
Expand Down
Loading

0 comments on commit 484d35a

Please sign in to comment.