Author: suokko
Date: Mon Sep  1 01:25:57 2008
New Revision: 29152

URL: http://svn.gna.org/viewcvs/wesnoth?rev=29152&view=rev
Log:
Fixed cancelation of asyncronous operations to work (cancel connect operation)

Modified:
    trunk/src/network.cpp
    trunk/src/thread.cpp
    trunk/src/thread.hpp

Modified: trunk/src/network.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network.cpp?rev=29152&r1=29151&r2=29152&view=diff
==============================================================================
--- trunk/src/network.cpp (original)
+++ trunk/src/network.cpp Mon Sep  1 01:25:57 2008
@@ -481,14 +481,14 @@
 
 connection connect(const std::string& host, int port, threading::waiter& 
waiter)
 {
-       const threading::async_operation_holder<connect_operation> op(new 
connect_operation(host,port));
-       const connect_operation::RESULT res = op.operation().execute(waiter);
+       const threading::async_operation_ptr op(new 
connect_operation(host,port));
+       const connect_operation::RESULT res = op->execute(op, waiter);
        if(res == connect_operation::ABORTED) {
                return 0;
        }
 
-       op.operation().check_error();
-       return op.operation().result();
+       static_cast<connect_operation*>(op.get())->check_error();
+       return static_cast<connect_operation*>(op.get())->result();
 }
 
 connection accept_connection()

Modified: trunk/src/thread.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/thread.cpp?rev=29152&r1=29151&r2=29152&view=diff
==============================================================================
--- trunk/src/thread.cpp (original)
+++ trunk/src/thread.cpp Mon Sep  1 01:25:57 2008
@@ -26,7 +26,7 @@
 
 static int run_async_operation(void* data)
 {
-       threading::async_operation* const op = 
reinterpret_cast<threading::async_operation*>(data);
+       threading::async_operation_ptr 
op(*reinterpret_cast<threading::async_operation_ptr*>(data));
        op->run();
 
        const threading::lock l(op->get_mutex());
@@ -148,7 +148,7 @@
        return finished_.notify_one();
 }
 
-async_operation::RESULT async_operation::execute(waiter& wait)
+async_operation::RESULT async_operation::execute(async_operation_ptr this_ptr, 
waiter& wait)
 {
        //the thread must be created after the lock, and also destroyed after 
it.
        //this is because during the thread's execution, we must always hold 
the mutex
@@ -157,10 +157,9 @@
        //
        //we cannot hold the mutex while waiting for the thread to join though, 
because
        //the thread needs access to the mutex before it terminates
-       std::auto_ptr<thread> t(NULL);
        {
                const lock l(get_mutex());
-               t = std::auto_ptr<thread>(new thread(run_async_operation,this));
+               thread_.reset(new thread(run_async_operation,&this_ptr));
 
                bool completed = false;
                while(wait.process() == waiter::WAIT) {

Modified: trunk/src/thread.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/thread.hpp?rev=29152&r1=29151&r2=29152&view=diff
==============================================================================
--- trunk/src/thread.hpp (original)
+++ trunk/src/thread.hpp Mon Sep  1 01:25:57 2008
@@ -17,6 +17,9 @@
 
 #include "SDL.h"
 #include "SDL_thread.h"
+
+#include <boost/scoped_ptr.hpp>
+#include <boost/smart_ptr.hpp>
 
 // Threading primitives wrapper for SDL_Thread.
 //
@@ -190,6 +193,10 @@
        virtual ACTION process() = 0;
 };
 
+class async_operation;
+
+typedef boost::shared_ptr<async_operation> async_operation_ptr;
+
 //class which defines an asynchronous operation. Objects of this class are 
accessed from
 //both the worker thread and the calling thread, and so it has 'strange' 
allocation semantics.
 //It is allocated by the caller, and generally deleted by the caller. However, 
in some cases
@@ -206,10 +213,10 @@
        enum RESULT { COMPLETED, ABORTED };
 
        async_operation() : 
-               aborted_(false), finished_(), finishedVar_(false), mutex_() {}
+               thread_(), aborted_(false), finished_(), finishedVar_(false), 
mutex_() {}
        virtual ~async_operation() {}
 
-       RESULT execute(waiter& wait);
+       RESULT execute(async_operation_ptr this_ptr, waiter& wait);
 
        mutex& get_mutex() { return mutex_; }
 
@@ -225,32 +232,13 @@
        bool is_aborted() const { return aborted_; }
 
 private:
+       boost::scoped_ptr<thread> thread_;
        bool aborted_;
        condition finished_;
        bool finishedVar_;
        mutex mutex_;
 };
 
-//T should be a type derived from async_operation
-template<typename T>
-class async_operation_holder
-{
-public:
-       explicit async_operation_holder(T* op) : op_(op)
-       {}
-
-       ~async_operation_holder() {
-               //it's okay to call is_aborted() without the mutex here,
-               //because we are in the calling thread, not the worker thread
-               delete op_;
-       }
-
-       T& operation() const { return *op_; }
-
-private:
-       T* const op_;
-};
-
 }
 
 #endif


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to