diff options
| author | Martin Sustrik <sustrik@250bpm.com> | 2012-04-14 16:39:39 +0200 | 
|---|---|---|
| committer | Martin Sustrik <sustrik@250bpm.com> | 2012-04-15 06:57:37 +0200 | 
| commit | da8b8000bd2a84c04a3a9cd6ce57f9bc814595cf (patch) | |
| tree | c35be3404a8133ba5c4ef2a203bb7fa6fc108e22 | |
| parent | e86827511b35231679085dc236e9744184ed4609 (diff) | |
thread_t rewritten in C style
thread_t is one of the classes where C++ syntax creates more
problems then it solves. This patch converts it into simple
C-style pseudo-class.
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
| -rw-r--r-- | src/devpoll.cpp | 4 | ||||
| -rw-r--r-- | src/epoll.cpp | 4 | ||||
| -rw-r--r-- | src/kqueue.cpp | 4 | ||||
| -rw-r--r-- | src/poll.cpp | 4 | ||||
| -rw-r--r-- | src/select.cpp | 4 | ||||
| -rw-r--r-- | src/thread.cpp | 34 | ||||
| -rw-r--r-- | src/thread.hpp | 41 | 
7 files changed, 36 insertions, 59 deletions
| diff --git a/src/devpoll.cpp b/src/devpoll.cpp index e9f172b..76943f0 100644 --- a/src/devpoll.cpp +++ b/src/devpoll.cpp @@ -47,7 +47,7 @@ xs::devpoll_t::devpoll_t (xs::ctx_t *ctx_, uint32_t tid_) :  xs::devpoll_t::~devpoll_t ()  { -    worker.stop (); +    thread_stop (&worker);      close (devpoll_fd);  } @@ -138,7 +138,7 @@ void xs::devpoll_t::reset_pollout (handle_t handle_)  void xs::devpoll_t::xstart ()  { -    worker.start (worker_routine, this); +    thread_start (&worker, worker_routine, this);  }  void xs::devpoll_t::xstop () diff --git a/src/epoll.cpp b/src/epoll.cpp index e1d3f8b..8ba2f17 100644 --- a/src/epoll.cpp +++ b/src/epoll.cpp @@ -45,7 +45,7 @@ xs::epoll_t::epoll_t (xs::ctx_t *ctx_, uint32_t tid_) :  xs::epoll_t::~epoll_t ()  {      //  Wait till the worker thread exits. -    worker.stop (); +    thread_stop (&worker);      close (epoll_fd);      for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) @@ -121,7 +121,7 @@ void xs::epoll_t::reset_pollout (handle_t handle_)  void xs::epoll_t::xstart ()  { -    worker.start (worker_routine, this); +    thread_start (&worker, worker_routine, this);  }  void xs::epoll_t::xstop () diff --git a/src/kqueue.cpp b/src/kqueue.cpp index d680915..c3ef5b5 100644 --- a/src/kqueue.cpp +++ b/src/kqueue.cpp @@ -55,7 +55,7 @@ xs::kqueue_t::kqueue_t (xs::ctx_t *ctx_, uint32_t tid_) :  xs::kqueue_t::~kqueue_t ()  { -    worker.stop (); +    thread_stop (&worker);      close (kqueue_fd);  } @@ -144,7 +144,7 @@ void xs::kqueue_t::reset_pollout (handle_t handle_)  void xs::kqueue_t::xstart ()  { -    worker.start (worker_routine, this); +    thread_start (&worker, worker_routine, this);  }  void xs::kqueue_t::xstop () diff --git a/src/poll.cpp b/src/poll.cpp index 3076b78..c0f070e 100644 --- a/src/poll.cpp +++ b/src/poll.cpp @@ -41,7 +41,7 @@ xs::poll_t::poll_t (xs::ctx_t *ctx_, uint32_t tid_) :  xs::poll_t::~poll_t ()  { -    worker.stop (); +    thread_stop (&worker);  }  xs::handle_t xs::poll_t::add_fd (fd_t fd_, i_poll_events *events_) @@ -109,7 +109,7 @@ void xs::poll_t::reset_pollout (handle_t handle_)  void xs::poll_t::xstart ()  { -    worker.start (worker_routine, this); +    thread_start (&worker, worker_routine, this);  }  void xs::poll_t::xstop () diff --git a/src/select.cpp b/src/select.cpp index 8e56a2d..eeac5a9 100644 --- a/src/select.cpp +++ b/src/select.cpp @@ -57,7 +57,7 @@ xs::select_t::select_t (xs::ctx_t *ctx_, uint32_t tid_) :  xs::select_t::~select_t ()  { -    worker.stop (); +    thread_stop (&worker);  }  xs::handle_t xs::select_t::add_fd (fd_t fd_, i_poll_events *events_) @@ -141,7 +141,7 @@ void xs::select_t::reset_pollout (handle_t handle_)  void xs::select_t::xstart ()  { -    worker.start (worker_routine, this); +    thread_start (&worker, worker_routine, this);  }  void xs::select_t::xstop () diff --git a/src/thread.cpp b/src/thread.cpp index e7af627..961bee1 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -35,20 +35,20 @@ extern "C"      }  } -void xs::thread_t::start (thread_fn *tfn_, void *arg_) +void xs::thread_start (xs::thread_t *self_, thread_fn *tfn_, void *arg_)  { -    tfn = tfn_; -    arg =arg_; -    descriptor = (HANDLE) _beginthreadex (NULL, 0, -        &::thread_routine, this, 0 , NULL); -    win_assert (descriptor != NULL);     +    self_->tfn = tfn_; +    self_->arg =arg_; +    self_->handle = (HANDLE) _beginthreadex (NULL, 0, +        &::thread_routine, self_, 0 , NULL); +    win_assert (self_->handle != NULL);      } -void xs::thread_t::stop () +void xs::thread_stop (xs::thread_t *self_)  { -    DWORD rc = WaitForSingleObject (descriptor, INFINITE); +    DWORD rc = WaitForSingleObject (self_->handle, INFINITE);      win_assert (rc != WAIT_FAILED); -    BOOL rc2 = CloseHandle (descriptor); +    BOOL rc2 = CloseHandle (self_->handle);      win_assert (rc2 != 0);  } @@ -76,23 +76,19 @@ extern "C"      }  } -void xs::thread_t::start (thread_fn *tfn_, void *arg_) +void xs::thread_start (xs::thread_t *self_, thread_fn *tfn_, void *arg_)  { -    tfn = tfn_; -    arg =arg_; -    int rc = pthread_create (&descriptor, NULL, thread_routine, this); +    self_->tfn = tfn_; +    self_->arg =arg_; +    int rc = pthread_create (&self_->handle, NULL, thread_routine, self_);      posix_assert (rc);  } -void xs::thread_t::stop () +void xs::thread_stop (xs::thread_t *self_)  { -    int rc = pthread_join (descriptor, NULL); +    int rc = pthread_join (self_->handle, NULL);      posix_assert (rc);  }  #endif - - - - diff --git a/src/thread.hpp b/src/thread.hpp index b7a7607..8258dab 100644 --- a/src/thread.hpp +++ b/src/thread.hpp @@ -35,45 +35,26 @@ namespace xs      typedef void (thread_fn) (void*); -    //  Class encapsulating OS thread. Thread initiation/termination is done -    //  using special functions rather than in constructor/destructor so that -    //  thread isn't created during object construction by accident, causing -    //  newly created thread to access half-initialised object. Same applies -    //  to the destruction process: Thread should be terminated before object -    //  destruction begins, otherwise it can access half-destructed object. +    //  Class encapsulating OS thread. -    class thread_t +    struct thread_t      { -    public: - -        inline thread_t () -        { -        } - -        //  Creates OS thread. 'tfn' is main thread function. It'll be passed -        //  'arg' as an argument. -        void start (thread_fn *tfn_, void *arg_); - -        //  Waits for thread termination. -        void stop (); - -        //  These are internal members. They should be private, however then -        //  they would not be accessible from the main C routine of the thread.          thread_fn *tfn;          void *arg; -         -    private: -  #ifdef XS_HAVE_WINDOWS -        HANDLE descriptor; +        HANDLE handle;  #else -        pthread_t descriptor; +        pthread_t handle;  #endif - -        thread_t (const thread_t&); -        const thread_t &operator = (const thread_t&);      }; +    //  Creates OS thread. 'tfn' is main thread function. It'll be passed +    //  'arg' as an argument. +    void thread_start (thread_t *self_, thread_fn *tfn_, void *arg_); + +    //  Waits for thread termination. +    void thread_stop (thread_t *self_); +  }  #endif | 
