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