summaryrefslogtreecommitdiff
path: root/tests/SV_Shared_Memory_Test.cpp
diff options
context:
space:
mode:
authorschmidt <douglascraigschmidt@users.noreply.github.com>1996-12-02 05:11:41 +0000
committerschmidt <douglascraigschmidt@users.noreply.github.com>1996-12-02 05:11:41 +0000
commitd3355fd45d2b9c5a982a6bf0f64885188afd24d1 (patch)
treebcaf3d4bdbb674d1740bc832aed8ccfe01acef31 /tests/SV_Shared_Memory_Test.cpp
parent09593654ece6c713b2690f7dd5cc0cc42f9ff1bf (diff)
downloadATCD-d3355fd45d2b9c5a982a6bf0f64885188afd24d1.tar.gz
foo
Diffstat (limited to 'tests/SV_Shared_Memory_Test.cpp')
-rw-r--r--tests/SV_Shared_Memory_Test.cpp112
1 files changed, 79 insertions, 33 deletions
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<ACE_SHARED_MEMORY_POOL, ACE_SV_Semaphore_Simple> 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<ACE_SHARED_MEMORY_POOL, ACE_SV_Semaphore_Simple>;
+#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */