intl/icu/source/common/mutex.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/mutex.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/*
     1.5 +******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1997-2013, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +******************************************************************************
    1.11 +*/
    1.12 +//----------------------------------------------------------------------------
    1.13 +// File:     mutex.h
    1.14 +//
    1.15 +// Lightweight C++ wrapper for umtx_ C mutex functions
    1.16 +//
    1.17 +// Author:   Alan Liu  1/31/97
    1.18 +// History:
    1.19 +// 06/04/97   helena         Updated setImplementation as per feedback from 5/21 drop.
    1.20 +// 04/07/1999  srl               refocused as a thin wrapper
    1.21 +//
    1.22 +//----------------------------------------------------------------------------
    1.23 +#ifndef MUTEX_H
    1.24 +#define MUTEX_H
    1.25 +
    1.26 +#include "unicode/utypes.h"
    1.27 +#include "unicode/uobject.h"
    1.28 +#include "umutex.h"
    1.29 +
    1.30 +U_NAMESPACE_BEGIN
    1.31 +
    1.32 +//----------------------------------------------------------------------------
    1.33 +// Code within that accesses shared static or global data should
    1.34 +// should instantiate a Mutex object while doing so. You should make your own 
    1.35 +// private mutex where possible.
    1.36 +
    1.37 +// For example:
    1.38 +// 
    1.39 +// UMutex myMutex;
    1.40 +// 
    1.41 +// void Function(int arg1, int arg2)
    1.42 +// {
    1.43 +//    static Object* foo;     // Shared read-write object
    1.44 +//    Mutex mutex(&myMutex);  // or no args for the global lock
    1.45 +//    foo->Method();
    1.46 +//    // When 'mutex' goes out of scope and gets destroyed here, the lock is released
    1.47 +// }
    1.48 +//
    1.49 +// Note:  Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function
    1.50 +//        returning a Mutex. This is a common mistake which silently slips through the
    1.51 +//        compiler!!
    1.52 +//
    1.53 +
    1.54 +class U_COMMON_API Mutex : public UMemory {
    1.55 +public:
    1.56 +  inline Mutex(UMutex *mutex = NULL);
    1.57 +  inline ~Mutex();
    1.58 +
    1.59 +private:
    1.60 +  UMutex   *fMutex;
    1.61 +
    1.62 +  Mutex(const Mutex &other); // forbid copying of this class
    1.63 +  Mutex &operator=(const Mutex &other); // forbid copying of this class
    1.64 +};
    1.65 +
    1.66 +inline Mutex::Mutex(UMutex *mutex)
    1.67 +  : fMutex(mutex)
    1.68 +{
    1.69 +  umtx_lock(fMutex);
    1.70 +}
    1.71 +
    1.72 +inline Mutex::~Mutex()
    1.73 +{
    1.74 +  umtx_unlock(fMutex);
    1.75 +}
    1.76 +
    1.77 +U_NAMESPACE_END
    1.78 +
    1.79 +#endif //_MUTEX_
    1.80 +//eof

mercurial