]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/mutex.h
ICU-57131.0.1.tar.gz
[apple/icu.git] / icuSources / common / mutex.h
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
3*
57a6839d 4* Copyright (C) 1997-2013, International Business Machines
b75a7d8f
A
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
27U_NAMESPACE_BEGIN
28
29//----------------------------------------------------------------------------
374ca955 30// Code within that accesses shared static or global data should
b75a7d8f
A
31// should instantiate a Mutex object while doing so. You should make your own
32// private mutex where possible.
33
34// For example:
35//
51004dcb 36// UMutex myMutex;
b75a7d8f 37//
b75a7d8f
A
38// void Function(int arg1, int arg2)
39// {
374ca955 40// static Object* foo; // Shared read-write object
b75a7d8f
A
41// Mutex mutex(&myMutex); // or no args for the global lock
42// foo->Method();
43// // When 'mutex' goes out of scope and gets destroyed here, the lock is released
44// }
45//
46// Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function
47// returning a Mutex. This is a common mistake which silently slips through the
48// compiler!!
49//
50
51class U_COMMON_API Mutex : public UMemory {
52public:
51004dcb 53 inline Mutex(UMutex *mutex = NULL);
b75a7d8f
A
54 inline ~Mutex();
55
56private:
51004dcb 57 UMutex *fMutex;
b75a7d8f
A
58
59 Mutex(const Mutex &other); // forbid copying of this class
60 Mutex &operator=(const Mutex &other); // forbid copying of this class
61};
62
51004dcb 63inline Mutex::Mutex(UMutex *mutex)
b75a7d8f
A
64 : fMutex(mutex)
65{
66 umtx_lock(fMutex);
67}
68
69inline Mutex::~Mutex()
70{
71 umtx_unlock(fMutex);
72}
73
74U_NAMESPACE_END
75
76#endif //_MUTEX_
77//eof