summaryrefslogtreecommitdiff
path: root/tests/SV_Shared_Memory_Test.cpp
diff options
context:
space:
mode:
authorschmidt <douglascraigschmidt@users.noreply.github.com>1999-10-03 18:22:24 +0000
committerschmidt <douglascraigschmidt@users.noreply.github.com>1999-10-03 18:22:24 +0000
commite9d2d014efbbe4a4636a485eb762d7c606303f6c (patch)
treee8c692d4a484c1066d29ba855fe802d57e18b2c2 /tests/SV_Shared_Memory_Test.cpp
parentd7e49c5a2385b69c9e3ab774a1b9cc04de5cb0e7 (diff)
downloadATCD-e9d2d014efbbe4a4636a485eb762d7c606303f6c.tar.gz
ChangeLogTag:Sun Oct 3 12:47:35 1999 Douglas C. Schmidt <schmidt@tango.cs.wustl.edu>
Diffstat (limited to 'tests/SV_Shared_Memory_Test.cpp')
-rw-r--r--tests/SV_Shared_Memory_Test.cpp116
1 files changed, 66 insertions, 50 deletions
diff --git a/tests/SV_Shared_Memory_Test.cpp b/tests/SV_Shared_Memory_Test.cpp
index 1b232d3beb8..517429a1494 100644
--- a/tests/SV_Shared_Memory_Test.cpp
+++ b/tests/SV_Shared_Memory_Test.cpp
@@ -9,11 +9,16 @@
// SV_Shared_Memory_Test.cpp
//
// = DESCRIPTION
-// This is a simple test of ACE_SV_Shared_Memory and ACE_Malloc
-// using the ACE_Shared_Memory_Pool.
+// This is a simple test of <ACE_SV_Shared_Memory> and
+// <ACE_Malloc> using the <ACE_Shared_Memory_Pool>. The test
+// forks two processes and then executes client and server
+// allowing them to exchange data using shared memory. No user
+// input is required as far as command line arguments are
+// concerned.
//
// = AUTHOR
-// Prashant Jain and Doug Schmidt
+// Prashant Jain <pjain@cs.wustl.edu>
+// and Douglas C. Schmidt <schmidt@cs.wustl.edu>
//
// ============================================================================
@@ -29,19 +34,22 @@ USELIB("..\ace\aced.lib");
#if defined (ACE_HAS_SYSV_IPC) && !defined(ACE_LACKS_SYSV_SHMEM)
-// Shared memory allocator (note that this chews up the
-// ACE_DEFAULT_SEM_KEY). Hide the allocator inside this function so
-// that it doesn't get constructed until after the ACE_Object_Manager
-// gets constructed, even with ACE_HAS_NONSTATIC_OBJECT_MANAGER.
+// The shared memory allocator, which uses up the ACE_DEFAULT_SEM_KEY.
+// We hide the allocator inside this function so that it doesn't get
+// constructed until after the ACE_Object_Manager gets constructed,
+// even with ACE_HAS_NONSTATIC_OBJECT_MANAGER.
+
static
ACE_Malloc<ACE_SHARED_MEMORY_POOL, ACE_SV_Semaphore_Simple> &
-myallocator ()
+myallocator (void)
{
static ACE_Malloc<ACE_SHARED_MEMORY_POOL,
ACE_SV_Semaphore_Simple> myallocator;
return myallocator;
}
+// Create some more keys that are different from the
+// ACE_DEFAULT_SEM_KEY used by the allocator.
static const int SEM_KEY_1 = ACE_DEFAULT_SEM_KEY + 1;
static const int SEM_KEY_2 = ACE_DEFAULT_SEM_KEY + 2;
@@ -54,69 +62,73 @@ static ACE_SV_Semaphore_Complex *parent_synch = 0;
static int
parent (char *shm)
{
- // This for loop executes in a critical section proteced by <parent_mutex>.
+ // This for loop executes in a critical section proteced by
+ // <parent_mutex>.
for (int i = 0; i < SHMSZ; i++)
shm[i] = SHMDATA[i];
- if (parent_mutex->release () == -1)
- ACE_ERROR ((LM_ERROR,
- ASYS_TEXT ("(%P) %p"),
- ASYS_TEXT ("parent mutex.release")));
- else if (parent_synch->acquire () == -1)
- ACE_ERROR ((LM_ERROR,
- ASYS_TEXT ("(%P) %p"),
- ASYS_TEXT ("parent synch.acquire")));
-
- if (myallocator ().remove () == -1)
- ACE_ERROR ((LM_ERROR,
- ASYS_TEXT ("(%P) %p\n"),
- ASYS_TEXT ("parent allocator.remove")));
- if (parent_mutex->remove () == -1)
- ACE_ERROR ((LM_ERROR,
- ASYS_TEXT ("(%P) %p\n"),
- ASYS_TEXT ("parent mutex.remove")));
- if (parent_synch->remove () == -1)
- ACE_ERROR ((LM_ERROR,
- ASYS_TEXT ("(%P) %p\n"),
- ASYS_TEXT ("parent synch.remove")));
+ int result;
+ result = parent_mutex->release ();
+ ACE_ASSERT (result != -1);
+
+ result = parent_synch->acquire ();
+ ACE_ASSERT (result != -1);
+
+ result = myallocator ().remove ();
+ ACE_ASSERT (result != -1);
+
+ result = parent_mutex->remove ();
+ ACE_ASSERT (result != -1);
+
+ result = parent_synch->remove ();
+ ACE_ASSERT (result != -1);
+
return 0;
}
static int
child (char *shm)
{
+ int result;
+
ACE_SV_Semaphore_Complex mutex;
+
// This semaphore is initially created with a count of 0, i.e., it
// is "locked."
- ACE_ASSERT (mutex.open (SEM_KEY_1,
- ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1);
+ result = mutex.open (SEM_KEY_1,
+ ACE_SV_Semaphore_Complex::ACE_CREATE,
+ 0);
+ ACE_ASSERT (result != -1);
ACE_SV_Semaphore_Complex synch;
// This semaphore is initially created with a count of 0, i.e., it
// is "locked."
- ACE_ASSERT (synch.open (SEM_KEY_2,
- ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1);
+ result = synch.open (SEM_KEY_2,
+ ACE_SV_Semaphore_Complex::ACE_CREATE,
+ 0);
+ ACE_ASSERT (result != -1);
// Perform "busy waiting" here until we acquire the semaphore. This
// isn't really a good design -- it's just to illustrate that you
// can do non-blocking acquire() calls with the ACE System V
// semaphore wrappers.
- while (mutex.tryacquire () == -1)
+ while ((result = mutex.tryacquire ()) == -1)
if (errno == EAGAIN)
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%P) spinning in child!\n")));
else
- ACE_ERROR_RETURN ((LM_ERROR,
- ASYS_TEXT ("(%P) child mutex.tryacquire")),
- 1);
+ {
+ ACE_ERROR ((LM_ERROR,
+ ASYS_TEXT ("(%P) child mutex.tryacquire")));
+ ACE_ASSERT (result != -1);
+ }
for (int i = 0; i < SHMSZ; i++)
ACE_ASSERT (SHMDATA[i] == shm[i]);
- if (synch.release () == -1)
- ACE_ERROR_RETURN ((LM_ERROR,
- ASYS_TEXT ("(%P) child synch.release")),
- 1);
+ result = synch.release ();
+ ACE_ASSERT (result != -1);
+
return 0;
}
@@ -139,25 +151,29 @@ main (int, ASYS_TCHAR *[])
#if defined (ACE_HAS_SYSV_IPC) && !defined (ACE_LACKS_FORK) && \
!defined(ACE_LACKS_SYSV_SHMEM)
- char *shm = ACE_reinterpret_cast (char *, myallocator ().malloc (SHMSZ));
+ char *shm = ACE_reinterpret_cast (char *,
+ myallocator ().malloc (SHMSZ));
// Create the mutex and synch before spawning the child process, to
// avoid race condition between their creation in the parent and use
// in the child.
- ACE_NEW_RETURN (parent_mutex, ACE_SV_Semaphore_Complex, -1);
- ACE_NEW_RETURN (parent_synch, ACE_SV_Semaphore_Complex, -1);
+ ACE_NEW_RETURN (parent_mutex,
+ ACE_SV_Semaphore_Complex,
+ -1);
+ ACE_NEW_RETURN (parent_synch,
+ ACE_SV_Semaphore_Complex,
+ -1);
// This semaphore is initially created with a count of 0, i.e., it
// is "locked."
ACE_ASSERT (parent_mutex->open (SEM_KEY_1,
- ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1);
-
+ ACE_SV_Semaphore_Complex::ACE_CREATE,
+ 0) != -1);
// This semaphore is initially created with a count of 0, i.e., it
// is "locked."
ACE_ASSERT (parent_synch->open (SEM_KEY_2,
- ACE_SV_Semaphore_Complex::ACE_CREATE, 0) != -1);
-
-
+ ACE_SV_Semaphore_Complex::ACE_CREATE,
+ 0) != -1);
switch (ACE_OS::fork ("SV_Shared_Memory_Test.cpp"))
{
case -1: