Changeset 1479:d92a97f2db7c


Ignore:
Timestamp:
08/29/10 12:15:06 (18 months ago)
Author:
niam
Branch:
default
Message:

{issue #14[resolved]} event-notification interface

Location:
sources
Files:
2 edited
4 copied

Legend:

Unmodified
Added
Removed
  • sources/include/libdodo/pcNotificationThread.h

    r1439 r1479  
    11/*************************************************************************** 
    2  *            pcSyncThread.h 
     2 *            pcNotificationThread.h 
    33 * 
    4  *  Tue Nov 29 2005 
    5  *  Copyright  2005  Dmytro Milinevskyy 
     4 *  Sun Aug 29 2010 
     5 *  Copyright  2010  Dmytro Milinevskyy 
    66 *  milinevskyy@gmail.com 
    77 ****************************************************************************/ 
     
    2828 */ 
    2929 
    30 #ifndef _PCSYNCTHREAD_H_ 
    31 #define _PCSYNCTHREAD_H_ 1 
     30#ifndef _PCNOTIFICATIONTHREAD_H_ 
     31#define _PCNOTIFICATIONTHREAD_H_ 1 
    3232 
    3333#include <libdodo/directives.h> 
    34  
    35 #include <libdodo/pcSyncProtector.h> 
    3634 
    3735namespace dodo { 
    3836    namespace pc { 
    3937        namespace sync { 
     38            class thread; 
     39        }; 
     40 
     41        namespace notification { 
    4042            /** 
    4143             * @class thread 
    4244             * @brief provides lock mechanism for threads 
    4345             */ 
    44             class thread : public sync::protector { 
     46            class thread { 
    4547              private: 
    4648 
     
    5658                 * constructor 
    5759                 */ 
    58                 thread(); 
     60                thread(sync::thread &lock); 
    5961 
    6062                /** 
     
    6466 
    6567                /** 
    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 
    6871                 * @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 
    6975                 */ 
    70                 virtual void acquire(unsigned long timeout = 0); 
     76                virtual bool wait(unsigned long timeout = 0); 
    7177 
    7278                /** 
    73                  * unlock 
     79                 * notify waiters 
     80                 * @param all defines whether all waiters should be notified 
    7481                 */ 
    75                 virtual void release(); 
     82                virtual void notify(bool all = false); 
    7683 
    7784              protected: 
    7885 
    79                 struct __lock__; 
    80                 __lock__ *lock;         ///< lock 
     86                sync::thread &lock;         ///< lock 
     87 
     88                struct __wake__; 
     89                __wake__ *wake; ///< thread wake handler 
    8190            }; 
    8291        }; 
  • sources/include/libdodo/pcNotificationThreadEx.h

    r1439 r1479  
    11/*************************************************************************** 
    2  *            pcSyncThreadEx.h 
     2 *            pcNotificationThreadEx.h 
    33 * 
    4  *  Wed Oct 5 2005 
    5  *  Copyright  2005  Dmytro Milinevskyy 
     4 *  Sun Aug 29 2010 
     5 *  Copyright  2010  Dmytro Milinevskyy 
    66 *  milinevskyy@gmail.com 
    77 ****************************************************************************/ 
     
    2828 */ 
    2929 
    30 #ifndef _PCSYNCTHREADEX_H_ 
    31 #define _PCSYNCTHREADEX_H_ 1 
     30#ifndef _PCNOTIFICATIONTHREADEX_H_ 
     31#define _PCNOTIFICATIONTHREADEX_H_ 1 
    3232 
    3333#include <libdodo/directives.h> 
     
    3737namespace dodo { 
    3838    namespace pc { 
    39         namespace sync { 
     39        namespace notification { 
    4040            /** 
    4141             * libdodo defined errors 
     
    4848             * explanations for libdodo defined errors 
    4949             */ 
    50 #define PCSYNCTHREADEX_CANNOTLOCK_STR      "Item is currently locked, timeout exhousted" 
     50#define PCNOTIFICATIONTHREADEX_CANNOTLOCK_STR      "Item is currently locked, timeout exhousted" 
    5151 
    5252            /** 
     
    5454             */ 
    5555            enum threadFunctionsID { 
    56                 THREADEX_ACQUIRE, 
    57                 THREADEX_RELEASE, 
     56                THREADEX_WAIT, 
     57                THREADEX_NOTIFY, 
    5858                THREADEX_CONSTRUCTOR, 
    5959            }; 
  • sources/include/libdodo/pcSyncThread.h

    r1439 r1479  
    3737namespace dodo { 
    3838    namespace pc { 
     39        namespace notification { 
     40            class thread; 
     41        }; 
     42 
    3943        namespace sync { 
    4044            /** 
     
    4347             */ 
    4448            class thread : public sync::protector { 
     49                friend class notification::thread; 
     50 
    4551              private: 
    4652 
  • sources/src/pcNotificationThread.cc

    r1477 r1479  
    11/*************************************************************************** 
    2  *            pcSyncThread.cc 
     2 *            pcNotificationThread.cc 
    33 * 
    4  *  Wed Nov 30 2005 
    5  *  Copyright  2005  Dmytro Milinevskyy 
     4 *  Sun Aug 29 2010 
     5 *  Copyright  2010  Dmytro Milinevskyy 
    66 *  milinevskyy@gmail.com 
    77 ****************************************************************************/ 
     
    4040 
    4141#include <libdodo/pcSyncThread.h> 
    42 #include <libdodo/pcSyncThreadEx.h> 
    43 #include <libdodo/pcSyncProtector.h> 
     42#include <libdodo/pcNotificationThreadEx.h> 
     43#include <libdodo/pcNotificationThread.h> 
    4444#include <libdodo/types.h> 
     45 
     46#include "pcSyncThread.inline" 
    4547 
    4648namespace dodo { 
    4749    namespace pc { 
    48         namespace sync { 
     50        namespace notification { 
    4951            /** 
    50              * @struct thread::__lock__ 
    51              * @brief defines thread mutex 
     52             * @struct thread::__wake__ 
     53             * @brief defines conditional locking 
    5254             */ 
    53             struct thread::__lock__ { 
     55            struct thread::__wake__ { 
    5456#ifdef PTHREAD_EXT 
    55                 pthread_mutex_t keeper;  ///< lock 
     57                pthread_cond_t cond;  ///< lock 
    5658#endif 
    5759            }; 
     
    6062}; 
    6163 
    62 using namespace dodo::pc::sync; 
     64using namespace dodo::pc::notification; 
    6365 
    64 thread::thread(thread &) 
     66thread::thread(thread &) : lock(* new sync::thread) 
    6567{ 
    6668} 
     
    6870//------------------------------------------------------------------- 
    6971 
    70 thread::thread() : lock(new thread::__lock__) 
     72thread::thread(sync::thread &lock) : lock(lock), 
     73                                     wake(new __wake__) 
    7174{ 
    7275#ifdef PTHREAD_EXT 
    73     pthread_mutexattr_t attr; 
     76    errno = pthread_cond_init(&wake->cond, NULL); 
     77    if (errno != 0) { 
     78        delete wake; 
    7479 
    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__); 
    10181    } 
    10282#endif 
     
    10888{ 
    10989#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); 
    11491#endif 
    11592 
    116     delete lock; 
     93    delete wake; 
    11794} 
    11895 
    11996//------------------------------------------------------------------- 
    12097 
    121 void 
    122 thread::acquire(unsigned long microseconds) 
     98bool 
     99thread::wait(unsigned long microseconds) 
    123100{ 
    124101#ifdef PTHREAD_EXT 
    125102    if (microseconds == 0) { 
    126         errno = pthread_mutex_lock(&lock->keeper); 
     103        errno = pthread_cond_wait(&wake->cond, &lock.lock->keeper); 
    127104        if (errno != 0) 
    128             dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, 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__); 
    129106    } else { 
    130107        timespec ts = {microseconds/1000000, (microseconds%1000000)*1000}; 
     
    139116        } 
    140117 
    141         errno = pthread_mutex_timedlock(&lock->keeper, &ts); 
     118        errno = pthread_cond_timedwait(&wake->cond, &lock.lock->keeper, &ts); 
    142119        if (errno != 0) { 
    143120            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; 
    145122            else 
    146                 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, 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__); 
    147124        } 
    148125    } 
     126#endif 
     127 
     128    return true; 
     129} 
     130 
     131//------------------------------------------------------------------- 
     132 
     133void 
     134thread::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__); 
    149143#endif 
    150144} 
     
    152146//------------------------------------------------------------------- 
    153147 
    154 void 
    155 thread::release() 
    156 { 
    157 #ifdef PTHREAD_EXT 
    158     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 #endif 
    162 } 
    163  
    164 //------------------------------------------------------------------- 
    165  
  • sources/src/pcSyncThread.cc

    r1477 r1479  
    4444#include <libdodo/types.h> 
    4545 
    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" 
    6147 
    6248using namespace dodo::pc::sync; 
  • sources/src/pcSyncThread.inline

    r1477 r1479  
    11/*************************************************************************** 
    2  *            pcSyncThread.cc 
     2 *            pcSyncThread.inline 
    33 * 
    44 *  Wed Nov 30 2005 
     
    3434#endif 
    3535 
    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  
    4636namespace dodo { 
    4737    namespace pc { 
     
    6050}; 
    6151 
    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_EXT 
    73     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 #endif 
    103 } 
    104  
    105 //------------------------------------------------------------------- 
    106  
    107 thread::~thread() 
    108 { 
    109 #ifdef PTHREAD_EXT 
    110     if (pthread_mutex_trylock(&lock->keeper) == 0) 
    111         pthread_mutex_unlock(&lock->keeper); 
    112  
    113     pthread_mutex_destroy(&lock->keeper); 
    114 #endif 
    115  
    116     delete lock; 
    117 } 
    118  
    119 //------------------------------------------------------------------- 
    120  
    121 void 
    122 thread::acquire(unsigned long microseconds) 
    123 { 
    124 #ifdef PTHREAD_EXT 
    125     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             else 
    146                 dodo_throw exception::basic(exception::MODULE_PCSYNCTHREAD, THREADEX_ACQUIRE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    147         } 
    148     } 
    149 #endif 
    150 } 
    151  
    152 //------------------------------------------------------------------- 
    153  
    154 void 
    155 thread::release() 
    156 { 
    157 #ifdef PTHREAD_EXT 
    158     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 #endif 
    162 } 
    163  
    164 //------------------------------------------------------------------- 
    165  
Note: See TracChangeset for help on using the changeset viewer.