Object.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 X-Way Rights BV
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00028 #ifndef _CLASS_BEE_LANG_OBJECT_H
00029 #define _CLASS_BEE_LANG_OBJECT_H
00030 
00031 #include "beecrypt/c++/lang/CloneNotSupportedException.h"
00032 using beecrypt::lang::CloneNotSupportedException;
00033 #include "beecrypt/c++/lang/InterruptedException.h"
00034 using beecrypt::lang::InterruptedException;
00035 #include "beecrypt/c++/lang/IllegalMonitorStateException.h"
00036 using beecrypt::lang::IllegalMonitorStateException;
00037 #include "beecrypt/c++/lang/RuntimeException.h"
00038 using beecrypt::lang::RuntimeException;
00039 
00040 #ifdef __cplusplus
00041 
00042 namespace beecrypt {
00043     namespace util {
00044         namespace concurrent {
00045             namespace locks {
00046                 class BEECRYPTCXXAPI ReentrantLock;
00047             }
00048         }
00049     }
00050     namespace lang {
00053         class BEECRYPTCXXAPI Object
00054         {
00055             friend class Thread;
00056             friend class beecrypt::util::concurrent::locks::ReentrantLock;
00057 
00058             friend BEECRYPTCXXAPI void collection_attach(Object*) throw ();
00059             friend BEECRYPTCXXAPI void collection_detach(Object*) throw ();
00060             friend BEECRYPTCXXAPI void collection_remove(Object*) throw ();
00061             friend BEECRYPTCXXAPI void collection_rcheck(Object*) throw ();
00062 
00063         protected:
00067             class Monitor
00068             {
00069                 friend class Object;
00070 
00071             protected:
00072                 bc_threadid_t         _owner;
00073                 bc_threadid_t         _interruptee;
00074                 bc_mutex_t            _lock;
00075                 volatile unsigned int _lock_count;
00076 
00077                 void internal_state_lock();
00078                 void internal_state_unlock();
00079 
00080                 Monitor();
00081 
00082             public:
00083                 virtual ~Monitor() {}
00084 
00085                 virtual void interrupt(bc_threadid_t) = 0;
00086                 virtual bool interrupted(bc_threadid_t);
00087                 virtual bool isInterrupted(bc_threadid_t);
00088                 virtual bool isLocked();
00089                 virtual void lock() = 0;
00090                 virtual void lockInterruptibly() throw (InterruptedException) = 0;
00091                 virtual bool tryLock() = 0;
00092                 virtual void notify() = 0;
00093                 virtual void notifyAll() = 0;
00094                 virtual void unlock() = 0;
00095                 virtual void wait(jlong timeout) throw (InterruptedException) = 0;
00096 
00097                 static Monitor* getInstance(bool fair = false);
00098             };
00099 
00100         private:
00103             class NonfairMonitor : public Monitor
00104             {
00105             private:
00106                 #if WIN32
00107                 HANDLE        _lock_sig; // semaphore
00108                 bool          _lock_sig_all;
00109                 HANDLE        _lock_sig_all_done; // event
00110                 unsigned int  _lock_wthreads;
00111                 HANDLE        _notify_sig; // semaphore
00112                 bool          _notify_sig_all;
00113                 HANDLE        _notify_sig_all_done; // event
00114                 unsigned int  _notify_wthreads;
00115                 #else
00116                 bc_cond_t     _lock_sig;
00117                 unsigned int  _lock_wthreads;
00118                 bc_cond_t     _notify_sig;
00119                 unsigned int  _notify_wthreads;
00120                 #endif
00121 
00122             public:
00123                 NonfairMonitor();
00124                 virtual ~NonfairMonitor();
00125 
00126                 virtual void interrupt(bc_threadid_t);
00127                 virtual void lock();
00128                 virtual void lockInterruptibly() throw (InterruptedException);
00129                 virtual bool tryLock();
00130                 virtual void unlock();
00131                 virtual void notify();
00132                 virtual void notifyAll();
00133                 virtual void wait(jlong timeout) throw (InterruptedException);
00134             };
00135 
00136             class FairMonitor : public Monitor
00137             {
00138             private:
00139                 struct waiter
00140                 {
00141                     bc_threadid_t owner;
00142                     unsigned int  lock_count;
00143                     bc_cond_t     event;
00144                     waiter*       next;
00145                     waiter*       prev;
00146 
00147                     waiter(bc_threadid_t owner, unsigned int lock_count);
00148                     ~waiter();
00149                 };
00150 
00151                 waiter* _lock_head;
00152                 waiter* _lock_tail;
00153                 waiter* _notify_head;
00154                 waiter* _notify_tail;
00155 
00156             public:
00157                 FairMonitor();
00158                 virtual ~FairMonitor();
00159 
00160                 virtual void interrupt(bc_threadid_t);
00161                 virtual void lock();
00162                 virtual void lockInterruptibly() throw (InterruptedException);
00163                 virtual bool tryLock();
00164                 virtual void unlock();
00165                 virtual void notify();
00166                 virtual void notifyAll();
00167                 virtual void wait(jlong timeout) throw (InterruptedException);
00168             };
00169 
00170         private:
00174             volatile unsigned int _ref_count;
00175 
00176         private:
00185             mutable Monitor* monitor;
00186 
00187         public:
00191             class BEECRYPTCXXAPI Synchronizer
00192             {
00193             private:
00194                 const Object* _ref;
00195                 bool _once;
00196 
00197             public:
00198                 Synchronizer(const Object* obj);
00199                 Synchronizer(const Object& obj);
00200                 ~Synchronizer();
00201 
00202                 bool checkonce();
00203             };
00204 
00208             #define synchronized(obj) for (Object::Synchronizer _s(obj); _s.checkonce(); )
00209 
00210         private:
00211             static bc_mutex_t _init_lock;
00212             static bc_mutex_t init();
00213 
00214         private:
00215             void lock() const;
00216             void unlock() const;
00217 
00218         protected:
00219             virtual Object* clone() const throw (CloneNotSupportedException);
00220 
00221         public:
00222             Object();
00223             virtual ~Object();
00224 
00225             virtual bool equals(const Object* obj) const throw ();
00226             virtual jint hashCode() const throw ();
00227             void notify() const;
00228             void notifyAll() const;
00229             virtual String toString() const throw ();
00230             void wait(jlong millis = 0) const throw (InterruptedException);
00231         };
00232 
00236         BEECRYPTCXXAPI
00237         void collection_attach(Object*) throw ();
00241         BEECRYPTCXXAPI
00242         void collection_detach(Object*) throw ();
00247         BEECRYPTCXXAPI
00248         void collection_remove(Object*) throw ();
00252         BEECRYPTCXXAPI
00253         void collection_rcheck(Object*) throw ();
00254     }
00255 }
00256 
00257 #endif
00258 
00259 #endif

Generated on Fri Jun 19 13:39:40 2009 for BeeCrypt C++ by  doxygen 1.5.8