XDispatch 0.7.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
synchronized.h
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2011-2012 MLBA-Team. All rights reserved.
00003 *
00004 * @MLBA_OPEN_LICENSE_HEADER_START@
00005 *
00006 * Licensed under the Apache License, Version 2.0 (the "License");
00007 * you may not use this file except in compliance with the License.
00008 * You may obtain a copy of the License at
00009 *
00010 *     http://www.apache.org/licenses/LICENSE-2.0
00011 *
00012 * Unless required by applicable law or agreed to in writing, software
00013 * distributed under the License is distributed on an "AS IS" BASIS,
00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 * See the License for the specific language governing permissions and
00016 * limitations under the License.
00017 *
00018 * @MLBA_OPEN_LICENSE_HEADER_END@
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 /* SYNCHRONIZED_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Sun Oct 28 2012 22:12:47 for XDispatch by Doxygen 1.7.6.1
© 2010-2012 MLBA (about | privacy) All Rights reserved.