From d3355fd45d2b9c5a982a6bf0f64885188afd24d1 Mon Sep 17 00:00:00 2001 From: schmidt Date: Mon, 2 Dec 1996 05:11:41 +0000 Subject: foo --- tests/SV_Shared_Memory_Test.cpp | 112 ++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 33 deletions(-) (limited to 'tests/SV_Shared_Memory_Test.cpp') diff --git a/tests/SV_Shared_Memory_Test.cpp b/tests/SV_Shared_Memory_Test.cpp index d05bce465d9..f04931aba56 100644 --- a/tests/SV_Shared_Memory_Test.cpp +++ b/tests/SV_Shared_Memory_Test.cpp @@ -9,7 +9,8 @@ // SV_Shared_Memory_Test.cpp // // = DESCRIPTION -// This is a simple test of ACE_SV_Shared_Memory +// This is a simple test of ACE_SV_Shared_Memory and ACE_Malloc +// using the ACE_Shared_Memory_Pool. // // = AUTHOR // Prashant Jain and Doug Schmidt @@ -20,65 +21,110 @@ #include "ace/SV_Shared_Memory.h" #include "test_config.h" +#if defined (ACE_HAS_SYSV_IPC) const int SHMSZ = 27; -const int SEM_KEY = 1234; -const int SHM_KEY = 5678; +const int SEM_KEY = ACE_DEFAULT_SEM_KEY; +const int SHM_KEY = ACE_DEFAULT_SHM_KEY; -static void -client (void) -{ - ACE_SV_Shared_Memory shm_client; - char t = 'a'; +// Shared memory allocator (note that this chews up the +// ACE_DEFAULT_SEM_KEY). +static ACE_Malloc allocator; - ACE_ASSERT (shm_client.open_and_attach (SHM_KEY, SHMSZ, ACE_SV_Shared_Memory::ACE_CREATE) != -1); - - for (char *s = (char *) shm_client.get_segment_ptr (); *s != '\0'; s++) - { - ACE_ASSERT (t == s[0]); - t++; - } - - *(char *) shm_client.get_segment_ptr () = '*'; - ACE_OS::exit (0); -} +const int SEM_KEY_1 = ACE_DEFAULT_SEM_KEY + 1; +const int SEM_KEY_2 = ACE_DEFAULT_SEM_KEY + 2; +const int SHMSZ = 27; -static void -server (void) +static int +parent (char *shm) { - ACE_SV_Shared_Memory shm_server; + ACE_SV_Semaphore_Complex mutex; + ACE_ASSERT (mutex.open (SEM_KEY_1, + ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1); - ACE_ASSERT (shm_server.open_and_attach (SHM_KEY, SHMSZ, ACE_SV_Shared_Memory::ACE_CREATE) != -1); + ACE_SV_Semaphore_Complex synch; + ACE_ASSERT (synch.open (SEM_KEY_2, + ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1); - char *s = (char *) shm_server.get_segment_ptr (); + char *s = shm; for (char c = 'a'; c <= 'z'; c++) *s++ = c; *s = '\0'; - for (s = (char *) shm_server.get_segment_ptr (); *s != '*'; ) - ACE_OS::sleep (1); + if (mutex.release () == -1) + ACE_ERROR ((LM_ERROR, "%p", "parent mutex.release")); + else if (synch.acquire () == -1) + ACE_ERROR ((LM_ERROR, "%p", "parent synch.acquire")); + + if (allocator.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "allocator.remove")); + if (mutex.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "mutex.remove")); + if (synch.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "synch.remove")); + return 0; +} + +static int +child (char *shm) +{ + ACE_SV_Semaphore_Complex mutex; + ACE_ASSERT (mutex.open (SEM_KEY_1, + ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1); + + ACE_SV_Semaphore_Complex synch; + ACE_ASSERT (synch.open (SEM_KEY_2, + ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1); - if (shm_server.remove () < 0) - ACE_OS::perror ("remove"), ACE_OS::exit (1); + while (mutex.tryacquire () == -1) + if (errno == EAGAIN) + ACE_DEBUG ((LM_DEBUG, "spinning in child!\n")); + else + ACE_ERROR_RETURN ((LM_ERROR, "child mutex.tryacquire"), 1); + + char t = 'a'; + for (char *s = (char *) shm; *s != '\0'; s++) + { + ACE_ASSERT (t == s[0]); + t++; + } + + ACE_DEBUG ((LM_DEBUG, "\n")); + + if (synch.release () == -1) + ACE_ERROR_RETURN ((LM_ERROR, "child synch.release"), 1); + return 0; } +#endif /* ACE_HAS_SYSV_IPC */ int main (int, char *argv[]) { ACE_START_TEST ("SV_Shared_Memory_Test.cpp"); +#if defined (ACE_HAS_SYSV_IPC) + char *shm = (char *) allocator.malloc (27); + switch (ACE_OS::fork ()) { case -1: - ACE_ERROR ((LM_ERROR, "%p%a", "main", 1)); - case 0: - client (); + ACE_ERROR_RETURN ((LM_ERROR, "fork failed\n"), -1); + /* NOTREACHED */ + case 0: + // Child. + child (shm); default: - server (); + parent (shm); } - +#else + ACE_ERROR ((LM_ERROR, + "SYSV IPC is not supported on this platform\n")); +#endif /* ACE_HAS_SYSV_IPC */ ACE_END_TEST; return 0; } +#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) +template class ACE_Malloc; +#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ -- cgit v1.2.1