Changeset 3:e4ca2e057cec in xplcommon


Ignore:
Timestamp:
12/27/12 13:01:22 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Reorganized the code a bit

Files:
8 added
3 edited
8 moved

Legend:

Unmodified
Added
Removed
  • configure.ac

    r2 r3  
    2626        src/Makefile
    2727        src/xplcommon/Makefile
     28        src/xplcommon/posix/Makefile
     29        src/xplcommon/win32/Makefile
    2830])
    2931
  • src/xplcommon/Makefile.am

    r2 r3  
     1SUBDIRS=
     2if TARGET_API_POSIX
     3SUBDIRS+=posix
     4endif
     5if TARGET_API_WIN32
     6SUBDIRS+=win32
     7endif
     8
    19lib_LTLIBRARIES=libxplcommon.la
    210
     
    412
    513if TARGET_API_POSIX
    6 libxplcommon_la_SOURCES +=      \
    7         POSIXThread.cc          \
    8         POSIXWaitable.cc        \
    9         POSIXWaiter.cc
     14libxplcommon_la_LIBADD=posix/libxplcommon_posix.la
    1015endif
    1116
    1217if TARGET_API_WIN32
    13 libxplcommon_la_SOURCES +=      \
    14         Win32Thread.cc
     18libxplcommon_la_LIBADD=win32/libxplcommon_win32.la
    1519endif
    1620
    1721include_xplcommondir=$(includedir)/xplcommon
    1822include_xplcommon_HEADERS=\
    19         POSIXThread.h           \
    20         Win32Thread.h           \
    21         Thread.h                \
    22         POSIXWaitable.h         \
    23         POSIXWaiter.h
     23        config.h        \
     24        Thread.h
  • src/xplcommon/Thread.h

    r1 r3  
    3434#include "config.h"
    3535
    36 #ifdef HAVE_WINDOWS_H
     36//------------------------------------------------------------------------------
    3737
    38 #include "Win32Thread.h"
     38#if TARGET_API_WIN32
    3939
    40 #else
     40#include "win32/Thread.h"
    4141
    42 #include "POSIXThread.h"
     42namespace xplcommon { typedef win32::Thread Thread; }
    4343
    44 #endif
     44//------------------------------------------------------------------------------
     45#else // TARGET_API_WIN32
     46//------------------------------------------------------------------------------
     47
     48#include "posix/Thread.h"
     49
     50namespace xplcommon { typedef posix::Thread Thread; }
     51
     52//------------------------------------------------------------------------------
     53
     54#endif // TARGET_API_WIN32
    4555
    4656//------------------------------------------------------------------------------
  • src/xplcommon/posix/Thread.cc

    r1 r3  
    3030//------------------------------------------------------------------------------
    3131
    32 #include "config.h"
    33 #ifdef HAVE_PTHREAD_H
     32#include "Thread.h"
    3433
    3534//------------------------------------------------------------------------------
    3635
    37 #include "POSIXThread.h"
     36using xplcommon::posix::Thread;
    3837
    3938//------------------------------------------------------------------------------
    4039
    41 using xplcommon::POSIXThread;
    42 
    43 //------------------------------------------------------------------------------
    44 
    45 void* POSIXThread::threadFn(void* arg)
     40void* Thread::threadFn(void* arg)
    4641{
    47     reinterpret_cast<POSIXThread*>(arg)->run();
     42    reinterpret_cast<Thread*>(arg)->run();
    4843    return 0;
    4944}
    5045
    5146//------------------------------------------------------------------------------
    52 #endif // HAVE_PTHREAD_H
    53 
    5447
    5548// Local Variables:
  • src/xplcommon/posix/Thread.h

    r1 r3  
    2828// either expressed or implied, of the FreeBSD Project.
    2929
    30 #ifndef XPLCOMMON_POSIXTHREAD_H
    31 #define XPLCOMMON_POSIXTHREAD_H
    32 //------------------------------------------------------------------------------
    33 
    34 #include "config.h"
    35 
    36 #ifdef HAVE_PTHREAD_H
    37 
     30#ifndef XPLCOMMON_POSIX_THREAD_H
     31#define XPLCOMMON_POSIX_THREAD_H
    3832//------------------------------------------------------------------------------
    3933
     
    4236//------------------------------------------------------------------------------
    4337
    44 namespace xplcommon {
     38namespace xplcommon { namespace posix {
    4539
    4640//------------------------------------------------------------------------------
     
    4943 * A pthreads-based thread implementation.
    5044 */
    51 class POSIXThread
     45class Thread
    5246{
    5347private:
     
    6660     * Virtual destructor.
    6761     */
    68     virtual ~POSIXThread();
     62    virtual ~Thread();
    6963
    7064    /**
     
    8781
    8882//------------------------------------------------------------------------------
    89 
    90 /// Common type for OS-independent code
    91 typedef POSIXThread Thread;
    92 
    93 //------------------------------------------------------------------------------
    9483// Inline definitions
    9584//------------------------------------------------------------------------------
    9685
    97 inline POSIXThread::~POSIXThread()
     86inline Thread::~Thread()
    9887{
    9988}
     
    10190//------------------------------------------------------------------------------
    10291
    103 inline bool POSIXThread::start()
     92inline bool Thread::start()
    10493{
    10594    return pthread_create(&thread, 0, &threadFn, this)>=0;
     
    10897//------------------------------------------------------------------------------
    10998
    110 inline void POSIXThread::join()
     99inline void Thread::join()
    111100{
    112101    pthread_join(thread, 0);
     
    115104//------------------------------------------------------------------------------
    116105
    117 } /* namespace xplcommon */
     106} /* namespace xplcommon::posix */ } /* namespace xplcommon */
    118107
    119108//------------------------------------------------------------------------------
    120 #endif // HAVE_PTHREAD_H
    121 
    122 #endif // XPLCOMMON_POSIXTHREAD_H
     109#endif // XPLCOMMON_POSIX_THREAD_H
    123110
    124111// Local Variables:
  • src/xplcommon/posix/Waitable.cc

    r1 r3  
    3030//------------------------------------------------------------------------------
    3131
    32 #include "config.h"
    33 #ifdef HAVE_POLL_H
     32#include "Waitable.h"
     33
     34#include "Waiter.h"
    3435
    3536//------------------------------------------------------------------------------
    3637
    37 #include "POSIXWaitable.h"
    38 
    39 #include "POSIXWaiter.h"
     38using xplcommon::posix::Waitable;
    4039
    4140//------------------------------------------------------------------------------
    4241
    43 using xplcommon::POSIXWaitable;
    44 
    45 //------------------------------------------------------------------------------
    46 
    47 POSIXWaitable::POSIXWaitable(POSIXWaiter* waiter, int fd, short events) :
     42Waitable::Waitable(Waiter* waiter, int fd, short events) :
    4843    waiter(waiter),
    4944    fd(fd),
     
    5550//------------------------------------------------------------------------------
    5651
    57 POSIXWaitable::~POSIXWaitable()
     52Waitable::~Waitable()
    5853{
    5954    if (waiter!=0) waiter->remove(this);
     
    6156
    6257//------------------------------------------------------------------------------
    63 #endif // HAVE_POLL_H
    6458
    6559// Local Variables:
  • src/xplcommon/posix/Waitable.h

    r1 r3  
    2828// either expressed or implied, of the FreeBSD Project.
    2929
    30 #ifndef XPLCOMMON_POSIXWAITABLE_H
    31 #define XPLCOMMON_POSIXWAITABLE_H
     30#ifndef XPLCOMMON_POSIX_WAITABLE_H
     31#define XPLCOMMON_POSIX_WAITABLE_H
    3232//------------------------------------------------------------------------------
    3333
    34 #include "config.h"
    35 
    36 #ifdef HAVE_POLL_H
     34namespace xplcommon { namespace posix {
    3735
    3836//------------------------------------------------------------------------------
    3937
    40 namespace xplcommon {
    41 
    42 //------------------------------------------------------------------------------
    43 
    44 class POSIXWaiter;
     38class Waiter;
    4539
    4640//------------------------------------------------------------------------------
    4741
    4842/**
    49  * Base class of objects for which one can wait in a POSIXWaiter.
     43 * Base class of objects for which one can wait in a Waiter.
    5044 *
    5145 * It is basically a file descriptor.
    5246 */
    53 class POSIXWaitable
     47class Waitable
    5448{
    5549protected:
     
    5751     * The waiter this waitable belongs to, if any.
    5852     */
    59     POSIXWaiter* waiter;
     53    Waiter* waiter;
    6054
    6155    /**
     
    7367     * Construct the waitable.
    7468     */
    75     POSIXWaitable(POSIXWaiter* waiter, int fd = -1, short events = 0);
     69    Waitable(Waiter* waiter, int fd = -1, short events = 0);
    7670
    7771public:
     
    7973     * Destroy the waitable. It will be removed from the waiter.
    8074     */
    81     virtual ~POSIXWaitable();
     75    virtual ~Waitable();
    8276
    8377    /**
     
    9690    virtual void handleEvents(short events) = 0;
    9791
    98     friend class POSIXWaiter;
     92    friend class Waiter;
    9993};
    10094
    10195//------------------------------------------------------------------------------
    10296
    103 typedef POSIXWaitable Waitable;
     97} /* namespace xplcommon::posix */ } /* namespace xplcommon */
    10498
    10599//------------------------------------------------------------------------------
    106 
    107 } /* namespace xplcommon */
    108 
    109 //------------------------------------------------------------------------------
    110 #endif // HAVE_POLL_H
    111 
    112100#endif // XPLCOMMON_POSIXWAITABLE_H
    113101
  • src/xplcommon/posix/Waiter.cc

    r1 r3  
    3030//------------------------------------------------------------------------------
    3131
    32 #include "config.h"
    33 #ifdef HAVE_POLL_H
     32#include "Waiter.h"
    3433
    35 //------------------------------------------------------------------------------
    36 
    37 #include "POSIXWaiter.h"
    38 #include "POSIXWaitable.h"
     34#include "Waitable.h"
    3935
    4036#include <cassert>
     
    4339//------------------------------------------------------------------------------
    4440
    45 using xplcommon::POSIXWaiter;
     41using xplcommon::posix::Waiter;
    4642
    4743//------------------------------------------------------------------------------
    4844
    49 inline bool POSIXWaiter::hasReady() const
     45inline bool Waiter::hasReady() const
    5046{
    5147    for(waitables_t::const_iterator i = waitables.begin(); i!=waitables.end();
    5248        ++i)
    5349    {
    54         POSIXWaitable* waitable = i->second;
     50        Waitable* waitable = i->second;
    5551        if (waitable->ready()) return true;
    5652    }
     
    6056//------------------------------------------------------------------------------
    6157
    62 inline size_t POSIXWaiter::setupPollFDs(pollfd* pollFDs)
     58inline size_t Waiter::setupPollFDs(pollfd* pollFDs)
    6359{
    6460    size_t numValid = 0;
     
    6763        ++i)
    6864    {
    69         POSIXWaitable* waitable = i->second;
     65        Waitable* waitable = i->second;
    7066        if (waitable->events==0) continue;
    7167
     
    8278//------------------------------------------------------------------------------
    8379
    84 inline void POSIXWaiter::processPollFDs(const pollfd* pollFDs, size_t size)
     80inline void Waiter::processPollFDs(const pollfd* pollFDs, size_t size)
    8581{
    8682    for(size_t i = 0; i<size; ++i) {
     
    8985            waitables_t::iterator j = waitables.find(pollFD.fd);
    9086            assert(j!=waitables.end());
    91             POSIXWaitable* waitable = j->second;
     87            Waitable* waitable = j->second;
    9288            waitable->handleEvents(pollFD.revents);
    9389        }
     
    9793//------------------------------------------------------------------------------
    9894
    99 POSIXWaiter::~POSIXWaiter()
     95Waiter::~Waiter()
    10096{
    10197    for(waitables_t::iterator i = waitables.begin(); i!=waitables.end(); ++i)
    10298    {
    103         POSIXWaitable* waitable = i->second;
     99        Waitable* waitable = i->second;
    104100        waitable->waiter = 0;
    105101    }
     
    108104//------------------------------------------------------------------------------
    109105
    110 bool POSIXWaiter::wait(int timeout)
     106bool Waiter::wait(int timeout)
    111107{
    112108    if (hasReady()) return true;
     
    118114
    119115    if (result<0) {
    120         perror("POSIXWaiter::wait: poll");
     116        perror("xplcommon::posix::Waiter::wait: poll");
    121117        return false;
    122118    } else if (result>0) {
     
    129125//------------------------------------------------------------------------------
    130126
    131 void POSIXWaiter::add(POSIXWaitable* waitable)
     127void Waiter::add(Waitable* waitable)
    132128{
    133129    assert(waitable->fd>=0);
     
    139135//------------------------------------------------------------------------------
    140136
    141 void POSIXWaiter::remove(POSIXWaitable* waitable)
     137void Waiter::remove(Waitable* waitable)
    142138{
    143139    assert(numWaitables>0);
     
    150146
    151147//------------------------------------------------------------------------------
    152 #endif // HAVE_POLL_H
    153148
    154149// Local Variables:
  • src/xplcommon/posix/Waiter.h

    r1 r3  
    2828// either expressed or implied, of the FreeBSD Project.
    2929
    30 #ifndef XPLCOMMON_POSIXWAITER_H
    31 #define XPLCOMMON_POSIXWAITER_H
    32 //------------------------------------------------------------------------------
    33 
    34 #include "config.h"
    35 
    36 #ifdef HAVE_POLL_H
    37 
     30#ifndef XPLCOMMON_POSIX_WAITER_H
     31#define XPLCOMMON_POSIX_WAITER_H
    3832//------------------------------------------------------------------------------
    3933
     
    4640//------------------------------------------------------------------------------
    4741
    48 namespace xplcommon {
     42namespace xplcommon { namespace posix {
    4943
    5044//------------------------------------------------------------------------------
    5145
    52 class POSIXWaitable;
     46class Waitable;
    5347
    5448//------------------------------------------------------------------------------
     
    5852 * for them.
    5953 */
    60 class POSIXWaiter
     54class Waiter
    6155{
    6256private:
     
    6458     * Type for the set of waitables.
    6559     */
    66     typedef std::map<int, POSIXWaitable*> waitables_t;
     60    typedef std::map<int, Waitable*> waitables_t;
    6761
    6862    /**
     
    8074     * Construct the waiter.
    8175     */
    82     POSIXWaiter();
     76    Waiter();
    8377
    8478    /**
     
    8680     * modified to point to no waiter.
    8781     */
    88     ~POSIXWaiter();
     82    ~Waiter();
    8983
    9084    /**
     
    10397     * Add a waitable.
    10498     */
    105     void add(POSIXWaitable* waitable);
     99    void add(Waitable* waitable);
    106100
    107101    /**
    108102     * Remove a waitable.
    109103     */
    110     void remove(POSIXWaitable* waitable);
     104    void remove(Waitable* waitable);
    111105
    112106private:
     
    134128    void processPollFDs(const pollfd* pollFDs, size_t size);
    135129
    136     friend class POSIXWaitable;
     130    friend class Waitable;
    137131};
    138 
    139 //------------------------------------------------------------------------------
    140 
    141 typedef POSIXWaiter Waiter;
    142132
    143133//------------------------------------------------------------------------------
     
    145135//------------------------------------------------------------------------------
    146136
    147 inline POSIXWaiter::POSIXWaiter() :
     137inline Waiter::Waiter() :
    148138    numWaitables(0)
    149139{
     
    152142//------------------------------------------------------------------------------
    153143
    154 } /* namespace xplcommon */
     144} /* namespace xplcommon::posix */ } /* namespace xplcommon */
    155145
    156146//------------------------------------------------------------------------------
    157 #endif // HAVE_POLL_H
    158 
    159 #endif // XPLCOMMON_POSIXWAITABLE_H
     147#endif // XPLCOMMON_POSIX_WAITABLE_H
    160148
    161149// Local Variables:
  • src/xplcommon/win32/Thread.cc

    r1 r3  
    3030//------------------------------------------------------------------------------
    3131
    32 #include "config.h"
    33 #ifdef HAVE_WINDOWS_H
     32#include "Thread.h"
    3433
    3534//------------------------------------------------------------------------------
    3635
    37 #include "Win32Thread.h"
     36using xplcommon::win32::Thread;
    3837
    3938//------------------------------------------------------------------------------
    4039
    41 using xplcommon::Win32Thread;
    42 
    43 //------------------------------------------------------------------------------
    44 
    45 DWORD Win32Thread::threadFn(LPVOID arg)
     40DWORD Thread::threadFn(LPVOID arg)
    4641{
    47     reinterpret_cast<Win32Thread*>(arg)->run();
     42    reinterpret_cast<Thread*>(arg)->run();
    4843    return 0;
    4944}
    5045
    5146//------------------------------------------------------------------------------
    52 #endif // HAVE_PTHREAD_H
    53 
    5447
    5548// Local Variables:
  • src/xplcommon/win32/Thread.h

    r1 r3  
    2828// either expressed or implied, of the FreeBSD Project.
    2929
    30 #ifndef XPLCOMMON_WIN32THREAD_H
    31 #define XPLCOMMON_WIN32THREAD_H
    32 //------------------------------------------------------------------------------
    33 
    34 #include "config.h"
    35 
    36 #ifdef HAVE_WINDOWS_H
     30#ifndef XPLCOMMON_WIN32_THREAD_H
     31#define XPLCOMMON_WIN32_THREAD_H
    3732//------------------------------------------------------------------------------
    3833
     
    4136//------------------------------------------------------------------------------
    4237
    43 namespace xplcommon {
     38namespace xplcommon { namespace win32 {
    4439
    4540//------------------------------------------------------------------------------
     
    4843 * A Win32 API thread implementation.
    4944 */
    50 class Win32Thread
     45class Thread
    5146{
    5247private:
     
    6560     * Virtual destructor.
    6661     */
    67     virtual ~Win32Thread();
     62    virtual ~Thread();
    6863
    6964    /**
     
    8681
    8782//------------------------------------------------------------------------------
    88 
    89 /// Common type for OS-independent code
    90 typedef Win32Thread Thread;
    91 
    92 //------------------------------------------------------------------------------
    9383// Inline definitions
    9484//------------------------------------------------------------------------------
    9585
    96 inline Win32Thread::~Win32Thread()
     86inline Thread::~Thread()
    9787{
    9888}
     
    10090//------------------------------------------------------------------------------
    10191
    102 inline bool Win32Thread::start()
     92inline bool Thread::start()
    10393{
    10494    handle = CreateThread(0, 0, &threadFn, this, 0, 0);
     
    10898//------------------------------------------------------------------------------
    10999
    110 inline void Win32Thread::join()
     100inline void Thread::join()
    111101{
    112102    WaitForSingleObject(handle, INFINITE);
     
    115105//------------------------------------------------------------------------------
    116106
    117 } /* namespace xplcommon */
     107} /* namespace xplcommon::win32 */ } /* namespace xplcommon */
    118108
    119109//------------------------------------------------------------------------------
    120 #endif // HAVE_PTHREAD_H
    121 
    122 #endif // XPLCOMMON_POSIXTHREAD_H
     110#endif // XPLCOMMON_WIN32_THREAD_H
    123111
    124112// Local Variables:
Note: See TracChangeset for help on using the changeset viewer.