Back to index

im-sdk  12.3.91
IMLock.hh
Go to the documentation of this file.
00001 #ifndef IMLOCK_HH_
00002 #define IMLOCK_HH_
00003 #include <config.h>
00004 #include <threaddef.h>
00005 #include "basictype.hh"
00006 
00007 class IMSyncObject
00008 {
00009     bool locked;
00010     THREAD_SYNC_OBJECT so;
00011 
00012     // inhibit duplication.
00013     IMSyncObject(const IMSyncObject& a);
00014     IMSyncObject& operator=(const IMSyncObject& a);
00015   public:
00016     bool locked_p()
00017     { return locked; }
00018     void lock()
00019     {
00020        LOCK_SYNC_OBJECT(so);
00021        locked = true;
00022     }
00023     void unlock()
00024     {
00025        if (locked) {
00026            locked = false;
00027            UNLOCK_SYNC_OBJECT(so);
00028        }
00029     }
00030     IMSyncObject()
00031     {
00032        locked = false;
00033        INIT_SYNC_OBJECT(so);
00034     }
00035     ~IMSyncObject()
00036     {
00037        if (locked) UNLOCK_SYNC_OBJECT(so);
00038        DESTROY_SYNC_OBJECT(so);
00039     }
00040 };
00041 
00042 class IMLock
00043 {
00044     bool locked;
00045     IMSyncObject *pso;
00046 
00047     // inhibit duplication.
00048     IMLock(const IMLock& a);
00049     IMLock& operator=(const IMLock& a);
00050   public:
00051     IMLock(IMSyncObject& so)
00052     {
00053        pso = &so;
00054        pso->lock();
00055        locked = true;
00056     }
00057     IMLock(IMSyncObject& so, bool lockp)
00058     {
00059        if (lockp) {
00060            pso = &so;
00061            pso->lock();
00062            locked = true;
00063        } else {
00064            locked = false;
00065        }
00066     }
00067     ~IMLock()
00068     {
00069        if (locked) pso->unlock();
00070     }
00071 };
00072 
00073 #endif /* not IMLOCK_HH_ */
00074 
00075 /* Local Variables: */
00076 /* c-file-style: "iiim-project" */
00077 /* End: */