Changeset 1479:d92a97f2db7c
- Timestamp:
- 08/29/10 12:15:06 (18 months ago)
- Branch:
- default
- Location:
- sources
- Files:
-
- 2 edited
- 4 copied
-
include/libdodo/pcNotificationThread.h (copied) (copied from sources/include/libdodo/pcSyncThread.h) (4 diffs)
-
include/libdodo/pcNotificationThreadEx.h (copied) (copied from sources/include/libdodo/pcSyncThreadEx.h) (5 diffs)
-
include/libdodo/pcSyncThread.h (modified) (2 diffs)
-
src/pcNotificationThread.cc (copied) (copied from sources/src/pcSyncThread.cc) (7 diffs)
-
src/pcSyncThread.cc (modified) (1 diff)
-
src/pcSyncThread.inline (copied) (copied from sources/src/pcSyncThread.cc) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sources/include/libdodo/pcNotificationThread.h
r1439 r1479 1 1 /*************************************************************************** 2 * pc SyncThread.h2 * pcNotificationThread.h 3 3 * 4 * Tue Nov 29 20055 * Copyright 20 05Dmytro Milinevskyy4 * Sun Aug 29 2010 5 * Copyright 2010 Dmytro Milinevskyy 6 6 * milinevskyy@gmail.com 7 7 ****************************************************************************/ … … 28 28 */ 29 29 30 #ifndef _PC SYNCTHREAD_H_31 #define _PC SYNCTHREAD_H_ 130 #ifndef _PCNOTIFICATIONTHREAD_H_ 31 #define _PCNOTIFICATIONTHREAD_H_ 1 32 32 33 33 #include <libdodo/directives.h> 34 35 #include <libdodo/pcSyncProtector.h>36 34 37 35 namespace dodo { 38 36 namespace pc { 39 37 namespace sync { 38 class thread; 39 }; 40 41 namespace notification { 40 42 /** 41 43 * @class thread 42 44 * @brief provides lock mechanism for threads 43 45 */ 44 class thread : public sync::protector{46 class thread { 45 47 private: 46 48 … … 56 58 * constructor 57 59 */ 58 thread( );60 thread(sync::thread &lock); 59 61 60 62 /** … … 64 66 65 67 /** 66 * lock 67 * @param timeout defines wait timeout for unlock in microseconds 68 * wait for notification 69 * @return true if nofitication was received 70 * @param timeout defines wait timeout for waiting for notification in microseconds 68 71 * @note if timeout is 0 it will wait infinitely 72 * the lock should be acquired before this call 73 * the lock is still acquired after the call 74 * if call may block the lock is released for the blocking time 69 75 */ 70 virtual void acquire(unsigned long timeout = 0);76 virtual bool wait(unsigned long timeout = 0); 71 77 72 78 /** 73 * unlock 79 * notify waiters 80 * @param all defines whether all waiters should be notified 74 81 */ 75 virtual void release();82 virtual void notify(bool all = false); 76 83 77 84 protected: 78 85 79 struct __lock__; 80 __lock__ *lock; ///< lock 86 sync::thread &lock; ///< lock 87 88 struct __wake__; 89 __wake__ *wake; ///< thread wake handler 81 90 }; 82 91 }; -
sources/include/libdodo/pcNotificationThreadEx.h
r1439 r1479 1 1 /*************************************************************************** 2 * pc SyncThreadEx.h2 * pcNotificationThreadEx.h 3 3 * 4 * Wed Oct 5 20055 * Copyright 20 05Dmytro Milinevskyy4 * Sun Aug 29 2010 5 * Copyright 2010 Dmytro Milinevskyy 6 6 * milinevskyy@gmail.com 7 7 ****************************************************************************/ … … 28 28 */ 29 29 30 #ifndef _PC SYNCTHREADEX_H_31 #define _PC SYNCTHREADEX_H_ 130 #ifndef _PCNOTIFICATIONTHREADEX_H_ 31 #define _PCNOTIFICATIONTHREADEX_H_ 1 32 32 33 33 #include <libdodo/directives.h> … … 37 37 namespace dodo { 38 38 namespace pc { 39 namespace sync{39 namespace notification { 40 40 /** 41 41 * libdodo defined errors … … 48 48 * explanations for libdodo defined errors 49 49 */ 50 #define PC SYNCTHREADEX_CANNOTLOCK_STR "Item is currently locked, timeout exhousted"50 #define PCNOTIFICATIONTHREADEX_CANNOTLOCK_STR "Item is currently locked, timeout exhousted" 51 51 52 52 /** … … 54 54 */ 55 55 enum threadFunctionsID { 56 THREADEX_ ACQUIRE,57 THREADEX_ RELEASE,56 THREADEX_WAIT, 57 THREADEX_NOTIFY, 58 58 THREADEX_CONSTRUCTOR, 59 59 }; -
sources/include/libdodo/pcSyncThread.h
r1439 r1479 37 37 namespace dodo { 38 38 namespace pc { 39 namespace notification { 40 class thread; 41 }; 42 39 43 namespace sync { 40 44 /** … … 43 47 */ 44 48 class thread : public sync::protector { 49 friend class notification::thread; 50 45 51 private: 46 52 -
sources/src/pcNotificationThread.cc
r1477 r1479 1 1 /*************************************************************************** 2 * pc SyncThread.cc2 * pcNotificationThread.cc 3 3 * 4 * Wed Nov 30 20055 * Copyright 20 05Dmytro Milinevskyy4 * Sun Aug 29 2010 5 * Copyright 2010 Dmytro Milinevskyy 6 6 * milinevskyy@gmail.com 7 7 ****************************************************************************/ … … 40 40 41 41 #include <libdodo/pcSyncThread.h> 42 #include <libdodo/pc SyncThreadEx.h>43 #include <libdodo/pc SyncProtector.h>42 #include <libdodo/pcNotificationThreadEx.h> 43 #include <libdodo/pcNotificationThread.h> 44 44 #include <libdodo/types.h> 45 46 #include "pcSyncThread.inline" 45 47 46 48 namespace dodo { 47 49 namespace pc { 48 namespace sync{50 namespace notification { 49 51 /** 50 * @struct thread::__ lock__51 * @brief defines thread mutex52 * @struct thread::__wake__ 53 * @brief defines conditional locking 52 54 */ 53 struct thread::__ lock__ {55 struct thread::__wake__ { 54 56 #ifdef PTHREAD_EXT 55 pthread_ mutex_t keeper; ///< lock57 pthread_cond_t cond; ///< lock 56 58 #endif 57 59 }; … … 60 62 }; 61 63 62 using namespace dodo::pc:: sync;64 using namespace dodo::pc::notification; 63 65 64 thread::thread(thread &) 66 thread::thread(thread &) : lock(* new sync::thread) 65 67 { 66 68 } … … 68 70 //------------------------------------------------------------------- 69 71 70 thread::thread() : lock(new thread::__lock__) 72 thread::thread(sync::thread &lock) : lock(lock), 73 wake(new __wake__) 71 74 { 72 75 #ifdef PTHREAD_EXT 73 pthread_mutexattr_t attr; 76 errno = pthread_cond_init(&wake->cond, NULL); 77 if (errno != 0) { 78 delete wake; 74 79 75 errno = pthread_mutexattr_init(&attr); 76 if (errno != 0) { 77 delete lock; 78 79 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 80 } 81 82 errno = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 83 if (errno != 0) { 84 delete lock; 85 86 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 87 } 88 89 errno = pthread_mutex_init(&lock->keeper, &attr); 90 if (errno != 0) { 91 delete lock; 92 93 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 94 } 95 96 errno = pthread_mutexattr_destroy(&attr); 97 if (errno != 0) { 98 delete lock; 99 100 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 80 dodo_throw exception::basic(exception::MODULE_PCNOTIFICATIONTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 101 81 } 102 82 #endif … … 108 88 { 109 89 #ifdef PTHREAD_EXT 110 if (pthread_mutex_trylock(&lock->keeper) == 0) 111 pthread_mutex_unlock(&lock->keeper); 112 113 pthread_mutex_destroy(&lock->keeper); 90 pthread_cond_destroy(&wake->cond); 114 91 #endif 115 92 116 delete lock;93 delete wake; 117 94 } 118 95 119 96 //------------------------------------------------------------------- 120 97 121 void 122 thread:: acquire(unsigned long microseconds)98 bool 99 thread::wait(unsigned long microseconds) 123 100 { 124 101 #ifdef PTHREAD_EXT 125 102 if (microseconds == 0) { 126 errno = pthread_ mutex_lock(&lock->keeper);103 errno = pthread_cond_wait(&wake->cond, &lock.lock->keeper); 127 104 if (errno != 0) 128 dodo_throw exception::basic(exception::MODULE_PC SYNCTHREAD, THREADEX_ACQUIRE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);105 dodo_throw exception::basic(exception::MODULE_PCNOTIFICATIONTHREAD, THREADEX_WAIT, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 129 106 } else { 130 107 timespec ts = {microseconds/1000000, (microseconds%1000000)*1000}; … … 139 116 } 140 117 141 errno = pthread_ mutex_timedlock(&lock->keeper, &ts);118 errno = pthread_cond_timedwait(&wake->cond, &lock.lock->keeper, &ts); 142 119 if (errno != 0) { 143 120 if (errno == ETIMEDOUT) 144 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_ACQUIRE, exception::ERRNO_ERRNO, THREADEX_CANNOTLOCK, PCSYNCTHREADEX_CANNOTLOCK_STR, __LINE__, __FILE__);121 return false; 145 122 else 146 dodo_throw exception::basic(exception::MODULE_PC SYNCTHREAD, THREADEX_ACQUIRE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);123 dodo_throw exception::basic(exception::MODULE_PCNOTIFICATIONTHREAD, THREADEX_WAIT, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 147 124 } 148 125 } 126 #endif 127 128 return true; 129 } 130 131 //------------------------------------------------------------------- 132 133 void 134 thread::notify(bool all) 135 { 136 #ifdef PTHREAD_EXT 137 if (all) 138 errno = pthread_cond_broadcast(&wake->cond); 139 else 140 errno = pthread_cond_signal(&wake->cond); 141 if (errno != 0) 142 dodo_throw exception::basic(exception::MODULE_PCNOTIFICATIONTHREAD, THREADEX_NOTIFY, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 149 143 #endif 150 144 } … … 152 146 //------------------------------------------------------------------- 153 147 154 void155 thread::release()156 {157 #ifdef PTHREAD_EXT158 errno = pthread_mutex_unlock(&lock->keeper);159 if (errno != 0)160 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_RELEASE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);161 #endif162 }163 164 //-------------------------------------------------------------------165 -
sources/src/pcSyncThread.cc
r1477 r1479 44 44 #include <libdodo/types.h> 45 45 46 namespace dodo { 47 namespace pc { 48 namespace sync { 49 /** 50 * @struct thread::__lock__ 51 * @brief defines thread mutex 52 */ 53 struct thread::__lock__ { 54 #ifdef PTHREAD_EXT 55 pthread_mutex_t keeper; ///< lock 56 #endif 57 }; 58 }; 59 }; 60 }; 46 #include "pcSyncThread.inline" 61 47 62 48 using namespace dodo::pc::sync; -
sources/src/pcSyncThread.inline
r1477 r1479 1 1 /*************************************************************************** 2 * pcSyncThread. cc2 * pcSyncThread.inline 3 3 * 4 4 * Wed Nov 30 2005 … … 34 34 #endif 35 35 36 #include <time.h>37 #include <errno.h>38 #include <string.h>39 #include <unistd.h>40 41 #include <libdodo/pcSyncThread.h>42 #include <libdodo/pcSyncThreadEx.h>43 #include <libdodo/pcSyncProtector.h>44 #include <libdodo/types.h>45 46 36 namespace dodo { 47 37 namespace pc { … … 60 50 }; 61 51 62 using namespace dodo::pc::sync;63 64 thread::thread(thread &)65 {66 }67 68 //-------------------------------------------------------------------69 70 thread::thread() : lock(new thread::__lock__)71 {72 #ifdef PTHREAD_EXT73 pthread_mutexattr_t attr;74 75 errno = pthread_mutexattr_init(&attr);76 if (errno != 0) {77 delete lock;78 79 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);80 }81 82 errno = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);83 if (errno != 0) {84 delete lock;85 86 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);87 }88 89 errno = pthread_mutex_init(&lock->keeper, &attr);90 if (errno != 0) {91 delete lock;92 93 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);94 }95 96 errno = pthread_mutexattr_destroy(&attr);97 if (errno != 0) {98 delete lock;99 100 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);101 }102 #endif103 }104 105 //-------------------------------------------------------------------106 107 thread::~thread()108 {109 #ifdef PTHREAD_EXT110 if (pthread_mutex_trylock(&lock->keeper) == 0)111 pthread_mutex_unlock(&lock->keeper);112 113 pthread_mutex_destroy(&lock->keeper);114 #endif115 116 delete lock;117 }118 119 //-------------------------------------------------------------------120 121 void122 thread::acquire(unsigned long microseconds)123 {124 #ifdef PTHREAD_EXT125 if (microseconds == 0) {126 errno = pthread_mutex_lock(&lock->keeper);127 if (errno != 0)128 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_ACQUIRE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);129 } else {130 timespec ts = {microseconds/1000000, (microseconds%1000000)*1000};131 timespec now;132 133 clock_gettime(CLOCK_REALTIME, &now);134 ts.tv_sec += now.tv_sec;135 ts.tv_nsec += now.tv_nsec;136 if (ts.tv_nsec > 999999999) {137 ts.tv_sec += 1;138 ts.tv_nsec -= 999999999;139 }140 141 errno = pthread_mutex_timedlock(&lock->keeper, &ts);142 if (errno != 0) {143 if (errno == ETIMEDOUT)144 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_ACQUIRE, exception::ERRNO_ERRNO, THREADEX_CANNOTLOCK, PCSYNCTHREADEX_CANNOTLOCK_STR, __LINE__, __FILE__);145 else146 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_ACQUIRE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);147 }148 }149 #endif150 }151 152 //-------------------------------------------------------------------153 154 void155 thread::release()156 {157 #ifdef PTHREAD_EXT158 errno = pthread_mutex_unlock(&lock->keeper);159 if (errno != 0)160 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_RELEASE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);161 #endif162 }163 164 //-------------------------------------------------------------------165
Note: See TracChangeset
for help on using the changeset viewer.
