diff options
Diffstat (limited to 'ACE/examples/Threads')
30 files changed, 0 insertions, 5333 deletions
diff --git a/ACE/examples/Threads/Makefile.am b/ACE/examples/Threads/Makefile.am deleted file mode 100644 index d9bd5ba8fa5..00000000000 --- a/ACE/examples/Threads/Makefile.am +++ /dev/null @@ -1,430 +0,0 @@ -## Process this file with automake to create Makefile.in -## -## $Id$ -## -## This file was generated by MPC. Any changes made directly to -## this file will be lost the next time it is generated. -## -## MPC Command: -## ./bin/mwc.pl -type automake -noreldefs ACE.mwc - -ACE_BUILDDIR = $(top_builddir) -ACE_ROOT = $(top_srcdir) - - -## Makefile.Threads_Auto_Event.am - -noinst_PROGRAMS = auto_event - -auto_event_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -auto_event_SOURCES = \ - auto_event.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -auto_event_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Barrier1.am - -noinst_PROGRAMS += barrier1 - -barrier1_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -barrier1_SOURCES = \ - barrier1.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -barrier1_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Barrier2.am - -noinst_PROGRAMS += barrier2 - -barrier2_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -barrier2_SOURCES = \ - barrier2.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -barrier2_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Cancel.am - -noinst_PROGRAMS += cancel - -cancel_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -cancel_SOURCES = \ - cancel.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -cancel_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Future1.am - -if !BUILD_ACE_FOR_TAO - -noinst_PROGRAMS += future1 - -future1_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -future1_SOURCES = \ - future1.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -future1_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -endif !BUILD_ACE_FOR_TAO - -## Makefile.Threads_Future2.am - -if !BUILD_ACE_FOR_TAO - -noinst_PROGRAMS += future2 - -future2_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -future2_SOURCES = \ - future2.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -future2_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -endif !BUILD_ACE_FOR_TAO - -## Makefile.Threads_Manual_Event.am - -noinst_PROGRAMS += manual_event - -manual_event_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -manual_event_SOURCES = \ - manual_event.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -manual_event_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Process_Mutex.am - -if !BUILD_ACE_FOR_TAO - -noinst_PROGRAMS += process_mutex - -process_mutex_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -process_mutex_SOURCES = \ - process_mutex.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -process_mutex_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -endif !BUILD_ACE_FOR_TAO - -## Makefile.Threads_Process_Semaphore.am - -if !BUILD_ACE_FOR_TAO - -noinst_PROGRAMS += process_semaphore - -process_semaphore_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -process_semaphore_SOURCES = \ - process_semaphore.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -process_semaphore_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -endif !BUILD_ACE_FOR_TAO - -## Makefile.Threads_Reader_Writer.am - -noinst_PROGRAMS += reader_writer - -reader_writer_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -reader_writer_SOURCES = \ - reader_writer.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -reader_writer_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Recursive_Mutex.am - -noinst_PROGRAMS += recursive_mutex - -recursive_mutex_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -recursive_mutex_SOURCES = \ - recursive_mutex.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -recursive_mutex_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Task_Five.am - -noinst_PROGRAMS += task_five - -task_five_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -task_five_SOURCES = \ - task_five.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -task_five_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Task_Four.am - -noinst_PROGRAMS += task_four - -task_four_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -task_four_SOURCES = \ - task_four.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -task_four_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Task_One.am - -noinst_PROGRAMS += task_one - -task_one_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -task_one_SOURCES = \ - task_one.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -task_one_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Task_Three.am - -noinst_PROGRAMS += task_three - -task_three_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -task_three_SOURCES = \ - task_three.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -task_three_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Task_Two.am - -noinst_PROGRAMS += task_two - -task_two_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -task_two_SOURCES = \ - task_two.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -task_two_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Thread_Manager.am - -noinst_PROGRAMS += thread_manager - -thread_manager_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -thread_manager_SOURCES = \ - thread_manager.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -thread_manager_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Thread_Pool.am - -noinst_PROGRAMS += thread_pool - -thread_pool_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -thread_pool_SOURCES = \ - thread_pool.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -thread_pool_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Thread_Specific.am - -noinst_PROGRAMS += thread_specific - -thread_specific_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -thread_specific_SOURCES = \ - thread_specific.cpp \ - thread_specific.h - -thread_specific_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Token.am - -noinst_PROGRAMS += token - -token_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -token_SOURCES = \ - token.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -token_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Tss1.am - -noinst_PROGRAMS += tss1 - -tss1_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -tss1_SOURCES = \ - tss1.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -tss1_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Makefile.Threads_Tss2.am - -noinst_PROGRAMS += tss2 - -tss2_CPPFLAGS = \ - -I$(ACE_ROOT) \ - -I$(ACE_BUILDDIR) - -tss2_SOURCES = \ - tss2.cpp \ - TSS_Data.h \ - TSS_Obj.h \ - TSS_Task.h \ - thread_specific.h - -tss2_LDADD = \ - $(ACE_BUILDDIR)/ace/libACE.la - -## Clean up template repositories, etc. -clean-local: - -rm -f *~ *.bak *.rpo *.sym lib*.*_pure_* core core.* - -rm -f gcctemp.c gcctemp so_locations *.ics - -rm -rf cxx_repository ptrepository ti_files - -rm -rf templateregistry ir.out - -rm -rf ptrepository SunWS_cache Templates.DB diff --git a/ACE/examples/Threads/TSS_Data.h b/ACE/examples/Threads/TSS_Data.h deleted file mode 100644 index 3c9dfcfe28c..00000000000 --- a/ACE/examples/Threads/TSS_Data.h +++ /dev/null @@ -1,40 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// examples -// -// = FILENAME -// TSS_Data.cpp -// -// = AUTHOR -// Prashant Jain and Doug Schmidt -// -// ============================================================================ - -#include "ace/Singleton.h" - -#if !defined (ACE_LACKS_PRAGMA_ONCE) -# pragma once -#endif /* ACE_LACKS_PRAGMA_ONCE */ - -#include "ace/Synch_Traits.h" - -class TSS_Data - // = TITLE - // Data that is stored in thread-specific storage. -{ -public: - void *data (void) { return this->data_; } - void data (void *v) { this->data_ = v; } - -private: - // = data_ will be thread-specific data so it doesn't need a lock. - void *data_; -}; - -typedef ACE_TSS_Singleton<TSS_Data, ACE_SYNCH_MUTEX> TSS_DATA; - - - diff --git a/ACE/examples/Threads/TSS_Obj.h b/ACE/examples/Threads/TSS_Obj.h deleted file mode 100644 index 7dc4e469851..00000000000 --- a/ACE/examples/Threads/TSS_Obj.h +++ /dev/null @@ -1,39 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// tests -// -// = FILENAME -// TSS_Test.cpp -// -// = DESCRIPTION -// This program tests various features of ACE_Thread and the -// thread-specific storage variant of <ACE_SingletonEx>. -// -// = AUTHOR -// Prashant Jain and Doug Schmidt -// -// ============================================================================ - -#include "ace/Atomic_Op.h" -#include "ace/Synch_Traits.h" - -#if !defined (ACE_LACKS_PRAGMA_ONCE) -# pragma once -#endif /* ACE_LACKS_PRAGMA_ONCE */ - -class TSS_Obj - // = TITLE - // This object is stored in thread-specific storage. -{ -public: - TSS_Obj (void); - ~TSS_Obj (void); - -private: - - static ACE_Atomic_Op<ACE_SYNCH_MUTEX, int> count_; -}; - diff --git a/ACE/examples/Threads/TSS_Task.h b/ACE/examples/Threads/TSS_Task.h deleted file mode 100644 index 0612f0c46a9..00000000000 --- a/ACE/examples/Threads/TSS_Task.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -*- C++ -*- */ - -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// tests -// -// = FILENAME -// TSS_Task.h -// -// = AUTHOR -// Prashant Jain and Doug Schmidt -// -// ============================================================================ - -#include "ace/config-all.h" - -#if !defined (ACE_LACKS_PRAGMA_ONCE) -# pragma once -#endif /* ACE_LACKS_PRAGMA_ONCE */ - -#include "ace/Token.h" -#include "ace/Atomic_Op.h" - -class Test_Task -{ -public: - - Test_Task (void); - ~Test_Task (void); - - int open (void *arg); - - static void *svc (void *arg); - - static ACE_Atomic_Op<ACE_Token, int> wait_count_; - static ACE_Atomic_Op<ACE_Token, int> max_count_; - -private: - static ACE_Atomic_Op<ACE_Token, int> count_; -}; diff --git a/ACE/examples/Threads/Threads.mpc b/ACE/examples/Threads/Threads.mpc deleted file mode 100644 index 2c57feda243..00000000000 --- a/ACE/examples/Threads/Threads.mpc +++ /dev/null @@ -1,139 +0,0 @@ -// -*- MPC -*- -// $Id$ - -project(*auto_event) : aceexe { - exename = auto_event - Source_Files { - auto_event.cpp - } -} -project(*barrier1) : aceexe { - exename = barrier1 - Source_Files { - barrier1.cpp - } -} -project(*barrier2) : aceexe { - exename = barrier2 - Source_Files { - barrier2.cpp - } -} -project(*cancel) : aceexe { - exename = cancel - Source_Files { - cancel.cpp - } -} -project(*future1) : aceexe { - avoids += ace_for_tao - exename = future1 - Source_Files { - future1.cpp - } -} -project(*future2) : aceexe { - avoids += ace_for_tao - exename = future2 - Source_Files { - future2.cpp - } -} -project(*manual_event) : aceexe { - exename = manual_event - Source_Files { - manual_event.cpp - } -} -project(*process_mutex) : aceexe { - avoids += ace_for_tao - exename = process_mutex - Source_Files { - process_mutex.cpp - } -} -project(*process_semaphore) : aceexe { - avoids += ace_for_tao - exename = process_semaphore - Source_Files { - process_semaphore.cpp - } -} -project(*reader_writer) : aceexe { - exename = reader_writer - Source_Files { - reader_writer.cpp - } -} -project(*recursive_mutex) : aceexe { - exename = recursive_mutex - Source_Files { - recursive_mutex.cpp - } -} -project(*task_five) : aceexe { - exename = task_five - Source_Files { - task_five.cpp - } -} -project(*task_four) : aceexe { - exename = task_four - Source_Files { - task_four.cpp - } -} -project(*task_three) : aceexe { - exename = task_three - Source_Files { - task_three.cpp - } -} -project(*task_two) : aceexe { - exename = task_two - Source_Files { - task_two.cpp - } -} -project(*task_one) : aceexe { - exename = task_one - Source_Files { - task_one.cpp - } -} -project(*thread_manager) : aceexe { - exename = thread_manager - Source_Files { - thread_manager.cpp - } -} -project(*thread_pool) : aceexe { - exename = thread_pool - Source_Files { - thread_pool.cpp - } -} -project(*thread_specific) : aceexe { - exename = thread_specific - Source_Files { - thread_specific.cpp - } -} -project(*token) : aceexe { - exename = token - Source_Files { - token.cpp - } -} -project(*tss1) : aceexe { - exename = tss1 - Source_Files { - tss1.cpp - } -} -project(*tss2) : aceexe { - exename = tss2 - Source_Files { - tss2.cpp - } -} diff --git a/ACE/examples/Threads/auto_event.cpp b/ACE/examples/Threads/auto_event.cpp deleted file mode 100644 index e568ffe84d1..00000000000 --- a/ACE/examples/Threads/auto_event.cpp +++ /dev/null @@ -1,120 +0,0 @@ -// $Id$ - -// This test shows the use of an ACE_Auto_Event as a signaling -// mechanism. Two threads are created (one a reader, the other a -// writer). The reader waits till the writer has completed -// calculations. Upon waking up the reader prints the data calculated -// by the writer. The writer thread calculates the value and signals -// the reader when the calculation completes. - -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Auto_Event.h" -#include "ace/Singleton.h" -#include "ace/Thread_Manager.h" - -ACE_RCSID(Threads, auto_event, "$Id$") - -#if defined (ACE_HAS_THREADS) -// Shared event between reader and writer. The ACE_Thread_Mutex is -// necessary to make sure that only one ACE_Auto_Event is created. -// The default constructor for ACE_Auto_Event sets it initially into -// the non-signaled state. - -typedef ACE_Singleton <ACE_Auto_Event, ACE_Thread_Mutex> EVENT; - -// work time for writer -static int work_time; - -// Reader thread. -static void * -reader (void *arg) -{ - // Shared data via a reference. - int& data = *(int *) arg; - - // Wait for writer to complete. - - ACE_DEBUG ((LM_DEBUG, "(%t) reader: waiting...... \n")); - - if (EVENT::instance ()->wait () == -1) - { - ACE_ERROR ((LM_ERROR, "thread wait failed")); - ACE_OS::exit (0); - } - - // Read shared data. - ACE_DEBUG ((LM_DEBUG, "(%t) reader: value of data is: %d \n", data)); - - return 0; -} - -// Writer thread. -static void * -writer (void *arg) -{ - int& data = *(int *) arg; - - // Calculate (work). - ACE_DEBUG ((LM_DEBUG, "(%t) writer: working for %d secs\n", work_time)); - ACE_OS::sleep (work_time); - - // Write shared data. - data = 42; - - // Wake up reader. - ACE_DEBUG ((LM_DEBUG, "(%t) writer: calculation complete, waking reader\n")); - - if (EVENT::instance ()->signal () == -1) - { - ACE_ERROR ((LM_ERROR, "thread signal failed")); - ACE_OS::exit (0); - } - - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR **argv) -{ - // Shared data: set by writer, read by reader. - int data; - - // Work time for writer. - work_time = argc == 2 ? ACE_OS::atoi (argv[1]) : 5; - - // threads manager - ACE_Thread_Manager& tm = *ACE_Thread_Manager::instance (); - - // Create reader thread. - if (tm.spawn ((ACE_THR_FUNC) reader, (void *) &data) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "thread create for reader failed"), -1); - - // Create writer thread. - if (tm.spawn ((ACE_THR_FUNC) writer, (void *) &data) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "thread create for writer failed"), -1); - - // Wait for both. - if (tm.wait () == -1) - ACE_ERROR_RETURN ((LM_ERROR, "thread wait failed"), -1); - else - ACE_DEBUG ((LM_ERROR, "graceful exit\n")); - - return 0; -} - -#if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION) -template ACE_Singleton<ACE_Auto_Event, ACE_Thread_Mutex> * - ACE_Singleton<ACE_Auto_Event, ACE_Thread_Mutex>::singleton_; -#endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */ - - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/barrier1.cpp b/ACE/examples/Threads/barrier1.cpp deleted file mode 100644 index 4d3762b1eb6..00000000000 --- a/ACE/examples/Threads/barrier1.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// $Id$ - -// This test program illustrates how the ACE barrier synchronization -// mechanisms work. - -#include "ace/OS_main.h" -#include "ace/Barrier.h" -#include "ace/Thread_Manager.h" -#include "ace/Service_Config.h" - -ACE_RCSID(Threads, barrier1, "$Id$") - -#if defined (ACE_HAS_THREADS) - -struct Tester_Args - // = TITLE - // These arguments are passed into each test thread. -{ - Tester_Args (ACE_Barrier &tb, int i) - : tester_barrier_ (tb), - n_iterations_ (i) {} - - ACE_Barrier &tester_barrier_; - // Reference to the tester barrier. This controls each miteration of - // the tester function running in every thread. - - int n_iterations_; - // Number of iterations to run. -}; - -// Iterate <n_iterations> time printing off a message and "waiting" -// for all other threads to complete this iteration. - -static void * -tester (Tester_Args *args) -{ - for (int iterations = 1; - iterations <= args->n_iterations_; - iterations++) - { - ACE_DEBUG ((LM_DEBUG, "(%t) in iteration %d\n", iterations)); - - // Block until all other threads have waited, then continue. - args->tester_barrier_.wait (); - } - - return 0; -} - -// Default number of threads to spawn. -static const int DEFAULT_ITERATIONS = 5; - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - ACE_Service_Config daemon (argv[0]); - - int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_THREADS; - int n_iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_ITERATIONS; - - ACE_Barrier tester_barrier (n_threads); - - Tester_Args args (tester_barrier, n_iterations); - - if (ACE_Thread_Manager::instance ()->spawn_n - (int(n_threads), ACE_THR_FUNC (tester), - (void *) &args, THR_NEW_LWP | THR_DETACHED) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn_n"), 1); - - // Wait for all the threads to reach their exit point. - ACE_Thread_Manager::instance ()->wait (); - - ACE_DEBUG ((LM_DEBUG, "(%t) done\n")); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/barrier2.cpp b/ACE/examples/Threads/barrier2.cpp deleted file mode 100644 index 3264df56c11..00000000000 --- a/ACE/examples/Threads/barrier2.cpp +++ /dev/null @@ -1,319 +0,0 @@ -// $Id$ - -// This test program illustrates how the ACE task workers/barrier -// synchronization mechanisms work in conjunction with the ACE_Task -// and the ACE_Thread_Manager. The manual flag not set simulates user -// input, if set input comes from stdin until RETURN only is entered -// which stops all workers via a message block of length 0. This is an -// alernative shutdown of workers compared to queue deactivate. The -// delay_put flag simulates a delay between the shutdown puts. All -// should work with this flag disabled! The BARRIER_TYPE is supposed -// to enable/disable barrier sync on each svc a worker has done. - -#include "ace/OS_NS_string.h" -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Task.h" -#include "ace/Service_Config.h" - -ACE_RCSID(Threads, barrier2, "$Id$") - -#if defined (ACE_HAS_THREADS) - -#include "ace/Null_Barrier.h" -#define BARRIER_TYPE ACE_Null_Barrier - -template <class BARRIER> -class Worker_Task : public ACE_Task<ACE_MT_SYNCH> -{ -public: - Worker_Task (ACE_Thread_Manager *thr_mgr, - int n_threads, - int inp_serialize = 1); - - virtual int producer (void); - // produce input for workers - - virtual int input (ACE_Message_Block *mb); - // Fill one message block via a certain input strategy. - - virtual int output (ACE_Message_Block *mb); - // Forward one message block via a certain output strategy to the - // next task if any. - - virtual int service (ACE_Message_Block *mb, int iter); - // Perform one message block dependant service. - -private: - virtual int put (ACE_Message_Block *mb, ACE_Time_Value *tv=0); - - virtual int svc (void); - // Iterate <n_iterations> time printing off a message and "waiting" - // for all other threads to complete this iteration. - - //FUZZ: disable check_for_lack_ACE_OS - // = Not needed for this test. - virtual int open (void *) { return 0; } - virtual int close (u_long) - { - //FUZZ: enable check_for_lack_ACE_OS - - ACE_DEBUG ((LM_DEBUG, - "(%t) in close of worker\n")); - return 0; - } - - int nt_; - // Number of worker threads to run. - - int inp_serialize_; - - BARRIER barrier_; -}; - -template <class BARRIER> -Worker_Task<BARRIER>::Worker_Task (ACE_Thread_Manager *thr_mgr, - int n_threads, - int inp_serialize) - : ACE_Task<ACE_MT_SYNCH> (thr_mgr), - barrier_ (n_threads) -{ - nt_ = n_threads; - - // Create worker threads. - inp_serialize_ = inp_serialize; - - // Use the task's message queue for serialization (default) or run - // service in the context of the caller thread. - - if (nt_ > 0 && inp_serialize == 1) - if (this->activate (THR_NEW_LWP, n_threads) == -1) - ACE_ERROR ((LM_ERROR, - "%p\n", - "activate failed")); -} - -// Simply enqueue the Message_Block into the end of the queue. - -template <class BARRIER> int -Worker_Task<BARRIER>::put (ACE_Message_Block *mb, - ACE_Time_Value *tv) -{ - int result; - - if (this->inp_serialize_) - result = this->putq (mb, tv); - else - { - static int iter = 0; - result = this->service (mb, iter++); - - if (this->output (mb) < 0) - ACE_DEBUG ((LM_DEBUG, - "(%t) output not connected!\n")); - - mb->release (); - } - return result; -} - -template <class BARRIER> int -Worker_Task<BARRIER>::service (ACE_Message_Block *mb, - int iter) -{ - size_t length = mb->length (); - - if (length > 0) - { - ACE_DEBUG ((LM_DEBUG, - "(%t) in iteration %d len=%d text got:\n", - iter, - length)); - ACE_OS::write (ACE_STDOUT, - mb->rd_ptr (), - length); - ACE_DEBUG ((LM_DEBUG, - "\n")); - } - return 0; -} - -// Iterate <n_iterations> time printing off a message and "waiting" -// for all other threads to complete this iteration. - -template <class BARRIER> int -Worker_Task<BARRIER>::svc (void) -{ - // Note that the <ACE_Task::svc_run> method automatically adds us to - // the Thread_Manager when the thread begins. - - // Keep looping, reading a message out of the queue, until we get a - // message with a length == 0, which signals us to quit. - - for (int iter = 1; ;iter++) - { - ACE_Message_Block *mb = 0; - - int result = this->getq (mb); - - if (result == -1) - { - ACE_ERROR ((LM_ERROR, - "(%t) in iteration %d\n", - "error waiting for message in iteration", - iter)); - break; - } - - size_t length = mb->length (); - this->service (mb,iter); - - if (length == 0) - { - ACE_DEBUG ((LM_DEBUG, - "(%t) in iteration %d got quit, exit!\n", - iter)); - mb->release (); - break; - } - - this->barrier_.wait (); - this->output (mb); - - mb->release (); - } - - // Note that the <ACE_Task::svc_run> method automatically removes us - // from the Thread_Manager when the thread exits. - - return 0; -} - -template <class BARRIER> int -Worker_Task<BARRIER>::producer (void) -{ - // Keep reading stdin, until we reach EOF. - - for (;;) - { - // Allocate a new message. - ACE_Message_Block *mb; - - ACE_NEW_RETURN (mb, - ACE_Message_Block (BUFSIZ), - -1); - - if (this->input (mb) == -1) - return -1; - } - - ACE_NOTREACHED (return 0); -} - -template <class BARRIER> int -Worker_Task<BARRIER>::output (ACE_Message_Block *mb) -{ - return this->put_next (mb); -} - -template <class BARRIER> int -Worker_Task<BARRIER>::input (ACE_Message_Block *mb) -{ - ACE_Message_Block *mb1; - -#if !defined (manual) - static int l = 0; - char str[] = "kalle"; - ACE_OS::strcpy (mb->rd_ptr (), str); - - size_t n = ACE_OS::strlen (str); - - if (l == 1000) - n = 1; - l++; - - if (l == 0 || (l % 100 == 0)) - ACE_OS::sleep (5); - if (n <= 1) -#else - ACE_DEBUG ((LM_DEBUG, - "(%t) press chars and enter to put a new message into task queue ...\n")); - n = ACE_OS::read (ACE_STDIN, - mb->rd_ptr (), - mb->size ()); - if (n <= 1) -#endif /* manual */ - { - // Send a shutdown message to the waiting threads and exit. - // cout << "\nvor loop, dump of task msg queue:\n" << endl; - // this->msg_queue ()->dump (); - - for (int i = 0; i < nt_; i++) - { - ACE_DEBUG ((LM_DEBUG, - "(%t) eof, sending block for thread=%d\n", - i + 1)); - - ACE_NEW_RETURN (mb1, - ACE_Message_Block (2), - -1); - mb1->length (0); - - if (this->put (mb1) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "put")); -#if defined (delay_put) - // this sleep helps to shutdown correctly -> was an error! - ACE_OS::sleep (1); -#endif /* delay_put */ - } - return -1; - } - else - { - // Send a normal message to the waiting threads and continue - // producing. - mb->wr_ptr (n); - - if (this->put (mb) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "put")); - } - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_THREADS; - - ACE_DEBUG ((LM_DEBUG, - "(%t) worker threads running=%d\n", - n_threads)); - - Worker_Task<BARRIER_TYPE> worker_task (ACE_Thread_Manager::instance (), - /* n_threads */ 0, - 0); - worker_task.producer (); - - // Wait for all the threads to reach their exit point. - ACE_DEBUG ((LM_DEBUG, - "(%t) waiting with thread manager ...\n")); - - ACE_Thread_Manager::instance ()->wait (); - - ACE_DEBUG ((LM_DEBUG, - "(%t) done correct!\n")); - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/cancel.cpp b/ACE/examples/Threads/cancel.cpp deleted file mode 100644 index f525ea7ea8e..00000000000 --- a/ACE/examples/Threads/cancel.cpp +++ /dev/null @@ -1,77 +0,0 @@ -// $Id$ - -// Test out the cooperative thread cancellation mechanisms provided by -// the ACE_Thread_Manager. - -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Thread_Manager.h" - -ACE_RCSID(Threads, cancel, "$Id$") - -#if defined (ACE_HAS_THREADS) - -static void * -worker (intptr_t iterations) -{ - for (intptr_t i = 0; i < iterations; i++) - { - if ((i % 10) == 0 - && (ACE_Thread_Manager::instance ()->testcancel (ACE_Thread::self ()) != 0)) - { - ACE_DEBUG ((LM_DEBUG, "(%t) has been cancelled before iteration!\n", i)); - break; - } - } - - return 0; -} - -static const int DEFAULT_THREADS = ACE_DEFAULT_THREADS; -static const intptr_t DEFAULT_ITERATIONS = 100000; - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - ACE_Service_Config daemon; - - daemon.open (argv[0]); - - int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : DEFAULT_THREADS; - intptr_t n_iterations = - argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_ITERATIONS; - - ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance (); - - int grp_id = thr_mgr->spawn_n (n_threads, ACE_THR_FUNC (worker), - (void *) n_iterations, - THR_NEW_LWP | THR_DETACHED); - - // Wait for 2 seconds and then suspend every thread in the group. - ACE_OS::sleep (2); - thr_mgr->suspend_grp (grp_id); - - // Wait for 2 more seconds and then resume every thread in the - // group. - ACE_OS::sleep (ACE_Time_Value (2)); - thr_mgr->resume_grp (grp_id); - - // Wait for 2 more seconds and then send a SIGINT to every thread in - // the group. - ACE_OS::sleep (ACE_Time_Value (2)); - thr_mgr->kill_grp (grp_id, SIGINT); - - // Wait for 2 more seconds and then exit (which should kill all the - // threads)! - ACE_OS::sleep (ACE_Time_Value (2)); - - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR_RETURN ((LM_ERROR, "threads not supported on this platform\n"), -1); -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/future1.cpp b/ACE/examples/Threads/future1.cpp deleted file mode 100644 index a73549fd36c..00000000000 --- a/ACE/examples/Threads/future1.cpp +++ /dev/null @@ -1,407 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// tests -// -// = FILENAME -// Test_Future.cpp -// -// = DESCRIPTION -// This example tests the ACE Future. -// -// = AUTHOR -// Andres Kruse <Andres.Kruse@cern.ch> and Douglas C. Schmidt -// <schmidt@cs.wustl.edu> -// -// ============================================================================ - -#include "ace/OS_NS_string.h" -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/ACE.h" -#include "ace/Task.h" -#include "ace/Thread_Mutex.h" -#include "ace/Message_Queue.h" -#include "ace/Future.h" -#include "ace/Method_Request.h" -#include "ace/Activation_Queue.h" -#include "ace/Auto_Ptr.h" -#include "ace/Atomic_Op.h" - -ACE_RCSID(Threads, future1, "$Id$") - -#if defined (ACE_HAS_THREADS) - -typedef ACE_Atomic_Op<ACE_Thread_Mutex, int> ATOMIC_INT; - -// a counter for the tasks.. -static ATOMIC_INT task_count (0); - -// a counter for the futures.. -static ATOMIC_INT future_count (0); -static ATOMIC_INT future_no (0); - -// a counter for the capsules.. -static ATOMIC_INT capsule_count (0); -static ATOMIC_INT capsule_no (0); - -// a counter for the method objects... -static ATOMIC_INT methodobject_count (0); -static ATOMIC_INT methodobject_no (0); - -class Scheduler : public ACE_Task_Base - // = TITLE - // Active Object Scheduler. -{ - friend class Method_RequestWork; -public: - Scheduler (const char *, Scheduler * = 0); - virtual ~Scheduler (void); - - //FUZZ: disable check_for_lack_ACE_OS - virtual int open (void *args = 0); - virtual int close (u_long flags = 0); - //FUZZ: enable check_for_lack_ACE_OS - - virtual int svc (void); - - ACE_Future<u_long> work (u_long param, int count = 1); - ACE_Future<const char*> name (void); - void end (void); - - u_long work_i (u_long, int); - const char *name_i (void); - -private: - char *name_; - ACE_Activation_Queue activation_queue_; - Scheduler *scheduler_; -}; - -class Method_Request_work : public ACE_Method_Request - // = TITLE - // Reification of the <work> method. -{ -public: - Method_Request_work (Scheduler *, u_long, int, ACE_Future<u_long> &); - virtual ~Method_Request_work (void); - virtual int call (void); - -private: - Scheduler *scheduler_; - u_long param_; - int count_; - ACE_Future<u_long> future_result_; -}; - -Method_Request_work::Method_Request_work (Scheduler* new_Scheduler, - u_long new_param, - int new_count, - ACE_Future<u_long> &new_result) - : scheduler_ (new_Scheduler), - param_ (new_param), - count_ (new_count), - future_result_ (new_result) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) Method_Request_work created\n")); -} - -Method_Request_work::~Method_Request_work (void) -{ - ACE_DEBUG ((LM_DEBUG, "(%t) Method_Request_work will be deleted.\n")); -} - - -int -Method_Request_work::call (void) -{ - return this->future_result_.set (this->scheduler_->work_i (this->param_, this->count_)); -} - -class Method_Request_name : public ACE_Method_Request - // = TITLE - // Reification of the <name> method. -{ -public: - Method_Request_name (Scheduler *, ACE_Future<const char*> &); - virtual ~Method_Request_name (void); - virtual int call (void); - -private: - Scheduler *scheduler_; - ACE_Future<const char *> future_result_; -}; - -Method_Request_name::Method_Request_name (Scheduler *new_scheduler, - ACE_Future<const char *> &new_result) - : scheduler_ (new_scheduler), - future_result_ (new_result) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) Method_Request_name created\n")); -} - -Method_Request_name::~Method_Request_name (void) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) Method_Request_name will be deleted.\n")); -} - -int -Method_Request_name::call (void) -{ - return future_result_.set (scheduler_->name_i ()); -} - -class Method_Request_end : public ACE_Method_Request - // = TITLE - // Reification of the <end> method. -{ -public: - Method_Request_end (Scheduler *new_scheduler): scheduler_ (new_scheduler) {} - virtual ~Method_Request_end (void) {} - virtual int call (void) { return -1; } - -private: - Scheduler *scheduler_; - // Keep track of our scheduler. -}; - -// Constructor. -Scheduler::Scheduler (const char *newname, - Scheduler *new_scheduler) -{ - ACE_NEW (this->name_, char[ACE_OS::strlen (newname) + 1]); - ACE_OS::strcpy (this->name_, newname); - this->scheduler_ = new_scheduler; - ACE_DEBUG ((LM_DEBUG, "(%t) Scheduler %s created\n", this->name_)); -} - -// Destructor -Scheduler::~Scheduler (void) -{ - ACE_DEBUG ((LM_DEBUG, "(%t) Scheduler %s will be destroyed\n", this->name_)); - delete [] this->name_; -} - -// open -int -Scheduler::open (void *) -{ - task_count++; - ACE_DEBUG ((LM_DEBUG, "(%t) Scheduler %s open\n", this->name_)); - return this->activate (THR_BOUND); -} - -// close -int -Scheduler::close (u_long) -{ - ACE_DEBUG ((LM_DEBUG, "(%t) Scheduler %s close\n", this->name_)); - task_count--; - return 0; -} - -// service.. -int -Scheduler::svc (void) -{ - for (;;) - { - // Dequeue the next method object (we use an auto pointer in - // case an exception is thrown in the <call>). - auto_ptr<ACE_Method_Request> mo (this->activation_queue_.dequeue ()); - - ACE_DEBUG ((LM_DEBUG, "(%t) calling method object\n")); - // Call it. - if (mo->call () == -1) - break; - // Destructor automatically deletes it. - } - - /* NOTREACHED */ - return 0; -} - -void -Scheduler::end (void) -{ - this->activation_queue_.enqueue (new Method_Request_end (this)); -} - - -// Here's where the Work takes place. -u_long -Scheduler::work_i (u_long param, - int count) -{ - ACE_UNUSED_ARG (count); - - return ACE::is_prime (param, 2, param / 2); -} - -const char * -Scheduler::name_i (void) -{ - char *the_name; - - ACE_NEW_RETURN (the_name, char[ACE_OS::strlen (this->name_) + 1], 0); - ACE_OS::strcpy (the_name, this->name_); - - return the_name; -} - -ACE_Future<const char *> -Scheduler::name (void) -{ - if (this->scheduler_) - // Delegate to the Scheduler. - return this->scheduler_->name (); - else - { - ACE_Future<const char*> new_future; - - // @@ What happens if new fails here? - this->activation_queue_.enqueue - (new Method_Request_name (this, new_future)); - - return new_future; - } -} - -ACE_Future<u_long> -Scheduler::work (u_long newparam, - int newcount) -{ - if (this->scheduler_) { - return this->scheduler_->work (newparam, newcount); - } - else { - ACE_Future<u_long> new_future; - - this->activation_queue_.enqueue - (new Method_Request_work (this, newparam, newcount, new_future)); - return new_future; - } -} - -// @@ These values should be set by the command line options! - -// Total number of loops. -static size_t n_loops = 100; - -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - Scheduler *andres, *peter, *helmut, *matias; - - // Create active objects.. - // @@ Should "open" be subsumed within the constructor of - // Scheduler()? - ACE_NEW_RETURN (andres, Scheduler ("andres"), -1); - andres->open (); - ACE_NEW_RETURN (peter, Scheduler ("peter"), -1); - peter->open (); - ACE_NEW_RETURN (helmut, Scheduler ("helmut"), -1); - helmut->open (); - - // Matias passes all asynchronous method calls on to Andres... - ACE_NEW_RETURN (matias, Scheduler ("matias", andres), -1); - matias->open (); - - for (size_t i = 0; i < n_loops; i++) - { - { - 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 (9013); - fresultb = peter->work (9013); - fresultc = helmut->work (9013); - fresultd = matias->work (9013); - fname = andres->name (); - - // see if the result is available... - if (fresulta.ready ()) - ACE_DEBUG ((LM_DEBUG, "(%t) wow.. work is ready.....\n")); - - ACE_DEBUG ((LM_DEBUG, "(%t) non-blocking call done... now blocking...\n")); - - // Save the result of fresulta. - - fresulte = fresulta; - - if (i % 3 == 0) - { - // Every 3rd time... disconnect the futures... - // but "fresulte" should still contain the result... - fresulta.cancel (10); - fresultb.cancel (20); - fresultc.cancel (30); - fresultd.cancel (40); - } - - u_long resulta = 0, resultb = 0, resultc = 0, resultd = 0, resulte = 0; - - fresulta.get (resulta); - fresultb.get (resultb); - fresultc.get (resultc); - fresultd.get (resultd); - fresulte.get (resulte); - - 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 = 0; - - fname.get (name); - - ACE_DEBUG ((LM_DEBUG, "(%t) name %s\n", name)); - delete [] (char *) name; - } - - ACE_DEBUG ((LM_DEBUG, - "(%t) task_count %d future_count %d capsule_count %d methodobject_count %d\n", - task_count.value (), - future_count.value (), - capsule_count.value (), - methodobject_count.value ())); - } - - // Close things down. - andres->end (); - peter->end (); - helmut->end (); - matias->end (); - - ACE_OS::sleep (2); - - ACE_DEBUG ((LM_DEBUG, - "(%t) task_count %d future_count %d capsule_count %d methodobject_count %d\n", - task_count.value (), - future_count.value (), - capsule_count.value (), - methodobject_count.value ())); - - ACE_DEBUG ((LM_DEBUG,"(%t) th' that's all folks!\n")); - - ACE_OS::sleep (5); - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/future2.cpp b/ACE/examples/Threads/future2.cpp deleted file mode 100644 index b907e58ddc7..00000000000 --- a/ACE/examples/Threads/future2.cpp +++ /dev/null @@ -1,529 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// tests -// -// = FILENAME -// Test_Future.cpp -// -// = DESCRIPTION -// This example tests the ACE Future. -// -// = AUTHOR -// Andres Kruse <Andres.Kruse@cern.ch> and Douglas C. Schmidt -// <schmidt@cs.wustl.edu> -// -// Modification History -// Aug. 96; A.Kruse; dev. -// Aug. 96; D.Schmidt; complete workover -// 08/27/96; A.Kruse; - the friends of Scheduler are "Method_Request_name" -// and "Method_Request_work". -// - make the methods "work_i" and "name_i" private -// 09/2/96; D.Schmidt; Integrate with new ACE_Future API and rearrange -// the tests so they are more modular. -// ============================================================================ - -#include "ace/OS_NS_string.h" -#include "ace/OS_NS_sys_time.h" -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/ACE.h" -#include "ace/Task.h" -#include "ace/Message_Queue.h" -#include "ace/Future.h" -#include "ace/Method_Request.h" -#include "ace/Activation_Queue.h" -#include "ace/Auto_Ptr.h" -#include "ace/Atomic_Op.h" - -ACE_RCSID(Threads, future2, "$Id$") -#if defined (ACE_HAS_THREADS) - -typedef ACE_Atomic_Op<ACE_Thread_Mutex, int> ATOMIC_INT; - -// a counter for the tasks.. -static ATOMIC_INT scheduler_open_count (0); - -// forward declarations -class Method_Request_work; -class Method_Request_name; - -class Scheduler : public ACE_Task_Base - // = TITLE - // Active Object Scheduler. -{ - // Every method object has to be able to access the private methods. - - friend class Method_Request_work; - friend class Method_Request_name; - friend class Method_Request_end; -public: - - Scheduler (const char *, Scheduler * = 0); - virtual ~Scheduler (void); - - //FUZZ: disable check_for_lack_ACE_OS - virtual int open (void *args = 0); - // The method that is used to start the active object. - //FUZZ: enable check_for_lack_ACE_OS - - // = Here are the methods exported by the class. They return an - // <ACE_Future>. - ACE_Future<u_long> work (u_long param, int count = 1); - ACE_Future<char*> name (void); - void end (void); - -private: - //FUZZ: disable check_for_lack_ACE_OS - virtual int close (u_long flags = 0); - // Should not be accessible from outside... (use end () instead). - //FUZZ: enable check_for_lack_ACE_OS - - virtual int svc (void); - // Here the actual servicing of all requests is happening.. - - // = Implementation methods. - u_long work_i (u_long, int); - char *name_i (void); - - char *name_; - ACE_Activation_Queue activation_queue_; - Scheduler *scheduler_; -}; - -class Method_Request_work : public ACE_Method_Request - // = TITLE - // Reification of the <work> method. -{ -public: - Method_Request_work (Scheduler *, u_long, int, ACE_Future<u_long> &); - virtual ~Method_Request_work (void); - virtual int call (void); - -private: - Scheduler *scheduler_; - u_long param_; - int count_; - ACE_Future<u_long> future_result_; -}; - -Method_Request_work::Method_Request_work (Scheduler* new_Scheduler, - u_long new_param, - int new_count, - ACE_Future<u_long> &new_result) - : scheduler_ (new_Scheduler), - param_ (new_param), - count_ (new_count), - future_result_ (new_result) -{ -} - -Method_Request_work::~Method_Request_work (void) -{ -} - -int -Method_Request_work::call (void) -{ - return this->future_result_.set (this->scheduler_->work_i (this->param_, this->count_)); -} - -class Method_Request_name : public ACE_Method_Request - // = TITLE - // Reification of the <name> method. -{ -public: - Method_Request_name (Scheduler *, ACE_Future<char*> &); - virtual ~Method_Request_name (void); - virtual int call (void); - -private: - Scheduler *scheduler_; - ACE_Future<char*> future_result_; -}; - - -Method_Request_name::Method_Request_name (Scheduler *new_scheduler, - ACE_Future<char*> &new_result) - : scheduler_ (new_scheduler), - future_result_ (new_result) -{ - ACE_DEBUG ((LM_DEBUG, - " (%t) Method_Request_name created\n")); -} - -Method_Request_name::~Method_Request_name (void) -{ - ACE_DEBUG ((LM_DEBUG, - " (%t) Method_Request_name will be deleted.\n")); -} - -int -Method_Request_name::call (void) -{ - return future_result_.set (scheduler_->name_i ()); -} - -class Method_Request_end : public ACE_Method_Request - // = TITLE - // Reification of the <end> method. -{ -public: - Method_Request_end (Scheduler *new_Scheduler): scheduler_ (new_Scheduler) {} - virtual ~Method_Request_end (void) {} - virtual int call (void) { return -1; } - -private: - Scheduler *scheduler_; - // Keep track of our scheduler. -}; - -// constructor -Scheduler::Scheduler (const char *newname, Scheduler *new_Scheduler) -{ - ACE_NEW (this->name_, char[ACE_OS::strlen (newname) + 1]); - ACE_OS::strcpy ((char *) this->name_, newname); - this->scheduler_ = new_Scheduler; - ACE_DEBUG ((LM_DEBUG, " (%t) Scheduler %s created\n", this->name_)); -} - -// Destructor -Scheduler::~Scheduler (void) -{ - ACE_DEBUG ((LM_DEBUG, " (%t) Scheduler %s will be destroyed\n", this->name_)); -} - -int -Scheduler::open (void *) -{ - scheduler_open_count++; - ACE_DEBUG ((LM_DEBUG, " (%t) Scheduler %s open\n", this->name_)); - return this->activate (THR_BOUND); -} - -int -Scheduler::close (u_long) -{ - ACE_DEBUG ((LM_DEBUG, " (%t) Scheduler %s close\n", this->name_)); - scheduler_open_count--; - return 0; -} - -int -Scheduler::svc (void) -{ - // Main event loop for this active object. - for (;;) - { - // Dequeue the next method object (we use an auto pointer in - // case an exception is thrown in the <call>). - auto_ptr<ACE_Method_Request> mo (this->activation_queue_.dequeue ()); - - ACE_DEBUG ((LM_DEBUG, " (%t) calling method object\n")); - // Call it. - if (mo->call () == -1) - break; - // Smart pointer destructor automatically deletes mo. - } - - /* NOTREACHED */ - return 0; -} - -void -Scheduler::end (void) -{ - this->activation_queue_.enqueue (new Method_Request_end (this)); -} - -// Here's where the Work takes place. -u_long -Scheduler::work_i (u_long param, - int count) -{ - ACE_UNUSED_ARG (count); - - return ACE::is_prime (param, 2, param / 2); -} - -char * -Scheduler::name_i (void) -{ - char *the_name; - - ACE_NEW_RETURN (the_name, char[ACE_OS::strlen (this->name_) + 1], 0); - ACE_OS::strcpy (the_name, this->name_); - - return the_name; -} - -ACE_Future<char *> -Scheduler::name (void) -{ - if (this->scheduler_) - // Delegate to the other scheduler - return this->scheduler_->name (); - else - { - ACE_Future<char*> new_future; - - if (this->thr_count () == 0) - { - // This scheduler is inactive... so we execute the user - // request right away... - - auto_ptr<ACE_Method_Request> mo (new Method_Request_name (this, new_future)); - - mo->call (); - // Smart pointer destructor automatically deletes mo. - } - else - // @@ What happens if new fails here? - this->activation_queue_.enqueue - (new Method_Request_name (this, new_future)); - - return new_future; - } -} - -ACE_Future<u_long> -Scheduler::work (u_long newparam, int newcount) -{ - if (this->scheduler_) - return this->scheduler_->work (newparam, newcount); - else - { - ACE_Future<u_long> new_future; - - if (this->thr_count () == 0) - { - auto_ptr<ACE_Method_Request> mo - (new Method_Request_work (this, newparam, newcount, new_future)); - mo->call (); - // Smart pointer destructor automatically deletes it. - } - else - this->activation_queue_.enqueue - (new Method_Request_work (this, newparam, newcount, new_future)); - - return new_future; - } -} - -static int -determine_iterations (void) -{ - int n_iterations; - - ACE_DEBUG ((LM_DEBUG," (%t) determining the number of iterations...\n")); - Scheduler *worker_a; - - ACE_NEW_RETURN (worker_a, Scheduler ("worker A"), -1); - - ACE_Time_Value tstart (ACE_OS::gettimeofday ()); - ACE_Time_Value tend (ACE_OS::gettimeofday ()); - - // Determine the number of iterations... we want so many that the - // work () takes about 1 second... - - for (n_iterations = 1; - (tend.sec () - tstart.sec ()) < 1; - n_iterations *= 2) - { - tstart = ACE_OS::gettimeofday (); - - worker_a->work (9013, n_iterations); - - tend = ACE_OS::gettimeofday (); - } - - ACE_DEBUG ((LM_DEBUG," (%t) n_iterations %d\n", - (int) n_iterations)); - - worker_a->end (); - // @@ Can we safely delete worker_a here? - return n_iterations; -} - -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. - - Scheduler *worker_a; - Scheduler *worker_b; - Scheduler *worker_c; - - ACE_NEW (worker_a, Scheduler ("worker A")); - ACE_NEW (worker_b, Scheduler ("worker B")); - // Have worker_c delegate his work to worker_a. - ACE_NEW (worker_c, Scheduler ("worker C", worker_a)); - - // loop 0: - // test the Schedulers when they are not active. - // now the method objects will be created but since - // there is no active thread they will also be - // immediately executed, in the "main" thread. - // loop 1: - // do the same test but with the schedulers - // activated - for (int i = 0; i < 2; i++) - { - if (i == 1) - { - worker_a->open (); - worker_b->open (); - worker_c->open (); - } - - 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) - { - if (!fresulta.ready ()) - ACE_DEBUG ((LM_DEBUG," (%t) ERROR: worker A is should be ready!!!\n")); - if (!fresultb.ready ()) - ACE_DEBUG ((LM_DEBUG," (%t) ERROR: worker B is should be ready!!!\n")); - if (!fresultc.ready ()) - ACE_DEBUG ((LM_DEBUG," (%t) ERROR: worker C is should be ready!!!\n")); - } - - // When the workers are active we will block here until the - // results are available. - - 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 (); - ACE_Future<char *> fnamec = worker_c->name (); - - char *namea = fnamea; - char *nameb = fnameb; - char *namec = fnamec; - - 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", - scheduler_open_count.value ())); - - worker_a->end (); - worker_b->end (); - worker_c->end (); - - ACE_DEBUG ((LM_DEBUG, " (%t) scheduler_open_count %d immediately after end ()\n", - scheduler_open_count.value ())); - - ACE_OS::sleep (2); - - ACE_DEBUG ((LM_DEBUG, " (%t) scheduler_open_count %d after waiting\n", - scheduler_open_count.value ())); - // @@ Can we safely delete worker_a, worker_b, and worker_c? -} - -static void -test_cancellation (int n_iterations) -{ - ACE_DEBUG ((LM_DEBUG," (%t) testing cancellation of a future...\n")); - - // Now test the cancelling a future. - - Scheduler *worker_a; - ACE_NEW (worker_a, Scheduler ("worker A")); - worker_a->open (); - - ACE_Future<u_long> fresulta = worker_a->work (9013, n_iterations); - - // save the result by copying the future - ACE_Future<u_long> fresultb = fresulta; - - // now we cancel the first future.. but the - // calculation will still go on... - fresulta.cancel (10); - - if (!fresulta.ready ()) - ACE_DEBUG ((LM_DEBUG," (%t) ERROR: future A is should be ready!!!\n")); - - u_long resulta = fresulta; - - ACE_DEBUG ((LM_DEBUG, " (%t) cancelled result %u\n", (u_int) 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 %u\n", (u_int) resulta)); - - worker_a->end (); - // @@ Can we safely delete worker_a here? -} - -static void -test_timeout (int n_iterations) -{ - ACE_DEBUG ((LM_DEBUG," (%t) testing timeout on waiting for the result...\n")); - Scheduler *worker_a; - ACE_NEW (worker_a, Scheduler ("worker A")); - worker_a->open (); - - 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)); - - u_long resulta = 0; - fresulta.get (resulta, delay); - - if (fresulta.ready ()) - ACE_DEBUG ((LM_DEBUG," (%t) ERROR: future A is should not be ready!!!\n")); - else - ACE_DEBUG ((LM_DEBUG," (%t) timed out on future A\n")); - - // now we wait until we are done... - fresulta.get (resulta); - ACE_DEBUG ((LM_DEBUG, " (%t) result %u\n", (u_int) resulta)); - - worker_a->end (); - // @@ Can we safely delete worker_a here? -} - -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - int n_iterations = determine_iterations (); - - test_active_object (n_iterations); - test_cancellation (n_iterations); - test_timeout (n_iterations); - - ACE_DEBUG ((LM_DEBUG," (%t) that's all folks!\n")); - - ACE_OS::sleep (5); - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/manual_event.cpp b/ACE/examples/Threads/manual_event.cpp deleted file mode 100644 index d4b37975f23..00000000000 --- a/ACE/examples/Threads/manual_event.cpp +++ /dev/null @@ -1,116 +0,0 @@ -// $Id$ - -// The test shows the use of an ACE_Manual_Event to create a -// Pseudo_Barrier. Multiple threads are created which do the -// following: -// -// 1. work -// 2. synch with other threads -// 3. more work -// -// ACE_Manual_Event is use to synch with other -// threads. ACE_Manual_Event::signal() is used for broadcasting. - -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Thread_Mutex.h" -#include "ace/Manual_Event.h" -#include "ace/Thread_Manager.h" -#include "ace/Atomic_Op.h" - -ACE_RCSID(Threads, manual_event, "$Id$") - -#if defined (ACE_HAS_THREADS) -static ACE_Atomic_Op <ACE_Thread_Mutex, int> amount_of_work = 0; - -class Pseudo_Barrier - // = TITLE - // A barrier class using ACE manual-reset events. - // - // = DESCRIPTION - // This is *not* a real barrier. - // Pseudo_Barrier is more like a ``one shot'' barrier. - // All waiters after the Nth waiter are allowed to go. - // The barrier does not reset after the Nth waiter. - // For an example of a real barrier, please see class ACE_Barrier. -{ -public: - Pseudo_Barrier (u_long count); - - //FUZZ: disable check_for_lack_ACE_OS - int wait (void); - //FUZZ: enable check_for_lack_ACE_OS - -private: - ACE_Atomic_Op <ACE_Thread_Mutex, int> counter_; - ACE_Manual_Event event_; -}; - -Pseudo_Barrier::Pseudo_Barrier (u_long count) - : counter_ (count) -{ -} - -int -Pseudo_Barrier::wait (void) -{ - if (--this->counter_ == 0) - return this->event_.signal (); - else - return this->event_.wait (); -} - -static void * -worker (void *arg) -{ - Pseudo_Barrier &thread_barrier = *(Pseudo_Barrier *) arg; - - // work - ACE_DEBUG ((LM_DEBUG, "(%t) working (%d secs)\n", ++::amount_of_work)); - ACE_OS::sleep (::amount_of_work.value ()); - - // synch with everybody else - ACE_DEBUG ((LM_DEBUG, "(%t) waiting to synch with others \n")); - thread_barrier.wait (); - - // more work - ACE_DEBUG ((LM_DEBUG, "(%t) more work (%d secs)\n", ++::amount_of_work)); - ACE_OS::sleep (::amount_of_work.value ()); - - ACE_DEBUG ((LM_DEBUG, "(%t) dying \n")); - - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR **argv) -{ - int n_threads = argc == 2 ? ACE_OS::atoi (argv[1]) : 5; - - ACE_Thread_Manager &tm = *ACE_Thread_Manager::instance (); - - // synch object shared by all threads - Pseudo_Barrier thread_barrier (n_threads); - - // create workers - if (tm.spawn_n (n_threads, (ACE_THR_FUNC) worker, &thread_barrier) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "thread creates for worker failed"), -1); - - // wait for all workers to exit - if (tm.wait () == -1) - ACE_ERROR_RETURN ((LM_ERROR, "thread wait failed"), -1); - else - ACE_DEBUG ((LM_ERROR, "graceful exit\n")); - - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/process_manager.cpp b/ACE/examples/Threads/process_manager.cpp deleted file mode 100644 index b8c2455b469..00000000000 --- a/ACE/examples/Threads/process_manager.cpp +++ /dev/null @@ -1,296 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// examples/Threads/ -// -// = FILENAME -// process_manager.cpp -// -// = DESCRIPTION -// Test out the mechanisms provided by the ACE_Process_Manager. -// Using the global ACE_Process_Manager::instance(), we first spawn -// some processes (re-invoke this program, and plain-old-fork on -// systems that support it), and try the wait() functions. -// -// Then, we register the Process_Manager with -// ACE_Reactor::instance() and spawn more processes, counting on the -// autoreap to clean up. -// -// Specific-pid and generic exit-handler functions are also tested. -// -// = AUTHOR -// Douglas C. Schmidt <schmidt@cs.wustl.edu> and -// Dave Madden <dhm@mersenne.com> -// -// ============================================================================ - -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Thread_Manager.h" -#include "ace/Process_Manager.h" -#include "ace/Get_Opt.h" - -ACE_RCSID(Threads, process_manager, "$Id$") - -class ExitHandler : public ACE_Event_Handler -{ -public: - ExitHandler (const char *name); - - virtual ~ExitHandler (void); - virtual int handle_exit (ACE_Process *proc); - virtual int handle_timeout (const ACE_Time_Value &tv, - const void *arg = 0); - virtual int handle_close (ACE_HANDLE handle, - ACE_Reactor_Mask close_mask); - // Called when object is removed from the <ACE_Reactor>. -private: - const char *name_; -}; - -ExitHandler::ExitHandler (const char *name) - : ACE_Event_Handler (), - name_ (name) -{ -} - -ExitHandler::~ExitHandler (void) -{ - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) ExitHandler \"%s\" destroyed\n", - name_)); -} - -int -ExitHandler::handle_exit (ACE_Process *proc) -{ - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) ExitHandler \"%s\" handle_exit for pid %d status %d\n", - name_, - proc->getpid (), - proc->exit_code ())); - return 0; -} - -int -ExitHandler::handle_timeout(const ACE_Time_Value &, - const void *) -{ - static int tick_tock = 0; - - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) \"%s\" %s\n", - name_, - ACE_ODD (tick_tock) ? "Tock" : "Tick")); - tick_tock++; - return 0; -} - -int -ExitHandler::handle_close (ACE_HANDLE, - ACE_Reactor_Mask) -{ - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) ExitHandler \"%s\" handle_close\n", - name_)); - delete this; - return 0; -} - -// Spin furiously <iterations> times, pausing every 100 cycles to -// print a message and sleep for a few seconds. - -static void -worker (size_t iterations) -{ - for (size_t i = 0; - i <= iterations; - i++) - if (i && (i % 100) == 0) - { - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) worker spinning furiously... (%u)\n", - i)); - ACE_OS::sleep (1); - } - - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) worker finished\n")); -} - -static int n_iterations = 500; -static int child = 0; -static int exit_code = 0; - -// Parse the command-line arguments and set options. -static void -parse_args (int argc, ACE_TCHAR *argv[]) -{ - ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("i:e:cu")); - - int c; - - while ((c = get_opt ()) != -1) - switch (c) - { - case 'i': - n_iterations = ACE_OS::atoi (get_opt.opt_arg ()); - break; - case 'e': - exit_code = ACE_OS::atoi (get_opt.opt_arg ()); - break; - case 'c': - child = 1; - break; - case 'u': - default: - ACE_DEBUG ((LM_DEBUG, "usage:\n" - "-p <processes>\n" - "-i <iterations>\n")); - break; - } -} - -// Use ACE_Process_Manager::instance() to spawn another copy of this -// process. - -static pid_t -respawn_self (const ACE_TCHAR *myname, - int iter, - int exit_code) -{ - ACE_Process_Options options; - options.command_line ("%s -c -i %d -e %d", - myname, - iter, - exit_code); - return ACE_Process_Manager::instance ()->spawn (options); -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - ACE_Service_Config daemon; - - daemon.open (argv[0]); - - parse_args (argc, argv); - - if (child) - { - worker (n_iterations); - - ACE_OS::exit (exit_code); - } - - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Process_Manager test. Expect output from" - "2 or 3 processes...\n")); - - ACE_Process_Manager::instance ()->register_handler - (new ExitHandler ("default")); - - pid_t pid1 = respawn_self (argv[0], - n_iterations, - 111); - pid_t pid2 = respawn_self (argv[0], - n_iterations + 500, - 222); - -#if !defined (ACE_WIN32) - pid_t pid3 = ACE_OS::fork (); - - if (!pid3) - { - worker (n_iterations); - return 999; - } -#endif /* ACE_WIN32 */ - - ACE_Process_Manager::instance ()->register_handler (new ExitHandler ("specific"), - pid2); - - if (pid1 == ACE_INVALID_PID || pid2 == ACE_INVALID_PID) - ACE_ERROR_RETURN ((LM_ERROR, - "(%P|%t) %p\n", - "start_n"), - 1); - - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Test parent waiting (synchronously, " - "up to 6 seconds) for children...\n")); - - int result = - ACE_Process_Manager::instance ()->wait (ACE_Time_Value (6)); - - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Test parent: %d processes left\n", - result)); - - if (result > 0) - { - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Test parent waiting (synchronously, " - "indefinitely) for remaining children...\n")); - result = - ACE_Process_Manager::instance ()->wait (); - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Test parent finished waiting: %d\n", - result)); - } - - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Test parent: try auto-reap functions\n")); - - ACE_Process_Manager::instance ()->open (ACE_Process_Manager::DEFAULT_SIZE, - ACE_Reactor::instance ()); - - pid1 = respawn_self (argv[0], - n_iterations + 200, - 333 ); - pid2 = respawn_self (argv[0], - n_iterations + 500, - 444); - -#if !defined (ACE_WIN32) - pid3 = ACE_OS::fork (); - - if (!pid3) - { - worker (n_iterations); - return 888; - } -#endif /* ACE_WIN32 */ - - ExitHandler *main_thread_work = 0; - ACE_NEW_RETURN (main_thread_work, - ExitHandler ("main thread worker"), - 1); - - ACE_Reactor::instance ()->schedule_timer (main_thread_work, - 0, - ACE_Time_Value (2), - ACE_Time_Value (1, 500000)); - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Test parent: expect several Processes " - "to be auto-detected over the next 30 seconds.\n" - "The main thread will do some other work, too.\n" )); - - ACE_Time_Value briefly (30); - - result = ACE_Reactor::run_event_loop (briefly); - - ACE_DEBUG ((LM_DEBUG, - "(%P|%t@%T) Test parent: finished (%d) %d.%d. Close" - "Process_Manager...\n", - result, - briefly.sec (), - briefly.usec ())); - - ACE_Process_Manager::instance ()->close (); - - return 0; -} diff --git a/ACE/examples/Threads/process_mutex.cpp b/ACE/examples/Threads/process_mutex.cpp deleted file mode 100644 index bbb03c08d0b..00000000000 --- a/ACE/examples/Threads/process_mutex.cpp +++ /dev/null @@ -1,75 +0,0 @@ -// $Id$ - -// This program tests ACE_Process_Mutexes. To run it, open 3 or 4 -// windows and run this program in each window... - -#include "ace/OS_main.h" -#include "ace/OS_NS_unistd.h" -#include "ace/Thread_Mutex.h" -#include "ace/Signal.h" -#include "ace/Log_Msg.h" -#include "ace/Process_Mutex.h" - -ACE_RCSID(Threads, process_mutex, "$Id$") - -#if defined (ACE_HAS_THREADS) - -static sig_atomic_t done; - -extern "C" void -handler (int) -{ - done = 1; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - const ACE_TCHAR *name = argc > 1 ? argv[1] : ACE_TEXT("hello"); - int iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : 100; - - ACE_Process_Mutex pm (name); - - // Register a signal handler. - ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); - ACE_UNUSED_ARG (sa); - - for (int i = 0; i < iterations && !done; i++) - { - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquiring\n")); - if (pm.acquire () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "acquire failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquired\n")); - - ACE_OS::sleep (3); - - if (pm.release () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n")); - - if (pm.tryacquire () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "tryacquire failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = tryacquire\n")); - - if (pm.release () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n")); - } - - if (argc > 2) - pm.remove (); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR_RETURN ((LM_ERROR, - "ACE doesn't support support threads on this platform (yet)\n"), - -1); -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/process_semaphore.cpp b/ACE/examples/Threads/process_semaphore.cpp deleted file mode 100644 index e7adbb6a3ac..00000000000 --- a/ACE/examples/Threads/process_semaphore.cpp +++ /dev/null @@ -1,63 +0,0 @@ -// $Id$ - -// This program tests ACE_Process_Semaphore. To run it, open 3 or 4 -// windows and run this program in each window... - -#include "ace/OS_main.h" -#include "ace/OS_NS_unistd.h" -#include "ace/Signal.h" -#include "ace/Log_Msg.h" -#include "ace/Process_Semaphore.h" -#include "ace/OS_NS_stdlib.h" - -ACE_RCSID(Threads, process_semaphore, "$Id$") - -static sig_atomic_t done; - -extern "C" void -handler (int) -{ - done = 1; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - const ACE_TCHAR *name = argc == 1 ? ACE_TEXT("hello") : argv[1]; - int iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : 100; - - ACE_Process_Semaphore pm (1, name); - - ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); - ACE_UNUSED_ARG (sa); - - for (int i = 0; i < iterations && !done; i++) - { - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquiring\n")); - if (pm.acquire () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "acquire failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquired\n")); - - ACE_OS::sleep (3); - - if (pm.release () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n")); - - if (pm.tryacquire () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "tryacquire failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = tryacquire\n")); - - if (pm.release () == -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed")); - else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n")); - } - - if (argc > 2) - pm.remove (); - return 0; -} diff --git a/ACE/examples/Threads/reader_writer.cpp b/ACE/examples/Threads/reader_writer.cpp deleted file mode 100644 index 5344ba4d5b4..00000000000 --- a/ACE/examples/Threads/reader_writer.cpp +++ /dev/null @@ -1,188 +0,0 @@ -// $Id$ - -// This test program verifies the functionality of the ACE_OS -// implementation of readers/writer locks on Win32 and Posix pthreads. - -#include "ace/OS_main.h" -#include "ace/Thread.h" -#include "ace/Thread_Manager.h" -#include "ace/Get_Opt.h" -#include "ace/Atomic_Op.h" - -ACE_RCSID(Threads, reader_writer, "$Id$") - -#if defined (ACE_HAS_THREADS) - -#include "ace/Guard_T.h" -#include "ace/RW_Mutex.h" - -// Default number of iterations. -static int n_iterations = 1000; - -// Default number of loops. -static int n_loops = 100; - -// Default number of readers. -static int n_readers = 6; - -// Default number of writers. -static int n_writers = 2; - -// Thread id of last writer. -static ACE_thread_t shared_thr_id; - -// Lock for shared_thr_id. -static ACE_RW_Mutex rw_mutex; - -// Count of the number of readers and writers. -ACE_Atomic_Op<ACE_Thread_Mutex, int> current_readers, current_writers; - -// Thread manager -static ACE_Thread_Manager thr_mgr; - -// Explain usage and exit. -static void -print_usage_and_die (void) -{ - ACE_DEBUG ((LM_DEBUG, - "usage: %n [-r n_readers] [-w n_writers] [-n iteration_count]\n")); - ACE_OS::exit (1); -} - -// Parse the command-line arguments and set options. -static void -parse_args (int argc, ACE_TCHAR *argv[]) -{ - ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("r:w:n:l:")); - - int c; - - while ((c = get_opt ()) != -1) - switch (c) - { - case 'r': - n_readers = ACE_OS::atoi (get_opt.opt_arg ()); - break; - case 'w': - n_writers = ACE_OS::atoi (get_opt.opt_arg ()); - break; - case 'n': - n_iterations = ACE_OS::atoi (get_opt.opt_arg ()); - break; - case 'l': - n_loops = ACE_OS::atoi (get_opt.opt_arg ()); - break; - default: - print_usage_and_die (); - break; - } -} - -// Iterate <n_iterations> each time checking that nobody modifies the -// data while we have a read lock. - -static void * -reader (void *) -{ - ACE_DEBUG ((LM_DEBUG, "(%t) reader starting\n")); - - for (int iterations = 1; - iterations <= n_iterations; iterations++) - { - ACE_Read_Guard<ACE_RW_Mutex> g(rw_mutex); - - ++current_readers; - - if (current_writers > 0) - ACE_DEBUG ((LM_DEBUG, "(%t) writers found!!!\n")); - - ACE_thread_t thr_id = shared_thr_id; - - for (int loop = 1; loop <= n_loops; loop++) - { - ACE_Thread::yield(); - - if (ACE_OS::thr_equal (shared_thr_id, thr_id) == 0) - ACE_DEBUG ((LM_DEBUG, - "(%t) somebody changed %d to %d\n", - thr_id, shared_thr_id)); - } - - --current_readers; - - ACE_Thread::yield (); - } - return 0; -} - -// Iterate <n_iterations> each time modifying the global data -// and checking that nobody steps on it while we can write it. - -static void * -writer (void *) -{ - ACE_DEBUG ((LM_DEBUG, "(%t) writer starting\n")); - - for (int iterations = 1; - iterations <= n_iterations; - iterations++) - { - ACE_Write_Guard<ACE_RW_Mutex> g(rw_mutex); - - ++current_writers; - - if (current_writers > 1) - ACE_DEBUG ((LM_DEBUG, "(%t) other writers found!!!\n")); - - if (current_readers > 0) - ACE_DEBUG ((LM_DEBUG, "(%t) readers found!!!\n")); - - ACE_thread_t self = ACE_Thread::self (); - shared_thr_id = self; - - for (int loop = 1; loop <= n_loops; loop++) - { - ACE_Thread::yield(); - - if (ACE_OS::thr_equal (shared_thr_id, self) == 0) - ACE_DEBUG ((LM_DEBUG, "(%t) somebody wrote on my data %d\n", - shared_thr_id)); - } - - --current_writers; - - ACE_Thread::yield (); - } - return 0; -} - -// Spawn off threads. - -int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - ACE_LOG_MSG->open (argv[0]); - parse_args (argc, argv); - - current_readers = 0; // Possibly already done - current_writers = 0; // Possibly already done - - ACE_DEBUG ((LM_DEBUG, "(%t) main thread starting\n")); - - if (thr_mgr.spawn_n (n_readers, (ACE_THR_FUNC) reader, 0, THR_NEW_LWP) == -1 || - thr_mgr.spawn_n (n_writers, (ACE_THR_FUNC) writer, 0, THR_NEW_LWP) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn_n"), 1); - - thr_mgr.wait (); - - ACE_DEBUG ((LM_DEBUG, "(%t) exiting main thread\n")); - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/recursive_mutex.cpp b/ACE/examples/Threads/recursive_mutex.cpp deleted file mode 100644 index 3c915682d90..00000000000 --- a/ACE/examples/Threads/recursive_mutex.cpp +++ /dev/null @@ -1,113 +0,0 @@ -// $Id$ - -// This test program verifies the functionality of the ACE_OS -// implementation of recursive mutexes on Win32 and Posix pthreads. - -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Thread_Manager.h" -#include "ace/Get_Opt.h" - -ACE_RCSID(Threads, recursive_mutex, "$Id$") - -#if defined (ACE_HAS_THREADS) - -#include "ace/Guard_T.h" -#include "ace/Recursive_Thread_Mutex.h" - -// Total number of iterations. -static size_t n_iterations = 1000; -static size_t n_threads = 4; - -// Explain usage and exit. -static void -print_usage_and_die (void) -{ - ACE_DEBUG ((LM_DEBUG, - "usage: %n [-t n_threads] [-n iteration_count]\n")); - ACE_OS::exit (1); -} - -// Parse the command-line arguments and set options. - -static void -parse_args (int argc, ACE_TCHAR *argv[]) -{ - ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("n:t:")); - - int c; - - while ((c = get_opt ()) != -1) - switch (c) - { - case 'n': - n_iterations = ACE_OS::atoi (get_opt.opt_arg ()); - break; - case 't': - n_threads = ACE_OS::atoi (get_opt.opt_arg ()); - break; - default: - print_usage_and_die (); - break; - } -} - -static void -recursive_worker (size_t nesting_level, - ACE_Recursive_Thread_Mutex *rm) -{ - if (nesting_level < n_iterations) - { - ACE_DEBUG ((LM_DEBUG, - "(%P|%t) = trying to acquire, nesting = %d, thread id = %u\n", - rm->get_nesting_level (), rm->get_thread_id ())); - { - // This illustrates the use of the ACE_Guard<LOCK> with an - // ACE_Recursive_Thread_Mutex. - ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon, *rm); - ACE_DEBUG ((LM_DEBUG, - "(%P|%t) = acquired, nesting = %d, thread id = %u\n", - rm->get_nesting_level (), rm->get_thread_id ())); - - recursive_worker (nesting_level + 1, rm); - } - ACE_DEBUG ((LM_DEBUG, - "(%P|%t) = released, nesting = %d, thread id = %u\n", - rm->get_nesting_level (), rm->get_thread_id ())); - } -} - -static void * -worker (void *arg) -{ - ACE_Recursive_Thread_Mutex *rm - = (ACE_Recursive_Thread_Mutex *) arg; - - recursive_worker (0, rm); - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - ACE_Service_Config daemon (argv[0]); - - parse_args (argc, argv); - ACE_Recursive_Thread_Mutex rm; - - ACE_Thread_Manager::instance ()->spawn_n (n_threads, - ACE_THR_FUNC (worker), - (void *) &rm); - - ACE_Thread_Manager::instance ()->wait (); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR_RETURN ((LM_ERROR, - "ACE doesn't support support process mutexes on this platform (yet)\n"), - -1); -} -#endif /* ACE_WIN32 */ diff --git a/ACE/examples/Threads/task_five.cpp b/ACE/examples/Threads/task_five.cpp deleted file mode 100644 index 66fcee550c4..00000000000 --- a/ACE/examples/Threads/task_five.cpp +++ /dev/null @@ -1,181 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// examples/Threads/ -// -// = FILENAME -// task_five.cpp -// -// = DESCRIPTION -// Stress testing thread creation and thread cancellation using -// ACE_Task. -// -// = AUTHOR -// Author: Detlef Becker <Detlef.Becker@med.siemens.de> -// -// ============================================================================ - - -#include "ace/OS_main.h" -#include "ace/Thread_Manager.h" -#include "ace/Task.h" -#include "ace/OS_NS_unistd.h" - -ACE_RCSID(Threads, task_five, "$Id$") - -static const int DEFAULT_TASKS = 100; -static const int DEFAULT_ITERATIONS = 10; - -// Default stack size -static size_t default_stack_size = -#if defined (ACE_WIN32) - 0; -#else - 8192; -#endif /* ACE_WIN32 */ - u_int loop_count = 0; - u_int error_count = 0; - -class Test_Task : public ACE_Task<ACE_SYNCH> -{ -public: - Test_Task (ACE_Thread_Manager * = ACE_Thread_Manager::instance ()); - ~Test_Task (void) {}; - - //FUZZ: disable check_for_lack_ACE_OS - int open (void * = 0); - int svc (void); - int close (u_long); - - int shutdown (void); - //FUZZ: enable check_for_lack_ACE_OS - - int synch (void); -}; - -Test_Task::Test_Task (ACE_Thread_Manager *thrmgr) - : ACE_Task<ACE_SYNCH> (thrmgr) -{ -} - -int -Test_Task::open (void *) -{ - return this->activate (0, - 1, - 0, - ACE_DEFAULT_THREAD_PRIORITY, - -1, - 0, - 0, - 0, - &default_stack_size); -} - -int -Test_Task::svc (void) -{ - while (thr_mgr_->testcancel (ACE_OS::thr_self ()) == 0) - // Sleep for 350 msecs. - ACE_OS::sleep (ACE_Time_Value (0, 350000)); - - return 0; -} - -int -Test_Task::close (u_long) -{ - ACE_DEBUG ((LM_DEBUG, "(%t) closing down\n")); - return 0; -} - -int -Test_Task::shutdown (void) -{ - return thr_mgr_->cancel_grp (grp_id_); -} - -int -Test_Task::synch (void) -{ - return thr_mgr_->wait_grp (grp_id_); -} - -static void -work (ACE_Thread_Manager *thr_mgr, - int n_tasks, - size_t stack_size) -{ - ACE_UNUSED_ARG (stack_size); - - int i; - Test_Task *task_array; - - ACE_NEW (task_array, - Test_Task[n_tasks]); - - ACE_DEBUG ((LM_DEBUG, - "Opening Tasks, loop count = %d, error count = %d\n", - loop_count, - error_count)); - - for (i = 0; - i < n_tasks; - i++) - task_array[i].open (); - - ACE_OS::sleep (1); - - ACE_DEBUG ((LM_DEBUG, - "Cancelling Tasks, loop count = %d, error count = %d\n", - loop_count, - error_count)); - - for (i = 0; i < n_tasks; i++) - task_array[i].shutdown (); - - ACE_DEBUG ((LM_DEBUG, - "Synching Tasks, loop count = %d, error count = %d\n", - loop_count, - error_count)); - - for (i = 0; - - i < n_tasks; i++) - if (-1 == task_array[i].synch ()) - { - ACE_ERROR ((LM_ERROR, - "Error in synch! loop count = %d, error count = %d\n", - loop_count, - error_count)); - error_count++; - } - - ACE_DEBUG ((LM_DEBUG, - "thr_mgr->wait ();! loop count = %d, error count = %d\n", - loop_count, - error_count)); - - // Wait for all the threads to finish. - thr_mgr->wait (); - - delete [] task_array; - loop_count++; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - size_t stack_size = argc > 1 ? ACE_OS::atoi (argv[1]) : default_stack_size; - const int n_tasks = argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_TASKS; - u_int iterations = argc > 3 ? ACE_OS::atoi (argv[3]) : DEFAULT_ITERATIONS; - - for (u_int i = 0; i < iterations; i++) - work (ACE_Thread_Manager::instance (), - n_tasks, - stack_size); - - return 0; -} diff --git a/ACE/examples/Threads/task_four.cpp b/ACE/examples/Threads/task_four.cpp deleted file mode 100644 index 7f256dcb88f..00000000000 --- a/ACE/examples/Threads/task_four.cpp +++ /dev/null @@ -1,313 +0,0 @@ -// $Id$ - -// The following test was written by Hamutal Yanay & Ari Erev's -// (Ari_Erev@comverse.com). -// -// This test program test enhancements to the thread_manager and task -// classes. The purpose of these enhancements was to allow the -// thread_manager to recognize the concept of an ACE_Task and to be -// able to group ACE_Tasks in groups. -// -// There are two main ACE_Tasks in this sample: -// -// Invoker_Task - is run from main (). It's purpose is to run a number of -// ACE_Tasks of type Worker_Task. The number can be specified -// on the command line. -// After starting the tasks, the Invoker_Task groups all the tasks -// in one group and then uses the -// num_tasks_in_group () to find out if the real number of tasks -// that are now running (should be the same as the number of tasks -// started). -// It also, suspends and resumes all the threads in the group to -// test the suspend_grp () and resume_grp () methods. -// Then it waits for all the tasks to end. -// -// Worker_Task - ACE_Tasks that are started by the Invoker_Task. -// Each Worker_Task can start a number of threads. -// The Worker_Task threads perform some work (iteration). The number -// of the iterations can be specified on the command line. -// -// The command line syntax is: -// -// test_task [num_tasks] [num_threads] [num_iterations] - -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Task.h" -#include "ace/Service_Config.h" - -ACE_RCSID(Threads, task_four, "$Id$") - -#if defined (ACE_HAS_THREADS) - -#include "ace/Task.h" - -class Invoker_Task : public ACE_Task<ACE_MT_SYNCH> -{ -public: - Invoker_Task (ACE_Thread_Manager *thr_mgr, - size_t n_tasks, - size_t n_threads, - size_t n_iterations); - virtual int svc (void); - // creats <n_tasks> and wait for them to finish - -private: - size_t n_tasks_; - // Number of tasks to start. - size_t n_threads_; - // Number of threads per task. - size_t n_iterations_; - // Number of iterations per thread. -}; - -class Worker_Task : public ACE_Task<ACE_MT_SYNCH> -{ -public: - Worker_Task (ACE_Thread_Manager *thr_mgr, - size_t n_threads, - size_t n_iterations); - virtual int svc (void); - - //FUZZ: disable check_for_lack_ACE_OS - // Does a small work... - virtual int open (void * = NULL); - //FUZZ: enable check_for_lack_ACE_OS - -private: - static size_t workers_count_; - size_t index_; - size_t n_threads_; - size_t n_iterations_; - - //FUZZ: disable check_for_lack_ACE_OS - // = Not needed for this test. - virtual int close (u_long); - //FUZZ: enable check_for_lack_ACE_OS - - virtual int put (ACE_Message_Block *, ACE_Time_Value *) { return 0; } -}; - -size_t Worker_Task::workers_count_ = 1; - -int -Worker_Task::close (u_long) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) closing task %d\n", - this->index_)); - delete this; - return 0; -} - -Worker_Task::Worker_Task (ACE_Thread_Manager *thr_mgr, - size_t n_threads, - size_t n_iterations) - : ACE_Task<ACE_MT_SYNCH> (thr_mgr), - index_ (Worker_Task::workers_count_++), - n_threads_ (n_threads), - n_iterations_ (n_iterations) -{ -} - -int -Worker_Task::open (void *) -{ - // Create the pool of worker threads. - return this->activate (THR_NEW_LWP, - n_threads_, - 0, - -1, - -1, - this); -} - -int -Worker_Task::svc (void) -{ - ACE_DEBUG ((LM_DEBUG, - " (%t) in worker %d\n", - index_)); - - for (size_t iterations = 1; - iterations <= this->n_iterations_; - iterations++) - { - ACE_DEBUG ((LM_DEBUG, - " (%t) in iteration %d\n", - iterations)); - ACE_OS::sleep (0); - } - - ACE_DEBUG ((LM_DEBUG, - " (%t) worker %d ends\n", - index_)); - - return 0; -} - -Invoker_Task::Invoker_Task (ACE_Thread_Manager *thr_mgr, - size_t n_tasks, - size_t n_threads, - size_t n_iterations) - : ACE_Task<ACE_MT_SYNCH> (thr_mgr), - n_tasks_ (n_tasks), - n_threads_ (n_threads), - n_iterations_ (n_iterations) -{ - // Create a single worker thread. - if (this->activate (THR_NEW_LWP, - 1, - 0, - -1, - -1, - this) == -1) - ACE_ERROR ((LM_ERROR, - "%p\n", - "activate failed")); -} - -// Iterate <n_iterations> time printing off a message and "waiting" -// for all other threads to complete this iteration. - -int -Invoker_Task::svc (void) -{ - // Note that the ACE_Task::svc_run () method automatically adds us - // to the Thread_Manager when the thread begins. - - ACE_Thread_Manager *thr_mgr = - ACE_Thread_Manager::instance (); - Worker_Task **worker_task = 0; - - ACE_NEW_RETURN (worker_task, - Worker_Task *[n_tasks_], - -1); - size_t task = 0; - - for (task = 0; - task < this->n_tasks_; - task++) - { - ACE_DEBUG ((LM_DEBUG, - " (%t) in task %d\n", - task + 1)); - - ACE_NEW_RETURN (worker_task[task], - Worker_Task (thr_mgr, - n_threads_, - n_iterations_), - -1); - - if (worker_task[task]->open () == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "%p\n", - "open failed"), - -1); - } - - // Set all tasks to be one group - ACE_DEBUG ((LM_DEBUG, - " (%t) setting tasks group id\n")); - - for (task = 0; - task < this->n_tasks_; - task++) - if (thr_mgr->set_grp (worker_task[task], - 1) == -1) - ACE_ERROR ((LM_DEBUG, - " (%t) %p\n", - "set_grp")); - - size_t n_tasks = - thr_mgr->num_tasks_in_group (1); - ACE_DEBUG ((LM_DEBUG, - "Number of tasks in group 1: %d\n", - n_tasks)) ; - - // Wait for 1 second and then suspend every thread in the group. - ACE_OS::sleep (1); - ACE_DEBUG ((LM_DEBUG, - " (%t) suspending group\n")); - - if (thr_mgr->suspend_grp (1) == -1) - ACE_ERROR ((LM_DEBUG, - " (%t) %p\n", - "suspend_grp")); - - // Wait for 3 more second and then resume every thread in the group. - ACE_OS::sleep (ACE_Time_Value (2)); - - ACE_DEBUG ((LM_DEBUG, - " (%t) resuming group\n")); - - if (thr_mgr->resume_grp (1) == -1) - ACE_ERROR ((LM_DEBUG, - " (%t) %p\n", - "resume_grp")); - - // Wait for all the tasks to reach their exit point. - thr_mgr->wait (); - - // Note that the ACE_Task::svc_run () method automatically removes - // us from the Thread_Manager when the thread exits. - return 0; -} - -// Default number of tasks and iterations. -static const size_t DEFAULT_TASKS = 4; -static const size_t DEFAULT_ITERATIONS = 5; - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - size_t n_tasks = argc > 1 ? ACE_OS::atoi (argv[1]) : DEFAULT_TASKS; - size_t n_threads = argc > 2 ? ACE_OS::atoi (argv[2]) : ACE_DEFAULT_THREADS; - size_t n_iterations = argc > 3 ? ACE_OS::atoi (argv[3]) : DEFAULT_ITERATIONS; - - // Since ACE_Thread_Manager can only wait for all threads, we'll - // have special manager for the Invoker_Task. - ACE_Thread_Manager invoker_manager; - - Invoker_Task invoker (&invoker_manager, - n_tasks, - n_threads, - n_iterations); - - // Wait for 1 second and then suspend the invoker task - ACE_OS::sleep (1); - ACE_DEBUG ((LM_DEBUG, - " (%t) suspending invoker task\n")); - - if (invoker_manager.suspend_task (&invoker) == -1) - ACE_ERROR ((LM_DEBUG, - " (%t) %p\n", - "suspend_task")); - - // Wait for 3 more second and then resume the invoker task. - ACE_OS::sleep (ACE_Time_Value (3)); - - ACE_DEBUG ((LM_DEBUG, - " (%t) resuming invoker task\n")); - - if (invoker_manager.resume_task (&invoker) == -1) - ACE_ERROR ((LM_DEBUG, - " (%t) %p\n", - "resume_task")); - - // Wait for all the threads to reach their exit point. - invoker_manager.wait (); - - ACE_DEBUG ((LM_DEBUG, - " (%t) done\n")); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, - "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/task_one.cpp b/ACE/examples/Threads/task_one.cpp deleted file mode 100644 index c2c23306152..00000000000 --- a/ACE/examples/Threads/task_one.cpp +++ /dev/null @@ -1,102 +0,0 @@ -// $Id$ - -// This test program illustrates how the ACE barrier synchronization -// mechanisms work in conjunction with the ACE_Task and the -// ACE_Thread_Manager. It is instructive to compare this with the -// test_barrier.cpp test to see how they differ. - -#include "ace/OS_main.h" -#include "ace/Task.h" -#include "ace/Service_Config.h" - -ACE_RCSID(Threads, task_one, "$Id$") - -#if defined (ACE_HAS_THREADS) - -#include "ace/Task.h" -#include "ace/Barrier.h" - -class Barrier_Task : public ACE_Task<ACE_MT_SYNCH> -{ -public: - Barrier_Task (ACE_Thread_Manager *thr_mgr, - int n_threads, - int n_iterations); - - virtual int svc (void); - // Iterate <n_iterations> time printing off a message and "waiting" - // for all other threads to complete this iteration. - -private: - ACE_Barrier barrier_; - // Reference to the tester barrier. This controls each - // iteration of the tester function running in every thread. - - int n_iterations_; - // Number of iterations to run. -}; - -Barrier_Task::Barrier_Task (ACE_Thread_Manager *thr_mgr, - int n_threads, - int n_iterations) - : ACE_Task<ACE_MT_SYNCH> (thr_mgr), - barrier_ (n_threads), - n_iterations_ (n_iterations) -{ - // Create worker threads. - if (this->activate (THR_NEW_LWP, n_threads) == -1) - ACE_ERROR ((LM_ERROR, "%p\n", "activate failed")); -} - -// Iterate <n_iterations> time printing off a message and "waiting" -// for all other threads to complete this iteration. - -int -Barrier_Task::svc (void) -{ - // Note that the ACE_Task::svc_run() method automatically adds us to - // the Thread_Manager when the thread begins. - - for (int iterations = 1; - iterations <= this->n_iterations_; - iterations++) - { - ACE_DEBUG ((LM_DEBUG, "(%t) in iteration %d\n", iterations)); - - // Block until all other threads have waited, then continue. - this->barrier_.wait (); - } - - // Note that the ACE_Task::svc_run() method automatically removes us - // from the Thread_Manager when the thread exits. - - return 0; -} - -// Default number of threads to spawn. -static const int DEFAULT_ITERATIONS = 5; - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_THREADS; - int n_iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_ITERATIONS; - - Barrier_Task barrier_task (ACE_Thread_Manager::instance (), - n_threads, - n_iterations); - - // Wait for all the threads to reach their exit point. - ACE_Thread_Manager::instance ()->wait (); - - ACE_DEBUG ((LM_DEBUG, "(%t) done\n")); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/task_three.cpp b/ACE/examples/Threads/task_three.cpp deleted file mode 100644 index 51bc95d3346..00000000000 --- a/ACE/examples/Threads/task_three.cpp +++ /dev/null @@ -1,271 +0,0 @@ -// $Id$ - -// Exercise more tests for the <ACE_Task>s. This also shows off some -// Interesting uses of the <ACE_Log_Msg>'s ability to print to -// ostreams. BTW, make sure that you set the out_stream in *every* -// thread that you want to have write to the output file, i.e.: -// -// if (out_stream) -// { -// ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM); -// ACE_LOG_MSG->msg_ostream (out_stream); -// } - -#include "ace/OS_NS_unistd.h" -#include "ace/OS_NS_stdio.h" -#include "ace/OS_main.h" -#include "ace/Reactor.h" -#include "ace/Service_Config.h" -#include "ace/Task.h" - -// FUZZ: disable check_for_streams_include -#include "ace/streams.h" - -#include "ace/Signal.h" - -ACE_RCSID(Threads, task_three, "$Id$") - -#if defined (ACE_HAS_THREADS) - -static ACE_OSTREAM_TYPE *out_stream = 0; -static sig_atomic_t done = 0; -static const size_t NUM_INVOCATIONS = 100; -static const size_t TASK_COUNT = 130; - -class Test_Task : public ACE_Task<ACE_MT_SYNCH> -{ -public: - Test_Task (void); - ~Test_Task (void); - - //FUZZ: disable check_for_lack_ACE_OS - virtual int open (void *args = 0); - virtual int close (u_long flags = 0); - //FUZZ: enable check_for_lack_ACE_OS - - virtual int svc (void); - - virtual int handle_input (ACE_HANDLE fd); - - ACE_Reactor *r_; - size_t handled_; - static size_t current_count_; - static size_t done_cnt_; -}; - -size_t Test_Task::current_count_ = 0; -size_t Test_Task::done_cnt_ = 0; - -static ACE_Thread_Mutex Lock; - -Test_Task::Test_Task (void) -{ - ACE_GUARD (ACE_Thread_Mutex, ace_mon, Lock); - - this->handled_ = 0; - Test_Task::current_count_++; - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("Test_Task constructed, current_count_ = %d\n"), - Test_Task::current_count_)); -} - -Test_Task::~Test_Task (void) -{ - ACE_GUARD (ACE_Thread_Mutex, ace_mon, Lock); - - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("Test_Task destroyed, current_count_ = %d\n"), - Test_Task::current_count_)); -} - -int -Test_Task::open (void *args) -{ - r_ = reinterpret_cast <ACE_Reactor *> (args); - return ACE_Task<ACE_MT_SYNCH>::activate (THR_NEW_LWP); -} - -int -Test_Task::close (u_long) -{ - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, Lock, -1); - - Test_Task::current_count_--; - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("Test_Task::close () current_count_ = %d.\n"), - Test_Task::current_count_)); - return 0; -} - -int -Test_Task::svc (void) -{ - // Every thread must register the same stream to write to file. - if (out_stream) - { - ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM); - ACE_LOG_MSG->msg_ostream (out_stream); - } - - for (size_t index = 0; index < NUM_INVOCATIONS; index++) - { - ACE_OS::thr_yield (); - - if (r_->notify (this, ACE_Event_Handler::READ_MASK) == -1) - { - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, Lock, -1); - - ACE_ERROR_RETURN ((LM_ERROR, - ACE_TEXT ("Test_Task: error %p!\n"), - ACE_TEXT ("notifying reactor")), - 0); - } - } - - ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%t) returning from svc ()\n"))); - return 0; -} - -int -Test_Task::handle_input (ACE_HANDLE) -{ - this->handled_++; - - if (this->handled_ == NUM_INVOCATIONS) - { - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, Lock, -1); - Test_Task::done_cnt_++; - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT (" (%t) Test_Task: handle_input done_cnt_ = %d.\n"), - Test_Task::done_cnt_)); - } - - ACE_OS::thr_yield (); - return -1; -} - -static void * -dispatch (void *arg) -{ - // every thread must register the same stream to write to file - if (out_stream) - { - ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM); - ACE_LOG_MSG->msg_ostream (out_stream); - } - - ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%t) Dispatcher Thread started!\n"))); - ACE_Reactor *r = reinterpret_cast <ACE_Reactor *> (arg); - int result; - - r->owner (ACE_OS::thr_self ()); - - while (1) - { - result = r->handle_events (); - - if (result <= 0) - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("Dispatch: handle_events (): %d"), - result)); - } - - ACE_NOTREACHED (return 0); -} - -extern "C" void -handler (int) -{ - done = 1; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *[]) -{ - if (argc > 1) - { - // Send output to file. -#if !defined (ACE_LACKS_IOSTREAM_TOTALLY) - ACE_NEW_RETURN (out_stream, - ofstream ("test_task_three.out", - ios::trunc|ios::out), - -1); -#else - if ((out_stream = ACE_OS::fopen ("test_task_three.out", "w")) == 0) - return -1; -#endif - ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM); - ACE_LOG_MSG->msg_ostream (out_stream); - } - - // Register a signal handler. - ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); - ACE_UNUSED_ARG (sa); - - ACE_Reactor *reactor1 = ACE_Reactor::instance (); - ACE_Reactor *reactor2 = new ACE_Reactor (); - - Test_Task t1[TASK_COUNT]; - Test_Task t2[TASK_COUNT]; - - ACE_Thread::spawn (ACE_THR_FUNC (dispatch), - reactor2); - - reactor1->owner (ACE_OS::thr_self ()); - - for (size_t index = 0; index < TASK_COUNT; index++) - { - t1[index].open (reactor1); - t2[index].open (reactor2); - } - - ACE_OS::sleep (3); - - while (done == 0) - { - ACE_Time_Value timeout (2); - - if (reactor1->handle_events (timeout) <= 0) - { - if (errno == ETIME) - { - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("no activity within 2 seconds, shutting down\n"))); - break; - } - else - ACE_ERROR ((LM_ERROR, - ACE_TEXT ("%p error handling events\n"), - ACE_TEXT ("main"))); - } - } - - if (argc > 1) - { -#if !defined (ACE_LACKS_IOSTREAM_TOTALLY) - *out_stream << flush; - delete out_stream; -#else - ACE_OS::fflush(out_stream); - ACE_OS::fclose(out_stream); -#endif - ACE_LOG_MSG->clr_flags (ACE_Log_Msg::OSTREAM); - ACE_LOG_MSG->msg_ostream (0); - } - - // Bail out here so that we don't call the destructors for the tasks.. - ACE_OS::exit (0); - /* NOTREACHED */ - - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, - ACE_TEXT ("threads not supported on this platform\n"))); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/task_two.cpp b/ACE/examples/Threads/task_two.cpp deleted file mode 100644 index 91b6de2bd1a..00000000000 --- a/ACE/examples/Threads/task_two.cpp +++ /dev/null @@ -1,150 +0,0 @@ -// $Id$ - -// Exercise more tests for the ACE Tasks. This test can spawn off -// zillions of tasks and then wait for them using both polling and the -// ACE Thread Manager. - -#include "ace/OS_main.h" -#include "ace/Task.h" - -#include "ace/Service_Config.h" -#include "ace/Atomic_Op.h" - -ACE_RCSID(Threads, task_two, "$Id$") - -#if defined (ACE_HAS_THREADS) - -typedef ACE_Atomic_Op<ACE_Thread_Mutex, int> ATOMIC_INT; - -static int zero = 0; -static ATOMIC_INT task_count (zero); -static ATOMIC_INT max_count (zero); -static ATOMIC_INT wait_count (zero); - -static int n_threads = 0; - -// Default number of tasks. -static const int default_threads = ACE_DEFAULT_THREADS; - -// Default number of times to run the test. -static const int default_iterations = 1000; - -class Task_Test : public ACE_Task<ACE_MT_SYNCH> -{ -public: - //FUZZ: disable check_for_lack_ACE_OS - virtual int open (void *args = 0); - virtual int close (u_long flags = 0); - //FUZZ: enable check_for_lack_ACE_OS - - virtual int svc (void); - -private: - static ACE_Thread_Mutex lock_; -}; - -ACE_Thread_Mutex Task_Test::lock_; - -int -Task_Test::open (void *) -{ - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, Task_Test::lock_, -1); - - task_count++; - ACE_DEBUG ((LM_DEBUG, "(%t) creating Task_Test, task count = %d\n", - task_count.value ())); - - return this->activate (THR_BOUND); -} - -int -Task_Test::close (u_long) -{ - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, Task_Test::lock_, -1); - - task_count--; - ACE_DEBUG ((LM_DEBUG, "(%t) destroying Task_Test, task count = %d\n", - task_count.value ())); - wait_count--; - return 0; -} - -int -Task_Test::svc (void) -{ - wait_count++; - max_count++; - - ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting\n")); - - for (;;) - if (max_count >= n_threads) - break; - else - ACE_Thread::yield (); - - ACE_DEBUG ((LM_DEBUG, "(%t) svc: finished waiting\n")); - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : default_threads; - int n_iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : default_iterations; - - Task_Test **task_array = new Task_Test *[n_threads]; - - for (int i = 1; i <= n_iterations; i++) - { - ACE_DEBUG ((LM_DEBUG, "(%t) iteration = %d, max_count %d\n", - i, max_count.value ())); - max_count = 0; - - ACE_DEBUG ((LM_DEBUG, "(%t) starting %d task%s\n", - n_threads, n_threads == 1 ? "" : "s")); - - // Launch the new tasks. - for (int j = 0; j < n_threads; j++) - { - task_array[j] = new Task_Test; - // Activate the task, i.e., make it an active object. - task_array[j]->open (); - } - - // Wait for initialization to kick in. - while (max_count == 0) - ACE_Thread::yield (); - - ACE_DEBUG ((LM_DEBUG, "(%t) waiting for threads to finish\n")); - - // Wait for the threads to finish this iteration. - while (max_count != n_threads && wait_count != 0) - ACE_Thread::yield (); - - ACE_DEBUG ((LM_DEBUG, - "(%t) iteration %d finished, max_count %d, wait_count %d, waiting for tasks to exit\n", - i, max_count.value (), wait_count.value ())); - - // Wait for all the tasks to exit. - ACE_Thread_Manager::instance ()->wait (); - - // Delete the existing tasks. - for (int k = 0; k < n_threads; k++) - delete task_array[k]; - } - - delete [] task_array; - - ACE_DEBUG ((LM_DEBUG, "(%t) shutting down the test\n")); - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/thread_manager.cpp b/ACE/examples/Threads/thread_manager.cpp deleted file mode 100644 index b2bee7db4c6..00000000000 --- a/ACE/examples/Threads/thread_manager.cpp +++ /dev/null @@ -1,109 +0,0 @@ -// $Id$ - -// Test out the group management mechanisms provided by the -// ACE_Thread_Manager, including the group signal handling, group -// suspension and resumption, and cooperative thread cancellation -// mechanisms. - -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Thread_Manager.h" -#include "ace/Signal.h" - -ACE_RCSID(Threads, thread_manager, "$Id$") - -#if defined (ACE_HAS_THREADS) - -extern "C" void -handler (int signum) -{ - ACE_DEBUG ((LM_DEBUG, "(%t) received signal %d\n", signum)); -} - -static void * -worker (intptr_t iterations) -{ - for (intptr_t i = 0; i < iterations; i++) - { - if ((i % 1000) == 0) - { - ACE_DEBUG ((LM_DEBUG, - "(%t) checking cancellation before iteration %d!\n", - i)); - - if (ACE_Thread_Manager::instance ()->testcancel (ACE_Thread::self ()) != 0) - { - ACE_DEBUG ((LM_DEBUG, - "(%t) has been cancelled before iteration %d!\n", - i)); - break; - } - } - } - - // Destructor removes thread from Thread_Manager. - return 0; -} - -static const int DEFAULT_THREADS = ACE_DEFAULT_THREADS; -static const intptr_t DEFAULT_ITERATIONS = 100000; - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - ACE_Service_Config daemon; - - daemon.open (argv[0]); - - // Register a signal handler. - ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); - ACE_UNUSED_ARG (sa); - - int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : DEFAULT_THREADS; - intptr_t n_iterations = - argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_ITERATIONS; - - ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance (); - - int grp_id = thr_mgr->spawn_n (n_threads, ACE_THR_FUNC (worker), - reinterpret_cast<void *> (n_iterations), - THR_NEW_LWP | THR_DETACHED); - - // Wait for 1 second and then suspend every thread in the group. - ACE_OS::sleep (1); - ACE_DEBUG ((LM_DEBUG, "(%t) suspending group\n")); - if (thr_mgr->suspend_grp (grp_id) == -1) - ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "suspend_grp")); - - // Wait for 1 more second and then resume every thread in the - // group. - ACE_OS::sleep (ACE_Time_Value (1)); - ACE_DEBUG ((LM_DEBUG, "(%t) resuming group\n")); - if (thr_mgr->resume_grp (grp_id) == -1) - ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "resume_grp")); - - // Wait for 1 more second and then send a SIGINT to every thread in - // the group. - ACE_OS::sleep (ACE_Time_Value (1)); - ACE_DEBUG ((LM_DEBUG, "(%t) signaling group\n")); - if (thr_mgr->kill_grp (grp_id, SIGINT) == -1) - ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "kill_grp")); - - // Wait for 1 more second and then cancel all the threads. - ACE_OS::sleep (ACE_Time_Value (1)); - ACE_DEBUG ((LM_DEBUG, "(%t) cancelling group\n")); - if (thr_mgr->cancel_grp (grp_id) == -1) - ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "cancel_grp")); - - // Perform a barrier wait until all the threads have shut down. - thr_mgr->wait (); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR_RETURN ((LM_ERROR, "threads not supported on this platform\n"), -1); -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/thread_pool.cpp b/ACE/examples/Threads/thread_pool.cpp deleted file mode 100644 index 01013fee406..00000000000 --- a/ACE/examples/Threads/thread_pool.cpp +++ /dev/null @@ -1,277 +0,0 @@ -// $Id$ - -// This test program illustrates how the <ACE_Task> synchronization -// mechanisms work in conjunction with the <ACE_Thread_Manager>. If -// the <manual> flag is set input comes from stdin until the user -// enters a return -- otherwise, the input is generated automatically. -// All worker threads shutdown when they receive a message block of -// length 0. -// -// This code is original based on a test program written by Karlheinz -// Dorn <Karlheinz.Dorn@med.siemens.de>. It was modified to utilize -// more ACE features by Doug Schmidt <schmidt@cs.wustl.edu>. - -#include "ace/OS_NS_stdio.h" -#include "ace/OS_NS_string.h" -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Task.h" -#include "ace/Service_Config.h" - -ACE_RCSID(Threads, thread_pool, "$Id$") - -#if defined (ACE_HAS_THREADS) - -// Default number of iterations to run the test. -static int n_iterations = 100; - -// Controls whether the input is generated "manually" or automatically. -static int manual = 0; - -class Thread_Pool : public ACE_Task<ACE_MT_SYNCH> -{ - // = TITLE - // Defines a thread pool abstraction based on the <ACE_Task>. -public: - Thread_Pool (ACE_Thread_Manager *thr_mgr, - int n_threads); - // Constructor activates <n_threads> in the thread pool. - - ~Thread_Pool (void); - // Destructor... - - virtual int svc (void); - // Iterate <n_iterations> time printing off a message and "waiting" - // for all other threads to complete this iteration. - - virtual int put (ACE_Message_Block *mb, - ACE_Time_Value *tv = 0); - // This allows the producer to pass messages to the <Thread_Pool>. - -private: - //FUZZ: disable check_for_lack_ACE_OS - virtual int close (u_long); - // Close hook. - //FUZZ: enable check_for_lack_ACE_OS -}; - -int -Thread_Pool::close (u_long) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) worker thread closing down\n")); - return 0; -} - -Thread_Pool::Thread_Pool (ACE_Thread_Manager *thr_mgr, - int n_threads) - : ACE_Task<ACE_MT_SYNCH> (thr_mgr) -{ - // Create the pool of worker threads. - if (this->activate (THR_NEW_LWP, - n_threads) == -1) - ACE_ERROR ((LM_ERROR, - "%p\n", - "activate failed")); -} - -Thread_Pool::~Thread_Pool (void) -{ -} - -// Simply enqueue the Message_Block into the end of the queue. - -int -Thread_Pool::put (ACE_Message_Block *mb, - ACE_Time_Value *tv) -{ - return this->putq (mb, tv); -} - -// Iterate <n_iterations> time printing off a message and "waiting" -// for all other threads to complete this iteration. - -int -Thread_Pool::svc (void) -{ - // Note that the <ACE_Task::svc_run> method automatically adds us to - // the Thread_Manager when the thread begins. - - int count = 1; - - // Keep looping, reading a message out of the queue, until we get a - // message with a length == 0, which signals us to quit. - - for (;; count++) - { - ACE_Message_Block *mb; - - ACE_DEBUG ((LM_DEBUG, - "(%t) in iteration %d before getq ()\n", - count)); - - if (this->getq (mb) == -1) - { - ACE_ERROR ((LM_ERROR, - "(%t) in iteration %d, got result -1, exiting\n", - count)); - break; - } - - size_t length = mb->length (); - - if (length > 0) - ACE_DEBUG ((LM_DEBUG, - "(%t) in iteration %d, length = %d, text = \"%*s\"\n", - count, - length, - length - 1, - mb->rd_ptr ())); - - // We're responsible for deallocating this. - mb->release (); - - if (length == 0) - { - //FUZZ: disable check_for_NULL - ACE_DEBUG ((LM_DEBUG, - "(%t) in iteration %d, got NULL message, exiting\n", - count)); - //FUZZ: enable check_for_NULL - - break; - } - } - - // Note that the <ACE_Task::svc_run> method automatically removes us - // from the <ACE_Thread_Manager> when the thread exits. - return 0; -} - -static void -producer (Thread_Pool &thread_pool) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) producer start, generating data for the <Thread_Pool>\n")); - // thread_pool.dump (); - - for (int n; ;) - { - // Allocate a new message. - ACE_Message_Block *mb = 0; - ACE_NEW (mb, - ACE_Message_Block (BUFSIZ)); - - if (manual) - { - ACE_DEBUG ((LM_DEBUG, - "(%t) enter a new message for the task pool...")); - n = ACE_OS::read (ACE_STDIN, - mb->rd_ptr (), - mb->size ()); - } - else - { - static int count = 0; - - ACE_OS::sprintf (mb->rd_ptr (), - "%d\n", - count); - n = ACE_OS::strlen (mb->rd_ptr ()); - - if (count == n_iterations) - n = 1; // Indicate that we need to shut down. - else - count++; - - if (count == 0 || (count % 20 == 0)) - ACE_OS::sleep (1); - } - - if (n > 1) - { - // Send a normal message to the waiting threads and continue - // producing. - mb->wr_ptr (n); - - // Pass the message to the Thread_Pool. - if (thread_pool.put (mb) == -1) - ACE_ERROR ((LM_ERROR, - " (%t) %p\n", - "put")); - } - else - { - ACE_DEBUG ((LM_DEBUG, - "\n(%t) start loop, dump of task:\n")); - // thread_pool.dump (); - - // Send a shutdown message to the waiting threads and exit. - for (size_t i = thread_pool.thr_count (); i > 0; i--) - { - //FUZZ: disable check_for_NULL - ACE_DEBUG ((LM_DEBUG, - "(%t) EOF, enqueueing NULL block for thread = %d\n", - i)); - //FUZZ: enable check_for_NULL - - // Enqueue a NULL message to flag each consumer to - // shutdown. - ACE_Message_Block *mb = 0; - ACE_NEW (mb, - ACE_Message_Block); - if (thread_pool.put (mb) == -1) - ACE_ERROR ((LM_ERROR, - " (%t) %p\n", - "put")); - } - - ACE_DEBUG ((LM_DEBUG, - "\n(%t) end loop\n")); - // thread_pool.dump (); - break; - } - } -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - int n_threads = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_THREADS; - n_iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : n_iterations; - manual = argc > 3 ? 1 : 0; - - ACE_DEBUG ((LM_DEBUG, - "(%t) argc = %d, threads = %d\n", - argc, - n_threads)); - - // Create the worker tasks. - Thread_Pool thread_pool (ACE_Thread_Manager::instance (), - n_threads); - - // Create work for the worker tasks to process in their own threads. - producer (thread_pool); - - ACE_DEBUG ((LM_DEBUG, - "(%t) waiting for threads to exit in Thread_Pool destructor...\n")); - // Wait for all the threads to reach their exit point. - if (thread_pool.wait () == -1) - //FUZZ: disable check_for_lack_ACE_OS - ACE_ERROR_RETURN ((LM_ERROR, "(%t) wait() failed\n"), - 1); - //FUZZ: enable check_for_lack_ACE_OS - - ACE_DEBUG ((LM_DEBUG, - "(%t) destroying worker tasks and exiting...\n")); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, - "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/thread_specific.cpp b/ACE/examples/Threads/thread_specific.cpp deleted file mode 100644 index 316b7717713..00000000000 --- a/ACE/examples/Threads/thread_specific.cpp +++ /dev/null @@ -1,238 +0,0 @@ -// $Id$ - -#include "ace/OS_NS_stdio.h" -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Thread_Manager.h" -#include "ace/Signal.h" - -ACE_RCSID(Threads, thread_specific, "$Id$") - -#if defined (ACE_HAS_THREADS) - -#include "thread_specific.h" - -// Static variables. -ACE_MT (ACE_Thread_Mutex Errno::lock_); -int Errno::flags_; - -// This is our thread-specific error handler... -static ACE_TSS<Errno> tss_error; - -// Serializes output via cout. -static ACE_SYNCH_MUTEX printf_lock; - -#if defined (ACE_HAS_THREADS) -typedef ACE_TSS_Guard<ACE_Thread_Mutex> GUARD; -#else -typedef ACE_Guard<ACE_Null_Mutex> GUARD; -#endif /* ACE_HAS_THREADS */ - -extern "C" void -cleanup (void *ptr) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) in cleanup, ptr = %x\n", - ptr)); - - delete reinterpret_cast<char *> (ptr); -} - -// This worker function is the entry point for each thread. - -static void * -worker (void *c) -{ - intptr_t count = reinterpret_cast<intptr_t> (c); - - ACE_thread_key_t key = ACE_OS::NULL_key; - int *ip = 0; - - // Make one key that will be available when the thread exits so that - // we'll have something to cleanup! - - if (ACE_Thread::keycreate (&key, cleanup) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::keycreate")); - - ACE_NEW_RETURN (ip, - int, - 0); - - if (ACE_Thread::setspecific (key, (void *) ip) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::setspecific")); - - for (intptr_t i = 0; i < count; i++) - { - if (ACE_Thread::keycreate (&key, cleanup) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::keycreate")); - - ACE_NEW_RETURN (ip, - int, - 0); - - ACE_DEBUG ((LM_DEBUG, - "(%t) in worker 1, key = %d, ip = %x\n", - key, - ip)); - - { - // tmp is workaround for gcc strict aliasing warning. - void *tmp = reinterpret_cast <void *> (ip); - - if (ACE_Thread::setspecific (key, tmp) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::setspecific")); - - if (ACE_Thread::getspecific (key, &tmp) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::setspecific")); - - if (ACE_Thread::setspecific (key, (void *) 0) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::setspecific")); - } - - delete ip; - - if (ACE_Thread::keyfree (key) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::keyfree")); - - // Cause an error. - ACE_OS::read (ACE_INVALID_HANDLE, 0, 0); - - // The following two lines set the thread-specific state. - tss_error->error (errno); - tss_error->line (__LINE__); - - // This sets the static state (note how C++ makes it easy to do - // both). - tss_error->flags (count); - - { - ACE_hthread_t handle; - ACE_Thread::self (handle); - - // Use the guard to serialize access to printf... - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, printf_lock, 0); - -#if defined(linux) || defined(__OpenBSD__) - // @@ Normally the platform specific way to print a thread ID - // is encapsulated in Log_Msg.cpp, but for this small example - // we cannot (or do not want to) use ACE_Log_Msg. - ACE_OS::printf ("(%lu)", (unsigned long)handle); -#else - ACE_OS::printf ("(%u)", handle); -#endif /* ! linux */ - ACE_OS::printf (" errno = %d, lineno = %d, flags = %d\n", - tss_error->error (), - tss_error->line (), - tss_error->flags ()); - } - key = ACE_OS::NULL_key; - - if (ACE_Thread::keycreate (&key, cleanup) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::keycreate")); - - ACE_NEW_RETURN (ip, - int, - 0); - - ACE_DEBUG ((LM_DEBUG, - "(%t) in worker 2, key = %d, ip = %x\n", - key, - ip)); - - { - // Tmp is workaround for GCC strict aliasing warning. - void *tmp (reinterpret_cast <void *> (ip)); - - if (ACE_Thread::setspecific (key, tmp) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::setspecific")); - - if (ACE_Thread::getspecific (key, &tmp) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::setspecific")); - - if (ACE_Thread::setspecific (key, (void *) 0) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::setspecific")); - } - - delete ip; - - if (ACE_Thread::keyfree (key) == -1) - ACE_ERROR ((LM_ERROR, - "(%t) %p\n", - "ACE_Thread::keyfree")); - } - - ACE_DEBUG ((LM_DEBUG, - "(%t) exiting\n")); - return 0; -} - -extern "C" void -handler (int signum) -{ - ACE_DEBUG ((LM_DEBUG, - "signal = %S\n", signum)); - ACE_Thread_Manager::instance ()->exit (0); -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - // The Service_Config must be the first object defined in main... - ACE_Service_Config daemon (argv[0]); - - int threads = argc > 1 ? ACE_OS::atoi (argv[1]) : 4; - intptr_t count = argc > 2 ? ACE_OS::atoi (argv[2]) : 10000; - - // Register a signal handler. - ACE_Sig_Action sa ((ACE_SignalHandler) (handler), SIGINT); - ACE_UNUSED_ARG (sa); - -#if defined (ACE_HAS_THREADS) - if (ACE_Thread_Manager::instance ()->spawn_n (threads, - ACE_THR_FUNC (&worker), - reinterpret_cast<void *> (count), - THR_BOUND | THR_DETACHED) == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "%p\n", - "ACE_Thread_Manager::spawn_n"), - -1); - - ACE_Thread_Manager::instance ()->wait (); -#else - worker ((void *) count); -#endif /* ACE_HAS_THREADS */ - return 0; -} - -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR_RETURN ((LM_ERROR, - "ACE doesn't support support threads on this platform (yet)\n"), - -1); -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/thread_specific.h b/ACE/examples/Threads/thread_specific.h deleted file mode 100644 index 1ae48ee114f..00000000000 --- a/ACE/examples/Threads/thread_specific.h +++ /dev/null @@ -1,53 +0,0 @@ -// $Id$ - -#ifndef ACE_THREAD_SPECIFIC_H - -#include "ace/Guard_T.h" -#include "ace/Thread_Mutex.h" - -// Define a class that will be stored in thread-specific data. Note -// that as far as this class is concerned it's just a regular C++ -// class. The ACE_TSS wrapper transparently ensures that objects of -// this class will be placed in thread-specific storage. All calls on -// ACE_TSS::operator->() are delegated to the appropriate method in -// the Errno class. - -class Errno -{ -public: - int error (void) { return this->errno_; } - void error (int i) { this->errno_ = i; } - - int line (void) { return this->lineno_; } - void line (int l) { this->lineno_ = l; } - - // Errno::flags_ is a static variable, so we've got to protect it - // with a mutex since it isn't kept in thread-specific storage. - int flags (void) - { - ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, Errno::lock_, -1); - - return Errno::flags_; - } - - void flags (int f) - { - ACE_GUARD (ACE_Thread_Mutex, ace_mon, Errno::lock_); - - Errno::flags_ = f; - } - -private: - // = errno_ and lineno_ will be thread-specific data so they don't - // need a lock. - int errno_; - int lineno_; - - static int flags_; -#if defined (ACE_HAS_THREADS) - // flags_ needs a lock. - static ACE_Thread_Mutex lock_; -#endif /* ACE_HAS_THREADS */ -}; - -#endif /* ACE_THREAD_SPECIFIC_H */ diff --git a/ACE/examples/Threads/token.cpp b/ACE/examples/Threads/token.cpp deleted file mode 100644 index bbda579369b..00000000000 --- a/ACE/examples/Threads/token.cpp +++ /dev/null @@ -1,78 +0,0 @@ -// $Id$ - -// Test out the ACE Token class. - -#include "ace/OS_main.h" -#include "ace/Token.h" -#include "ace/Task.h" -#include "ace/OS_NS_time.h" - -ACE_RCSID(Threads, token, "$Id$") - -#if defined (ACE_HAS_THREADS) - -class My_Task : public ACE_Task<ACE_MT_SYNCH> -{ -public: - My_Task (int n); - virtual int svc (void); - - static void sleep_hook (void *); - -private: - ACE_Token token_; -}; - -My_Task::My_Task (int n) -{ - // Make this Task into an Active Object. - this->activate (THR_BOUND | THR_DETACHED, n); - - // Wait for all the threads to exit. - this->thr_mgr ()->wait (); -} - -void -My_Task::sleep_hook (void *) -{ - ACE_DEBUG ((LM_ERROR, "(%u) blocking, My_Task::sleep_hook () called\n", - ACE_Thread::self())) ; -} - -// Test out the behavior of the ACE_Token class. - -int -My_Task::svc (void) -{ - for (size_t i = 0; i < 100; i++) - { - // Wait for up to 1 millisecond past the current time to get the token. - ACE_Time_Value timeout (ACE_OS::time (0), 1000); - - if (this->token_.acquire (&My_Task::sleep_hook, 0, &timeout) == 1) - { - this->token_.acquire (); - this->token_.renew (); - this->token_.release (); - this->token_.release (); - } - else - ACE_Thread::yield (); - } - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - My_Task tasks (argc > 1 ? ACE_OS::atoi (argv[1]) : 4); - - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR_RETURN ((LM_ERROR, "your platform doesn't support threads\n"), -1); -} -#endif /* */ diff --git a/ACE/examples/Threads/tss1.cpp b/ACE/examples/Threads/tss1.cpp deleted file mode 100644 index edf1ee37164..00000000000 --- a/ACE/examples/Threads/tss1.cpp +++ /dev/null @@ -1,159 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// tests -// -// = FILENAME -// TSS_Test.cpp -// -// = DESCRIPTION -// This program tests thread specific storage of data. The ACE_TSS -// wrapper transparently ensures that the objects of this class -// will be placed in thread-specific storage. All calls on -// ACE_TSS::operator->() are delegated to the appropriate method -// in the Errno class. Note that each thread of control has its -// own unique TSS object. -// -// = AUTHOR -// Detlef Becker <Detlef.Becker@med.siemens.de> -// -// ============================================================================ - -#include "ace/OS_main.h" -#include "ace/Service_Config.h" -#include "ace/Task.h" - -ACE_RCSID(Threads, tss1, "$Id$") - -#if defined (ACE_HAS_THREADS) - -#include "thread_specific.h" - -// (Sun C++ 4.2 with -O3 won't link if the following is not const.) -static const int iterations = 100; - -// Static variables. -ACE_MT (ACE_Thread_Mutex Errno::lock_); -int Errno::flags_; - -// This is our thread-specific error handler... -// (Sun C++ 4.2 with -O3 won't link if the following is static.) -ACE_TSS<Errno> TSS_Error; - -#if defined (ACE_HAS_THREADS) - typedef ACE_TSS_Guard<ACE_Thread_Mutex> GUARD; -#else - typedef ACE_Guard<ACE_Null_Mutex> GUARD; -#endif /* ACE_HAS_THREADS */ - -// Keeps track of whether Tester::close () has started. -// (Sun C++ 4.2 with -O3 won't link if the following is static.) -int close_started = 0; - -template <ACE_SYNCH_DECL> -class Tester: public ACE_Task<ACE_SYNCH_USE> -{ -public: - Tester (void) {} - ~Tester (void) {} - - virtual int svc (void); - - //FUZZ: disable check_for_lack_ACE_OS - virtual int open (void *args = 0); - // Activate the thread. - - virtual int close (u_long args = 0); - //FUZZ: enable check_for_lack_ACE_OS -}; - -template <ACE_SYNCH_DECL> int -Tester<ACE_SYNCH_USE>::svc (void) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) svc: setting error code to 1\n")); - TSS_Error->error (1); - - for (int i = 0; i < iterations; i++) - // Print out every tenth iteration. - if ((i % 10) == 1) - ACE_DEBUG ((LM_DEBUG, - "(%t) error = %d\n", - TSS_Error->error ())); - this->close (); - - return 0; -} - -template <ACE_SYNCH_DECL> int -Tester<ACE_SYNCH_USE>::open (void *) -{ - // Make this an Active Object. - return this->activate (); -} - -template <ACE_SYNCH_DECL> -int Tester<ACE_SYNCH_USE>::close (u_long) -{ - ACE_DEBUG ((LM_DEBUG, - "(%t) close running\n")); - close_started = 1; - ACE_DEBUG ((LM_DEBUG, - "(%t) close: setting error code to 7\n")); - TSS_Error->error (7); - ACE_DEBUG ((LM_DEBUG, - "(%t) close: error = %d\n", - TSS_Error->error ())); - //close_started = 0; - return 0; -} - -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - Tester<ACE_MT_SYNCH> tester; - - ACE_DEBUG ((LM_DEBUG, - "(%t) main: setting error code to 3\n")); - TSS_Error->error (3); - ACE_DEBUG ((LM_DEBUG, - "(%t) main: error = %d\n", - TSS_Error->error ())); - - // Spawn off a thread and make test an Active Object. - tester.open (); - - // Keep looping until <Tester::close> is called. - for (int i = 0; !close_started; i++) { - // while (!close_started) - if ((i % 100) == 0) { - ACE_DEBUG ((LM_DEBUG, - "(%t) error = %d\n", - TSS_Error->error ())); - } - } - ACE_DEBUG ((LM_DEBUG, - "(%t) main: setting error code to 4\n")); - TSS_Error->error (4); - ACE_DEBUG ((LM_DEBUG, - "(%t) main: error = %d\n", - TSS_Error->error ())); - - // Keep looping until <Tester::close> finishes. - while (close_started != 0) - ACE_DEBUG ((LM_DEBUG, - "(%t) error = %d\n", - TSS_Error->error ())); - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR_RETURN ((LM_ERROR, - "ACE doesn't support support threads on this platform (yet)\n"), - -1); -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/tss2.cpp b/ACE/examples/Threads/tss2.cpp deleted file mode 100644 index 0c7fb9ac3ee..00000000000 --- a/ACE/examples/Threads/tss2.cpp +++ /dev/null @@ -1,190 +0,0 @@ -// $Id$ - -// ============================================================================ -// -// = LIBRARY -// tests -// -// = FILENAME -// TSS_Test.cpp -// -// = DESCRIPTION -// This program tests various features of ACE_Thread and the -// thread-specific storage variant of <ACE_Singleton>. -// -// = AUTHOR -// Prashant Jain and Doug Schmidt -// -// ============================================================================ - -#include "ace/OS_main.h" -#include "ace/Thread.h" -#include "ace/Log_Msg.h" -#include "ace/Atomic_Op.h" -#include "TSS_Data.h" -#include "TSS_Obj.h" -#include "TSS_Task.h" - -ACE_RCSID(Threads, tss2, "$Id$") - -#if defined (ACE_HAS_THREADS) - -const int MAX_TASKS = 4; -const int MAX_ITERATIONS = 10; - -ACE_Atomic_Op<ACE_Token, int> Test_Task::count_ (0); -ACE_Atomic_Op<ACE_Token, int> Test_Task::wait_count_ (0); -ACE_Atomic_Op<ACE_Token, int> Test_Task::max_count_ (0); -int num_tasks = 0; - -// ACE synchronization object. -static ACE_Token token; - -ACE_Atomic_Op<ACE_Thread_Mutex, int> TSS_Obj::count_ = 0; - -TSS_Obj::TSS_Obj (void) -{ - TSS_Obj::count_++; - ACE_DEBUG ((LM_DEBUG, "(%t) TSS_Obj+: %d\n", TSS_Obj::count_.value ())); -} - -TSS_Obj::~TSS_Obj (void) -{ - TSS_Obj::count_--; - ACE_DEBUG ((LM_DEBUG, "(%t) TSS_Obj-: %d\n", TSS_Obj::count_.value ())); -} - -Test_Task::Test_Task (void) -{ - Test_Task::count_++; - ACE_DEBUG ((LM_DEBUG, - "(%t) Test_Task+: %d\n", Test_Task::count_.value ())); -} - -Test_Task::~Test_Task (void) -{ - Test_Task::count_--; - - ACE_DEBUG ((LM_DEBUG, - "(%t) Test_Task-: %d\n", Test_Task::count_.value ())); - Test_Task::wait_count_--; -} - -void * -Test_Task::svc (void *arg) -{ - // When the thread exits this thread-specific object will be deleted - // automatically. - ACE_TSS<TSS_Obj> tss (new TSS_Obj); - - TSS_DATA::instance ()->data (arg); - - Test_Task::wait_count_++; - Test_Task::max_count_++; - - ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting (data = %u)\n", - arg)); - - // Do a bunch of set operations on the TSS data just to make sure - // that it's truly in TSS (it it weren't, the assertion would fail). - - while (Test_Task::max_count_ < num_tasks) - { - TSS_DATA::instance ()->data (arg); - ACE_Thread::yield (); - } - - ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting finished (data = %u)\n", - arg)); - -#if 0 - ACE_ASSERT (TSS_DATA::instance ()->data () == arg); -#endif - - delete (Test_Task *) arg; - - return 0; -} - -int -Test_Task::open (void *arg) -{ - if (ACE_Thread::spawn ((ACE_THR_FUNC) Test_Task::svc, arg) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Thread::spawn"), 0); - - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - num_tasks = argc > 1 ? ACE_OS::atoi (argv[1]) : MAX_TASKS; - - Test_Task **task_arr = 0; - - ACE_NEW_RETURN (task_arr, Test_Task *[num_tasks], -1); - - for (int i = 0; i < MAX_ITERATIONS; i++) - { - ACE_DEBUG ((LM_DEBUG, - "(%t) ********* iteration %d **********\n" - "Test_Task::max_count_ %d\n", - i, - Test_Task::max_count_.value ())); - Test_Task::max_count_ = 0; - - for (int j = 0; j < num_tasks; j++) - { - ACE_NEW_RETURN (task_arr[j], Test_Task, -1); - task_arr[j]->open (task_arr[j]); - } - - ACE_DEBUG ((LM_DEBUG, "(%t) waiting for first thread started\n")); - - for (;;) - { - ACE_Thread::yield (); - - if (Test_Task::max_count_ != 0 ) - break; - } - - ACE_DEBUG ((LM_DEBUG, "(%t) First thread started\n" - "Waiting for all threads finished\n")); - - for (;;) - { - if (!(Test_Task::max_count_ == num_tasks - && Test_Task::wait_count_ == 0)) - { - ACE_Thread::yield (); - continue; - } - ACE_DEBUG ((LM_DEBUG, - "(%t) Test_Task::max_count_ = %d," - " Test_Task::wait_count_ = %d", - Test_Task::max_count_.value (), - Test_Task::wait_count_.value ())); - break; - } - - ACE_DEBUG ((LM_DEBUG, "(%t) all threads finished\n")); - } - - return 0; -} - -#if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION) -template ACE_TSS_Singleton<TSS_Data, ACE_SYNCH_MUTEX> * - ACE_TSS_Singleton<TSS_Data, ACE_SYNCH_MUTEX>::singleton_; -#endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */ - -#else - -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); - return 0; -} -#endif /* ACE_HAS_THREADS */ diff --git a/ACE/examples/Threads/wfmo.cpp b/ACE/examples/Threads/wfmo.cpp deleted file mode 100644 index d7336d4e831..00000000000 --- a/ACE/examples/Threads/wfmo.cpp +++ /dev/null @@ -1,135 +0,0 @@ -// $Id$ - -// This test program illustrates that the Win32 -// <WaitForMultipleObjects> function can be called in multiple -// threads, all of which wait on the same set of HANDLEs. Note that -// the dispatching of the threads should be relatively "fair" (i.e., -// everyone gets a chance to process the various HANDLEs as they -// become active). Thanks to Ari Erev <Ari_Erev@comverse.com> for -// suggesting this and providing the initial code. - -#include "ace/Task.h" -#include "ace/OS_NS_unistd.h" -#include "ace/OS_main.h" - -ACE_RCSID(Threads, wfmo, "$Id$") - -#if defined (ACE_WIN32) - -// Number of threads. -static const int THREAD_COUNT = 5; - -// Number of iterations. -static const int MAX_ITERATIONS = 100; - -class WFMO_Test : public ACE_Task <ACE_NULL_SYNCH> -{ -public: - //FUZZ: disable check_for_lack_ACE_OS - virtual int open (void *); - //FUZZ: enable check_for_lack_ACE_OS - - virtual int svc (void); - - // Use two handles here.. - ACE_sema_t sema_handles_[2]; - int semaphore_count_; -}; - -static WFMO_Test wfmo_test; - -int -WFMO_Test::open (void *arg) -{ - int thread_count = int (arg); - int result = this->activate (0, thread_count); - - ACE_ASSERT (result != -1); - return 0; -} - -int -WFMO_Test::svc (void) -{ - while(1) - { - int result = ::WaitForMultipleObjects (2, this->sema_handles_, - FALSE, INFINITE); - if (result == WAIT_OBJECT_0) - { - // Signal the other semaphore just to see if we can get - // another thread to wakeup. - result = ACE_OS::sema_post (&sema_handles_[1]); - ACE_ASSERT (result != -1); - } - else if (result == WAIT_OBJECT_0 + 1) - ; - else - { - ACE_ERROR ((LM_ERROR, "Error in WaitForMultipleObejcts\n")); - ACE_OS::exit (0); - } - - // semaphore_count_ will be displayed by the "main" thread. - // It's value must be 2. Note that although this is a shared - // resource it's not protected via a mutex because the ++ - // operation on Intel is atomic. - - semaphore_count_++; - ACE_DEBUG ((LM_DEBUG, - "(%t) thread has been signaled.\n")); - - // Yield this thread so that the other one(s) have a chance to - // run. - ACE_OS::thr_yield (); - } - - return 0; -} - -int -ACE_TMAIN (int argc, ACE_TCHAR *argv[]) -{ - int thread_count = THREAD_COUNT; - - if (argc > 1) - thread_count = ACE_OS::atoi (argv[1]); - - wfmo_test.open ((void *) thread_count); - - // Initialize the semaphores. - int result = ACE_OS::sema_init (&wfmo_test.sema_handles_[0], thread_count + 5); - ACE_ASSERT (result != -1); - - result = ACE_OS::sema_init (&wfmo_test.sema_handles_[1], thread_count + 5); - ACE_ASSERT (result != -1); - - for (int i = 0; i < MAX_ITERATIONS; i++) - { - wfmo_test.semaphore_count_ = 0; - - result = ACE_OS::sema_post (&wfmo_test.sema_handles_[0]); - ACE_ASSERT (result != -1); - - // No real synchronization here. Just sleep enough so that at - // least one (or two threads) run as a result of the semaphore. - ACE_OS::sleep (1); - - // Add one for the other thread that was signaled. - ACE_DEBUG ((LM_DEBUG, - "semaphore_count_ = %d (should have been %d).\n", - wfmo_test.semaphore_count_, - 2)); // Two semaphores should have been released. - } - - ACE_OS::exit (0); - - return 0; -} -#else -int -ACE_TMAIN (int, ACE_TCHAR *[]) -{ - ACE_DEBUG ((LM_DEBUG, "this test only runs on Win32\n")); -} -#endif /* ACE_WIN32 */ |