File: ParSet.C

package info (click to toggle)
maria 1.3.5-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,980 kB
  • ctags: 5,458
  • sloc: cpp: 43,402; yacc: 8,080; ansic: 436; sh: 404; lisp: 395; makefile: 291; perl: 21
file content (237 lines) | stat: -rw-r--r-- 6,065 bytes parent folder | download | duplicates (5)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Parallelized, lossless reachability set storage -*- c++ -*-

#ifdef __GNUC__
# pragma implementation "ParSet.h"
#endif // __GNUC__

#if defined WIN32 || defined __WIN32
# undef __STRICT_ANSI__
# include "ParSet.h"
# include <sys/types.h>
# include <winsock.h>
typedef long ssize_t;
# undef HAVE_POLL
#else
# include "ParSet.h"
# include <sys/types.h>
# include <sys/socket.h>
# if defined __CYGWIN__ || defined __APPLE__
#  undef HAVE_POLL
#  include <sys/select.h>
# elif defined __OpenBSD__
#  define HAVE_POLL
extern "C" {
#  include <sys/poll.h>
};
# else
#  define HAVE_POLL
#  include <sys/poll.h>
# endif
#endif
#ifndef USE_MMAP
# include <unistd.h>
#endif // !USE_MMAP

#include <stdlib.h> // exit(2)
#ifndef __WIN32
# include <sys/errno.h>
#endif // !__WIN32
#include <errno.h>

#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif // MSG_NOSIGNAL

/** @file ParSet.C
 * Parallelized, lossless reachability set storage
 */

/* Copyright  2002-2003 Marko Mkel (msmakela@tcs.hut.fi).

   This file is part of MARIA, a reachability analyzer and model checker
   for high-level Petri nets.

   MARIA is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   MARIA is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */

ParSet::ParSet (int s) : StateSet (),
  myFD (s), myRecv (), mySend (), myRecvCount (0), mySendCount (0)
{
}

ParSet::~ParSet ()
{
  close (myFD);
}

/** Send pending data to a socket
 * @param fd	the socket to be written
 * @param sbuf	the buffer to write
 * @param sbufu	index of the first unsent byte in sbuf
 */
static void
sendBuf (int fd,
	 class BytePacker& sbuf,
	 unsigned& sbufu)
{
  assert (sbuf.getLength () > sbufu);
  const char* buf = reinterpret_cast<const char*>(sbuf.getBuf ());
  ssize_t i = send (fd, buf + sbufu, sbuf.getLength () - sbufu,
		    MSG_NOSIGNAL);
  if (i < 0) {
#if defined WIN32 || defined __WIN32
    fprintf (stderr, "send: WinSock error %d\n", WSAGetLastError ());
#else
    perror ("send");
#endif
    exit (-1);
  }
  if ((sbufu += i) == sbuf.getLength ())
    sbuf.clear (), sbufu = 0;
}

void
ParSet::reject (const void* dstate,
		size_t dlen,
		const class StateSetReporter&,
		enum StateSet::Code reason,
		bool reduced) const
{
  assert (!dstate == !dlen);
  mySend.append (reduced ? unsigned (reason) | 0x20 : unsigned (reason));
  mySend.append (dlen);
  mySend.append (dstate, dlen);
  // flush the buffer to notify the server of the rejected state
  do
    sendBuf (myFD, mySend, mySendCount);
  while (mySendCount);
}

bool
ParSet::do_add (const void* buf,
		size_t size)
{
  mySearch.push (buf, size, 0);
  return true;
}

word_t*
ParSet::pop (bool, size_t& size)
{
  if (unsigned numStates = mySearch.size ()) {
    if (getNumArcs ())
      mySend.append (addStates), mySend.append (numStates);
    else
      assert (numStates == 1), mySend.append (initialState);
    for (StateList::const_iterator i = mySearch.begin ();
	 i != mySearch.end (); i++)
      mySend.append (i->size), mySend.append (i->data, i->size);
  }
  else
    mySend.append (addStates), mySend.append (0);

  mySearch.clear ();
 again:
#ifndef HAVE_POLL
  fd_set readfds, writefds;
  FD_ZERO (&readfds); FD_ZERO (&writefds);
  FD_SET (myFD, &readfds); FD_SET (myFD, &writefds);
  switch (select (myFD + 1, &readfds, &writefds, 0, 0)) {
  default:
#if defined WIN32 || defined __WIN32
    if (WSAGetLastError () == WSAEINTR)
      goto again;
    fprintf (stderr, "select: WinSock error %d\n", WSAGetLastError ());
#else
    if (errno == EINTR)
      goto again;
    perror ("select");
#endif
    return 0;
  case 1:
  case 2:
    if (FD_ISSET (myFD, &writefds))
      sendBuf (myFD, mySend, mySendCount);
    if (FD_ISSET (myFD, &readfds))
      goto enqueue;
    // fall through: no data available for reading
  case 0:
    goto dequeue;
  }
#else
  struct pollfd ufd;
  ufd.fd = myFD;
  ufd.events = POLLIN | POLLOUT;
  ufd.revents = 0;
  switch (poll (&ufd, 1, -1)) {
  default:
    if (errno == EINTR)
      goto again;
    perror ("poll");
    return 0;
  case 1:
    if (ufd.revents & POLLOUT)
      sendBuf (myFD, mySend, mySendCount);
    if (ufd.revents & POLLIN)
      goto enqueue;
    // fall through: no data available for reading
  case 0:
    goto dequeue;
  }
#endif

 dequeue:
  {
    class ByteUnpacker u (myRecv.getBuf () + myRecvCount);
    unsigned len;
    if (!u.extract (myRecv, len) || !u.ensureData (myRecv, len)) {
      assert (myRecv.getBuf () + myRecvCount <= u.buf);
      goto enqueue;
    }
    if ((myRecvCount = u.buf - myRecv.getBuf () + len) == myRecv.getLength ())
      myRecvCount = 0, myRecv.clear ();

    size = len;
    return static_cast<word_t*>
      (memcpy (new word_t[1 + (len - 1) / sizeof (word_t)], u.buf, len));
  }

 enqueue:
  assert (myRecvCount
	  ? myRecvCount < myRecv.getLength ()
	  : !myRecv.getLength ());
  myRecv.allocate (2048);

  ssize_t rlen = recv (myFD, reinterpret_cast<char*>
		       (myRecv.getBuf () + myRecv.getLength ()),
		       myRecv.getAllocated () - myRecv.getLength (),
		       MSG_NOSIGNAL);
  if (rlen < 0) {
#if defined WIN32 || defined __WIN32
    int wsaerror = WSAGetLastError ();
    if (wsaerror != WSAECONNRESET)
      fprintf (stderr, "recv: WinSock error %d\n", wsaerror);
#else
    if (errno != ECONNRESET)
      perror ("recv");
#endif
    return 0;
  }
  else if (!rlen) // the remote end tells us to terminate
    return 0;

  myRecv.setLength (myRecv.getLength () + rlen);
  goto dequeue;
}