summaryrefslogtreecommitdiff
path: root/performance-tests/Synch-Benchmarks/pipe_proc_test.cpp
blob: 7d9c74d190d10bc979225d577044625585116afd (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
#include "Options.h"
// $Id$

#include "Benchmark.h"

#if defined (ACE_HAS_THREADS)

extern int buffer;
extern int synch_count;

class Pipe_Proc_Test : public Benchmark
{
public:
  int init (int, char **);
  virtual int svc (void);

private:
  int pipe_handles[2];

  void reader (ACE_HANDLE handle);
};

int
Pipe_Proc_Test::init (int, char **)
{
  synch_count = 1;

  if (ACE_OS::pipe (this->pipe_handles) == -1)
    ACE_OS::perror ("pipe"), ACE_OS::exit (1);

  switch (ACE_OS::fork ())
    {
    case -1:
      ACE_OS::perror ("fork"), ACE_OS::exit (1);
    case 0:
      this->reader (pipe_handles[0]);
      /* NOTREACHED */
      break;
    default:
      break;
    }
  return 1;
}

void
Pipe_Proc_Test::reader (ACE_HANDLE handle)
{
  int  ni     = this->thr_id ();
  int  length = options.msg_size ();
  char *to    = new char[length];
  int  n;

  while ((n = ACE_OS::read (handle, to, length)) > 0)
    options.thr_work_count[ni]++;
}


int
Pipe_Proc_Test::svc (void)
{
  ssize_t length = options.msg_size ();
  char *from  = new char[length];
  int ni = this->thr_id ();
  ACE_HANDLE handle = this->pipe_handles[1];

  while (!this->done ())
    if (ACE_OS::write (handle, from, length) == length)
      options.thr_work_count[ni]++;
    else
      ACE_OS::perror ("write");
    
  ACE_OS::close (this->pipe_handles[0]);
  ACE_OS::close (this->pipe_handles[1]);
  return 0;
}

extern "C" ACE_Service_Object *pipe_proc_test (void);

ACE_Service_Object *pipe_proc_test (void)
{
  return new Pipe_Proc_Test;
}

// ACE_Service_Object_Type ppt (&pipe_proc_test, "Pipe_Proc_Test");
#endif /* ACE_HAS_THREADS */