dbus-socket-set-poll.c 8.65 KB
Newer Older
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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-socket-set-poll.c - a socket set implemented via _dbus_poll
 *
 * Copyright © 2011 Nokia Corporation
 *
 * Licensed under the Academic Free License version 2.1
 *
 * 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 of the License, 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301  USA
 *
 */

#include <config.h>
#include "dbus-socket-set.h"

#include <dbus/dbus-internals.h>
#include <dbus/dbus-list.h>
#include <dbus/dbus-sysdeps.h>
#include <dbus/dbus-watch.h>

#ifndef DOXYGEN_SHOULD_SKIP_THIS

typedef struct {
    DBusSocketSet      parent;
    DBusPollFD        *fds;
    int                n_fds;
    int                n_reserved;
    int                n_allocated;
} DBusSocketSetPoll;

#define REALLOC_INCREMENT 8
#define MINIMUM_SIZE 8

/* If we're in the regression tests, force reallocation to happen sooner */
47
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
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
#define DEFAULT_SIZE_HINT 1
#else
#define DEFAULT_SIZE_HINT MINIMUM_SIZE
#endif

static inline DBusSocketSetPoll *
socket_set_poll_cast (DBusSocketSet *set)
{
  _dbus_assert (set->cls == &_dbus_socket_set_poll_class);
  return (DBusSocketSetPoll *) set;
}

/* this is safe to call on a partially-allocated socket set */
static void
socket_set_poll_free (DBusSocketSet *set)
{
  DBusSocketSetPoll *self = socket_set_poll_cast (set);

  dbus_free (self->fds);
  dbus_free (self);
  _dbus_verbose ("freed socket set %p\n", self);
}

DBusSocketSet *
_dbus_socket_set_poll_new (int size_hint)
{
  DBusSocketSetPoll *ret;

  if (size_hint <= 0)
    size_hint = DEFAULT_SIZE_HINT;

  ret = dbus_new0 (DBusSocketSetPoll, 1);

  if (ret == NULL)
    return NULL;

  ret->parent.cls = &_dbus_socket_set_poll_class;
  ret->n_fds = 0;
  ret->n_allocated = size_hint;

  ret->fds = dbus_new0 (DBusPollFD, size_hint);

  if (ret->fds == NULL)
    {
      /* socket_set_poll_free specifically supports half-constructed
       * socket sets */
      socket_set_poll_free ((DBusSocketSet *) ret);
      return NULL;
    }

  _dbus_verbose ("new socket set at %p\n", ret);
  return (DBusSocketSet *) ret;
}

static short
watch_flags_to_poll_events (unsigned int flags)
{
  short events = 0;

  if (flags & DBUS_WATCH_READABLE)
    events |= _DBUS_POLLIN;
  if (flags & DBUS_WATCH_WRITABLE)
    events |= _DBUS_POLLOUT;

  return events;
}

