libcopp  2.2.0
sample_readme_10.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include <libcopp/utils/config/libcopp_build_features.h>
4 
5 #if (defined(LIBCOTASK_MACRO_ENABLED) && LIBCOTASK_MACRO_ENABLED) && defined(LIBCOPP_MACRO_ENABLE_WIN_FIBER) && \
6  LIBCOPP_MACRO_ENABLE_WIN_FIBER
7 // include task header file
8 # include <libcotask/task.h>
9 
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;
13  using value_type = int;
14 };
15 
16 typedef cotask::task<my_task_macro_t> my_task_t;
17 
18 # ifdef _MSC_VER
19 # pragma warning(push)
20 # pragma warning(disable : 4091)
21 
22 # include <atlconv.h>
23 # include <imagehlp.h>
24 
25 # pragma comment(lib, "dbghelp.lib")
26 
27 # ifdef UNICODE
28 # define SAMPLE_VC_TEXT(x) A2W(x)
29 # else
30 # define SAMPLE_VC_TEXT(x) x
31 # endif
32 
33 LPTOP_LEVEL_EXCEPTION_FILTER g_msvc_debuger_old_handle = nullptr;
34 std::string g_msvc_debuger_pattern;
35 
36 inline void CreateMiniDump(EXCEPTION_POINTERS *pep, LPCTSTR strFileName) {
37  HANDLE hFile =
38  CreateFile(strFileName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
39 
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;
45  // MINIDUMP_CALLBACK_INFORMATION mci;
46  // mci.CallbackRoutine = (MINIDUMP_CALLBACK_ROUTINE)MiniDumpCallback;
47  // mci.CallbackParam = 0;
48  MINIDUMP_TYPE mdt =
49  (MINIDUMP_TYPE)(MiniDumpWithPrivateReadWriteMemory | MiniDumpWithDataSegs | MiniDumpWithHandleData |
50  MiniDumpWithFullMemoryInfo | MiniDumpWithThreadInfo | MiniDumpWithUnloadedModules);
51  MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, (pep != 0) ? &mdei : 0, 0, nullptr);
52  CloseHandle(hFile);
53  }
54 }
55 
56 LONG WINAPI GPTUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo) {
57  //得到当前时间
58  SYSTEMTIME st;
59  ::GetLocalTime(&st);
60  //得到程序所在文件夹
61  // TCHAR exeFullPath[256]; // MAX_PATH
62  // GetModuleFileName(nullptr, exeFullPath, 256);//得到程序模块名称,全路径
63 
64  TCHAR szFileName[_MAX_FNAME] = {0};
65 
66  USES_CONVERSION;
67 
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);
71 
72  if (nullptr == g_msvc_debuger_old_handle) {
73  return EXCEPTION_EXECUTE_HANDLER; // 下一个Handle, 一般是程序停止运行
74  }
75 
76  return g_msvc_debuger_old_handle(pExceptionInfo);
77 }
78 
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;
84  }
85 }
86 # endif
87 
88 int main() {
89 # ifdef _MSC_VER
90  sample_setup_msvc_mini_dump("d:/libcopp-test-minidump");
91 # endif
92  // create a task using factory function [with lambda expression]
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;
97 
98  // ! Make crash and it's will generate a mini dump into d:/libcopp-test-minidump-*.dmp
99  // copp::this_coroutine::get_coroutine()->yield();
100  return 0;
101  });
102 
103  std::cout << "task " << task->get_id() << " created" << std::endl;
104  // start a task
105  task->start();
106 
107  std::cout << "task " << task->get_id() << " yield" << std::endl;
108  task->resume();
109  std::cout << "task " << task->get_id() << " stoped, ready to be destroyed." << std::endl;
110 
111  return 0;
112 }
113 
114 # ifdef _MSC_VER
115 # pragma warning(pop)
116 # endif
117 
118 #else
119 int main() {
120  std::cerr << "lambda not supported, or fiber is not supported, this sample is not available." << std::endl;
121  return 0;
122 }
123 #endif
UTIL_FORCEINLINE id_type get_id() const LIBCOPP_MACRO_NOEXCEPT
Definition: task_impl.h:74
virtual int yield(void **priv_data)=0
Definition: task.h:37
int start(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_CREATED) override
Definition: task.h:462
int resume(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_WAITING) override
Definition: task.h:543
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
Definition: this_task.cpp:9
std::shared_ptr< cli::cmd_option_value > value_type
Definition: cmd_option.h:50
int main()
cotask::task my_task_t