summaryrefslogtreecommitdiff
path: root/tests/MM_Shared_Memory_Test.cpp
blob: be1d26fff0d773f9280093b7588d962f887a4260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Shared_Memory_MM_Test.cpp
//
// = DESCRIPTION
//     This is a simple test of ACE_Shared_Memory_MM. The test forks
//     two processes or spawns two threads (depending upon the
//     platform) 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
//
// ============================================================================

#include "test_config.h"
#include "ace/Shared_Memory_MM.h"
#include "ace/Thread.h"
#include "ace/Thread_Manager.h"

ACE_RCSID(tests, MM_Shared_Memory_Test, "$Id$")

#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */

const int SHMSZ = 27;
static TCHAR shm_key[] = ACE_TEMP_FILE_NAME ACE_TEXT ("XXXXXX");

static void *
client (void * = 0)
{
  ACE_OS::sleep (ACE_DEFAULT_TIMEOUT);
  char *t = ACE_ALPHABET;
  ACE_Shared_Memory_MM shm_client;

  if (shm_client.open (shm_key) == -1)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("(%P|%t) %p\n%a"),
                ASYS_TEXT ("ACE_Shared_Memory_MM::open() failed in client"),
                1));
    /* NOTREACHED */

  char *shm = (char *) shm_client.malloc ();

  if (shm == 0)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("(%P|%t) %p\n%a"),
                ASYS_TEXT ("ACE_Shared_Memory_MM::malloc () failed in client"),
                1));
    /* NOTREACHED */

  for (char *s = shm; *s != '\0'; s++)
    {
      ACE_ASSERT (*t == s[0]);
      t++;
    }

  *shm = '*';

  return 0;
}

static void *
server (void * = 0)
{
  ACE_Shared_Memory_MM shm_server;

  if (shm_server.open (shm_key, SHMSZ) == -1)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("(%P|%t) %p\n%a"),
                ASYS_TEXT ("ACE_Shared_Memory_MM::open () failed in server"),
                1));
    /* NOTREACHED */

  char *shm = (char *) shm_server.malloc ();

  if (shm == 0)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("(%P|%t) %p\n%a"),
                ASYS_TEXT ("ACE_Shared_Memory_MM::malloc () failed in server"),
                1));
    /* NOTREACHED */

  char *s = shm;

  for (char *c = ACE_ALPHABET; *c != '\0'; c++)
    *s++ = *c;

  *s = '\0';

  // Perform a busy wait (ugh).
  while (*shm != '*')
    ACE_OS::sleep (1);

  if (shm_server.remove () == -1)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("(%P|%t) %p\n"),
                ASYS_TEXT ("remove")));

  ACE_OS::unlink (shm_key);
  return 0;
}

static void
spawn (void)
{
#if !defined (ACE_LACKS_FORK)
  switch (ACE_OS::fork (ASYS_TEXT ("child")))
    {
    case -1:
      ACE_ERROR ((LM_ERROR,
                  ASYS_TEXT ("(%P|%t) %p\n%a"),
                  ASYS_TEXT ("fork failed"),
                  1));
      /* NOTREACHED */
    case 0:
      server ();
      break;
    default:
      client ();
      break;
    }
#elif defined (ACE_HAS_THREADS)
  if (ACE_Thread_Manager::instance ()->spawn
      (ACE_THR_FUNC (client),
       (void *) 0,
       THR_NEW_LWP | THR_DETACHED) == -1)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("(%P|%t) %p\n%a"),
                ASYS_TEXT ("thread create failed"),
                1));

  if (ACE_Thread_Manager::instance ()->spawn
      (ACE_THR_FUNC (server),
       (void *) 0,
       THR_NEW_LWP | THR_DETACHED) == -1)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("(%P|%t) %p\n%a"),
                ASYS_TEXT ("thread create failed"),
               1));
  ACE_Thread_Manager::instance ()->wait ();
#else
  ACE_ERROR ((LM_ERROR,
              ASYS_TEXT ("only one thread may be run in a process on this platform\n%a"), 1));
#endif /* ACE_HAS_THREADS */
}

int
main (int, ASYS_TCHAR *[])
{
  ACE_START_TEST (ASYS_TEXT ("MM_Shared_Memory_Test"));

  if (ACE_OS::mktemp (shm_key) == 0
      || (ACE_OS::unlink (shm_key) == -1
          && errno == EPERM))
    ACE_ERROR_RETURN ((LM_ERROR,
                       ASYS_TEXT ("(%P|%t) %p\n"),
                       shm_key),
                      1);
  spawn ();

  ACE_END_TEST;
  return 0;
}