Modern CMake

但凡有点历史的东西,在演进到一个新的阶段时,总会总结一套新的 practices 然后冠之以 modern,例如 modern C++,还有今天的主题 —— modern CMake。

上周花了一点时间稍微研究了一下所谓的 modern cmake,然后将 KBase 在 POSIX 上的 cmake 文件都按照 modern cmake 的做法做了修改,结果可见

Read More

Build Your Own HTTP Proxy Server Using Go

Go is a quite awesome programming language for building network applications. So I built my own HTTP proxy server using go last week.

To grasp the essence of how HTTP proxy server works, I choosed to implement it on TCP transport layer, forwarding TCP traffic directly.

Essentially, it runs a server, accepting incoming TCP connections and from which the server extracts target host of a request by parsing HTTP protocol messages. The server then establishes a connection to the target host, and finally operates as an intermedia, forwarding traffic from one host to another.

Implementing a TCP server which is able to handle concurrent requests uing go is easier than you thought: just runs a loop to accept requests, launching a new goroutine for each new connection:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
type Server struct {
addr string
listener net.Listener
}

func (srv *Server) Start() {
var err error
srv.listener, err = net.Listen("tcp", srv.addr)
if err != nil {
log.Fatalf("Failed to listen at %s : %v", srv.addr, err)
}

log.Printf("Proxy is listening at %s", srv.addr)

srv.runLoop()
}

func (srv *Server) runLoop() {
for {
conn, err := srv.listener.Accept()
if err != nil {
log.Printf("Failed to accept an incoming connection : %v", err)
continue
}

go srv.handleConnection(conn)
}
}

func (srv *Server) handleConnection(conn net.Conn) {
c := newConnection(conn)
c.serve()
}

Read More

Monthly Read Posts in May 2018

Misc

怎样花两年时间去面试一个人

当前行业招聘的不靠谱,招揽优秀人员的难度大。

脱颖而出的核心:良好的阅读习惯 + Github 项目

这里说的两年是针对应届生来说的,对于已经工作的人来说,可以当作是虚指。

不过文中提到的,拥抱变化的三个核心点:

  1. 触动内心的大象
  2. 建立清晰明确的目标
  3. 扫清前进道路的障碍

颇有道理。


何判断一个技术(中间件/库/工具)的靠谱程度?

如何做技术选型

System & Architecture

聊聊Linux IO

A brief introduction to Linux I/O stack.

这篇 post 的质量在国内技术博客里算是少有的干货。

另,关于 page cache 和 buffer cache 的最新的内容,可以参考 Robert Love 在 quora 上的一个回答


CppCon 2015: John Farrier “Demystifying Floating Point”

工程实践上使用浮点数(IEEE-754)需要注意的一些坑。

看之前最好翻一下 CSAPP 中关于浮点数 IEEE-754 模型的基础知识

Read More

Build Your Own Threadpool With C++

Why Threadpool Matters

Why on the earth do we need thread-pool? The answer is obvious: for doing jobs behind the scenes.

That is, saying, you have a constant stream of incoming tasks to complete, and most of which either incur heavy computation or invovle device I/O, you definitely don’t want to execute them on your main thread, because it will block your main thread until the job is done, making your application less responsive.

However, with thread-pool, you can simply submit a task to the pool, then continue what was doing; the task will eventually be completed on a thread of the thread-pool.

If your processor has multiple cores, the task is possibly performed concurrently with your jobs on the main thread.

What Should a Threadpool Provide

Before we switch our focus to editor, we are better to think twice about what we can do with the thread-pool we will build.

Read More

浅析 RefCounted 和 WeakPtr:Chromium Base 篇

序言请移步此处

MSVC STL 的分析版本请移步此处

Libstdc++ 的分析版本请移步此处

Boost 的分析版本请移步此处

注 1:因为这不是第一篇分析,所以会直入主题,跳过文学写作常用的累赘的过渡。

注 2:这是系列最后一篇。

目标版本选择

Chromium tag 68.0.3421.1

代码位置:base/memory/ref_counted.{h, cc}

RefCountedBase 和 RefCounted

两个类实现了非线程安全的引用计数,即:内部计数使用的是 built-in integer

先看看 RefCountedBase 的大致结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class RefCountedBase {
protected:
explicit RefCountedBase(StartRefCountFromZeroTag);

explicit RefCountedBase(StartRefCountFromOneTag);

~RefCountedBase();

void AddRef() const;

bool Release() const;

private:
mutable uint32_t ref_count_ = 0;

#if DCHECK_IS_ON()
mutable bool needs_adopt_ref_ = false;
mutable bool in_dtor_ = false;
mutable SequenceChecker sequence_checker_;
#endif

DISALLOW_COPY_AND_ASSIGN(RefCountedBase);
};

可以看出核心 ref_count_ 类型是 uint32_t

ctor 和 dtor 都被定义为 protected,说明这类使用做基类;同时提供了 AddRef()Release(),进行内部的计数增减。

Read More

浅析 shared_ptr:Boost 篇

序言请移步此处

MSVC STL 的分析版本请移步此处

Libstdc++ 的分析版本请移步此处

注:因为这不是第一篇分析,所以会直入主题,跳过文学写作常用的累赘的过渡。

目标版本选择

选用最新的 Boost 1.67 作为研究目标

在开始正题前,先简单看一下 shared_ptr 的类成员,方便后续分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
namespace detail {

template< class T > struct sp_element
{
typedef T type;
};

}

template<class T>
class shared_ptr {
public:
typedef typename boost::detail::sp_element< T >::type element_type;

// omitted other

private:
element_type * px; // contained pointer
boost::detail::shared_count pn; // reference counter
};

Read More

Monthly Read Post in Apr 2018

What is Load Balancing?

Digital Ocean 出品的良心文章,覆盖了:

    Read More