code_junkie

C++: Error handling problem across threads

Nov 13th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. void Callback(HWND hwnd, boost::function<void(HWND,LPARAM)> call, LPARAM lParam)
  2. {
  3. //Make our stack allocated function object into a heap allocated one
  4. boost::function<void(HWND,LPARAM)> *callH = new boost::function<void(HWND,LPARAM)>(call);
  5. //send a message with a pointer to our function object in the WPARAM
  6. PostMessage(hwnd, WM_CALLBACK, (unsigned)callH, lParam);
  7. }
  8. LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  9. {
  10. //check for our custom message
  11. if(msg == WM_CALLBACK)
  12. {
  13. //retreive the function pointer from the WPARAM
  14. boost::function<void(HWND,LPARAM)> *callH = (boost::function<void(HWND,LPARAM)>*)wParam;
  15. //call it
  16. (*callH)(hwnd,lParam);
  17. //delete our heap allocated function object
  18. delete callH;
  19. return 0;
  20. }
  21. else
  22. //if there was nothing relevant to us, call the old message procedure
  23. return CallWindowProc(hooked[hwnd], hwnd, msg, wParam, lParam);
  24. }
  25. //std::map<HWND, WNDPROC> hooked;
  26.  
  27. Graphics::Graphics(IWindow *_window, Size2<unsigned> _size)
  28. :lost(false), reset(false), refCnt(0), backCol(0xFF000000),
  29. started(false), exited(false),
  30. window(_window), size(_size)
  31. {
  32. window->AddRef();
  33. HWND hwnd = *((HWND*)window->GetHandle());
  34. CallbackHook(hwnd);
  35. Callback(hwnd, boost::bind(&Graphics::create, this), 0);
  36.  
  37. while(!started)Sleep(100);
  38. }
  39. void Graphics::create()
  40. {
  41. ...code that may throw various exceptions
  42. started = true;
  43. }
  44.  
  45. class Context
  46. {
  47. public:
  48. Context(HWND hwnd, const boost::function<void(HWND,LPARAM)>& f, LPARAM lParam)
  49. {
  50. // TODO: reroute call through Wrapper function.
  51. }
  52.  
  53. void wait()
  54. {
  55. mutex::scoped_lock l(m);
  56. while (!finished)
  57. {
  58. c.wait(l);
  59. }
  60. if (ex)
  61. rethrow_exception(ex);
  62. }
  63.  
  64. private:
  65. void Wrapper()
  66. {
  67. try
  68. {
  69. f(/*params*/);
  70. }
  71. catch (...)
  72. {
  73. ex = current_exception();
  74. }
  75. mutex::scoped_lock l(m);
  76. finished = true;
  77. c.notify_all();
  78. }
  79.  
  80. boost::function<void(HWND,LPARAM)> f;
  81. exception_ptr ex;
  82. bool finished;
  83. mutex m;
  84. condition c;
  85. };
  86.  
  87. void Callback(HWND hwnd, const boost::function<void(HWND,LPARAM)>& f, LPARAM lParam)
  88. {
  89. Context ctx(hwnd, f, lParam);
  90.  
  91. ctx.wait();
  92. }
  93.  
  94. void Callback(HWND hwnd, boost::function<void(HWND,LPARAM)> call, LPARAM lParam)
  95. {
  96. //send a message with a pointer to our function object in the WPARAM
  97. if (SendMessage(hwnd, WM_CALLBACK, &call, lParam))
  98. {
  99. // error handling.
  100. }
  101. }
  102. LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  103. {
  104. //check for our custom message
  105. if(msg == WM_CALLBACK)
  106. {
  107. //retreive the function pointer from the WPARAM
  108. boost::function<void(HWND,LPARAM)> *callH = (boost::function<void(HWND,LPARAM)>*)wParam;
  109. //call it, catching exceptions in the process.
  110. try
  111. {
  112. (*callH)(hwnd,lParam);
  113. return 0;
  114. }
  115. catch (...)
  116. {
  117. return 1;
  118. }
  119. }
  120. else
  121. //if there was nothing relevant to us, call the old message procedure
  122. return CallWindowProc(hooked[hwnd], hwnd, msg, wParam, lParam);
  123. }
  124. //std::map<HWND, WNDPROC> hooked;
  125.  
  126. Graphics::Graphics(IWindow *_window, Size2<unsigned> _size)
  127. :lost(false), reset(false), refCnt(0), backCol(0xFF000000),
  128. started(false), exited(false),
  129. window(_window), size(_size)
  130. {
  131. window->AddRef();
  132. HWND hwnd = *((HWND*)window->GetHandle());
  133. CallbackHook(hwnd);
  134. Callback(hwnd, boost::bind(&Graphics::create, this), 0);
  135.  
  136. // No need to wait, as SendMessage is synchronous.
  137. }
  138. void Graphics::create()
  139. {
  140. ...code that may throw various exceptions
  141. started = true;
  142. }
  143.  
  144. Graphics::Graphics(IWindow *_window, Size2<unsigned> _size)
  145. :lost(false), reset(false), refCnt(0), backCol(0xFF000000),
  146. started(false), exited(false),
  147. window(_window), size(_size)
  148. {
  149. window->AddRef();
  150. HWND hwnd = *((HWND*)window->GetHandle());
  151. semaphore = CreateSemaphore(NULL, 0, 1, NULL);
  152. if (semaphore == NULL) throw whatever;
  153. CallbackHook(hwnd);
  154. Callback(hwnd, boost::bind(&Graphics::create, this), 0);
  155.  
  156. WaitForSingleObject(semaphore);
  157. create();
  158.  
  159. CloseHandle(semaphore); /* you won't need it */
  160. }
  161. void Graphics::create()
  162. {
  163. ...code that may throw various exceptions
  164. }
  165. void Graphics::create_callback()
  166. {
  167. ReleaseSemaphore(semaphore, 1, NULL);
  168. }
Add Comment
Please, Sign In to add comment