Skip to content

Commit 1defb6c

Browse files
committed
initial commit
0 parents  commit 1defb6c

File tree

106 files changed

+19703
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+19703
-0
lines changed

Diff for: .clang-format

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
BasedOnStyle: LLVM
2+
ColumnLimit: 0
3+
DerivePointerBinding: true
4+
AllowShortIfStatementsOnASingleLine: true
5+
AllowShortBlocksOnASingleLine: true
6+
PointerAlignment: Left
7+
IndentCaseLabels: true

Diff for: .editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true
10+
11+
[Makefile]
12+
indent_style = tab

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

Diff for: .markdownlint.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"MD013": false,
3+
"MD030": {
4+
"ol_single": 2,
5+
"ol_multi": 2
6+
},
7+
"MD033": {
8+
"allowed_elements": ["br"]
9+
},
10+
"MD041": false
11+
}

Diff for: .travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: cpp
2+
os: osx
3+
compiler: clang
4+
install: true
5+
script: make -C tests

Diff for: .vscode/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/browse.vc.db*

Diff for: .vscode/c_cpp_properties.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"browse": {
6+
"path": [
7+
"${workspaceFolder}/include",
8+
"${workspaceFolder}/example/vendor/include",
9+
"${workspaceFolder}/tests/vendor/include",
10+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++",
11+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
12+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.0/include",
13+
"/opt/local/include",
14+
"/usr/include",
15+
"/usr/include/c++/4.2.1",
16+
"/usr/local/include"
17+
],
18+
"limitSymbolsToIncludedHeaders": true,
19+
"databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db"
20+
},
21+
"defines": [],
22+
"includePath": [
23+
"${workspaceFolder}/include",
24+
"${workspaceFolder}/example/vendor/include",
25+
"${workspaceFolder}/tests/vendor/include",
26+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++",
27+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
28+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.0/include",
29+
"/opt/local/include",
30+
"/usr/include",
31+
"/usr/include/c++/4.2.1",
32+
"/usr/local/include"
33+
],
34+
"macFrameworkPath": []
35+
}
36+
],
37+
"version": 4
38+
}

Diff for: .vscode/settings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"files.exclude": {
3+
"**/build/": true,
4+
"**/vendor/cget/": true,
5+
".vscode/browse.vc.*": true
6+
},
7+
"search.exclude": {
8+
"**/vendor/": true
9+
}
10+
}

Diff for: LICENSE.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Boost Software License - Version 1.0 - August 17th, 2003
2+
3+
Permission is hereby granted, free of charge, to any person or organization
4+
obtaining a copy of the software and accompanying documentation covered by
5+
this license (the "Software") to use, reproduce, display, distribute,
6+
execute, and transmit the Software, and to prepare derivative works of the
7+
Software, and to permit third-parties to whom the Software is furnished to
8+
do so, all subject to the following:
9+
10+
The copyright notices in the Software and this entire statement, including
11+
the above license grant, this restriction and the following disclaimer,
12+
must be included in all copies of the Software, in whole or in part, and
13+
all derivative works of the Software, unless such copies or derivative
14+
works are solely in the form of machine-executable object code generated by
15+
a source language processor.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20+
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21+
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.

