From 66b61d560fc626b5b50ebe6199653660769587ad Mon Sep 17 00:00:00 2001
From: Luis Lavena <luislavena@gmail.com>
Date: Sun, 14 Mar 2010 17:37:24 +0100
Subject: [PATCH] Removed dead code that refers to libev.

---
 rakelib/vm.rake          |    5 +-
 vm/builtin/channel.cpp   |    1 -
 vm/builtin/io.cpp        |    2 -
 vm/event.cpp             |  393 ----------------------------------------------
 vm/event.hpp             |  241 ----------------------------
 vm/primitives.cpp        |    1 -
 vm/test/test_channel.hpp |    2 -
 vm/vm.cpp                |    1 -
 8 files changed, 2 insertions(+), 644 deletions(-)
 delete mode 100644 vm/event.cpp
 delete mode 100644 vm/event.hpp

diff --git a/rakelib/vm.rake b/rakelib/vm.rake
index 853269f..2d7413c 100644
--- a/rakelib/vm.rake
+++ b/rakelib/vm.rake
@@ -55,7 +55,7 @@ vm_objs     = %w[ vm/drivers/cli.o ]
 vm_srcs     = %w[ vm/drivers/cli.cpp ]
 
 EX_INC      = %w[ libtommath libgdtoa onig libffi/include
-                  libltdl libev
+                  libltdl
                 ].map { |f| "vm/external_libs/#{f}" }
 
 INSN_GEN    = %w[ vm/gen/instruction_names.cpp
@@ -134,8 +134,7 @@ EXTERNALS   = %W[ vm/external_libs/libtommath/libtommath.a
                   vm/external_libs/libgdtoa/libgdtoa.a
                   vm/external_libs/onig/.libs/libonig.a
                   vm/external_libs/libffi/.libs/libffi.a
-                  vm/external_libs/libltdl/.libs/libltdl.a
-                  vm/external_libs/libev/.libs/libev.a ]
+                  vm/external_libs/libltdl/.libs/libltdl.a ]
 
 INCLUDES      = EX_INC + %w[vm/test/cxxtest vm .]
 
diff --git a/vm/builtin/channel.cpp b/vm/builtin/channel.cpp
index 111524a..8ad1191 100644
--- a/vm/builtin/channel.cpp
+++ b/vm/builtin/channel.cpp
@@ -15,7 +15,6 @@
 
 #include "arguments.hpp"
 #include "dispatch.hpp"
-#include "event.hpp"
 #include "call_frame.hpp"
 #include "native_thread.hpp"
 
diff --git a/vm/builtin/io.cpp b/vm/builtin/io.cpp
index 7ddcc22..5f5a42c 100644
--- a/vm/builtin/io.cpp
+++ b/vm/builtin/io.cpp
@@ -16,8 +16,6 @@
 #include "builtin/bytearray.hpp"
 #include "primitives.hpp"
 
-#include "vm/event.hpp"
-
 #include "vm.hpp"
 #include "objectmemory.hpp"
 
