diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 1998-01-02 06:38:41 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 1998-01-02 06:38:41 +0000 |
commit | f611d5dca8cc705a728dafc8d0cd80cdfb93182e (patch) | |
tree | 056ab9034763cfb3f3ce929ac991503fb3459334 /tests | |
parent | 242a9609919b5b0e00731108069b99c4d7b8be8f (diff) | |
download | ATCD-f611d5dca8cc705a728dafc8d0cd80cdfb93182e.tar.gz |
*** empty log message ***
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Reader_Writer_Test.cpp | 159 |
1 files changed, 97 insertions, 62 deletions
diff --git a/tests/Reader_Writer_Test.cpp b/tests/Reader_Writer_Test.cpp index b9e58cf8bfe..4b85fdabf5a 100644 --- a/tests/Reader_Writer_Test.cpp +++ b/tests/Reader_Writer_Test.cpp @@ -42,19 +42,19 @@ static size_t n_writers = 4; static ACE_thread_t shared_data; // Lock for shared_data. -static ACE_RW_Mutex rw_mutex; +static ACE_RW_Thread_Mutex rw_mutex; // Count of the number of readers and writers. -static ACE_Atomic_Op<ACE_Thread_Mutex, int> current_readers; -static ACE_Atomic_Op<ACE_Thread_Mutex, int> current_writers; +static ACE_Atomic_Op<ACE_Thread_Mutex, int> current_readers = 0; +static ACE_Atomic_Op<ACE_Thread_Mutex, int> current_writers = 0; // 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); + ACE_ERROR ((LM_ERROR, + "usage: %n [-r n_readers] [-w n_writers] [-n iteration_count]\n%a", + 1)); } static void @@ -88,21 +88,23 @@ parse_args (int argc, char *argv[]) static void * reader (void *) { - ACE_DEBUG ((LM_DEBUG, " (%t) reader starting\n")); + ACE_DEBUG ((LM_DEBUG, + "(%t) reader starting\n")); // We use a random pause, around 2msec with 1msec jittering. - int usecs = 1000 + ACE_OS::rand() % 2000; - ACE_Time_Value pause(0, usecs); + int usecs = 1000 + ACE_OS::rand () % 2000; + ACE_Time_Value pause (0, usecs); - for (size_t iterations = 1; iterations <= n_iterations; iterations++) + for (size_t iterations = 1; + iterations <= n_iterations; + iterations++) { - ACE_OS::sleep(pause); + ACE_OS::sleep (pause); ACE_Read_Guard<ACE_RW_Mutex> g (rw_mutex); - // int n = ++current_readers; - // ACE_DEBUG ((LM_DEBUG, " (%t) I'm reader number %d\n", n)); if (current_writers > 0) - ACE_DEBUG ((LM_DEBUG, " (%t) writers found!!!\n")); + ACE_DEBUG ((LM_DEBUG, + "(%t) writers found!!!\n")); ACE_thread_t data = shared_data; @@ -110,73 +112,106 @@ reader (void *) { ACE_Thread::yield (); - if (!ACE_OS::thr_equal (shared_data, data)) + if (ACE_OS::thr_equal (shared_data, data) == 0) ACE_DEBUG ((LM_DEBUG, - " (%t) somebody changed %d to %d\n", - data, shared_data)); + "(%t) somebody changed %d to %d\n", + data, + shared_data)); + } + + if (rw_mutex.tryacquire_write_upgrade () == 0) + { + current_writers++; + + ACE_DEBUG ((LM_DEBUG, + "(%t) upgraded to write lock!\n")); + + ACE_thread_t self = ACE_Thread::self (); + + shared_data = self; + + for (size_t loop = 1; loop <= n_loops; loop++) + { + if (ACE_OS::thr_equal (shared_data, data) == 0) + ACE_DEBUG ((LM_DEBUG, + "(%t) somebody changed %d to %d\n", + data, + shared_data)); + } + + --current_writers; } --current_readers; - //ACE_DEBUG ((LM_DEBUG, " (%t) done with reading guarded data\n")); - ACE_DEBUG((LM_DEBUG, " (%t) read %d done at %T\n", iterations)); - // ACE_Thread::yield (); + ACE_DEBUG ((LM_DEBUG, + "(%t) read %d done at %T\n", + iterations)); } return 0; } -// Iterate <n_iterations> each time modifying the global data -// and checking that nobody steps on it while we can write it. +// 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")); + ACE_DEBUG ((LM_DEBUG, + "(%t) writer starting\n")); // We use a random pause, around 2msec with 1msec jittering. - int usecs = 1000 + ACE_OS::rand() % 2000; - ACE_Time_Value pause(0, usecs); + int usecs = 1000 + ACE_OS::rand () % 2000; + ACE_Time_Value pause (0, usecs); - for (size_t iterations = 1; iterations <= n_iterations; iterations++) + for (size_t iterations = 1; + iterations <= n_iterations; + iterations++) { - ACE_OS::sleep(pause); - - ACE_Write_Guard<ACE_RW_Mutex> g (rw_mutex); + ACE_OS::sleep (pause); - ++current_writers; - //ACE_DEBUG ((LM_DEBUG, " (%t) writing to guarded data\n")); + { + // Add an additional scope here to bound the duration that the + // write lock is held. + ACE_Write_Guard<ACE_RW_Mutex> g (rw_mutex); - if (current_writers > 1) - ACE_DEBUG ((LM_DEBUG, " (%t) other writers found!!!\n")); + ++current_writers; - if (current_readers > 0) - ACE_DEBUG ((LM_DEBUG, " (%t) readers found!!!\n")); + if (current_writers > 1) + ACE_DEBUG ((LM_DEBUG, + "(%t) other writers found!!!\n")); - ACE_thread_t self = ACE_Thread::self (); + if (current_readers > 0) + ACE_DEBUG ((LM_DEBUG, + "(%t) readers found!!!\n")); - shared_data = self; + ACE_thread_t self = ACE_Thread::self (); - for (size_t loop = 1; loop <= n_loops; loop++) - { - ACE_Thread::yield (); + shared_data = self; - if (!ACE_OS::thr_equal (shared_data, self)) - ACE_DEBUG ((LM_DEBUG, - " (%t) somebody wrote on my data %d\n", - shared_data)); - } + for (size_t loop = 1; + loop <= n_loops; + loop++) + { + ACE_Thread::yield (); - --current_writers; + if (ACE_OS::thr_equal (shared_data, self) == 0) + ACE_DEBUG ((LM_DEBUG, + "(%t) somebody wrote on my data %d\n", + shared_data)); + } - //ACE_DEBUG ((LM_DEBUG, " (%t) done with guarded data\n")); + --current_writers; + } - ACE_DEBUG((LM_DEBUG, " (%t) write %d done at %T\n", iterations)); - // ACE_Thread::yield (); + ACE_DEBUG ((LM_DEBUG, + "(%t) write %d done at %T\n", + iterations)); } + return 0; } - #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION) template class ACE_Atomic_Op<ACE_Thread_Mutex, int>; template class ACE_Read_Guard<ACE_RW_Mutex>; @@ -200,29 +235,29 @@ int main (int argc, char *argv[]) #if defined (ACE_HAS_THREADS) 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")); + ACE_DEBUG ((LM_DEBUG, + "(%t) main thread starting\n")); if (ACE_Thread_Manager::instance ()->spawn_n (n_readers, - ACE_THR_FUNC (reader), - 0, - THR_NEW_LWP) == -1) + ACE_THR_FUNC (reader), + 0, + THR_NEW_LWP) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn_n"), 1); else if (ACE_Thread_Manager::instance ()->spawn_n (n_writers, - ACE_THR_FUNC (writer), - 0, - THR_NEW_LWP) == -1) + ACE_THR_FUNC (writer), + 0, + THR_NEW_LWP) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn_n"), 1); ACE_Thread_Manager::instance ()->wait (); - ACE_DEBUG ((LM_DEBUG, " (%t) exiting main thread\n")); + ACE_DEBUG ((LM_DEBUG, + "(%t) exiting main thread\n")); #else ACE_UNUSED_ARG (argc); ACE_UNUSED_ARG (argv); - ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n")); + ACE_ERROR ((LM_ERROR, + "threads not supported on this platform\n")); #endif /* ACE_HAS_THREADS */ ACE_END_TEST; return 0; |