File: aiod.h

package info (click to toggle)
sfs 1%3A0.5k-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,388 kB
  • ctags: 8,556
  • sloc: cpp: 43,410; ansic: 17,574; sh: 8,412; makefile: 771; yacc: 277; lex: 96; sed: 47
file content (268 lines) | stat: -rw-r--r-- 7,116 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// -*-c++-*-
/* $Id: aiod.h,v 1.16 2001/01/13 19:46:10 dm Exp $ */

/*
 *
 * Copyright (C) 1998 David Mazieres (dm@uun.org)
 *
 * This program 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.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#ifndef _ASYNC_AIOD_H_
#define _ASYNC_AIOD_H_ 1

#include "async.h"
#include "bbuddy.h"
#include "ihash.h"
#include "aiod_prot.h"

struct aiod_req;

class aiobuf {
  friend class aiod;
  friend class aiofh;

  char *const buf;
  const size_t len;
  aiod *const iod;
  const size_t pos;

  aiobuf (const aiobuf &);
  aiobuf &operator= (const aiobuf &);
  
protected:
  aiobuf (aiod *d, size_t pos, size_t len);
  ~aiobuf ();

public:
  inline char *base ();
  inline const char *base () const;
  size_t size () const { return len; }
  inline char *lim ();
  inline const char *lim () const;
};

class aiod {
  friend class aiobuf;
  friend class aiofh;

  typedef callback<void, ptr<aiobuf> >::ref cbb;
  typedef callback<void, str, int>::ref cbsi;
  typedef callback<void, struct stat *, int>::ref cbstat;
  typedef callback<void, ptr<aiofh>, int>::ref cbopen;

  enum { maxwrite = (PIPE_BUF / sizeof (off_t)) * sizeof (off_t) };

  class writeq {
    static void checkmaxwrite () { switch (0) case maxwrite: case 0:; }
    suio wbuf;
    void output ();
  public:
    int wfd;
    writeq () : wfd (-1) {}
    ~writeq () { close (); }
    void close () {
      if (wfd >= 0) {
	fdcb (wfd, selread, NULL);
	fdcb (wfd, selwrite, NULL);
	::close (wfd);
	wfd = -1;
      }
    }
    void sendmsg (aiomsg_t msg);
  };

  struct daemon {
    pid_t pid;
    int fd;
    writeq wq;

    daemon () : pid (-1), fd (-1) {}
    bool launch (str path, int shmfd, int commonfd);
  };

  struct request {
    ref<aiobuf> buf;
    const size_t pos;
    vec<cbb, 1> cbvec;
    ihash_entry<request> hlink;
    request (ref<aiobuf> b);
  };
  friend class request;
  
  bool closed;
  bool finalized;
  bool growlock;
  bool bufwakereq;
  bool bufwakelock;
  size_t refcnt;

  int shmfd;
  size_t shmmax;
  size_t shmlen;
  char *shmbuf;
  bbuddy bb;
  vec<cbv> bbwaitq;

  writeq wq;

  const size_t ndaemons;
  daemon *dv;

  int fhno_ctr;
  vec<int> fhno_avail;

  ihash<const size_t, request, &request::pos, &request::hlink> rqtab;

  void delreq (request *r);
  void fail ();
  void input (int);
  void bufwake ();
  void bufalloc_cb1 (size_t inc, ptr<aiobuf> buf);
  void bufalloc_cb2 (size_t inc, ptr<aiobuf> buf);
  void sendmsg (ref<aiobuf> buf, cbb cb, int dst = -1);

  int fhno_alloc () { return fhno_avail.empty () ? fhno_ctr++
			: fhno_avail.pop_back (); }
  void fhno_free (int n) { fhno_avail.push_back (n); }

  static size_t buf2pos (aiobuf *buf) { return buf->pos; }
  static aiod_reqhdr *buf2hdr (aiobuf *buf)
    { return reinterpret_cast<aiod_reqhdr *> (buf->base ()); }
  static aiod_pathop *buf2pathop (aiobuf *buf)
    { return reinterpret_cast<aiod_pathop *> (buf->base ()); }
  static aiod_fhop *buf2fhop (aiobuf *buf)
    { return reinterpret_cast<aiod_fhop *> (buf->base ()); }
  static aiod_fstat *buf2fstat (aiobuf *buf)
    { return reinterpret_cast<aiod_fstat *> (buf->base ()); }
  static aiod_file *buf2file (aiobuf *buf)
    { return reinterpret_cast<aiod_file *> (buf->base ()); }
  static aiod_nop *buf2nop (aiobuf *buf)
    { return reinterpret_cast<aiod_nop *> (buf->base ()); }

  static void cbi_cb (cbi cb, ptr<aiobuf> buf);
  static void cbstat_cb (cbstat cb, ptr<aiobuf> buf);
  static void pathret_cb (cbsi cb, ptr<aiobuf> buf);
  static void open_cb (ref<aiofh> fh, cbopen cb, ptr<aiobuf> buf);

  void pathop (aiod_op op, str p1, str p2, cbb cb, size_t bufsize = 0);
  void statop (aiod_op op, const str &path, const cbstat &cb)
    { pathop (op, path, NULL, wrap (cbstat_cb, cb), sizeof (struct stat)); }

  ~aiod ();
  void addref () { refcnt++; }
  void delref () { if (!--refcnt && finalized) delete this; }

public:
  enum { minbuf = 0x40, maxbuf = 0x10000 };

  aiod (u_int nproc = 1, ssize_t shmsize = 0x200000, const str &path = "aiod");
  void finalize () { finalized = true; addref (); delref (); }

  ptr<aiobuf> bufalloc (size_t len);
  void bufwait (cbv cb) { bbwaitq.push_back (cb); }

  void unlink (str path, cbi cb)
    { pathop (AIOD_UNLINK, path, NULL, wrap (cbi_cb, cb)); }
  void link (str from, str to, cbi cb)
    { pathop (AIOD_LINK, from, to, wrap (cbi_cb, cb)); }
  void symlink (str from, str to, cbi cb)
    { pathop (AIOD_SYMLINK, from, to, wrap (cbi_cb, cb)); }
  void rename (str from, str to, cbi cb)
    { pathop (AIOD_RENAME, from, to, wrap (cbi_cb, cb)); }

  void readlink (str path, cbsi cb)
    { pathop (AIOD_READLINK, path, NULL, wrap (pathret_cb, cb), PATH_MAX); }
  void getcwd (str path, cbsi cb)
    { pathop (AIOD_GETCWD, path, NULL, wrap (pathret_cb, cb), PATH_MAX); }

  void stat (str path, cbstat cb) { statop (AIOD_STAT, path, cb); }
  void lstat (str path, cbstat cb) { statop (AIOD_LSTAT, path, cb); }

  void open (str path, int flags, int mode, cbopen cb);
};

inline char *
aiobuf::base ()
{
  return buf;
}

inline const char *
aiobuf::base () const
{
  return buf;
}

inline char *
aiobuf::lim ()
{
  return base () + len;
}

inline const char *
aiobuf::lim () const
{
  return base () + len;
}

inline
aiod::request::request (ref<aiobuf> b)
  : buf (b), pos (buf2pos (buf))
{
}

class aiofh : public virtual refcount {
  friend class aiod;

  typedef callback<void, ptr<aiobuf>, ssize_t, int>::ref cbrw;

  aiod *const iod;
  const ref<aiobuf> fh;
  const int fhno;
  bool closed;

  void rw (aiod_op op, off_t pos, ptr<aiobuf> iobuf, cbrw cb);
  void simpleop (aiod_op op, aiod::cbb cb, off_t length = 0);
  void sendclose (cbi::ptr cb = NULL);

  void rw_cb (ref<aiobuf> iobuf, cbrw cb, ptr<aiobuf> rqbuf);
  void cbstat_cb (aiod::cbstat cb, ptr<aiobuf> buf);
  static void close_cb (int *ctr, cbi::ptr cb, ptr<aiobuf> buf);
  void cbi_cb (cbi cb, ptr<aiobuf> buf);

protected:
  aiofh (aiod *iod, ref<aiobuf> fh);
  ~aiofh ();

public:
  void close (cbi cb);
  void fsync (cbi cb)
    { simpleop (AIOD_FSYNC, wrap (mkref (this), &aiofh::cbi_cb, cb)); }
  void ftrunc (off_t length, cbi cb) {
    simpleop (AIOD_FTRUNC, wrap (mkref (this), &aiofh::cbi_cb, cb), length);
  }
  void read (off_t pos, ptr<aiobuf> buf, cbrw cb)
    { rw (AIOD_READ, pos, buf, cb); }
  void write (off_t pos, ptr<aiobuf> buf, cbrw cb)
    { rw (AIOD_WRITE, pos, buf, cb); }
  void fstat (aiod::cbstat cb)
    { simpleop (AIOD_FSTAT, wrap (mkref (this), &aiofh::cbstat_cb, cb)); }
};


#endif /* !_ASYNC_AIOD_H_ */