This source file includes following definitions.
- grow_fd_sets
- do_fd_set
- do_fd_clear
- win32_init
- win32_add
- win32_del
- fd_set_copy
- win32_dispatch
- win32_dealloc
   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 #include <winsock2.h>
  30 #include <windows.h>
  31 #include <sys/types.h>
  32 #include <sys/queue.h>
  33 #include <limits.h>
  34 #include <signal.h>
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 #include <string.h>
  38 #include <errno.h>
  39 
  40 #include "event2/util.h"
  41 #include "event2/event-config.h"
  42 #include "util-internal.h"
  43 #include "log-internal.h"
  44 #include "event2/event.h"
  45 #include "event-internal.h"
  46 #include "evmap-internal.h"
  47 #include "event2/thread.h"
  48 #include "evthread-internal.h"
  49 
  50 #define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0)
  51 
  52 extern struct event_list timequeue;
  53 extern struct event_list addqueue;
  54 
  55 struct win_fd_set {
  56         u_int fd_count;
  57         SOCKET fd_array[1];
  58 };
  59 
  60 
  61 volatile double SIGFPE_REQ = 0.0f;
  62 
  63 struct idx_info {
  64         int read_pos_plus1;
  65         int write_pos_plus1;
  66 };
  67 
  68 struct win32op {
  69         unsigned num_fds_in_fd_sets;
  70         int resize_out_sets;
  71         struct win_fd_set *readset_in;
  72         struct win_fd_set *writeset_in;
  73         struct win_fd_set *readset_out;
  74         struct win_fd_set *writeset_out;
  75         struct win_fd_set *exset_out;
  76         unsigned signals_are_broken : 1;
  77 };
  78 
  79 static void *win32_init(struct event_base *);
  80 static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *_idx);
  81 static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *_idx);
  82 static int win32_dispatch(struct event_base *base, struct timeval *);
  83 static void win32_dealloc(struct event_base *);
  84 
  85 struct eventop win32ops = {
  86         "win32",
  87         win32_init,
  88         win32_add,
  89         win32_del,
  90         win32_dispatch,
  91         win32_dealloc,
  92         0, 
  93         0, 
  94         sizeof(struct idx_info),
  95 };
  96 
  97 #define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET)))
  98 
  99 static int
 100 grow_fd_sets(struct win32op *op, unsigned new_num_fds)
 101 {
 102         size_t size;
 103 
 104         EVUTIL_ASSERT(new_num_fds >= op->readset_in->fd_count &&
 105                new_num_fds >= op->writeset_in->fd_count);
 106         EVUTIL_ASSERT(new_num_fds >= 1);
 107 
 108         size = FD_SET_ALLOC_SIZE(new_num_fds);
 109         if (!(op->readset_in = mm_realloc(op->readset_in, size)))
 110                 return (-1);
 111         if (!(op->writeset_in = mm_realloc(op->writeset_in, size)))
 112                 return (-1);
 113         op->resize_out_sets = 1;
 114         op->num_fds_in_fd_sets = new_num_fds;
 115         return (0);
 116 }
 117 
 118 static int
 119 do_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read)
 120 {
 121         struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
 122         if (read) {
 123                 if (ent->read_pos_plus1 > 0)
 124                         return (0);
 125         } else {
 126                 if (ent->write_pos_plus1 > 0)
 127                         return (0);
 128         }
 129         if (set->fd_count == op->num_fds_in_fd_sets) {
 130                 if (grow_fd_sets(op, op->num_fds_in_fd_sets*2))
 131                         return (-1);
 132                 
 133                 set = read ? op->readset_in : op->writeset_in;
 134         }
 135         set->fd_array[set->fd_count] = s;
 136         if (read)
 137                 ent->read_pos_plus1 = set->fd_count+1;
 138         else
 139                 ent->write_pos_plus1 = set->fd_count+1;
 140         return (set->fd_count++);
 141 }
 142 
 143 static int
 144 do_fd_clear(struct event_base *base,
 145                         struct win32op *op, struct idx_info *ent, int read)
 146 {
 147         int i;
 148         struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
 149         if (read) {
 150                 i = ent->read_pos_plus1 - 1;
 151                 ent->read_pos_plus1 = 0;
 152         } else {
 153                 i = ent->write_pos_plus1 - 1;
 154                 ent->write_pos_plus1 = 0;
 155         }
 156         if (i < 0)
 157                 return (0);
 158         if (--set->fd_count != (unsigned)i) {
 159                 struct idx_info *ent2;
 160                 SOCKET s2;
 161                 s2 = set->fd_array[i] = set->fd_array[set->fd_count];
 162 
 163                 ent2 = evmap_io_get_fdinfo(&base->io, s2);
 164 
 165                 if (!ent2) 
 166                         return (0);
 167                 if (read)
 168                         ent2->read_pos_plus1 = i+1;
 169                 else
 170                         ent2->write_pos_plus1 = i+1;
 171         }
 172         return (0);
 173 }
 174 
 175 #define NEVENT 32
 176 void *
 177 win32_init(struct event_base *_base)
 178 {
 179         struct win32op *winop;
 180         size_t size;
 181         if (!(winop = mm_calloc(1, sizeof(struct win32op))))
 182                 return NULL;
 183         winop->num_fds_in_fd_sets = NEVENT;
 184         size = FD_SET_ALLOC_SIZE(NEVENT);
 185         if (!(winop->readset_in = mm_malloc(size)))
 186                 goto err;
 187         if (!(winop->writeset_in = mm_malloc(size)))
 188                 goto err;
 189         if (!(winop->readset_out = mm_malloc(size)))
 190                 goto err;
 191         if (!(winop->writeset_out = mm_malloc(size)))
 192                 goto err;
 193         if (!(winop->exset_out = mm_malloc(size)))
 194                 goto err;
 195         winop->readset_in->fd_count = winop->writeset_in->fd_count = 0;
 196         winop->readset_out->fd_count = winop->writeset_out->fd_count
 197                 = winop->exset_out->fd_count = 0;
 198 
 199         if (evsig_init(_base) < 0)
 200                 winop->signals_are_broken = 1;
 201 
 202         return (winop);
 203  err:
 204         XFREE(winop->readset_in);
 205         XFREE(winop->writeset_in);
 206         XFREE(winop->readset_out);
 207         XFREE(winop->writeset_out);
 208         XFREE(winop->exset_out);
 209         XFREE(winop);
 210         return (NULL);
 211 }
 212 
 213 int
 214 win32_add(struct event_base *base, evutil_socket_t fd,
 215                          short old, short events, void *_idx)
 216 {
 217         struct win32op *win32op = base->evbase;
 218         struct idx_info *idx = _idx;
 219 
 220         if ((events & EV_SIGNAL) && win32op->signals_are_broken)
 221                 return (-1);
 222 
 223         if (!(events & (EV_READ|EV_WRITE)))
 224                 return (0);
 225 
 226         event_debug(("%s: adding event for %d", __func__, (int)fd));
 227         if (events & EV_READ) {
 228                 if (do_fd_set(win32op, idx, fd, 1)<0)
 229                         return (-1);
 230         }
 231         if (events & EV_WRITE) {
 232                 if (do_fd_set(win32op, idx, fd, 0)<0)
 233                         return (-1);
 234         }
 235         return (0);
 236 }
 237 
 238 int
 239 win32_del(struct event_base *base, evutil_socket_t fd, short old, short events,
 240                   void *_idx)
 241 {
 242         struct win32op *win32op = base->evbase;
 243         struct idx_info *idx = _idx;
 244 
 245         event_debug(("%s: Removing event for "EV_SOCK_FMT,
 246                 __func__, EV_SOCK_ARG(fd)));
 247         if (events & EV_READ)
 248                 do_fd_clear(base, win32op, idx, 1);
 249         if (events & EV_WRITE)
 250                 do_fd_clear(base, win32op, idx, 0);
 251 
 252         return 0;
 253 }
 254 
 255 static void
 256 fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in)
 257 {
 258         out->fd_count = in->fd_count;
 259         memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET)));
 260 }
 261 
 262 
 263 
 264 
 265 
 266 
 267 
 268 
 269 
 270 
 271 
 272 
 273 int
 274 win32_dispatch(struct event_base *base, struct timeval *tv)
 275 {
 276         struct win32op *win32op = base->evbase;
 277         int res = 0;
 278         unsigned j, i;
 279         int fd_count;
 280         SOCKET s;
 281 
 282         if (win32op->resize_out_sets) {
 283                 size_t size = FD_SET_ALLOC_SIZE(win32op->num_fds_in_fd_sets);
 284                 if (!(win32op->readset_out = mm_realloc(win32op->readset_out, size)))
 285                         return (-1);
 286                 if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size)))
 287                         return (-1);
 288                 if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size)))
 289                         return (-1);
 290                 win32op->resize_out_sets = 0;
 291         }
 292 
 293         fd_set_copy(win32op->readset_out, win32op->readset_in);
 294         fd_set_copy(win32op->exset_out, win32op->writeset_in);
 295         fd_set_copy(win32op->writeset_out, win32op->writeset_in);
 296 
 297         fd_count =
 298             (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ?
 299             win32op->readset_out->fd_count : win32op->writeset_out->fd_count;
 300 
 301         if (!fd_count) {
 302                 long msec = tv ? evutil_tv_to_msec(tv) : LONG_MAX;
 303                 
 304                 if (msec < 0)
 305                         msec = LONG_MAX;
 306                 
 307                 Sleep(msec);
 308                 return (0);
 309         }
 310 
 311         EVBASE_RELEASE_LOCK(base, th_base_lock);
 312 
 313         res = select(fd_count,
 314                      (struct fd_set*)win32op->readset_out,
 315                      (struct fd_set*)win32op->writeset_out,
 316                      (struct fd_set*)win32op->exset_out, tv);
 317 
 318         EVBASE_ACQUIRE_LOCK(base, th_base_lock);
 319 
 320         event_debug(("%s: select returned %d", __func__, res));
 321 
 322         if (res <= 0) {
 323                 return res;
 324         }
 325 
 326         if (win32op->readset_out->fd_count) {
 327                 i = rand() % win32op->readset_out->fd_count;
 328                 for (j=0; j<win32op->readset_out->fd_count; ++j) {
 329                         if (++i >= win32op->readset_out->fd_count)
 330                                 i = 0;
 331                         s = win32op->readset_out->fd_array[i];
 332                         evmap_io_active(base, s, EV_READ);
 333                 }
 334         }
 335         if (win32op->exset_out->fd_count) {
 336                 i = rand() % win32op->exset_out->fd_count;
 337                 for (j=0; j<win32op->exset_out->fd_count; ++j) {
 338                         if (++i >= win32op->exset_out->fd_count)
 339                                 i = 0;
 340                         s = win32op->exset_out->fd_array[i];
 341                         evmap_io_active(base, s, EV_WRITE);
 342                 }
 343         }
 344         if (win32op->writeset_out->fd_count) {
 345                 SOCKET s;
 346                 i = rand() % win32op->writeset_out->fd_count;
 347                 for (j=0; j<win32op->writeset_out->fd_count; ++j) {
 348                         if (++i >= win32op->writeset_out->fd_count)
 349                                 i = 0;
 350                         s = win32op->writeset_out->fd_array[i];
 351                         evmap_io_active(base, s, EV_WRITE);
 352                 }
 353         }
 354         return (0);
 355 }
 356 
 357 void
 358 win32_dealloc(struct event_base *_base)
 359 {
 360         struct win32op *win32op = _base->evbase;
 361 
 362         evsig_dealloc(_base);
 363         if (win32op->readset_in)
 364                 mm_free(win32op->readset_in);
 365         if (win32op->writeset_in)
 366                 mm_free(win32op->writeset_in);
 367         if (win32op->readset_out)
 368                 mm_free(win32op->readset_out);
 369         if (win32op->writeset_out)
 370                 mm_free(win32op->writeset_out);
 371         if (win32op->exset_out)
 372                 mm_free(win32op->exset_out);
 373         
 374 
 375         memset(win32op, 0, sizeof(*win32op));
 376         mm_free(win32op);
 377 }