LCOV - code coverage report
Current view: top level - sal/osl/unx - conditn.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 75 96 78.1 %
Date: 2014-04-11 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      478527 : oslCondition SAL_CALL osl_createCondition()
      39             : {
      40             :       oslConditionImpl* pCond;
      41      478527 :     int nRet=0;
      42             : 
      43      478527 :     pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
      44             : 
      45      478527 :     if ( pCond == 0 )
      46             :     {
      47             :         SAL_WARN("sal.osl", "std::bad_alloc in C");
      48           0 :         return 0;
      49             :     }
      50             : 
      51      478527 :     pCond->m_State = false;
      52             : 
      53             :     /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
      54      478527 :     nRet =  pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
      55      478527 :     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      478527 :     nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
      67      478527 :     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      478527 :     return (oslCondition)pCond;
      85             : }
      86             : 
      87      477946 : void SAL_CALL osl_destroyCondition(oslCondition Condition)
      88             : {
      89             :     oslConditionImpl* pCond;
      90      477946 :     int nRet = 0;
      91             : 
      92      477946 :     if ( Condition )
      93             :     {
      94      477946 :         pCond = (oslConditionImpl*)Condition;
      95             : 
      96      477946 :         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      477947 :         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      477947 :         free(Condition);
     108             :     }
     109             : 
     110      477947 :     return;
     111             : }
     112             : 
     113     1450133 : sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
     114             : {
     115             :    oslConditionImpl* pCond;
     116     1450133 :    int nRet=0;
     117             : 
     118             :    assert(Condition);
     119     1450133 :    pCond = (oslConditionImpl*)Condition;
     120             : 
     121     1450133 :    if ( pCond == 0 )
     122             :    {
     123           1 :        return sal_False;
     124             :    }
     125             : 
     126     1450132 :    nRet = pthread_mutex_lock(&pCond->m_Lock);
     127     1450132 :    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     1450132 :    pCond->m_State = true;
     137     1450132 :    nRet = pthread_cond_broadcast(&pCond->m_Condition);
     138     1450131 :    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     1450131 :    nRet = pthread_mutex_unlock(&pCond->m_Lock);
     150     1450131 :    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     1450131 :    return sal_True;
     160             : 
     161             : }
     162             : 
     163     1351011 : sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
     164             : {
     165             :     oslConditionImpl* pCond;
     166     1351011 :     int nRet=0;
     167             : 
     168             :     assert(Condition);
     169             : 
     170     1351011 :     pCond = (oslConditionImpl*)Condition;
     171             : 
     172     1351011 :     if ( pCond == 0 )
     173             :     {
     174           4 :         return sal_False;
     175             :     }
     176             : 
     177     1351007 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     178     1351009 :     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     1351009 :     pCond->m_State = false;
     188             : 
     189     1351009 :     nRet = pthread_mutex_unlock(&pCond->m_Lock);
     190     1351009 :     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     1351009 :     return sal_True;
     199             : }
     200             : 
     201      835546 : oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
     202             : {
     203             :     oslConditionImpl* pCond;
     204      835546 :     int nRet=0;
     205      835546 :     oslConditionResult Result = osl_cond_result_ok;
     206             : 
     207             :     assert(Condition);
     208      835546 :     pCond = (oslConditionImpl*)Condition;
     209             : 
     210      835546 :     if ( pCond == 0 )
     211             :     {
     212           2 :         return osl_cond_result_error;
     213             :     }
     214             : 
     215      835544 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     216      835547 :     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      835547 :     if ( pTimeout )
     225             :     {
     226      227145 :         if ( ! pCond->m_State )
     227             :         {
     228             :             int                 ret;
     229             :             struct timeval      tp;
     230             :             struct timespec     to;
     231             : 
     232      227041 :             gettimeofday(&tp, NULL);
     233             : 
     234      227041 :             SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
     235             :                               tp.tv_usec * 1000 + pTimeout->Nanosec );
     236             : 
     237             :             /* spurious wake up prevention */
     238      179306 :             do
     239             :             {
     240      227040 :                 ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
     241      227034 :                 if ( ret != 0 )
     242             :                 {
     243       47728 :                     if ( ret == ETIME || ret == ETIMEDOUT )
     244             :                     {
     245       47728 :                         Result = osl_cond_result_timeout;
     246       47728 :                         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       95456 :                         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      179306 :             while ( !pCond->m_State );
     267             :         }
     268             :     }
     269             :     else
     270             :     {
     271     1433032 :         while ( !pCond->m_State )
     272             :         {
     273      216230 :             nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
     274      216228 :             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      787811 :     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      787806 :     return Result;
     299             : }
     300             : 
     301      953469 : sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
     302             : {
     303             :     bool State;
     304             :     oslConditionImpl* pCond;
     305      953469 :     int nRet=0;
     306             : 
     307             :     assert(Condition);
     308      953469 :     pCond = (oslConditionImpl*)Condition;
     309             : 
     310      953469 :     if ( pCond == 0 )
     311             :     {
     312           0 :         return sal_False;
     313             :     }
     314             : 
     315      953469 :     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      953469 :     State = pCond->m_State;
     322             : 
     323      953469 :     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      953469 :     return State;
     330             : }
     331             : 
     332             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10