1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ****************************************************************************** 5 * 6 * Copyright (C) 1997-2013, International Business Machines 7 * Corporation and others. All Rights Reserved. 8 * 9 ****************************************************************************** 10 */ 11 //---------------------------------------------------------------------------- 12 // File: mutex.h 13 // 14 // Lightweight C++ wrapper for umtx_ C mutex functions 15 // 16 // Author: Alan Liu 1/31/97 17 // History: 18 // 06/04/97 helena Updated setImplementation as per feedback from 5/21 drop. 19 // 04/07/1999 srl refocused as a thin wrapper 20 // 21 //---------------------------------------------------------------------------- 22 #ifndef MUTEX_H 23 #define MUTEX_H 24 25 #include "unicode/utypes.h" 26 #include "unicode/uobject.h" 27 #include "umutex.h" 28 29 U_NAMESPACE_BEGIN 30 31 /** 32 * Mutex is a helper class for convenient locking and unlocking of a UMutex. 33 * 34 * Creating a local scope Mutex will lock a UMutex, holding the lock until the Mutex 35 * goes out of scope. 36 * 37 * If no UMutex is specified, the ICU global mutex is implied. 38 * 39 * For example: 40 * 41 * static UMutex myMutex; 42 * 43 * void Function(int arg1, int arg2) 44 * { 45 * static Object* foo; // Shared read-write object 46 * Mutex mutex(&myMutex); // or no args for the global lock 47 * foo->Method(); 48 * // When 'mutex' goes out of scope and gets destroyed here, the lock is released 49 * } 50 * 51 * Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function 52 * returning a Mutex. This is a common mistake which silently slips through the 53 * compiler!! 54 */ 55 56 class U_COMMON_API Mutex : public UMemory { 57 public: fMutex(mutex)58 Mutex(UMutex *mutex = nullptr) : fMutex(mutex) { 59 umtx_lock(fMutex); 60 } ~Mutex()61 ~Mutex() { 62 umtx_unlock(fMutex); 63 } 64 65 Mutex(const Mutex &other) = delete; // forbid assigning of this class 66 Mutex &operator=(const Mutex &other) = delete; // forbid copying of this class 67 void *operator new(size_t s) = delete; // forbid heap allocation. Locals only. 68 69 private: 70 UMutex *fMutex; 71 }; 72 73 74 U_NAMESPACE_END 75 76 #endif //_MUTEX_ 77 //eof 78