XDispatch 0.7.0
 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();
00057     synclock(const synclock&);
00058     ~synclock();
00059 
00060     operator bool() const;
00061 
00062     void unlock();
00063 
00064 private:
00065     struct data;
00066     data* _d;
00067 
00068     synclock& operator= (synclock);
00069 
00070     friend XDISPATCH_EXPORT synclock get_lock_for_key(const std::string&);
00071     friend XDISPATCH_EXPORT synclock get_lock_for_key( dispatch_semaphore_t );
00072     synclock(dispatch_semaphore_t);
00073 };
00074 
00075 
00079 XDISPATCH_EXPORT void init_semaphore_for_synclock(void*);
00086 XDISPATCH_EXPORT XDISPATCH_DEPRECATED( synclock get_lock_for_key(const std::string& key) );
00090 inline synclock get_lock_for_key( dispatch_semaphore_t sem ){ return synclock(sem); }
00094 inline synclock get_lock_for_key( synclock& s ){ return s; }
00095 
00096 
00097 # define XDISPATCH_CONCAT(A,B) A ## B
00098 # define XDISPATCH_LOCK_VAR(X) XDISPATCH_CONCAT(xd_synclock_, X)
00099 # define XDISPATCH_LOCK_VAR_SEM(X) XDISPATCH_CONCAT( __xd_synclock_sem_, X)
00100 # define XDISPATCH_LOCK_VAR_ONCE(X) XDISPATCH_CONCAT( __xd_synclock_once_, X)
00101 # define XDISPATCH_SYNC_HEADER( lock ) for( xdispatch::synclock XDISPATCH_LOCK_VAR(__LINE__)(lock) ; XDISPATCH_LOCK_VAR(__LINE__) ; XDISPATCH_LOCK_VAR(__LINE__).unlock() )
00102 # define XDISPATCH_SYNC_DECL( id ) \
00103     static dispatch_semaphore_t XDISPATCH_LOCK_VAR_SEM( id ); \
00104     static dispatch_once_t XDISPATCH_LOCK_VAR_ONCE( id ); \
00105     dispatch_once_f( &XDISPATCH_LOCK_VAR_ONCE( id ), &XDISPATCH_LOCK_VAR_SEM( id ), xdispatch::init_semaphore_for_synclock ); \
00106     XDISPATCH_SYNC_HEADER( xdispatch::get_lock_for_key( XDISPATCH_LOCK_VAR_SEM( id ) ) )
00107 
00116 #  define XDISPATCH_SYNCHRONIZE( lock ) XDISPATCH_SYNC_HEADER( xdispatch::get_lock_for_key( lock ) )
00117 
00126 #  define XDISPATCH_SYNCHRONIZED XDISPATCH_SYNC_DECL( __COUNTER__ )
00127 
00277 #ifndef XDISPATCH_NO_KEYWORDS
00278 # ifndef synchronize
00279 #  define synchronize( lock ) XDISPATCH_SYNCHRONIZE(lock)
00280 # endif
00281 
00282 # ifndef synchronized
00283 #  define synchronized XDISPATCH_SYNCHRONIZED
00284 # endif
00285 #endif
00286 
00287 __XDISPATCH_END_NAMESPACE
00288 
00291 #endif /* SYNCHRONIZED_H_ */

Generated on Tue Jun 12 2012 14:11:47 for XDispatch by Doxygen 1.8.0
© 2010-2012 MLBA (about | privacy) All Rights reserved.