diff options
-rw-r--r-- | examples/Threads/future1.cpp | 83 | ||||
-rw-r--r-- | examples/Threads/future2.cpp | 97 | ||||
-rw-r--r-- | tests/Future_Test.cpp | 71 |
3 files changed, 106 insertions, 145 deletions
diff --git a/examples/Threads/future1.cpp b/examples/Threads/future1.cpp index 9f541f7f013..db184f5bfa9 100644 --- a/examples/Threads/future1.cpp +++ b/examples/Threads/future1.cpp @@ -17,9 +17,8 @@ // // ============================================================================ -#include <math.h> +#include "ace/ACE.h" #include "ace/Task.h" - #include "ace/Synch.h" #include "ace/Message_Queue.h" #include "ace/Future.h" @@ -53,17 +52,17 @@ class Scheduler : public ACE_Task<ACE_MT_SYNCH> friend class Method_ObjectWork; public: Scheduler (const char *, Scheduler * = 0); - ~Scheduler (void); + virtual ~Scheduler (void); virtual int open (void *args = 0); virtual int close (u_long flags = 0); virtual int svc (void); - ACE_Future<double> work (double param, int count); + ACE_Future<u_long> work (u_long param, int count = 1); ACE_Future<const char*> name (void); void end (void); - double work_i (double, int); + u_long work_i (u_long, int); const char *name_i (void); private: @@ -78,21 +77,21 @@ class Method_Object_work : public ACE_Method_Object // Reification of the <work> method. { public: - Method_Object_work (Scheduler *, double, int, ACE_Future<double> &); - ~Method_Object_work (void); + Method_Object_work (Scheduler *, u_long, int, ACE_Future<u_long> &); + virtual ~Method_Object_work (void); virtual int call (void); private: Scheduler *scheduler_; - double param_; + u_long param_; int count_; - ACE_Future<double> future_result_; + ACE_Future<u_long> future_result_; }; Method_Object_work::Method_Object_work (Scheduler* new_Scheduler, - double new_param, + u_long new_param, int new_count, - ACE_Future<double> &new_result) + ACE_Future<u_long> &new_result) : scheduler_ (new_Scheduler), param_ (new_param), count_ (new_count), @@ -120,7 +119,7 @@ class Method_Object_name : public ACE_Method_Object { public: Method_Object_name (Scheduler *, ACE_Future<const char*> &); - ~Method_Object_name (void); + virtual ~Method_Object_name (void); virtual int call (void); private: @@ -155,7 +154,7 @@ class Method_Object_end : public ACE_Method_Object { public: Method_Object_end (Scheduler *new_Scheduler): scheduler_ (new_Scheduler) {} - ~Method_Object_end (void) {} + virtual ~Method_Object_end (void) {} virtual int call (void) { this->scheduler_->close (); return -1; } private: @@ -224,22 +223,13 @@ Scheduler::end (void) // Here's where the Work takes place. -double -Scheduler::work_i (double param, +u_long +Scheduler::work_i (u_long param, int count) { - double x = 0.0, y = 0.0; - - // @@ We should probably do something fun here, like compute the - // Fibonacci sequence or something. - - for (int j = 0; j < count; j++) - { - x = x + param; - y = y + double(::sin (x)); - } + ACE_UNUSED_ARG (count); - return y; + return ACE::is_prime (param, 2, param / 2); } const char * @@ -271,14 +261,14 @@ Scheduler::name (void) } } -ACE_Future<double> -Scheduler::work (double newparam, int newcount) +ACE_Future<u_long> +Scheduler::work (u_long newparam, int newcount) { if (this->scheduler_) { return this->scheduler_->work (newparam, newcount); } else { - ACE_Future<double> new_future; + ACE_Future<u_long> new_future; this->activation_queue_.enqueue (new Method_Object_work (this, newparam, newcount, new_future)); @@ -288,9 +278,6 @@ Scheduler::work (double newparam, int newcount) // @@ These values should be set by the command line options! -// Total number of iterations to <work> -static int n_iterations = 50000; - // Total number of loops. static int n_loops = 100; @@ -316,15 +303,15 @@ main (int, char *[]) for (int i = 0; i < n_loops; i++) { { - ACE_Future<double> fresulta, fresultb, fresultc, fresultd, fresulte; + ACE_Future<u_long> fresulta, fresultb, fresultc, fresultd, fresulte; ACE_Future<const char*> fname; ACE_DEBUG ((LM_DEBUG, "(%t) going to do a non-blocking call\n")); - fresulta = andres->work (0.01, 100 + (n_iterations * (i % 2))); - fresultb = peter->work (0.01, 100 + (n_iterations * (i % 2))); - fresultc = helmut->work (0.01, 100 + (n_iterations * (i % 2))); - fresultd = matias->work (0.02, 100 + (n_iterations * (i % 2))); + fresulta = andres->work (9013); + fresultb = peter->work (9013); + fresultc = helmut->work (9013); + fresultd = matias->work (9013); fname = andres->name (); // see if the result is available... @@ -341,13 +328,13 @@ main (int, char *[]) { // Every 3rd time... disconnect the futures... // but "fresulte" should still contain the result... - fresulta.cancel (10.0); - fresultb.cancel (20.0); - fresultc.cancel (30.0); - fresultd.cancel (40.0); + fresulta.cancel (10); + fresultb.cancel (20); + fresultc.cancel (30); + fresultd.cancel (40); } - double resulta = 0, resultb = 0, resultc = 0, resultd = 0, resulte = 0; + u_long resulta = 0, resultb = 0, resultc = 0, resultd = 0, resulte = 0; fresulta.get (resulta); fresultb.get (resultb); @@ -355,11 +342,11 @@ main (int, char *[]) fresultd.get (resultd); fresulte.get (resulte); - ACE_DEBUG ((LM_DEBUG, "(%t) result a %f\n", resulte)); - ACE_DEBUG ((LM_DEBUG, "(%t) result b %f\n", resulta)); - ACE_DEBUG ((LM_DEBUG, "(%t) result c %f\n", resultb)); - ACE_DEBUG ((LM_DEBUG, "(%t) result d %f\n", resultc)); - ACE_DEBUG ((LM_DEBUG, "(%t) result e %f\n", resultd)); + ACE_DEBUG ((LM_DEBUG, "(%t) result a %u\n", (u_int) resulte)); + ACE_DEBUG ((LM_DEBUG, "(%t) result b %u\n", (u_int) resulta)); + ACE_DEBUG ((LM_DEBUG, "(%t) result c %u\n", (u_int) resultb)); + ACE_DEBUG ((LM_DEBUG, "(%t) result d %u\n", (u_int) resultc)); + ACE_DEBUG ((LM_DEBUG, "(%t) result e %u\n", (u_int) resultd)); const char *name; @@ -400,6 +387,8 @@ main (int, char *[]) #if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) template class ACE_Atomic_Op<ACE_Thread_Mutex, int>; +template class ACE_Future<const char *>; +template class ACE_Future<u_long>; #endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ #else diff --git a/examples/Threads/future2.cpp b/examples/Threads/future2.cpp index 5e22721e97d..e91f763998d 100644 --- a/examples/Threads/future2.cpp +++ b/examples/Threads/future2.cpp @@ -25,9 +25,8 @@ // the tests so they are more modular. // ============================================================================ -#include <math.h> +#include "ace/ACE.h" #include "ace/Task.h" - #include "ace/Synch.h" #include "ace/Message_Queue.h" #include "ace/Future.h" @@ -58,14 +57,14 @@ class Scheduler : public ACE_Task<ACE_MT_SYNCH> public: Scheduler (const char *, Scheduler * = 0); - ~Scheduler (void); + virtual ~Scheduler (void); virtual int open (void *args = 0); // The method that is used to start the active object. // = Here are the methods exported by the class. They return an // <ACE_Future>. - ACE_Future<double> work (double param, int count); + ACE_Future<u_long> work (u_long param, int count = 1); ACE_Future<char*> name (void); void end (void); @@ -77,7 +76,7 @@ private: // Here the actual servicing of all requests is happening.. // = Implementation methods. - double work_i (double, int); + u_long work_i (u_long, int); char *name_i (void); char *name_; @@ -90,21 +89,21 @@ class Method_Object_work : public ACE_Method_Object // Reification of the <work> method. { public: - Method_Object_work (Scheduler *, double, int, ACE_Future<double> &); - ~Method_Object_work (void); + Method_Object_work (Scheduler *, u_long, int, ACE_Future<u_long> &); + virtual ~Method_Object_work (void); virtual int call (void); private: Scheduler *scheduler_; - double param_; + u_long param_; int count_; - ACE_Future<double> future_result_; + ACE_Future<u_long> future_result_; }; Method_Object_work::Method_Object_work (Scheduler* new_Scheduler, - double new_param, + u_long new_param, int new_count, - ACE_Future<double> &new_result) + ACE_Future<u_long> &new_result) : scheduler_ (new_Scheduler), param_ (new_param), count_ (new_count), @@ -128,7 +127,7 @@ class Method_Object_name : public ACE_Method_Object { public: Method_Object_name (Scheduler *, ACE_Future<char*> &); - ~Method_Object_name (void); + virtual ~Method_Object_name (void); virtual int call (void); private: @@ -164,7 +163,7 @@ class Method_Object_end : public ACE_Method_Object { public: Method_Object_end (Scheduler *new_Scheduler): scheduler_ (new_Scheduler) {} - ~Method_Object_end (void) {} + virtual ~Method_Object_end (void) {} virtual int call (void) { this->scheduler_->close (); return -1; } private: @@ -230,19 +229,13 @@ Scheduler::end (void) } // Here's where the Work takes place. -double -Scheduler::work_i (double param, +u_long +Scheduler::work_i (u_long param, int count) { - double x = 0, y = 0; - - for (int j = 0; j < count; j++) - { - x = x + param; - y = y + ::sin (x); - } + ACE_UNUSED_ARG (count); - return y; + return ACE::is_prime (param, 2, param / 2); } char * @@ -285,14 +278,14 @@ Scheduler::name (void) } } -ACE_Future<double> -Scheduler::work (double newparam, int newcount) +ACE_Future<u_long> +Scheduler::work (u_long newparam, int newcount) { if (this->scheduler_) return this->scheduler_->work (newparam, newcount); else { - ACE_Future<double> new_future; + ACE_Future<u_long> new_future; if (this->thr_count () == 0) { @@ -331,7 +324,7 @@ determine_iterations (void) { tstart = ACE_OS::gettimeofday (); - worker_a->work (0.1, n_iterations); + worker_a->work (9013, n_iterations); tend = ACE_OS::gettimeofday (); } @@ -347,6 +340,8 @@ determine_iterations (void) static void test_active_object (int n_iterations) { + ACE_UNUSED_ARG (n_iterations); + ACE_DEBUG ((LM_DEBUG," (%t) testing active object pattern...\n")); // A simple example for the use of the active object pattern and // futures to return values from an active object. @@ -377,9 +372,9 @@ test_active_object (int n_iterations) worker_c->open (); } - ACE_Future<double> fresulta = worker_a->work (0.01, n_iterations); - ACE_Future<double> fresultb = worker_b->work (0.02, n_iterations); - ACE_Future<double> fresultc = worker_c->work (0.03, n_iterations); + ACE_Future<u_long> fresulta = worker_a->work (9013); + ACE_Future<u_long> fresultb = worker_b->work (9013); + ACE_Future<u_long> fresultc = worker_c->work (9013); if (i == 0) { @@ -394,9 +389,9 @@ test_active_object (int n_iterations) // When the workers are active we will block here until the // results are available. - double resulta = fresulta; - double resultb = fresultb; - double resultc = fresultc; + u_long resulta = fresulta; + u_long resultb = fresultb; + u_long resultc = fresultc; ACE_Future<char *> fnamea = worker_a->name (); ACE_Future<char *> fnameb = worker_b->name (); @@ -406,12 +401,12 @@ test_active_object (int n_iterations) char *nameb = fnameb; char *namec = fnamec; - ACE_DEBUG ((LM_DEBUG, " (%t) result from %s %f\n", - namea, resulta)); - ACE_DEBUG ((LM_DEBUG, " (%t) result from %s %f\n", - nameb, resultb)); - ACE_DEBUG ((LM_DEBUG, " (%t) result from %s %f\n", - namec, resultc)); + ACE_DEBUG ((LM_DEBUG, " (%t) result from %s %u\n", + namea, (u_int) resulta)); + ACE_DEBUG ((LM_DEBUG, " (%t) result from %s %u\n", + nameb, (u_int) resultb)); + ACE_DEBUG ((LM_DEBUG, " (%t) result from %s %u\n", + namec, (u_int) resultc)); } ACE_DEBUG ((LM_DEBUG, " (%t) scheduler_open_count %d before end ()\n", @@ -442,28 +437,28 @@ test_cancellation (int n_iterations) ACE_NEW (worker_a, Scheduler ("worker A")); worker_a->open (); - ACE_Future<double> fresulta = worker_a->work (0.01, n_iterations); + ACE_Future<u_long> fresulta = worker_a->work (9013, n_iterations); // save the result by copying the future - ACE_Future<double> fresultb = fresulta; + ACE_Future<u_long> fresultb = fresulta; // now we cancel the first future.. but the // calculation will still go on... - fresulta.cancel (10.0); + fresulta.cancel (10); if (!fresulta.ready ()) ACE_DEBUG ((LM_DEBUG," (%t) ERROR: future A is should be ready!!!\n")); - double resulta = fresulta; + u_long resulta = fresulta; - ACE_DEBUG ((LM_DEBUG, " (%t) cancelled result %f\n", resulta)); + ACE_DEBUG ((LM_DEBUG, " (%t) cancelled result %u\n", (u_int) resulta)); - if (resulta != 10.0) - ACE_DEBUG ((LM_DEBUG, " (%t) cancelled result should be 10.0!!\n", resulta)); + if (resulta != 10) + ACE_DEBUG ((LM_DEBUG, " (%t) cancelled result should be 10!!\n", resulta)); resulta = fresultb; - ACE_DEBUG ((LM_DEBUG, " (%t) true result %f\n", resulta)); + ACE_DEBUG ((LM_DEBUG, " (%t) true result %u\n", (u_int) resulta)); worker_a->end (); // @@ Can we safely delete worker_a here? @@ -477,13 +472,13 @@ test_timeout (int n_iterations) ACE_NEW (worker_a, Scheduler ("worker A")); worker_a->open (); - ACE_Future<double> fresulta = worker_a->work (0.01, 2 * n_iterations); + ACE_Future<u_long> fresulta = worker_a->work (9013, 2 * n_iterations); // Should immediately return... and we should see an error... ACE_Time_Value *delay; ACE_NEW (delay, ACE_Time_Value (1)); - double resulta; + u_long resulta; fresulta.get (resulta, delay); if (fresulta.ready ()) @@ -493,7 +488,7 @@ test_timeout (int n_iterations) // now we wait until we are done... fresulta.get (resulta); - ACE_DEBUG ((LM_DEBUG, " (%t) result %f\n", resulta)); + ACE_DEBUG ((LM_DEBUG, " (%t) result %u\n", (u_int) resulta)); worker_a->end (); // @@ Can we safely delete worker_a here? @@ -516,6 +511,8 @@ main (int, char *[]) #if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) template class ACE_Atomic_Op<ACE_Thread_Mutex, int>; +template class ACE_Future<const char *>; +template class ACE_Future<u_long>; #endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ #else diff --git a/tests/Future_Test.cpp b/tests/Future_Test.cpp index 55d72e908b9..1195afb287d 100644 --- a/tests/Future_Test.cpp +++ b/tests/Future_Test.cpp @@ -17,6 +17,7 @@ // // ============================================================================ +#include "ace/ACE.h" #include "ace/Task.h" #include "ace/Synch.h" #include "ace/Message_Queue.h" @@ -45,34 +46,6 @@ static ATOMIC_INT capsule_no (0); static ATOMIC_INT methodobject_count (0); static ATOMIC_INT methodobject_no (0); -// Function that can burn up noticeable CPU time: brute-force -// determination of whether number "n" is prime. Returns 0 if -// it is prime, or the smallest factor if it is not prime. -static -unsigned long -is_prime (const unsigned long n, - const unsigned long min_factor, - const unsigned long max_factor) -{ - if (n > 3) - { - for (unsigned long factor = min_factor; factor <= max_factor; ++factor) - { - if (n / factor * factor == n) - { - return factor; - } - } - - return 0; - } - else - { - return 0; - } -} - - class Scheduler : public ACE_Task<ACE_MT_SYNCH> // = TITLE // Active Object Scheduler. @@ -86,11 +59,11 @@ public: virtual int close (u_long flags = 0); virtual int svc (void); - ACE_Future<unsigned long> work (unsigned long param, int count = 1); + ACE_Future<u_long> work (u_long param, int count = 1); ACE_Future<const char*> name (void); void end (void); - unsigned long work_i (unsigned long, int); + u_long work_i (u_long, int); const char *name_i (void); private: @@ -104,21 +77,21 @@ class Method_Object_work : public ACE_Method_Object // Reification of the <work> method. { public: - Method_Object_work (Scheduler *, unsigned long, int, ACE_Future<unsigned long> &); + Method_Object_work (Scheduler *, u_long, int, ACE_Future<u_long> &); virtual ~Method_Object_work (void); virtual int call (void); private: Scheduler *scheduler_; - unsigned long param_; + u_long param_; int count_; - ACE_Future<unsigned long> future_result_; + ACE_Future<u_long> future_result_; }; Method_Object_work::Method_Object_work (Scheduler* new_Scheduler, - unsigned long new_param, + u_long new_param, int new_count, - ACE_Future<unsigned long> &new_result) + ACE_Future<u_long> &new_result) : scheduler_ (new_Scheduler), param_ (new_param), count_ (new_count), @@ -252,13 +225,13 @@ Scheduler::end (void) // Here's where the Work takes place. -unsigned long -Scheduler::work_i (unsigned long param, +u_long +Scheduler::work_i (u_long param, int count) { ACE_UNUSED_ARG (count); - return is_prime (param, 2, param / 2); + return ACE::is_prime (param, 2, param / 2); } const char * @@ -290,14 +263,14 @@ Scheduler::name (void) } } -ACE_Future<unsigned long> -Scheduler::work (unsigned long newparam, int newcount) +ACE_Future<u_long> +Scheduler::work (u_long newparam, int newcount) { if (this->scheduler_) { return this->scheduler_->work (newparam, newcount); } else { - ACE_Future<unsigned long> new_future; + ACE_Future<u_long> new_future; this->activation_queue_.enqueue (new Method_Object_work (this, newparam, newcount, new_future)); @@ -312,6 +285,8 @@ static int n_loops = 100; #if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) template class ACE_Atomic_Op<ACE_Thread_Mutex, int>; +template class ACE_Future<const char *>; +template class ACE_Future<u_long>; #endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ #endif /* ACE_HAS_THREADS */ @@ -341,7 +316,7 @@ main (int, char *[]) for (int i = 0; i < n_loops; i++) { { - ACE_Future<unsigned long> fresulta, fresultb, fresultc, fresultd, fresulte; + ACE_Future<u_long> fresulta, fresultb, fresultc, fresultd, fresulte; ACE_Future<const char*> fname; ACE_DEBUG ((LM_DEBUG, "(%t) going to do a non-blocking call\n")); @@ -372,7 +347,7 @@ main (int, char *[]) fresultd.cancel (40ul); } - unsigned long resulta = 0, resultb = 0, resultc = 0, resultd = 0, resulte = 0; + u_long resulta = 0, resultb = 0, resultc = 0, resultd = 0, resulte = 0; fresulta.get (resulta); fresultb.get (resultb); @@ -380,11 +355,11 @@ main (int, char *[]) fresultd.get (resultd); fresulte.get (resulte); - ACE_DEBUG ((LM_DEBUG, "(%t) result a %u\n", (unsigned int) resulte)); - ACE_DEBUG ((LM_DEBUG, "(%t) result b %u\n", (unsigned int) resulta)); - ACE_DEBUG ((LM_DEBUG, "(%t) result c %u\n", (unsigned int) resultb)); - ACE_DEBUG ((LM_DEBUG, "(%t) result d %u\n", (unsigned int) resultc)); - ACE_DEBUG ((LM_DEBUG, "(%t) result e %u\n", (unsigned int) resultd)); + ACE_DEBUG ((LM_DEBUG, "(%t) result a %u\n", (u_int) resulte)); + ACE_DEBUG ((LM_DEBUG, "(%t) result b %u\n", (u_int) resulta)); + ACE_DEBUG ((LM_DEBUG, "(%t) result c %u\n", (u_int) resultb)); + ACE_DEBUG ((LM_DEBUG, "(%t) result d %u\n", (u_int) resultc)); + ACE_DEBUG ((LM_DEBUG, "(%t) result e %u\n", (u_int) resultd)); const char *name; |