Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update modules support #387

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ if (ASYNC_SIMPLE_ENABLE_ASAN)
endif()

# Start Detecing Uthread
set(UTHREAD OFF)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # uname -s
if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "x86_64|aarch64|ppc64le") # uname -m
set(UTHREAD ON)
Expand All @@ -164,7 +165,7 @@ else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
endif()

add_subdirectory(async_simple)
option(STD_MODULE_AVAILABLE "if the std module is available by default" OFF)

option(ASYNC_SIMPLE_BUILD_MODULES "Build async_simple library in C++20 Modules form" OFF)
if(${ASYNC_SIMPLE_BUILD_MODULES})
Expand All @@ -184,11 +185,10 @@ if(${ASYNC_SIMPLE_BUILD_MODULES})

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_subdirectory(modules)
endif()

add_subdirectory(async_simple)

option(ASYNC_SIMPLE_BUILD_DEMO_EXAMPLE "Build the demo example" ON)
if (ASYNC_SIMPLE_BUILD_DEMO_EXAMPLE)
add_subdirectory(demo_example)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ CC=clang CXX=clang++ cmake .. -DCMAKE_BUILD_TYPE=Release -DASYNC_SIMPLE_BUILD_MO
ninja
```

If std module is available, we can define `-DSTD_MODULE_AVAILABLE=ON` when configuring cmake to use std modules provided by vendors.

**Note** that the `async_simple` module in the main branch is actually a named module's wrapper for headers for compatibility. We can find the practical usage of C++20 Modules in https://github.com/alibaba/async_simple/tree/CXX20Modules, which contains the support for xmake and cmake as well.

# Questions
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ CC=clang CXX=clang++ cmake .. -DCMAKE_BUILD_TYPE=Release -DASYNC_SIMPLE_BUILD_MO
ninja
```

若 std module 在环境中可用,我们可以在配置 cmake 时定义 `-DSTD_MODULE_AVAILABLE=ON` 以使用官方的 std module。

**需要注意:** 出于兼容性考虑,目前 main 分支中的 `async_simple` Module 本质上只是将 `async_simple` 的头文件封装为了 `Named Modules` 而已。我们可以在 https://github.com/alibaba/async_simple/tree/CXX20Modules 中找到更完整的 `Named Modules` 使用方式。该分支中同样包含 xmake 和 cmake 的支持。

# 存在问题?
Expand Down
25 changes: 22 additions & 3 deletions async_simple/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,32 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
endif()
endif()
# If there is no Uthread, async_simple is a header only library
if(UTHREAD)
if(${UTHREAD} OR ${ASYNC_SIMPLE_BUILD_MODULES})
add_library(async_simple_static STATIC ${SRCS})
add_library(async_simple SHARED ${SRCS})

if (${ASYNC_SIMPLE_BUILD_MODULES})
set(MODULES_SRCS "")
list(APPEND MODULES_SRCS async_simple.cppm)
if (NOT ${STD_MODULE_AVAILABLE})
list(APPEND MODULES_SRCS std.mock.cppm)
endif()
target_sources(async_simple_static
PUBLIC
FILE_SET CXX_MODULES FILES
${MODULES_SRCS}
)
target_sources(async_simple
PUBLIC
FILE_SET CXX_MODULES FILES
${MODULES_SRCS}
)
endif()

target_link_libraries(async_simple PUBLIC async_simple_header_only)
target_link_libraries(async_simple_static PUBLIC async_simple_header_only)
install(TARGETS async_simple EXPORT async_simple_targets DESTINATION lib/)
install(TARGETS async_simple_static EXPORT async_simple_targets DESTINATION lib/)
install(TARGETS async_simple EXPORT async_simple_targets FILE_SET CXX_MODULES DESTINATION lib/)
install(TARGETS async_simple_static EXPORT async_simple_targets FILE_SET CXX_MODULES DESTINATION lib/)
else()
add_library(async_simple_static INTERFACE)
target_link_libraries(async_simple_static INTERFACE async_simple_header_only)
Expand Down
3 changes: 3 additions & 0 deletions async_simple/Collect.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
#ifndef ASYNC_SIMPLE_COLLECT_H
#define ASYNC_SIMPLE_COLLECT_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <iterator>
#include <vector>
#include "async_simple/Future.h"
#include "async_simple/Try.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

