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