00001 /* 00002 ***************************************************************************************** 00003 * 00004 * Copyright (C) 1997-1999, International Business Machines 00005 * Corporation and others. All Rights Reserved. 00006 * 00007 ***************************************************************************************** 00008 */ 00009 //------------------------------------------------------------------------------ 00010 // File: mutex.h 00011 // 00012 // Lightweight C++ wrapper for umtx_ C mutex functions 00013 // 00014 // Author: Alan Liu 1/31/97 00015 // History: 00016 // 06/04/97 helena Updated setImplementation as per feedback from 5/21 drop. 00017 // 04/07/1999 srl refocused as a thin wrapper 00018 // 00019 //------------------------------------------------------------------------------ 00020 #ifndef MUTEX_H 00021 #define MUTEX_H 00022 #include "unicode/utypes.h" 00023 00024 #include "umutex.h" 00025 00026 //------------------------------------------------------------------------------ 00027 // Code within this library which accesses protected data 00028 // should instantiate a Mutex object while doing so. You should make your own 00029 // private mutex where possible. 00030 00031 // For example: 00032 // 00033 // UMTX myMutex; 00034 // 00035 // int InitializeMyMutex() 00036 // { 00037 // umtx_init( &myMutex ); 00038 // return 0; 00039 // } 00040 // 00041 // static int initializeMyMutex = InitializeMyMutex(); 00042 // 00043 // void Function(int arg1, int arg2) 00044 // { 00045 // static Object* foo; // Shared read-write object 00046 // Mutex mutex(&myMutex); // or no args for the global lock 00047 // foo->Method(); 00048 // // When 'mutex' goes out of scope and gets destroyed here, the lock is released 00049 // } 00050 // 00051 // Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function 00052 // returning a Mutex. This is a common mistake which silently slips through the 00053 // compiler!! 00054 // 00055 00056 class U_COMMON_API Mutex 00057 { 00058 public: 00059 inline Mutex(UMTX *mutex = NULL); 00060 inline ~Mutex(); 00061 00062 private: 00063 UMTX *fMutex; 00064 }; 00065 00066 inline Mutex::Mutex(UMTX *mutex) 00067 : fMutex(mutex) 00068 { 00069 umtx_lock(fMutex); 00070 } 00071 00072 inline Mutex::~Mutex() 00073 { 00074 umtx_unlock(fMutex); 00075 } 00076 00077 #endif //_MUTEX_ 00078 //eof 00079 00080 00081 00082 00083 00084 00085 00086