static dbus_bool_t
socket_set_poll_add (DBusSocketSet  *set,
117
                     DBusPollable    fd,
118 119 120 121 122 123 124 125
                     unsigned int    flags,
                     dbus_bool_t     enabled)
{
  DBusSocketSetPoll *self = socket_set_poll_cast (set);
#ifndef DBUS_DISABLE_ASSERT
  int i;

  for (i = 0; i < self->n_fds; i++)
126
    _dbus_assert (!_dbus_pollable_equals (self->fds[i].fd, fd));
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#endif

  if (self->n_reserved >= self->n_allocated)
    {
      DBusPollFD *new_fds = dbus_realloc (self->fds,
          sizeof (DBusPollFD) * (self->n_allocated + REALLOC_INCREMENT));

      _dbus_verbose ("inflating set %p from %d en/%d res/%d alloc to %d\n",
                     self, self->n_fds, self->n_reserved, self->n_allocated,
                     self->n_allocated + REALLOC_INCREMENT);

      if (new_fds == NULL)
        return FALSE;

      self->fds = new_fds;
      self->n_allocated += REALLOC_INCREMENT;
    }

145
  _dbus_verbose ("before adding fd %" DBUS_POLLABLE_FORMAT " to %p, %d en/%d res/%d alloc\n",
146
                 _dbus_pollable_printable (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  _dbus_assert (self->n_reserved >= self->n_fds);
  _dbus_assert (self->n_allocated > self->n_reserved);

  self->n_reserved++;

  if (enabled)
    {
      self->fds[self->n_fds].fd = fd;
      self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
      self->n_fds++;
    }

  return TRUE;
}

static void
socket_set_poll_enable (DBusSocketSet *set,
164
                        DBusPollable   fd,
165 166 167 168 169 170 171
                        unsigned int   flags)
{
  DBusSocketSetPoll *self = socket_set_poll_cast (set);
  int i;

  for (i = 0; i < self->n_fds; i++)
    {
172
      if (_dbus_pollable_equals (self->fds[i].fd, fd))
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        {
          self->fds[i].events = watch_flags_to_poll_events (flags);
          return;
        }
    }

  /* we allocated space when the socket was added */
  _dbus_assert (self->n_fds < self->n_reserved);
  _dbus_assert (self->n_reserved <= self->n_allocated);

  self->fds[self->n_fds].fd = fd;
  self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
  self->n_fds++;
}

static void
socket_set_poll_disable (DBusSocketSet *set,
190
                         DBusPollable   fd)
191 192 193 194 195 196
{
  DBusSocketSetPoll *self = socket_set_poll_cast (set);
  int i;

  for (i = 0; i < self->n_fds; i++)
    {
197
      if (_dbus_pollable_equals (self->fds[i].fd, fd))
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        {
          if (i != self->n_fds - 1)
            {
              self->fds[i].fd = self->fds[self->n_fds - 1].fd;
              self->fds[i].events = self->fds[self->n_fds - 1].events;
            }

          self->n_fds--;
          return;
        }
    }
}

static void
socket_set_poll_remove (DBusSocketSet *set,
213
                        DBusPollable   fd)
214 215 216 217 218 219
{
  DBusSocketSetPoll *self = socket_set_poll_cast (set);

  socket_set_poll_disable (set, fd);
  self->n_reserved--;

220
  _dbus_verbose ("after removing fd %" DBUS_POLLABLE_FORMAT " from %p, %d en/%d res/%d alloc\n",
221
                 _dbus_pollable_printable (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  _dbus_assert (self->n_fds <= self->n_reserved);
  _dbus_assert (self->n_reserved <= self->n_allocated);

  if (self->n_reserved + MINIMUM_SIZE < self->n_allocated / 2)
    {
      /* Our array is twice as big as it needs to be - deflate it until it's
       * only slightly larger than the number reserved. */
      DBusPollFD *new_fds = dbus_realloc (self->fds,
          sizeof (DBusPollFD) * (self->n_reserved + MINIMUM_SIZE));

      _dbus_verbose ("before deflating %p, %d en/%d res/%d alloc\n",
                     self, self->n_fds, self->n_reserved, self->n_allocated);

      if (_DBUS_UNLIKELY (new_fds == NULL))
        {
          /* Weird. Oh well, never mind, the too-big array is untouched */
          return;
        }

      self->fds = new_fds;
      self->n_allocated = self->n_reserved;
    }
}

static unsigned int
watch_flags_from_poll_revents (short revents)
{
  unsigned int condition = 0;

  if (revents & _DBUS_POLLIN)
    condition |= DBUS_WATCH_READABLE;
  if (revents & _DBUS_POLLOUT)
    condition |= DBUS_WATCH_WRITABLE;
  if (revents & _DBUS_POLLHUP)
    condition |= DBUS_WATCH_HANGUP;
  if (revents & _DBUS_POLLERR)
    condition |= DBUS_WATCH_ERROR;

  if (_DBUS_UNLIKELY (revents & _DBUS_POLLNVAL))
    condition |= _DBUS_WATCH_NVAL;

  return condition;
}

/** This is basically Linux's epoll_wait(2) implemented in terms of poll(2);
 * it returns results into a caller-supplied buffer so we can be reentrant. */
static int
socket_set_poll_poll (DBusSocketSet   *set,
                      DBusSocketEvent *revents,
                      int              max_events,
                      int              timeout_ms)
{
  DBusSocketSetPoll *self = socket_set_poll_cast (set);
  int i;
  int n_events;
  int n_ready;

  _dbus_assert (max_events > 0);

  for (i = 0; i < self->n_fds; i++)
    self->fds[i].revents = 0;

  n_ready = _dbus_poll (self->fds, self->n_fds, timeout_ms);

  if (n_ready <= 0)
    return n_ready;

  n_events = 0;

  for (i = 0; i < self->n_fds; i++)
    {
      if (self->fds[i].revents != 0)
        {
          revents[n_events].fd = self->fds[i].fd;
          revents[n_events].flags = watch_flags_from_poll_revents (self->fds[i].revents);

          n_events += 1;

          /* We ignore events beyond max_events because we have nowhere to
           * put them. _dbus_poll is level-triggered, so we'll just be told
           * about them next time round the main loop anyway. */
          if (n_events == max_events)
            return n_events;
        }
    }

  return n_events;
}

DBusSocketSetClass _dbus_socket_set_poll_class = {
    socket_set_poll_free,
    socket_set_poll_add,
    socket_set_poll_remove,
    socket_set_poll_enable,
    socket_set_poll_disable,
    socket_set_poll_poll
};

#endif /* !DOXYGEN_SHOULD_SKIP_THIS */