diff --git a/vm/event.cpp b/vm/event.cpp
deleted file mode 100644
index 67db071..0000000
--- a/vm/event.cpp
+++ /dev/null
@@ -1,393 +0,0 @@
-/* A C++ wrapper around libev, which Rubinius uses for all event handling.
- * This is the folcrum of the thread subsystem. */
-
-#include <sstream>
-#include <vector>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <ev.h>
-
-#include "vm.hpp"
-#include "objectmemory.hpp"
-
-#include "vm/object_utils.hpp"
-
-#include "builtin/tuple.hpp"
-#include "builtin/integer.hpp"
-#include "builtin/fixnum.hpp"
-#include "builtin/io.hpp"
-#include "builtin/thread.hpp"
-
-#include "event.hpp"
-
-
-namespace rubinius {
-
-  namespace event {
-
-    static void dispatch(Event *obj) {
-      if(obj->activated()) {
-        if(obj->loop) {
-          obj->loop->remove_event(obj);
-        }
-
-        if(obj->tracked()) {
-          delete obj;
-        } else {
-          obj->stop();
-        }
-      }
-    }
-
-    template <typename S>
-      void tramp(EV_P_ S* ev, int revents) {
-        event::dispatch((Event*)ev->data);
-      }
-
-    Event::Event(STATE, ObjectCallback* chan) :
-      state(state), channel(chan), id(0),
-      buffer(state), loop(NULL) { }
-
-    void IO::stop() {
-      ev_io_stop(loop->base, &ev);
-    }
-
-    void IO::start() {
-      ev_io_start(loop->base, &ev);
-    }
-
-    bool IO::for_fd_p(int in) {
-      if(fd == in) return true;
-      return false;
-    }
-
-    Write::Write(STATE, ObjectCallback* chan, int ifd) : IO(state, chan) {
-      fd = ifd;
-      ev_io_init(&ev, event::tramp<struct ev_io>, fd, EV_WRITE);
-      ev.data = this;
-    }
-
-    bool Write::activated() {
-      channel->call(Integer::from(state, fd));
-      return true;
-    }
-
-    Read::Read(STATE, ObjectCallback* chan, int ifd) :
-        IO(state, chan), count(0) {
-      fd = ifd;
-      ev_io_init(&ev, event::tramp<struct ev_io>, fd, EV_READ);
-      ev.data = this;
-    }
-
-    void Read::into_buffer(Object* maybe_buffer, std::size_t bytes) {
-      count = bytes;
-      buffer.set(maybe_buffer);
-    }
-
-    bool Read::activated() {
-      Object* ret;
-
-      if(buffer->nil_p()) {
-        ret = Integer::from(state, fd);
-      } else {
-        /* the + 1 is for the null on the end */
-        size_t bytes_to_read = count + 1;
-
-        if(buffer->left() < bytes_to_read) {
-          bytes_to_read = buffer->left() - 1;
-        }
-
-        char* start = buffer->at_unused();
-        while(1) {
-          int i = read(fd, start, bytes_to_read);
-
-          /* EOF seen */
-          if(i == 0) {
-            ret = Qnil;
-
-          /* didn't work out... */
-          } else if(i == -1) {
-            /* we were interrupted, how rude. go again. */
-            if(errno == EINTR) continue;
-
-            /* not sure. Send a system error */
-            ret = Tuple::from(state, 2, state->symbol("error"), Fixnum::from(errno));
-          } else {
-            /* clamp */
-            start[i] = 0;
-
-            buffer->read_bytes(state, i);
-            ret = Fixnum::from(i);
-          }
-
-          break;
-        }
-      }
-
-      channel->call(ret);
-
-      return true;
-    }
-
-    Signal::Signal(STATE, ObjectCallback *chan, int sig):
-        Event(state, chan), signal(sig) {
-      ev_signal_init(&ev, event::tramp<struct ev_signal>, sig);
-      ev.data = this;
-    }
-
-    void Signal::start() {
-      /* Only one event of a given signal type per loop. */
-      loop->remove_signal(signal);
-      ev_signal_start(loop->base, &ev);
-    }
-
-    void Signal::stop() {
-      ev_signal_stop(loop->base, &ev);
-    }
-
-    bool Signal::activated() {
-      channel->call(state->current_thread());
-      return false;
-    }
-
-    Timer::Timer(STATE, ObjectCallback* chan, double seconds, Object* obj):
-      Event(state, chan), tag(obj), timer_(NULL)
-    {
-      timer_ = new struct ev_timer;
-      ev_timer_init(timer_, event::tramp<ev_timer>, (ev_tstamp)seconds, 0.);
-      timer_->data = this;
-    }
-
-    void Timer::start() {
-      ev_timer_start(loop->base, timer_);
-    }
-
-    void Timer::stop() {
-      ev_timer_stop(loop->base, timer_);
-      delete timer_;
-    }
-
-    bool Timer::activated() {
-      channel->call(tag);
-      return true;
-    }
-
-
-/* SIGCHLD */
-
-
-    Child::Child(ObjectCallback* channel, pid_t pid, int opts):
-      channel_(channel), options_(opts), pid_(pid) {
-    }
-
-    Child::~Child() {}
-
-    void Child::add(STATE, ObjectCallback* channel, pid_t pid, int opts) {
-      Child::waiters().push_back(new Child(channel, pid, opts));
-
-      /*  This seems a bit cheap, but we need to force a check to
-       *  catch the case where wait is called before any child is
-       *  created, because such an occurrence must result in ECHILD.
-       *
-       *  Non-hanging force a check too, so that they do not
-       *  need to wait until the next signal arrives.
-       */
-      if((opts & WNOHANG) || Child::waiters().size() == 1) {
-        Child::find_finished(state);
-      }
-    }
-
-    /**
-     *  The idea here is that this method is called each time we receive
-     *  a SIGCHLD. Since we do not know which child exited, we check each
-     *  using waitpid(). The WNOHANG means that if the child is still up,
-     *  we can just move on to the next candidate and come back to check
-     *  later.
-     *
-     *  To avoid a problem with calling wait before any children exist
-     *  not raising an error, ::add() explicitly runs this method when
-     *  the first waiter is added (whether at the start or at some point
-     *  when all waiters have been removed previously.)
-     *
-     *  @todo Support WUNTRACED and other options?
-     */
-    void Child::find_finished(VM* state) {
-      Waiters& all = Child::waiters();
-
-      for(Waiters::iterator it = all.begin(); it != all.end(); /* Updated in body */) {
-        int status = 0;
-        Child* waiter = *it;
-
-        pid_t pid = ::waitpid(waiter->pid(), &status, WNOHANG);
-
-        switch(pid) {
-        case -1:
-          if(errno == EINTR) {
-            continue; /* With the same iterator */
-          }
-
-          if(errno == ECHILD) {
-            waiter->channel()->call(Qfalse);
-            it = all.erase(it);
-            continue;
-          }
-
-          break;
-
-        case 0:       /* No stopped children. */
-          if(waiter->options() & WNOHANG) {
-            waiter->channel()->call(Qnil);
-            it = all.erase(it);
-            continue;
-          }
-
-          /* Only blocking waits remain */
-          ++it;
-          break;
-
-        default:      /* Found it */
-          Object* SP = Qtrue;
-
-          if(WIFEXITED(status)) {
-            SP = as<Object>(Fixnum::from(WEXITSTATUS(status)));
-          }
-
-          waiter->channel()->call(Tuple::from(state, 2, Fixnum::from(pid), SP));
-          it = all.erase(it);
-          continue;
-        }
-      }   /* for */
-    }
-
-    /** @todo Fix the options. --rue */
-    Loop::Loop(struct ev_loop *loop) :
-      base(loop), event_ids(0), options_(0), owner(false) { }
-
-    Loop::Loop(int opts) : event_ids(0), options_(opts), owner(false) {
-      base = ev_default_loop(options_);
-
-      /* @todo Should fail here if default returns NULL */
-    }
-
-    /* Gives this loop ownership of +ev+, letting it delete +ev+
-     * when +ev+ is done, then starts the event. */
-    void Loop::start(Event* ev) {
-      ev->loop = this;
-      ev->id = ++event_ids;
-      ev->start();
-
-      // It's important this is last. Signal::start removes older Signal
-      // events by looking through +events+ on start. We don't want it
-      // to remove itself, so we do this after the event has actually
-      // started.
-      events.push_back(ev);
-    }
-
-    /** @todo Figure out what to do with default vs. regular loops. --rue */
-    Loop::~Loop() {
-      if(owner) {
-        std::vector<Event*>::iterator it;
-        for(it = events.begin(); it != events.end(); it = events.erase(it)) {
-          delete *it;
-        }
-
-        if(base != ev_default_loop(0)) {
-          ev_loop_destroy(base);
-        }
-      }
-    }
-
-    size_t Loop::num_of_events() {
-      return events.size();
-    }
-
-    size_t Loop::loop_count() {
-      return (size_t)ev_loop_count(base);
-    }
-
-    void Loop::poll() {
-      ev_loop(base, EVLOOP_NONBLOCK);
-    }
-
-    void Loop::run_and_wait() {
-      ev_loop(base, EVLOOP_ONESHOT);
-    }
-
-    void Loop::clear_by_fd(int fd) {
-      std::vector<Event*>::iterator it;
-      for(it = events.begin(); it != events.end();) {
-        if((*it)->for_fd_p(fd)) {
-          delete *it;
-          it = events.erase(it);
-        } else {
-          it++;
-        }
-      }
-    }
-
-    void Loop::clear_by_channel(void* chan) {
-      std::vector<Event*>::iterator it;
-      for(it = events.begin(); it != events.end();) {
-        if((*it)->channel == chan) {
-          delete *it;
-          it = events.erase(it);
-        } else {
-          it++;
-        }
-      }
-    }
-
-    void Loop::clear_by_id(size_t id) {
-      std::vector<Event*>::iterator it;
-      for(it = events.begin(); it != events.end();) {
-        if((*it)->id == id) {
-          delete *it;
-          it = events.erase(it);
-        } else {
-          it++;
-        }
-      }
-    }
-
-    void Loop::remove_event(Event *ev) {
-      std::vector<Event*>::iterator it;
-      for(it = events.begin(); it != events.end();) {
-        if(*it == ev) {
-          it = events.erase(it);
-        } else {
-          it++;
-        }
-      }
-    }
-
-    // Look through events for a Signal object for +signal_number+
-    // If we find one, send nil to the channel and remove it.
-    void Loop::remove_signal(int signal_number) {
-      for(std::vector<Event*>::iterator it = events.begin();
-          it != events.end();) {
-
-        Event* ev = *it;
-        if(Signal* sig = dynamic_cast<Signal*>(ev)) {
-          if(sig->signal == signal_number) {
-            sig->channel->call(Qnil);
-            sig->stop();
-            it = events.erase(it);
-            continue;
-          }
-        }
-
-        it++;
-      }
-    }
-
-  } // event
-} // rubinius
diff --git a/vm/event.hpp b/vm/event.hpp
deleted file mode 100644
index d13b1fb..0000000
--- a/vm/event.hpp
+++ /dev/null
@@ -1,241 +0,0 @@
-#ifndef RBX_EVENT_HPP
-#define RBX_EVENT_HPP
-
-#include <list>
-
-#include <sys/wait.h>
-#include <sys/signal.h>
-#include <ev.h>
-
-#include "prelude.hpp"
-#include "virtual.hpp"
-
-
-/** @todo We are leaking ObjectCallbacks. They need to be deleted (elsewhere.) */
-namespace rubinius {
-
-  class IOBuffer;
-
-  namespace event {
-
-    class Loop;
-
-    class Event {
-    public:
-      STATE;
-      ObjectCallback* channel;
-      size_t id;
-      TypedRoot<IOBuffer*> buffer;
-      Loop* loop;
-
-      Event(STATE, ObjectCallback* chan);
-      virtual ~Event() { }
-      virtual void start() = 0;
-      virtual void stop() = 0;
-      virtual bool for_fd_p(int fd) { return false; }
-      virtual bool activated() = 0;
-      bool tracked() { return id > 0; }
-    };
-
-    class IO : public Event {
-    public:
-      struct ev_io ev;
-
-      int fd;
-
-      IO(STATE, ObjectCallback* chan) : Event(state, chan) { }
-      virtual ~IO() { stop(); }
-      virtual bool for_fd_p(int fd);
-      virtual void stop();
-      virtual void start();
-      virtual bool activated() = 0;
-    };
-
-    class Write : public IO {
-    public:
-      Write(STATE, ObjectCallback*, int fd);
-      virtual ~Write() { }
-      virtual bool activated();
-    };
-
-    class Read : public IO {
-    public:
-      size_t count;
-
-      Read(STATE, ObjectCallback* chan, int fd);
-      virtual ~Read() { }
-      void into_buffer(Object* maybe_buffer, std::size_t bytes);
-      virtual bool activated();
-    };
-
-    class Signal : public Event {
-    public:
-      struct ev_signal ev;
-      int signal;
-
-      Signal(STATE, ObjectCallback *chan, int sig);
-      virtual ~Signal() { stop(); }
-      virtual void start();
-      virtual void stop();
-      virtual bool activated();
-    };
-
-    /**
-     *  @note   Unlike other Events, we use a dynamically allocated
-     *          ev_timer here because some evil shit libev does makes
-     *          the compiler think there may be something wrong with
-     *          ev_timer_init. I could not be bothered to figure out
-     *          whether the warning was for cause. And no, casting
-     *          does not work because it would potentially create a
-     *          temporary. --rue
-     */
-    class Timer : public Event {
-    public:
-      Object* tag;
-      struct ev_timer* timer_;
-
-      Timer(STATE, ObjectCallback* chan, double seconds, Object* obj = Qnil);
-      virtual ~Timer() { stop(); }
-      virtual void start();
-      virtual void stop();
-      virtual bool activated();
-    };
-
-    /**
-     *  Slightly different, since these events are not
-     *  tracked discretely by libev. Instead, we abstract
-     *  them out into their own list to be accessed when
-     *  a SIGCHLD occurs.
-     *
-     *  A single special Event, a Child::Event object, is
-     *  inserted to track all SIGCHLDs. The actual Child
-     *  "events" are held internally here similarly to what
-     *  the real event loop does. When our singular Event
-     *  fires, we dispatch using the internal Childs.
-     *
-     *  @see  Further discussion below and in event.cpp.
-     *
-     *  @todo Cancel-by-id handling via negatives?
-     *
-     *  @todo Review when multiple loops or native threads.
-     */
-    class Child {
-    public:   /* Types */
-
-      /*
-       *  @todo If there is a really performance-strapped app that
-       *        bottlenecks here, revisit the map<pid_t, list<Child*>>
-       *        version. It is just overkill for now.
-       */
-      typedef std::list<Child*> Waiters;
-
-      typedef struct ev_signal Watcher;
-      typedef struct ev_loop EVLoop;
-
-      /**
-       *  Child::Event is a special case of Signal.
-       *
-       *  Whereas other Signals merely send to their waiting
-       *  Channels or Callbacks, our task is a bit trickier.
-       *
-       *  This is also a "singleton" Event: only one SIGCHLD
-       *  Event exists, and it uses the Child objects as its
-       *  internal event-like things somewhat analogously to
-       *  the other real Events.
-       */
-      class Event : public Signal {
-      public:
-
-        /** Create the Event. Nothing special here, activated() is the override. */
-        Event(STATE): Signal(state, NULL, SIGCHLD) {}
-        virtual ~Event() {}
-
-      public:   /* Interface */
-
-        /** Special override that invokes the main Child::find_finished(). */
-        virtual bool activated() { Child::find_finished(state); return false; }
-      };
-
-
-    private:   /* Ctors */
-
-      /** Create Child using API similar to "real" Events. */
-      Child(ObjectCallback* channel, pid_t pid, int opts);
-      ~Child();
-
-
-    public:   /* Class interface */
-
-      /** Add a new interested channel. */
-      static void     add(STATE, ObjectCallback* channel, pid_t pid, int opts);
-
-      /** Figure which child(ren) finished when SIGCHLD received. */
-      static void     find_finished(STATE);
-
-      /** All current Child events. */
-      static Waiters& waiters() { static Waiters our_waiters; return our_waiters; };
-
-
-    public:   /* Accessors */
-
-      /** Returns callback to write result to channel. */
-      ObjectCallback* channel() { return channel_; }
-      /** Returns options given to be passed to ::waitpid. */
-      int             options() { return options_; }
-      /** Returns pid given to wait for. */
-      pid_t           pid()     { return pid_; }
-
-
-    private:  /* Instance vars */
-
-      /** Callback to write result to channel when ready. */
-      ObjectCallback* channel_;
-      /** Options to pass to ::waitpid, currently just WNOHANG. */
-      int             options_;
-      /** Process ID to wait for, also -1 for any and others per system. */
-      pid_t           pid_;
-    };
-
-
-    /**
-     *  @todo Needs review when multiple loops are introduced.
-     */
-    class Loop {
-    public:   /* Ctors */
-
-      Loop(int options = 0);
-      Loop(struct ev_loop *loop);
-
-      /** Stop any remaining watchers and kill the loop if we control it. */
-      ~Loop();
-
-
-    public:   /* Interface */
-
-      size_t loop_count();
-      size_t num_of_events();
-      void start(Event* ev);
-      void poll();
-      void run_and_wait();
-      void clear_by_fd(int fd);
-      void clear_by_channel(void* chan);
-      void clear_by_id(size_t id);
-      void remove_event(Event* ev);
-      void remove_signal(int sig);
-
-
-    public:   /* Instance vars */
-
-      struct ev_loop*     base;
-      std::vector<Event*> events;
-      size_t              event_ids;
-      /** Options given at time of creation. */
-      int                 options_;
-      /** Whether this Loop controls the event loop it uses. */
-      bool                owner;
-    };
-
-  }     /* event */
-}       /* rubinius */
-
-#endif
diff --git a/vm/primitives.cpp b/vm/primitives.cpp
index 784f4f3..6f3e1ca 100644
--- a/vm/primitives.cpp
+++ b/vm/primitives.cpp
@@ -1,7 +1,6 @@
 #include "prelude.hpp"
 #include "vm.hpp"
 #include "primitives.hpp"
-#include "event.hpp"
 #include "gen/includes.hpp"
 #include "arguments.hpp"
 #include "call_frame.hpp"
diff --git a/vm/test/test_channel.hpp b/vm/test/test_channel.hpp
index 1ed6a7f..fc9393d 100644
--- a/vm/test/test_channel.hpp
+++ b/vm/test/test_channel.hpp
@@ -7,8 +7,6 @@
 #include "builtin/symbol.hpp"
 #include "builtin/thread.hpp"
 
-#include "event.hpp"
-
 #include <sys/time.h>
 
 class TestChannel : public CxxTest::TestSuite, public VMTest {
diff --git a/vm/vm.cpp b/vm/vm.cpp
index 24bced8..338ec68 100644
--- a/vm/vm.cpp
+++ b/vm/vm.cpp
@@ -1,6 +1,5 @@
 #include "vm.hpp"
 #include "objectmemory.hpp"
-#include "event.hpp"
 #include "global_cache.hpp"
 #include "gc/gc.hpp"
 
-- 
1.6.3.3

