Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef SYNCHRONIZED_H_
00024 #define SYNCHRONIZED_H_
00025
00031 #ifndef __XDISPATCH_INDIRECT__
00032 #error "Please #include <xdispatch/dispatch.h> instead of this file directly."
00033 #endif
00034
00035 #include <string>
00036
00037 __XDISPATCH_BEGIN_NAMESPACE
00038
00043 class XDISPATCH_EXPORT synclock;
00044
00045
00053 class XDISPATCH_EXPORT synclock {
00054
00055 public:
00056 synclock();
00063 XDISPATCH_DEPRECATED( synclock(const std::string&, const bool auto_lock = false ) );
00064 synclock(const synclock&, const bool auto_lock = false );
00065 synclock(const semaphore &, const bool auto_lock = false );
00066 ~synclock();
00067
00068 operator bool() const;
00069 synclock& operator= (const synclock&);
00070
00071 synclock& unlock();
00072 synclock& lock();
00073
00074 private:
00075 semaphore _semaphore;
00076 bool _lock_active;
00077
00078 };
00079
00080
00084 XDISPATCH_EXPORT void init_semaphore_for_synclock(void*);
00085
00086
00087 # define XDISPATCH_CONCAT(A,B) A ## B
00088 # define XDISPATCH_LOCK_VAR(X) XDISPATCH_CONCAT(xd_synclock_, X)
00089 # define XDISPATCH_LOCK_VAR_SEM(X) XDISPATCH_CONCAT( _xd_synclock_sem_, X)
00090 # define XDISPATCH_LOCK_VAR_ONCE(X) XDISPATCH_CONCAT( _xd_synclock_once_, X)
00091 # define XDISPATCH_SYNC_HEADER( lock ) for( \
00092 ::xdispatch::synclock XDISPATCH_LOCK_VAR(__LINE__)( lock, true ) ; \
00093 XDISPATCH_LOCK_VAR(__LINE__) ; \
00094 XDISPATCH_LOCK_VAR(__LINE__).unlock() \
00095 )
00096 # define XDISPATCH_SYNC_DECL( id ) \
00097 static ::xdispatch::semaphore XDISPATCH_LOCK_VAR_SEM( id ); \
00098 static dispatch_once_t XDISPATCH_LOCK_VAR_ONCE( id ) = 0; \
00099 dispatch_once_f( &XDISPATCH_LOCK_VAR_ONCE( id ), &XDISPATCH_LOCK_VAR_SEM( id ), ::xdispatch::init_semaphore_for_synclock ); \
00100 XDISPATCH_SYNC_HEADER( XDISPATCH_LOCK_VAR_SEM( id ) )
00101
00110 # define XDISPATCH_SYNCHRONIZE( lock ) XDISPATCH_SYNC_HEADER( lock )
00111
00120 # define XDISPATCH_SYNCHRONIZED XDISPATCH_SYNC_DECL( __COUNTER__ )
00121
00271 #ifndef XDISPATCH_NO_KEYWORDS
00272 # ifndef synchronize
00273 # define synchronize( lock ) XDISPATCH_SYNCHRONIZE(lock)
00274 # endif
00275
00276 # ifndef synchronized
00277 # define synchronized XDISPATCH_SYNCHRONIZED
00278 # endif
00279 #endif
00280
00281 __XDISPATCH_END_NAMESPACE
00282
00285 #endif