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