]> git.sesse.net Git - casparcg/blobdiff - common/executor.h
Fixed null pointer reference in executor
[casparcg] / common / executor.h
index aec4876342bd277096787a40b6ba1c856138171e..e67a0afa708e8adb04a458ea2382efe52a158c3a 100644 (file)
@@ -60,7 +60,8 @@ class executor final
        tbb::atomic<bool>                                                                                       is_running_;
        boost::thread                                                                                           thread_;        
        function_queue_t                                                                                        execution_queue_;
-               
+       tbb::atomic<bool>                                                                                       currently_in_task_;
+
 public:                
        executor(const std::wstring& name)
                : name_(name)
@@ -74,23 +75,32 @@ public:
                })
        {
                is_running_ = true;
+               currently_in_task_ = false;
                thread_ = boost::thread([this]{run();});
        }
        
        ~executor()
        {
+               CASPAR_LOG(debug) << L"Shutting down " << name_;
+
                try
                {
-                       internal_begin_invoke([=]
-                       {
-                               is_running_ = false;
-                       }).wait();
+                       if (is_running_)
+                               internal_begin_invoke([=]
+                               {
+                                       is_running_ = false;
+                               }).wait();
                }
                catch(...)
                {
                        CASPAR_LOG_CURRENT_EXCEPTION();
                }
                
+               join();
+       }
+
+       void join()
+       {
                thread_.join();
        }
 
@@ -161,7 +171,7 @@ public:
        {
                return execution_queue_.size(); 
        }
-               
+
        bool is_running() const
        {
                return is_running_; 
@@ -171,6 +181,16 @@ public:
        {
                return boost::this_thread::get_id() == thread_.get_id();
        }
+
+       bool is_currently_in_task() const
+       {
+               return currently_in_task_;
+       }
+
+       std::wstring name() const
+       {
+               return name_;
+       }
                
 private:       
 
@@ -219,7 +239,7 @@ private:
 
                if (!execution_queue_.try_push(priority, function))
                {
-                       CASPAR_LOG(debug) << print() << L" Overflow. Blocking caller.";
+                       CASPAR_LOG(warning) << print() << L" Overflow. Blocking caller.";
                        execution_queue_.push(priority, function);
                }
 
@@ -230,7 +250,24 @@ private:
                                function();
                        }
 
-                       return future.get();
+                       try
+                       {
+                               return future.get();
+                       }
+                       catch (const caspar_exception& e)
+                       {
+                               if (!is_current()) // Add context information from this thread before rethrowing.
+                               {
+                                       auto ctx_info = boost::get_error_info<context_info_t>(e);
+
+                                       if (ctx_info)
+                                               e << context_info(get_context() + *ctx_info);
+                                       else
+                                               e << context_info(get_context());
+                               }
+
+                               throw;
+                       }
                });
        }
 
@@ -243,12 +280,15 @@ private:
                        {
                                std::function<void ()> func;
                                execution_queue_.pop(func);
+                               currently_in_task_ = true;
                                func();
                        }
                        catch(...)
                        {
                                CASPAR_LOG_CURRENT_EXCEPTION();
                        }
+
+                       currently_in_task_ = false;
                }
        }       
 };