3 #include <libcopp/utils/config/libcopp_build_features.h>
5 #if (defined(LIBCOTASK_MACRO_ENABLED) && LIBCOTASK_MACRO_ENABLED) && defined(LIBCOPP_MACRO_ENABLE_WIN_FIBER) && \
6 LIBCOPP_MACRO_ENABLE_WIN_FIBER
10 struct my_task_macro_t {
11 using stack_allocator_type = copp::coroutine_fiber_context_default::allocator_type;
12 using coroutine_type = copp::coroutine_fiber_context_default;
16 typedef cotask::task<my_task_macro_t>
my_task_t;
19 # pragma warning(push)
20 # pragma warning(disable : 4091)
23 # include <imagehlp.h>
25 # pragma comment(lib, "dbghelp.lib")
28 # define SAMPLE_VC_TEXT(x) A2W(x)
30 # define SAMPLE_VC_TEXT(x) x
33 LPTOP_LEVEL_EXCEPTION_FILTER g_msvc_debuger_old_handle =
nullptr;
34 std::string g_msvc_debuger_pattern;
36 inline void CreateMiniDump(EXCEPTION_POINTERS *pep, LPCTSTR strFileName) {
38 CreateFile(strFileName, GENERIC_READ | GENERIC_WRITE, 0,
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
nullptr);
40 if ((hFile !=
nullptr) && (hFile != INVALID_HANDLE_VALUE)) {
41 MINIDUMP_EXCEPTION_INFORMATION mdei;
42 mdei.ThreadId = GetCurrentThreadId();
43 mdei.ExceptionPointers = pep;
44 mdei.ClientPointers = FALSE;
49 (MINIDUMP_TYPE)(MiniDumpWithPrivateReadWriteMemory | MiniDumpWithDataSegs | MiniDumpWithHandleData |
50 MiniDumpWithFullMemoryInfo | MiniDumpWithThreadInfo | MiniDumpWithUnloadedModules);
51 MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, (pep != 0) ? &mdei : 0, 0,
nullptr);
56 LONG WINAPI GPTUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo) {
64 TCHAR szFileName[_MAX_FNAME] = {0};
68 wsprintf(szFileName, TEXT(
"%s-%04d-%02d-%02d.%02d%02d%02d.%03d.dmp"), SAMPLE_VC_TEXT(g_msvc_debuger_pattern.c_str()),
69 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
70 CreateMiniDump(pExceptionInfo, szFileName);
72 if (
nullptr == g_msvc_debuger_old_handle) {
73 return EXCEPTION_EXECUTE_HANDLER;
76 return g_msvc_debuger_old_handle(pExceptionInfo);
79 void __cdecl sample_setup_msvc_mini_dump(
const char *prefix) {
80 g_msvc_debuger_pattern = prefix;
81 g_msvc_debuger_old_handle = SetUnhandledExceptionFilter(GPTUnhandledExceptionFilter);
82 if (g_msvc_debuger_old_handle == GPTUnhandledExceptionFilter) {
83 g_msvc_debuger_old_handle =
nullptr;
90 sample_setup_msvc_mini_dump(
"d:/libcopp-test-minidump");
93 my_task_t::ptr_t
task = my_task_t::create([]() {
94 std::cout <<
"task " << cotask::this_task::get<my_task_t>()->get_id() <<
" started" << std::endl;
96 std::cout <<
"task " << cotask::this_task::get<my_task_t>()->get_id() <<
" resumed" << std::endl;
103 std::cout <<
"task " <<
task->
get_id() <<
" created" << std::endl;
107 std::cout <<
"task " <<
task->
get_id() <<
" yield" << std::endl;
109 std::cout <<
"task " <<
task->
get_id() <<
" stoped, ready to be destroyed." << std::endl;
115 # pragma warning(pop)
120 std::cerr <<
"lambda not supported, or fiber is not supported, this sample is not available." << std::endl;
UTIL_FORCEINLINE id_type get_id() const LIBCOPP_MACRO_NOEXCEPT
virtual int yield(void **priv_data)=0
int start(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_CREATED) override
int resume(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_WAITING) override
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
std::shared_ptr< cli::cmd_option_value > value_type