Changeset 3:e4ca2e057cec in xplcommon for src
- Timestamp:
- 12/27/12 13:01:22 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/xplcommon
- Files:
-
- 8 added
- 2 edited
- 8 moved
Legend:
- Unmodified
- Added
- Removed
-
src/xplcommon/Makefile.am
r2 r3 1 SUBDIRS= 2 if TARGET_API_POSIX 3 SUBDIRS+=posix 4 endif 5 if TARGET_API_WIN32 6 SUBDIRS+=win32 7 endif 8 1 9 lib_LTLIBRARIES=libxplcommon.la 2 10 … … 4 12 5 13 if TARGET_API_POSIX 6 libxplcommon_la_SOURCES += \ 7 POSIXThread.cc \ 8 POSIXWaitable.cc \ 9 POSIXWaiter.cc 14 libxplcommon_la_LIBADD=posix/libxplcommon_posix.la 10 15 endif 11 16 12 17 if TARGET_API_WIN32 13 libxplcommon_la_SOURCES += \ 14 Win32Thread.cc 18 libxplcommon_la_LIBADD=win32/libxplcommon_win32.la 15 19 endif 16 20 17 21 include_xplcommondir=$(includedir)/xplcommon 18 22 include_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 34 34 #include "config.h" 35 35 36 #ifdef HAVE_WINDOWS_H 36 //------------------------------------------------------------------------------ 37 37 38 #i nclude "Win32Thread.h"38 #if TARGET_API_WIN32 39 39 40 # else40 #include "win32/Thread.h" 41 41 42 #include "POSIXThread.h" 42 namespace xplcommon { typedef win32::Thread Thread; } 43 43 44 #endif 44 //------------------------------------------------------------------------------ 45 #else // TARGET_API_WIN32 46 //------------------------------------------------------------------------------ 47 48 #include "posix/Thread.h" 49 50 namespace xplcommon { typedef posix::Thread Thread; } 51 52 //------------------------------------------------------------------------------ 53 54 #endif // TARGET_API_WIN32 45 55 46 56 //------------------------------------------------------------------------------ -
src/xplcommon/posix/Thread.cc
r1 r3 30 30 //------------------------------------------------------------------------------ 31 31 32 #include "config.h" 33 #ifdef HAVE_PTHREAD_H 32 #include "Thread.h" 34 33 35 34 //------------------------------------------------------------------------------ 36 35 37 #include "POSIXThread.h" 36 using xplcommon::posix::Thread; 38 37 39 38 //------------------------------------------------------------------------------ 40 39 41 using xplcommon::POSIXThread; 42 43 //------------------------------------------------------------------------------ 44 45 void* POSIXThread::threadFn(void* arg) 40 void* Thread::threadFn(void* arg) 46 41 { 47 reinterpret_cast< POSIXThread*>(arg)->run();42 reinterpret_cast<Thread*>(arg)->run(); 48 43 return 0; 49 44 } 50 45 51 46 //------------------------------------------------------------------------------ 52 #endif // HAVE_PTHREAD_H53 54 47 55 48 // Local Variables: -
src/xplcommon/posix/Thread.h
r1 r3 28 28 // either expressed or implied, of the FreeBSD Project. 29 29 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 38 32 //------------------------------------------------------------------------------ 39 33 … … 42 36 //------------------------------------------------------------------------------ 43 37 44 namespace xplcommon { 38 namespace xplcommon { namespace posix { 45 39 46 40 //------------------------------------------------------------------------------ … … 49 43 * A pthreads-based thread implementation. 50 44 */ 51 class POSIXThread45 class Thread 52 46 { 53 47 private: … … 66 60 * Virtual destructor. 67 61 */ 68 virtual ~ POSIXThread();62 virtual ~Thread(); 69 63 70 64 /** … … 87 81 88 82 //------------------------------------------------------------------------------ 89 90 /// Common type for OS-independent code91 typedef POSIXThread Thread;92 93 //------------------------------------------------------------------------------94 83 // Inline definitions 95 84 //------------------------------------------------------------------------------ 96 85 97 inline POSIXThread::~POSIXThread()86 inline Thread::~Thread() 98 87 { 99 88 } … … 101 90 //------------------------------------------------------------------------------ 102 91 103 inline bool POSIXThread::start()92 inline bool Thread::start() 104 93 { 105 94 return pthread_create(&thread, 0, &threadFn, this)>=0; … … 108 97 //------------------------------------------------------------------------------ 109 98 110 inline void POSIXThread::join()99 inline void Thread::join() 111 100 { 112 101 pthread_join(thread, 0); … … 115 104 //------------------------------------------------------------------------------ 116 105 117 } /* namespace xplcommon */106 } /* namespace xplcommon::posix */ } /* namespace xplcommon */ 118 107 119 108 //------------------------------------------------------------------------------ 120 #endif // HAVE_PTHREAD_H 121 122 #endif // XPLCOMMON_POSIXTHREAD_H 109 #endif // XPLCOMMON_POSIX_THREAD_H 123 110 124 111 // Local Variables: -
src/xplcommon/posix/Waitable.cc
r1 r3 30 30 //------------------------------------------------------------------------------ 31 31 32 #include "config.h" 33 #ifdef HAVE_POLL_H 32 #include "Waitable.h" 33 34 #include "Waiter.h" 34 35 35 36 //------------------------------------------------------------------------------ 36 37 37 #include "POSIXWaitable.h" 38 39 #include "POSIXWaiter.h" 38 using xplcommon::posix::Waitable; 40 39 41 40 //------------------------------------------------------------------------------ 42 41 43 using xplcommon::POSIXWaitable; 44 45 //------------------------------------------------------------------------------ 46 47 POSIXWaitable::POSIXWaitable(POSIXWaiter* waiter, int fd, short events) : 42 Waitable::Waitable(Waiter* waiter, int fd, short events) : 48 43 waiter(waiter), 49 44 fd(fd), … … 55 50 //------------------------------------------------------------------------------ 56 51 57 POSIXWaitable::~POSIXWaitable()52 Waitable::~Waitable() 58 53 { 59 54 if (waiter!=0) waiter->remove(this); … … 61 56 62 57 //------------------------------------------------------------------------------ 63 #endif // HAVE_POLL_H64 58 65 59 // Local Variables: -
src/xplcommon/posix/Waitable.h
r1 r3 28 28 // either expressed or implied, of the FreeBSD Project. 29 29 30 #ifndef XPLCOMMON_POSIX WAITABLE_H31 #define XPLCOMMON_POSIX WAITABLE_H30 #ifndef XPLCOMMON_POSIX_WAITABLE_H 31 #define XPLCOMMON_POSIX_WAITABLE_H 32 32 //------------------------------------------------------------------------------ 33 33 34 #include "config.h" 35 36 #ifdef HAVE_POLL_H 34 namespace xplcommon { namespace posix { 37 35 38 36 //------------------------------------------------------------------------------ 39 37 40 namespace xplcommon { 41 42 //------------------------------------------------------------------------------ 43 44 class POSIXWaiter; 38 class Waiter; 45 39 46 40 //------------------------------------------------------------------------------ 47 41 48 42 /** 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. 50 44 * 51 45 * It is basically a file descriptor. 52 46 */ 53 class POSIXWaitable47 class Waitable 54 48 { 55 49 protected: … … 57 51 * The waiter this waitable belongs to, if any. 58 52 */ 59 POSIXWaiter* waiter;53 Waiter* waiter; 60 54 61 55 /** … … 73 67 * Construct the waitable. 74 68 */ 75 POSIXWaitable(POSIXWaiter* waiter, int fd = -1, short events = 0);69 Waitable(Waiter* waiter, int fd = -1, short events = 0); 76 70 77 71 public: … … 79 73 * Destroy the waitable. It will be removed from the waiter. 80 74 */ 81 virtual ~ POSIXWaitable();75 virtual ~Waitable(); 82 76 83 77 /** … … 96 90 virtual void handleEvents(short events) = 0; 97 91 98 friend class POSIXWaiter;92 friend class Waiter; 99 93 }; 100 94 101 95 //------------------------------------------------------------------------------ 102 96 103 typedef POSIXWaitable Waitable; 97 } /* namespace xplcommon::posix */ } /* namespace xplcommon */ 104 98 105 99 //------------------------------------------------------------------------------ 106 107 } /* namespace xplcommon */108 109 //------------------------------------------------------------------------------110 #endif // HAVE_POLL_H111 112 100 #endif // XPLCOMMON_POSIXWAITABLE_H 113 101 -
src/xplcommon/posix/Waiter.cc
r1 r3 30 30 //------------------------------------------------------------------------------ 31 31 32 #include "config.h" 33 #ifdef HAVE_POLL_H 32 #include "Waiter.h" 34 33 35 //------------------------------------------------------------------------------ 36 37 #include "POSIXWaiter.h" 38 #include "POSIXWaitable.h" 34 #include "Waitable.h" 39 35 40 36 #include <cassert> … … 43 39 //------------------------------------------------------------------------------ 44 40 45 using xplcommon:: POSIXWaiter;41 using xplcommon::posix::Waiter; 46 42 47 43 //------------------------------------------------------------------------------ 48 44 49 inline bool POSIXWaiter::hasReady() const45 inline bool Waiter::hasReady() const 50 46 { 51 47 for(waitables_t::const_iterator i = waitables.begin(); i!=waitables.end(); 52 48 ++i) 53 49 { 54 POSIXWaitable* waitable = i->second;50 Waitable* waitable = i->second; 55 51 if (waitable->ready()) return true; 56 52 } … … 60 56 //------------------------------------------------------------------------------ 61 57 62 inline size_t POSIXWaiter::setupPollFDs(pollfd* pollFDs)58 inline size_t Waiter::setupPollFDs(pollfd* pollFDs) 63 59 { 64 60 size_t numValid = 0; … … 67 63 ++i) 68 64 { 69 POSIXWaitable* waitable = i->second;65 Waitable* waitable = i->second; 70 66 if (waitable->events==0) continue; 71 67 … … 82 78 //------------------------------------------------------------------------------ 83 79 84 inline void POSIXWaiter::processPollFDs(const pollfd* pollFDs, size_t size)80 inline void Waiter::processPollFDs(const pollfd* pollFDs, size_t size) 85 81 { 86 82 for(size_t i = 0; i<size; ++i) { … … 89 85 waitables_t::iterator j = waitables.find(pollFD.fd); 90 86 assert(j!=waitables.end()); 91 POSIXWaitable* waitable = j->second;87 Waitable* waitable = j->second; 92 88 waitable->handleEvents(pollFD.revents); 93 89 } … … 97 93 //------------------------------------------------------------------------------ 98 94 99 POSIXWaiter::~POSIXWaiter()95 Waiter::~Waiter() 100 96 { 101 97 for(waitables_t::iterator i = waitables.begin(); i!=waitables.end(); ++i) 102 98 { 103 POSIXWaitable* waitable = i->second;99 Waitable* waitable = i->second; 104 100 waitable->waiter = 0; 105 101 } … … 108 104 //------------------------------------------------------------------------------ 109 105 110 bool POSIXWaiter::wait(int timeout)106 bool Waiter::wait(int timeout) 111 107 { 112 108 if (hasReady()) return true; … … 118 114 119 115 if (result<0) { 120 perror(" POSIXWaiter::wait: poll");116 perror("xplcommon::posix::Waiter::wait: poll"); 121 117 return false; 122 118 } else if (result>0) { … … 129 125 //------------------------------------------------------------------------------ 130 126 131 void POSIXWaiter::add(POSIXWaitable* waitable)127 void Waiter::add(Waitable* waitable) 132 128 { 133 129 assert(waitable->fd>=0); … … 139 135 //------------------------------------------------------------------------------ 140 136 141 void POSIXWaiter::remove(POSIXWaitable* waitable)137 void Waiter::remove(Waitable* waitable) 142 138 { 143 139 assert(numWaitables>0); … … 150 146 151 147 //------------------------------------------------------------------------------ 152 #endif // HAVE_POLL_H153 148 154 149 // Local Variables: -
src/xplcommon/posix/Waiter.h
r1 r3 28 28 // either expressed or implied, of the FreeBSD Project. 29 29 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 38 32 //------------------------------------------------------------------------------ 39 33 … … 46 40 //------------------------------------------------------------------------------ 47 41 48 namespace xplcommon { 42 namespace xplcommon { namespace posix { 49 43 50 44 //------------------------------------------------------------------------------ 51 45 52 class POSIXWaitable;46 class Waitable; 53 47 54 48 //------------------------------------------------------------------------------ … … 58 52 * for them. 59 53 */ 60 class POSIXWaiter54 class Waiter 61 55 { 62 56 private: … … 64 58 * Type for the set of waitables. 65 59 */ 66 typedef std::map<int, POSIXWaitable*> waitables_t;60 typedef std::map<int, Waitable*> waitables_t; 67 61 68 62 /** … … 80 74 * Construct the waiter. 81 75 */ 82 POSIXWaiter();76 Waiter(); 83 77 84 78 /** … … 86 80 * modified to point to no waiter. 87 81 */ 88 ~ POSIXWaiter();82 ~Waiter(); 89 83 90 84 /** … … 103 97 * Add a waitable. 104 98 */ 105 void add( POSIXWaitable* waitable);99 void add(Waitable* waitable); 106 100 107 101 /** 108 102 * Remove a waitable. 109 103 */ 110 void remove( POSIXWaitable* waitable);104 void remove(Waitable* waitable); 111 105 112 106 private: … … 134 128 void processPollFDs(const pollfd* pollFDs, size_t size); 135 129 136 friend class POSIXWaitable;130 friend class Waitable; 137 131 }; 138 139 //------------------------------------------------------------------------------140 141 typedef POSIXWaiter Waiter;142 132 143 133 //------------------------------------------------------------------------------ … … 145 135 //------------------------------------------------------------------------------ 146 136 147 inline POSIXWaiter::POSIXWaiter() :137 inline Waiter::Waiter() : 148 138 numWaitables(0) 149 139 { … … 152 142 //------------------------------------------------------------------------------ 153 143 154 } /* namespace xplcommon */144 } /* namespace xplcommon::posix */ } /* namespace xplcommon */ 155 145 156 146 //------------------------------------------------------------------------------ 157 #endif // HAVE_POLL_H 158 159 #endif // XPLCOMMON_POSIXWAITABLE_H 147 #endif // XPLCOMMON_POSIX_WAITABLE_H 160 148 161 149 // Local Variables: -
src/xplcommon/win32/Thread.cc
r1 r3 30 30 //------------------------------------------------------------------------------ 31 31 32 #include "config.h" 33 #ifdef HAVE_WINDOWS_H 32 #include "Thread.h" 34 33 35 34 //------------------------------------------------------------------------------ 36 35 37 #include "Win32Thread.h" 36 using xplcommon::win32::Thread; 38 37 39 38 //------------------------------------------------------------------------------ 40 39 41 using xplcommon::Win32Thread; 42 43 //------------------------------------------------------------------------------ 44 45 DWORD Win32Thread::threadFn(LPVOID arg) 40 DWORD Thread::threadFn(LPVOID arg) 46 41 { 47 reinterpret_cast< Win32Thread*>(arg)->run();42 reinterpret_cast<Thread*>(arg)->run(); 48 43 return 0; 49 44 } 50 45 51 46 //------------------------------------------------------------------------------ 52 #endif // HAVE_PTHREAD_H53 54 47 55 48 // Local Variables: -
src/xplcommon/win32/Thread.h
r1 r3 28 28 // either expressed or implied, of the FreeBSD Project. 29 29 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 37 32 //------------------------------------------------------------------------------ 38 33 … … 41 36 //------------------------------------------------------------------------------ 42 37 43 namespace xplcommon { 38 namespace xplcommon { namespace win32 { 44 39 45 40 //------------------------------------------------------------------------------ … … 48 43 * A Win32 API thread implementation. 49 44 */ 50 class Win32Thread45 class Thread 51 46 { 52 47 private: … … 65 60 * Virtual destructor. 66 61 */ 67 virtual ~ Win32Thread();62 virtual ~Thread(); 68 63 69 64 /** … … 86 81 87 82 //------------------------------------------------------------------------------ 88 89 /// Common type for OS-independent code90 typedef Win32Thread Thread;91 92 //------------------------------------------------------------------------------93 83 // Inline definitions 94 84 //------------------------------------------------------------------------------ 95 85 96 inline Win32Thread::~Win32Thread()86 inline Thread::~Thread() 97 87 { 98 88 } … … 100 90 //------------------------------------------------------------------------------ 101 91 102 inline bool Win32Thread::start()92 inline bool Thread::start() 103 93 { 104 94 handle = CreateThread(0, 0, &threadFn, this, 0, 0); … … 108 98 //------------------------------------------------------------------------------ 109 99 110 inline void Win32Thread::join()100 inline void Thread::join() 111 101 { 112 102 WaitForSingleObject(handle, INFINITE); … … 115 105 //------------------------------------------------------------------------------ 116 106 117 } /* namespace xplcommon */107 } /* namespace xplcommon::win32 */ } /* namespace xplcommon */ 118 108 119 109 //------------------------------------------------------------------------------ 120 #endif // HAVE_PTHREAD_H 121 122 #endif // XPLCOMMON_POSIXTHREAD_H 110 #endif // XPLCOMMON_WIN32_THREAD_H 123 111 124 112 // Local Variables:
Note:
See TracChangeset
for help on using the changeset viewer.