michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 1997-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: */ michael@0: //---------------------------------------------------------------------------- michael@0: // File: mutex.h michael@0: // michael@0: // Lightweight C++ wrapper for umtx_ C mutex functions michael@0: // michael@0: // Author: Alan Liu 1/31/97 michael@0: // History: michael@0: // 06/04/97 helena Updated setImplementation as per feedback from 5/21 drop. michael@0: // 04/07/1999 srl refocused as a thin wrapper michael@0: // michael@0: //---------------------------------------------------------------------------- michael@0: #ifndef MUTEX_H michael@0: #define MUTEX_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/uobject.h" michael@0: #include "umutex.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // Code within that accesses shared static or global data should michael@0: // should instantiate a Mutex object while doing so. You should make your own michael@0: // private mutex where possible. michael@0: michael@0: // For example: michael@0: // michael@0: // UMutex myMutex; michael@0: // michael@0: // void Function(int arg1, int arg2) michael@0: // { michael@0: // static Object* foo; // Shared read-write object michael@0: // Mutex mutex(&myMutex); // or no args for the global lock michael@0: // foo->Method(); michael@0: // // When 'mutex' goes out of scope and gets destroyed here, the lock is released michael@0: // } michael@0: // michael@0: // Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function michael@0: // returning a Mutex. This is a common mistake which silently slips through the michael@0: // compiler!! michael@0: // michael@0: michael@0: class U_COMMON_API Mutex : public UMemory { michael@0: public: michael@0: inline Mutex(UMutex *mutex = NULL); michael@0: inline ~Mutex(); michael@0: michael@0: private: michael@0: UMutex *fMutex; michael@0: michael@0: Mutex(const Mutex &other); // forbid copying of this class michael@0: Mutex &operator=(const Mutex &other); // forbid copying of this class michael@0: }; michael@0: michael@0: inline Mutex::Mutex(UMutex *mutex) michael@0: : fMutex(mutex) michael@0: { michael@0: umtx_lock(fMutex); michael@0: } michael@0: michael@0: inline Mutex::~Mutex() michael@0: { michael@0: umtx_unlock(fMutex); michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif //_MUTEX_ michael@0: //eof