Non-blocking Connect(2) and Error Handling

这是我在实现 ezio connector 时遇到的一个比较有意(keng)思(die)的问题。

在使用 non-blocking 的 connect(2) 时,按照 manual 的说法,如果调用的返回被认为是合理的(例如 EINPROGRESS),那么就需要:

Read More

Naming a Native Thread

这里所说的 naming 主要是为了能够被 debugger 识别,所以单纯的通过 TLS 存储一个额外的字符串是不够的。

Windows

Windows 上的做法有两种。

第一种是利用现成的 API SetThreadDescription()

通过这个 API 设置的名字据说新版的 minidump 和 WinDBG 都能认了。

不过缺点是这个 API 很新,从 Windows 10 1607 (build 14393) 开始才有,所以稳妥的使用方式还是从 kernel32.dll 里动态获取。

第二种做法比较传统,而且很不直观,来源是 MSDN 的一篇 doc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const DWORD kVCThreadNameException = 0x406D1388;

typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;

void SetThreadName()
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = "Worker-Traditional";
info.dwThreadID = -1;
info.dwFlags = 0;

__try {
RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(DWORD),
reinterpret_cast<ULONG_PTR*>(&info));
} __except(EXCEPTION_EXECUTE_HANDLER) {
}
}

基本上照搬 doc 上的代码就好了。

Read More

Using Boolean Switch with Python Argparser the Right Way

The Context

之前拿 Python 写了一个 CMake 的 build driver,因为要控制一些编译参数,所以使用了如下代码:

Read More

Monthly Read Posts in Sep 2018

Programming Language

Strong types for strong interfaces

How to add a type wrapper for built-in types.

Besides using phantom template parameter to avoid alias, one can also use private inheritance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class NamedType
{
public:
explicit NamedType(T const& value) : value_(value) {}
explicit NamedType(T&& value) : value_(std::move(value)) {}
T& get() { return value_; }
T const& get() const {return value_; }
private:
T value_;
};

class Width : NamedType<int> {
public:
using NamedType::NamedType;
using NamedType::get;
};

Passing strong types by reference – First attempt

This post is the sequal of the above post.

The main problem the post tryies to solve is: how to make copy of NamedType values cheap.

However, I am conservative on using reference-wrapper as the solution, because doing this has to expose the lifetime of the wrapped object to public; after all, reference-wrapper is only a point per se.

Read More

给 ydiff 提了个 PR

ydiff 是基于 CLI 的一个 diff tool,支持 side-by-side 模式

前段时间在 surface 上 pip 安装后发现不能使用,直接提示找不到 SIGPIPE;看来这玩意儿压根没考虑过支持 Windows。

Read More

Git Invert-grep Bug

前段时间部门技术老板要求每个组统计每个研发同学在某个版本的千行 bug 率,所以第一步就要能够统计某个人在某个指定分支的提交信息。

这个很容易做,利用

1
git log d313a36e199^..HEAD --shortstat --author="Kingsley Chen"

Read More

Monthly Read Posts in Aug 2018

Programming Languages

CppCon 2015: Fedor Pikus “The Unexceptional Exceptions”

Handling exceptions is just as to handle errors using error code, exception is just a tool.

The key point is to maintain the program state in a well defiend state.

Turn error handling into resource management and use RAII to automate it; use explicit try…catch only when absolute necessary.

Bonus tip: avoid uses of pthread_cancel.


Pros and cons of functional programming

Pure function: avoid shared state; immutable structures; function composition.

Declarative: instead of answers the question ‘how to do’ in imperative style, it answers the question ‘what to do’; imperative relies on instructions while declarative relies more on expressions.

Cons of FP: not suitable for graph algorithms; major shift on mind patterns.


Understanding Shell Script’s idiom: 2>&1

General usage:

1
cat foo.txt > result.log 2>&1
  1. fd for stdout and stderr are 1 and 2 respectively
  2. a > b is a shortcut for a 1> b and 1 here is fd value for stdout
  3. you can use &fd to reference a fd value
  4. therefore, using 2>&1 would redirect stderr to stdout, and 1>&2 would do the opposite.

Read More