// collectAll - collect all the values for a range of futures.
Expand Down
3 changes: 3 additions & 0 deletions async_simple/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
#ifndef ASYNC_SIMPLE_COMMON_H
#define ASYNC_SIMPLE_COMMON_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <stdexcept>
#include "async_simple/CommonMacros.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {
// Different from assert, logicAssert is meaningful in
// release mode. logicAssert should be used in case that
Expand Down
3 changes: 3 additions & 0 deletions async_simple/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef ASYNC_SIMPLE_EXECUTOR_H
#define ASYNC_SIMPLE_EXECUTOR_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <chrono>
#include <functional>
#include <string>
Expand All @@ -25,6 +26,8 @@
#include "async_simple/experimental/coroutine.h"
#include "async_simple/util/move_only_function.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {
// Stat information for an executor.
// It contains the number of pending task
Expand Down
3 changes: 3 additions & 0 deletions async_simple/Future.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef ASYNC_SIMPLE_FUTURE_H
#define ASYNC_SIMPLE_FUTURE_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <condition_variable>
#include <mutex>
#include <type_traits>
Expand All @@ -25,6 +26,8 @@
#include "async_simple/LocalState.h"
#include "async_simple/Traits.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

template <typename T>
Expand Down
3 changes: 3 additions & 0 deletions async_simple/FutureState.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef ASYNC_SIMPLE_FUTURESTATE_H
#define ASYNC_SIMPLE_FUTURESTATE_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <atomic>
#include <cassert>
#include <iostream>
Expand All @@ -26,6 +27,8 @@
#include "async_simple/Try.h"
#include "async_simple/util/move_only_function.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

// Details about the State of Future/Promise,
Expand Down
3 changes: 3 additions & 0 deletions async_simple/IOExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
#ifndef ASYNC_SIMPLE_IO_EXECUTOR_H
#define ASYNC_SIMPLE_IO_EXECUTOR_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <cstdint>
#include <functional>

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

// IOExecutor accepts and performs io requests, callers will be notified by
Expand Down
3 changes: 3 additions & 0 deletions async_simple/LocalState.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
#ifndef ASYNC_SIMPLE_LOCALSTATE_H
#define ASYNC_SIMPLE_LOCALSTATE_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <functional>
#include <utility>

#include "async_simple/Executor.h"
#include "async_simple/Try.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

// A component of Future/Promise. LocalState is owned by
Expand Down
3 changes: 3 additions & 0 deletions async_simple/MoveWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
#ifndef ASYNC_SIMPLE_MOVEWRAPPER_H
#define ASYNC_SIMPLE_MOVEWRAPPER_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <utility>

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

// std::function requre copyConstructable, hence we provide MoveWrapper perform
Expand Down
3 changes: 3 additions & 0 deletions async_simple/Promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
#ifndef ASYNC_SIMPLE_PROMISE_H
#define ASYNC_SIMPLE_PROMISE_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <exception>
#include "async_simple/Common.h"
#include "async_simple/Future.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

template <typename T>
Expand Down
3 changes: 3 additions & 0 deletions async_simple/Traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
#ifndef ASYNC_SIMPLE_TRAITS_H
#define ASYNC_SIMPLE_TRAITS_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include "async_simple/Try.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

template <typename T>
Expand Down
3 changes: 3 additions & 0 deletions async_simple/Try.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef ASYNC_SIMPLE_TRY_H
#define ASYNC_SIMPLE_TRY_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <cassert>
#include <exception>
#include <functional>
Expand All @@ -24,6 +25,8 @@
#include "async_simple/Common.h"
#include "async_simple/Unit.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

