javacommons/utils/src/monitor.cpp
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2007-2007 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 #include "monitor.h"
       
    21 
       
    22 #ifndef __SYMBIAN32__
       
    23 #include <sys/time.h>
       
    24 #include <errno.h>
       
    25 #endif
       
    26 using namespace java::util;
       
    27 
       
    28 Monitor::Monitor() : mWaiting(false), mNotifyBeforeWait(false)
       
    29 {
       
    30     JELOG2(EUtils);
       
    31 }
       
    32 
       
    33 Monitor::~Monitor()
       
    34 {
       
    35     JELOG2(EUtils);
       
    36     pthread_mutex_destroy(&mMutex);
       
    37     pthread_cond_destroy(&mCondVar);
       
    38 }
       
    39 
       
    40 OS_EXPORT Monitor* Monitor::createMonitor()
       
    41 {
       
    42     JELOG2(EUtils);
       
    43     int result = -1;
       
    44     Monitor* monitor = new Monitor();
       
    45     if (monitor)
       
    46     {
       
    47         result = pthread_mutex_init(&monitor->mMutex, 0);
       
    48         if (result == 0)
       
    49         {
       
    50             result = pthread_cond_init(&monitor->mCondVar, 0);
       
    51             if (result != 0)
       
    52             {
       
    53                 ELOG(EUtils, "Monitor::createMonitor() "
       
    54                      "pthread_cond_init() failed");
       
    55             }
       
    56         }
       
    57         else
       
    58         {
       
    59             ELOG(EUtils, "Monitor::createMonitor() "
       
    60                  "pthread_mutex_init() failed");
       
    61         }
       
    62     }
       
    63     else
       
    64     {
       
    65         ELOG(EUtils, "Monitor::createMonitor() "
       
    66              "new Monitor() failed");
       
    67     }
       
    68     if (result != 0 && monitor)
       
    69     {
       
    70         delete monitor;
       
    71         monitor = 0;
       
    72     }
       
    73     return monitor;
       
    74 }
       
    75 
       
    76 OS_EXPORT int Monitor::wait(int timeOut)
       
    77 {
       
    78     JELOG4(EUtils, EEntryLog & EInfoHeavyLoad);
       
    79     pthread_mutex_lock(&mMutex);
       
    80     if (!mNotifyBeforeWait)
       
    81     {
       
    82         mWaiting = true;
       
    83         if (timeOut == 0)
       
    84         {
       
    85             while (mWaiting)
       
    86             {
       
    87                 pthread_cond_wait(&mCondVar, &mMutex);
       
    88             }
       
    89         }
       
    90         else
       
    91         {
       
    92             struct timeval currentTimeVal;
       
    93             struct timespec timeOutTime;
       
    94             gettimeofday(&currentTimeVal, 0);
       
    95             timeOutTime.tv_sec = currentTimeVal.tv_sec + timeOut / 1000;
       
    96             timeOutTime.tv_nsec =
       
    97                 currentTimeVal.tv_usec * 1000 + (timeOut % 1000) * 1000 * 1000;
       
    98 
       
    99             int err = pthread_cond_timedwait(&mCondVar, &mMutex, &timeOutTime);
       
   100             if (err != ETIMEDOUT)
       
   101             {
       
   102                 ELOG1(EUtils, "Monitor: Timed wait failed, err = %d", err);
       
   103             }
       
   104         }
       
   105     }
       
   106     else
       
   107     {
       
   108         mNotifyBeforeWait = false;
       
   109     }
       
   110     pthread_mutex_unlock(&mMutex);
       
   111     return 0;
       
   112 }
       
   113 
       
   114 OS_EXPORT int Monitor::notify()
       
   115 {
       
   116     JELOG4(EUtils, EEntryLog & EInfoHeavyLoad);
       
   117     pthread_mutex_lock(&mMutex);
       
   118     if (mWaiting)
       
   119     {
       
   120         pthread_cond_signal(&mCondVar);
       
   121     }
       
   122     else
       
   123     {
       
   124         mNotifyBeforeWait = true;
       
   125     }
       
   126     mWaiting = false;
       
   127     pthread_mutex_unlock(&mMutex);
       
   128     return 0;
       
   129 }