javacommons/utils/inc/scopedlocks.h
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  ScopedLocks
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef SCOPEDLOCKS_H
       
    19 #define SCOPEDLOCKS_H
       
    20 
       
    21 #include <pthread.h>
       
    22 
       
    23 #include "logger.h"
       
    24 
       
    25 namespace java
       
    26 {
       
    27 namespace util
       
    28 {
       
    29 
       
    30 class ScopedMutex
       
    31 {
       
    32 public:
       
    33     ScopedMutex()
       
    34     {
       
    35         pthread_mutexattr_t mutex_attr;
       
    36         pthread_mutexattr_init(&mutex_attr);
       
    37         pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
       
    38         pthread_mutex_init(&mMutex, &mutex_attr);
       
    39         pthread_mutexattr_destroy(&mutex_attr);
       
    40     }
       
    41     ~ScopedMutex()
       
    42     {
       
    43         pthread_mutex_destroy(&mMutex);
       
    44     }
       
    45     int lock()
       
    46     {
       
    47         return pthread_mutex_lock(&mMutex);
       
    48     }
       
    49     int unlock()
       
    50     {
       
    51         return pthread_mutex_unlock(&mMutex);
       
    52     }
       
    53 
       
    54 private:
       
    55     pthread_mutex_t mMutex;
       
    56 };
       
    57 
       
    58 class ScopedLock
       
    59 {
       
    60 public:
       
    61     ScopedLock(ScopedMutex& aMutex):mMutex(aMutex)
       
    62     {
       
    63         int status = mMutex.lock();
       
    64         if (0 != status)
       
    65         {
       
    66             ELOG1(EUtils, "ScopedLock().mutex.lock() failed with %d", status);
       
    67         }
       
    68     }
       
    69     ~ScopedLock()
       
    70     {
       
    71         int status = mMutex.unlock();
       
    72         if (0 != status)
       
    73         {
       
    74             ELOG1(EUtils, "ScopedLock().mutex.unlock() failed with %d", status);
       
    75         }
       
    76     }
       
    77 private:
       
    78     ScopedMutex& mMutex;
       
    79 };
       
    80 
       
    81 } //end namespace util
       
    82 } //end namespace java
       
    83 
       
    84 #endif // SCOPEDLOCKS_H
       
    85