LCOV - code coverage report
Current view: top level - sal/osl/unx - conditn.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 74 96 77.1 %
Date: 2014-11-03 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "sal/config.h"
      21             : 
      22             : #include <assert.h>
      23             : 
      24             : #include "system.h"
      25             : #include <sal/log.hxx>
      26             : #include <sal/types.h>
      27             : 
      28             : #include <osl/conditn.h>
      29             : #include <osl/time.h>
      30             : 
      31             : typedef struct _oslConditionImpl
      32             : {
      33             :     pthread_cond_t  m_Condition;
      34             :     pthread_mutex_t m_Lock;
      35             :     bool            m_State;
      36             : } oslConditionImpl;
      37             : 
      38     1030612 : oslCondition SAL_CALL osl_createCondition()
      39             : {
      40             :       oslConditionImpl* pCond;
      41     1030612 :     int nRet=0;
      42             : 
      43     1030612 :     pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
      44             : 
      45     1030612 :     if ( pCond == 0 )
      46             :     {
      47             :         SAL_WARN("sal.osl", "std::bad_alloc in C");
      48           0 :         return 0;
      49             :     }
      50             : 
      51     1030612 :     pCond->m_State = false;
      52             : 
      53             :     /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
      54     1030612 :     nRet =  pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
      55     1030615 :     if ( nRet != 0 )
      56             :     {
      57             :         SAL_WARN(
      58             :             "sal.osl",
      59             :             "pthread_cond_init failed, errno " << nRet << ", \""
      60             :                 << strerror(nRet) << '"');
      61             : 
      62           0 :         free(pCond);
      63           0 :         return 0;
      64             :     }
      65             : 
      66     1030615 :     nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
      67     1030615 :     if ( nRet != 0 )
      68             :     {
      69             :         SAL_WARN(
      70             :             "sal.osl",
      71             :             "pthread_mutex_init failed, errno " << nRet << ", \""
      72             :                 << strerror(nRet) << '"');
      73             : 
      74           0 :         nRet = pthread_cond_destroy(&pCond->m_Condition);
      75             :         SAL_WARN_IF(
      76             :             nRet != 0, "sal.osl",
      77             :             "pthread_cond_destroy failed, errno " << nRet << ", \""
      78             :                 << strerror(nRet) << '"');
      79             : 
      80           0 :         free(pCond);
      81           0 :         pCond = 0;
      82             :     }
      83             : 
      84     1030615 :     return (oslCondition)pCond;
      85             : }
      86             : 
      87     1030050 : void SAL_CALL osl_destroyCondition(oslCondition Condition)
      88             : {
      89             :     oslConditionImpl* pCond;
      90     1030050 :     int nRet = 0;
      91             : 
      92     1030050 :     if ( Condition )
      93             :     {
      94     1030050 :         pCond = (oslConditionImpl*)Condition;
      95             : 
      96     1030050 :         nRet = pthread_cond_destroy(&pCond->m_Condition);
      97             :         SAL_WARN_IF(
      98             :             nRet != 0, "sal.osl",
      99             :             "pthread_cond_destroy failed, errno " << nRet << ", \""
     100             :                 << strerror(nRet) << '"');
     101     1030054 :         nRet = pthread_mutex_destroy(&pCond->m_Lock);
     102             :         SAL_WARN_IF(
     103             :             nRet != 0, "sal.osl",
     104             :             "pthread_mutex_destroy failed, errno " << nRet << ", \""
     105             :                 << strerror(nRet) << '"');
     106             : 
     107     1030069 :         free(Condition);
     108             :     }
     109             : 
     110     1030069 :     return;
     111             : }
     112             : 
     113     3205653 : sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
     114             : {
     115             :    oslConditionImpl* pCond;
     116     3205653 :    int nRet=0;
     117             : 
     118             :    assert(Condition);
     119     3205653 :    pCond = (oslConditionImpl*)Condition;
     120             : 
     121     3205653 :    if ( pCond == 0 )
     122             :    {
     123           0 :        return sal_False;
     124             :    }
     125             : 
     126     3205654 :    nRet = pthread_mutex_lock(&pCond->m_Lock);
     127     3205657 :    if ( nRet != 0 )
     128             :    {
     129             :        SAL_WARN(
     130             :            "sal.osl",
     131             :            "pthread_mutex_lock failed, errno " << nRet << ", \""
     132             :                << strerror(nRet) << '"');
     133           0 :        return sal_False;
     134             :    }
     135             : 
     136     3205657 :    pCond->m_State = true;
     137     3205657 :    nRet = pthread_cond_broadcast(&pCond->m_Condition);
     138     3205655 :    if ( nRet != 0 )
     139             :    {
     140             :        SAL_WARN(
     141             :            "sal.osl",
     142             :            "pthread_cond_broadcast failed, errno " << nRet << ", \""
     143             :                << strerror(nRet) << '"');
     144             :        // try to unlock the mutex
     145           0 :        pthread_mutex_unlock(&pCond->m_Lock);
     146           0 :        return sal_False;
     147             :    }
     148             : 
     149     3205655 :    nRet = pthread_mutex_unlock(&pCond->m_Lock);
     150     3205658 :    if ( nRet != 0 )
     151             :    {
     152             :        SAL_WARN(
     153             :            "sal.osl",
     154             :            "pthread_mutex_unlock failed, errno " << nRet << ", \""
     155             :                << strerror(nRet) << '"');
     156           0 :        return sal_False;
     157             :    }
     158             : 
     159     3205658 :    return sal_True;
     160             : 
     161             : }
     162             : 
     163     2935108 : sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
     164             : {
     165             :     oslConditionImpl* pCond;
     166     2935108 :     int nRet=0;
     167             : 
     168             :     assert(Condition);
     169             : 
     170     2935108 :     pCond = (oslConditionImpl*)Condition;
     171             : 
     172     2935108 :     if ( pCond == 0 )
     173             :     {
     174           2 :         return sal_False;
     175             :     }
     176             : 
     177     2935106 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     178     2935123 :     if ( nRet != 0 )
     179             :     {
     180             :         SAL_WARN(
     181             :             "sal.osl",
     182             :             "pthread_mutex_lock failed, errno " << nRet << ", \""
     183             :                 << strerror(nRet) << '"');
     184           0 :         return sal_False;
     185             :     }
     186             : 
     187     2935123 :     pCond->m_State = false;
     188             : 
     189     2935123 :     nRet = pthread_mutex_unlock(&pCond->m_Lock);
     190     2935116 :     if ( nRet != 0 )
     191             :     {
     192             :        SAL_WARN(
     193             :            "sal.osl", "pthread_mutex_unlock failed, errno " << nRet <<", \""
     194             :                << strerror(nRet) << '"');
     195           0 :         return sal_False;
     196             :     }
     197             : 
     198     2935116 :     return sal_True;
     199             : }
     200             : 
     201     1625865 : oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
     202             : {
     203             :     oslConditionImpl* pCond;
     204     1625865 :     int nRet=0;
     205     1625865 :     oslConditionResult Result = osl_cond_result_ok;
     206             : 
     207             :     assert(Condition);
     208     1625865 :     pCond = (oslConditionImpl*)Condition;
     209             : 
     210     1625865 :     if ( pCond == 0 )
     211             :     {
     212           3 :         return osl_cond_result_error;
     213             :     }
     214             : 
     215     1625862 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     216     1625892 :     if ( nRet != 0 )
     217             :     {
     218             :         SAL_WARN(
     219             :             "sal.osl", "pthread_mutex_lock failed, errno " << nRet <<", \""
     220             :                 << strerror(nRet) << '"');
     221           0 :         return osl_cond_result_error;
     222             :     }
     223             : 
     224     1625892 :     if ( pTimeout )
     225             :     {
     226      386548 :         if ( ! pCond->m_State )
     227             :         {
     228             :             int                 ret;
     229             :             struct timeval      tp;
     230             :             struct timespec     to;
     231             : 
     232      386450 :             gettimeofday(&tp, NULL);
     233             : 
     234      386450 :             SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
     235             :                               tp.tv_usec * 1000 + pTimeout->Nanosec );
     236             : 
     237             :             /* spurious wake up prevention */
     238      367187 :             do
     239             :             {
     240      386455 :                 ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
     241      386446 :                 if ( ret != 0 )
     242             :                 {
     243       19259 :                     if ( ret == ETIME || ret == ETIMEDOUT )
     244             :                     {
     245       19259 :                         Result = osl_cond_result_timeout;
     246       19259 :                         nRet = pthread_mutex_unlock(&pCond->m_Lock);
     247             :                         SAL_WARN_IF(
     248             :                             nRet != 0, "sal.osl",
     249             :                             "pthread_mutex_unlock failed, errno " << nRet
     250             :                                 << ", \"" << strerror(nRet) << '"');
     251             : 
     252       38518 :                         return Result;
     253             :                     }
     254           0 :                     else if ( ret != EINTR )
     255             :                     {
     256           0 :                         Result = osl_cond_result_error;
     257           0 :                         nRet = pthread_mutex_unlock(&pCond->m_Lock);
     258             :                         SAL_WARN_IF(
     259             :                             nRet != 0, "sal.osl",
     260             :                             "pthread_mutex_unlock failed, errno " << nRet
     261             :                                 << ", \"" << strerror(nRet) << '"');
     262           0 :                         return Result;
     263             :                     }
     264             :                 }
     265             :             }
     266      367187 :             while ( !pCond->m_State );
     267             :         }
     268             :     }
     269             :     else
     270             :     {
     271     2941757 :         while ( !pCond->m_State )
     272             :         {
     273      463072 :             nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
     274      463069 :             if ( nRet != 0 )
     275             :             {
     276             :                 SAL_WARN(
     277             :                     "sal.osl",
     278             :                     "pthread_cond_wait failed, errno " << nRet << ", \""
     279             :                         << strerror(nRet) << '"');
     280           0 :                 Result = osl_cond_result_error;
     281           0 :                 nRet = pthread_mutex_unlock(&pCond->m_Lock);
     282             :                 SAL_WARN_IF(
     283             :                     nRet != 0, "sal.osl",
     284             :                     "pthread_mutex_unlock failed, errno " << nRet << ", \""
     285             :                         << strerror(nRet) << '"');
     286             : 
     287           0 :                 return Result;
     288             :             }
     289             :         }
     290             :     }
     291             : 
     292     1606621 :     nRet = pthread_mutex_unlock(&pCond->m_Lock);
     293             :     SAL_WARN_IF(
     294             :         nRet != 0, "sal.osl",
     295             :         "pthread_mutex_unlock failed, errno " << nRet << ", \""
     296             :             << strerror(nRet) << '"');
     297             : 
     298     1606589 :     return Result;
     299             : }
     300             : 
     301     2383750 : sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
     302             : {
     303             :     bool State;
     304             :     oslConditionImpl* pCond;
     305     2383750 :     int nRet=0;
     306             : 
     307             :     assert(Condition);
     308     2383750 :     pCond = (oslConditionImpl*)Condition;
     309             : 
     310     2383750 :     if ( pCond == 0 )
     311             :     {
     312           0 :         return sal_False;
     313             :     }
     314             : 
     315     2383750 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     316             :     SAL_WARN_IF(
     317             :         nRet != 0, "sal.osl",
     318             :         "pthread_mutex_lock failed, errno " << nRet << ", \"" << strerror(nRet)
     319             :             << '"');
     320             : 
     321     2383750 :     State = pCond->m_State;
     322             : 
     323     2383750 :     nRet = pthread_mutex_unlock(&pCond->m_Lock);
     324             :     SAL_WARN_IF(
     325             :         nRet != 0, "sal.osl",
     326             :         "pthread_mutex_unlock failed, errno " << nRet << ", \""
     327             :             << strerror(nRet) << '"');
     328             : 
     329     2383750 :     return State;
     330             : }
     331             : 
     332             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10