|
1 /* |
|
2 ****************************************************************************** |
|
3 * |
|
4 * Copyright (C) 1997-2003, International Business Machines |
|
5 * Corporation and others. All Rights Reserved. |
|
6 * |
|
7 ****************************************************************************** |
|
8 */ |
|
9 //---------------------------------------------------------------------------- |
|
10 // File: mutex.h |
|
11 // |
|
12 // Lightweight C++ wrapper for umtx_ C mutex functions |
|
13 // |
|
14 // Author: Alan Liu 1/31/97 |
|
15 // History: |
|
16 // 06/04/97 helena Updated setImplementation as per feedback from 5/21 drop. |
|
17 // 04/07/1999 srl refocused as a thin wrapper |
|
18 // |
|
19 //---------------------------------------------------------------------------- |
|
20 #ifndef MUTEX_H |
|
21 #define MUTEX_H |
|
22 |
|
23 #include "unicode/utypes.h" |
|
24 #include "unicode/uobject.h" |
|
25 #include "umutex.h" |
|
26 |
|
27 U_NAMESPACE_BEGIN |
|
28 |
|
29 //---------------------------------------------------------------------------- |
|
30 // Code within that accesses shared static or global data should |
|
31 // should instantiate a Mutex object while doing so. You should make your own |
|
32 // private mutex where possible. |
|
33 |
|
34 // For example: |
|
35 // |
|
36 // UMTX myMutex; |
|
37 // |
|
38 // int InitializeMyMutex() |
|
39 // { |
|
40 // umtx_init( &myMutex ); |
|
41 // return 0; |
|
42 // } |
|
43 // |
|
44 // static int initializeMyMutex = InitializeMyMutex(); |
|
45 // |
|
46 // void Function(int arg1, int arg2) |
|
47 // { |
|
48 // static Object* foo; // Shared read-write object |
|
49 // Mutex mutex(&myMutex); // or no args for the global lock |
|
50 // foo->Method(); |
|
51 // // When 'mutex' goes out of scope and gets destroyed here, the lock is released |
|
52 // } |
|
53 // |
|
54 // Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function |
|
55 // returning a Mutex. This is a common mistake which silently slips through the |
|
56 // compiler!! |
|
57 // |
|
58 |
|
59 class U_COMMON_API Mutex : public UMemory { |
|
60 public: |
|
61 inline Mutex(UMTX *mutex = NULL); |
|
62 inline ~Mutex(); |
|
63 |
|
64 private: |
|
65 UMTX *fMutex; |
|
66 |
|
67 Mutex(const Mutex &other); // forbid copying of this class |
|
68 Mutex &operator=(const Mutex &other); // forbid copying of this class |
|
69 }; |
|
70 |
|
71 inline Mutex::Mutex(UMTX *mutex) |
|
72 : fMutex(mutex) |
|
73 { |
|
74 umtx_lock(fMutex); |
|
75 } |
|
76 |
|
77 inline Mutex::~Mutex() |
|
78 { |
|
79 umtx_unlock(fMutex); |
|
80 } |
|
81 |
|
82 U_NAMESPACE_END |
|
83 |
|
84 #endif //_MUTEX_ |
|
85 //eof |