Diff for: README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[![Build Status](https://travis-ci.org/pqrs-org/cpp-osx-cf_run_loop_thread.svg?branch=master)](https://travis-ci.org/pqrs-org/cpp-osx-cf_run_loop_thread)
2+
[![License](https://img.shields.io/badge/license-Boost%20Software%20License-blue.svg)](https://github.com/pqrs-org/cpp-osx-cf_run_loop_thread/blob/master/LICENSE.md)
3+
4+
# cpp-osx-cf_run_loop_thread
5+
6+
`pqrs::cf_run_loop_thread` provides a thread for `CFRunLoop`.
7+
8+
## Requirements
9+
10+
cpp-osx-cf_run_loop_thread depends the following classes.
11+
12+
- [pqrs::cf_ptr](https://github.com/pqrs-org/cpp-osx-cf_ptr).
13+
- [pqrs::thread_wait](https://github.com/pqrs-org/cpp-thread_wait).
14+
15+
## Install
16+
17+
### Manual install
18+
19+
Copy `include/pqrs` directory into your include directory.
20+
21+
### Using package manager
22+
23+
You can also install `include/pqrs` by using [cget](https://github.com/pfultz2/cget).
24+
25+
```shell
26+
cget install pqrs-org/[email protected] --cmake header
27+
cget install pqrs-org/[email protected] --cmake header
28+
cget install pqrs-org/[email protected] --cmake header
29+
```

Diff for: example/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/cget/cget.cmake

Diff for: example/CMakeLists.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required (VERSION 3.9)
2+
3+
set(CMAKE_CXX_STANDARD 14)
4+
5+
add_compile_options(-Wall)
6+
add_compile_options(-Werror)
7+
add_compile_options(-O2)
8+
9+
project (example)
10+
11+
include_directories(SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../include)
12+
include_directories(SYSTEM ${CMAKE_CURRENT_LIST_DIR}/vendor/include)
13+
14+
add_executable(
15+
example
16+
main.cpp
17+
)
18+
19+
target_link_libraries(
20+
example
21+
"-framework CoreFoundation"
22+
)

Diff for: example/Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
all:
2+
mkdir -p build \
3+
&& cd build \
4+
&& cmake .. \
5+
&& make
6+
7+
clean:
8+
rm -r build
9+
10+
run:
11+
./build/example
12+
13+
update_vendor:
14+
cget install -f cget-requirements.txt --prefix vendor

Diff for: example/cget-requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pqrs-org/[email protected] --cmake header
2+
pqrs-org/[email protected] --cmake header

Diff for: example/main.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <pqrs/cf_run_loop_thread.hpp>
3+
4+
int main(void) {
5+
{
6+
pqrs::cf_run_loop_thread t;
7+
8+
CFRunLoopPerformBlock(t.get_run_loop(),
9+
kCFRunLoopCommonModes,
10+
^{
11+
std::cout << "hello" << std::endl;
12+
});
13+
14+
CFRunLoopWakeUp(t.get_run_loop());
15+
16+
t.terminate();
17+
}
18+
19+
return 0;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
3+
// pqrs::cf_ptr v1.1.0
4+
5+
// (C) Copyright Takayama Fumihiko 2018.
6+
// Distributed under the Boost Software License, Version 1.0.
7+
// (See http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#include <CoreFoundation/CoreFoundation.h>
10+
11+
namespace pqrs {
12+
template <typename T>
13+
class cf_ptr final {
14+
public:
15+
cf_ptr(void) : cf_ptr(nullptr) {
16+
}
17+
18+
cf_ptr(T _Nullable p) : p_(p) {
19+
if (p_) {
20+
CFRetain(p_);
21+
}
22+
}
23+
24+
cf_ptr(const cf_ptr& other) : p_(nullptr) {
25+
*this = other;
26+
}
27+
28+
cf_ptr& operator=(const cf_ptr& other) {
29+
auto old = p_;
30+
31+
p_ = other.p_;
32+
if (p_) {
33+
CFRetain(p_);
34+
}
35+
36+
if (old) {
37+
CFRelease(old);
38+
}
39+
40+
return *this;
41+
}
42+
43+
~cf_ptr(void) {
44+
reset();
45+
}
46+
47+
const T& get(void) const {
48+
return p_;
49+
}
50+
51+
T& get(void) {
52+
return const_cast<T&>((static_cast<const cf_ptr&>(*this)).get());
53+
}
54+
55+
void reset(void) {
56+
if (p_) {
57+
CFRelease(p_);
58+
p_ = nullptr;
59+
}
60+
}
61+
62+
operator bool(void) const {
63+
return p_ != nullptr;
64+
}
65+
66+
const T& operator*(void)const {
67+
return p_;
68+
}
69+
70+
T& operator*(void) {
71+
return const_cast<T&>(*(static_cast<const cf_ptr&>(*this)));
72+
}
73+
74+
private:
75+
T _Nullable p_;
76+
};
77+
} // namespace pqrs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#pragma once
2+
3+
// pqrs::thread_wait v1.1.0
4+
5+
// (C) Copyright Takayama Fumihiko 2018.
6+
// Distributed under the Boost Software License, Version 1.0.
7+
// (See http://www.boost.org/LICENSE_1_0.txt)
8+
9+
// `pqrs::thread_wait` can be used safely in a multi-threaded environment.
10+
11+
#include <memory>
12+
#include <mutex>
13+
14+
namespace pqrs {
15+
class thread_wait {
16+
public:
17+
// We have to use shared_ptr to avoid SEGV from a spuriously wake.
18+
//
19+
// Note:
20+
// If we don't use shared_ptr, `thread_wait::notify` rarely causes SEGV in the following case.
21+
//
22+
// 1. `notify` set notify_ = true.
23+
// 2. `wait_notice` exits by spuriously wake.
24+
// 3. `wait` is destructed.
25+
// 4. `notify` calls `cv_.notify_one` with released `cv_`. (SEGV)
26+
//
27+
// A bad example:
28+
// ----------------------------------------
29+
// {
30+
// pqrs::thread_wait w;
31+
// std::thread t([&w] {
32+
// w.notify(); // `notify` rarely causes SEGV.
33+
// });
34+
// t.detach();
35+
// w.wait_notice();
36+
// }
37+
// ----------------------------------------
38+
//
39+
// A good example:
40+
// ----------------------------------------
41+
// {
42+
// auto w = pqrs::make_thread_wait();
43+
// std::thread t([w] {
44+
// w->notify();
45+
// });
46+
// t.detach();
47+
// w->wait_notice();
48+
// }
49+
// ----------------------------------------
50+
51+
static std::shared_ptr<thread_wait> make_thread_wait(void) {
52+
struct impl : thread_wait {
53+
impl(void) : thread_wait() {}
54+
};
55+
return std::make_shared<impl>();
56+
}
57+
58+
virtual ~thread_wait(void) {
59+
}
60+
61+
void wait_notice(void) {
62+
std::unique_lock<std::mutex> lock(mutex_);
63+
64+
cv_.wait(lock, [this] {
65+
return notify_;
66+
});
67+
}
68+
69+
void notify(void) {
70+
{
71+
std::lock_guard<std::mutex> lock(mutex_);
72+
73+
notify_ = true;
74+
}
75+
76+
cv_.notify_one();
77+
}
78+
79+
private:
80+
thread_wait(void) : notify_(false) {
81+
}
82+
83+
bool notify_;
84+
std::mutex mutex_;
85+
std::condition_variable cv_;
86+
};
87+
88+
inline std::shared_ptr<thread_wait> make_thread_wait(void) {
89+
return thread_wait::make_thread_wait();
90+
}
91+
} // namespace pqrs

Diff for: example/vendor/include/pqrs/cf_ptr.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../cget/pkg/pqrs-org__cpp-osx-cf_ptr/install/include/pqrs/cf_ptr.hpp

Diff for: example/vendor/include/pqrs/thread_wait.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../cget/pkg/pqrs-org__cpp-thread_wait/install/include/pqrs/thread_wait.hpp

0 commit comments

Comments
 (0)