// Forward declaration
Expand Down
88 changes: 88 additions & 0 deletions async_simple/async_simple.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// There unhandled macro uses found in the body:
// 'CPU_SETSIZE' defined in /usr/include/sched.h:82:10
// 'CPU_ISSET' defined in /usr/include/sched.h:85:10
// 'CPU_ZERO' defined in /usr/include/sched.h:87:10
// 'CPU_SET' defined in /usr/include/sched.h:83:10
module;
// WARNING: Detected unhandled non interesting includes.
// It is not suggested mix includes and imports from the compiler's
// perspective. Since it may introduce redeclarations within different
// translation units and the compiler is not able to handle such patterns
// efficiently.
//
// See
// https://clang.llvm.org/docs/StandardCPlusPlusModules.html#performance-tips
#include <stdio.h>
#include <cassert>
#include <climits>
#include <version>

#ifdef __linux__
#include <sched.h>
#endif

export module async_simple;
import std;
#define ASYNC_SIMPLE_USE_MODULES
extern "C++" {
#include "coro/Traits.h"
#include "experimental/coroutine.h"
#include "util/move_only_function.h"
}
export extern "C++" {
#include "Executor.h"
#include "MoveWrapper.h"
}
extern "C++" {
#include "Common.h"
#include "CommonMacros.h"
#include "Unit.h"
}
export extern "C++" {
#include "Try.h"
}
extern "C++" {
#include "FutureState.h"
#include "LocalState.h"
#include "Traits.h"
}
export extern "C++" {
#include "Future.h"
#include "Promise.h"
#include "coro/DetachedCoroutine.h"
#include "coro/Lazy.h"
#include "coro/ViaCoroutine.h"
}
extern "C++" {
#include "uthread/internal/thread.h"
#include "uthread/internal/thread_impl.h"
}
export extern "C++" {
#include "Collect.h"
#include "IOExecutor.h"
#include "coro/Collect.h"
#include "coro/ConditionVariable.h"
#include "coro/CountEvent.h"
#include "coro/Dispatch.h"
#include "coro/FutureAwaiter.h"
#include "coro/Generator.h"
#include "coro/Latch.h"
#include "coro/Mutex.h"
#include "coro/PromiseAllocator.h"
#include "coro/ResumeBySchedule.h"
#include "coro/Semaphore.h"
#include "coro/SharedMutex.h"
#include "coro/Sleep.h"
#include "coro/SpinLock.h"
#include "coro/SyncAwait.h"
#include "executors/SimpleExecutor.h"
#include "executors/SimpleIOExecutor.h"
#include "uthread/Async.h"
#include "uthread/Await.h"
#include "uthread/Collect.h"
#include "uthread/Latch.h"
#include "uthread/Uthread.h"
#include "util/Condition.h"
#include "util/Queue.h"
#include "util/ThreadPool.h"
}
3 changes: 3 additions & 0 deletions async_simple/coro/Collect.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef ASYNC_SIMPLE_CORO_COLLECT_H
#define ASYNC_SIMPLE_CORO_COLLECT_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <array>
#include <exception>
#include <memory>
Expand All @@ -31,6 +32,8 @@
#include "async_simple/coro/Lazy.h"
#include "async_simple/experimental/coroutine.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {
namespace coro {

Expand Down
3 changes: 3 additions & 0 deletions async_simple/coro/ConditionVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
#ifndef ASYNC_SIMPLE_CORO_CONDITION_VARIABLE_H
#define ASYNC_SIMPLE_CORO_CONDITION_VARIABLE_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <mutex>
#include "async_simple/coro/Lazy.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {
namespace coro {

Expand Down
3 changes: 3 additions & 0 deletions async_simple/coro/CountEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
#ifndef ASYNC_SIMPLE_CORO_EVENT_H
#define ASYNC_SIMPLE_CORO_EVENT_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <atomic>
#include <cstddef>
#include <utility>
#include "async_simple/experimental/coroutine.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

namespace coro {
Expand Down
3 changes: 3 additions & 0 deletions async_simple/coro/DetachedCoroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
#ifndef ASYNC_SIMPLE_CORO_DETACHED_COROUTINE_H
#define ASYNC_SIMPLE_CORO_DETACHED_COROUTINE_H

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <stdio.h>
#include <exception>
#include "async_simple/experimental/coroutine.h"

#endif // ASYNC_SIMPLE_USE_MODULES

namespace async_simple {

namespace coro {
Expand Down
Loading
Loading