LCOV - code coverage report
Current view: top level - libreoffice/sal/osl/unx - conditn.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 72 95 75.8 %
Date: 2012-12-17 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             : 
      32             : typedef struct _oslConditionImpl
      33             : {
      34             :     pthread_cond_t  m_Condition;
      35             :     pthread_mutex_t m_Lock;
      36             :     sal_Bool            m_State;
      37             : } oslConditionImpl;
      38             : 
      39             : 
      40             : /*****************************************************************************/
      41             : /* osl_createCondition */
      42             : /*****************************************************************************/
      43       31323 : oslCondition SAL_CALL osl_createCondition()
      44             : {
      45             :       oslConditionImpl* pCond;
      46       31323 :     int nRet=0;
      47             : 
      48       31323 :     pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
      49             : 
      50       31323 :     if ( pCond == 0 )
      51             :     {
      52             :         SAL_WARN("sal.osl", "std::bad_alloc in C");
      53           0 :         return 0;
      54             :     }
      55             : 
      56       31323 :     pCond->m_State = sal_False;
      57             : 
      58             :     /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
      59       31323 :     nRet =  pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
      60       31323 :     if ( nRet != 0 )
      61             :     {
      62             :         SAL_WARN(
      63             :             "sal.osl",
      64             :             "pthread_cond_init failed, errno " << nRet << ", \""
      65             :                 << strerror(nRet) << '"');
      66             : 
      67           0 :         free(pCond);
      68           0 :         return 0;
      69             :     }
      70             : 
      71       31323 :     nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
      72       31323 :     if ( nRet != 0 )
      73             :     {
      74             :         SAL_WARN(
      75             :             "sal.osl",
      76             :             "pthread_mutex_init failed, errno " << nRet << ", \""
      77             :                 << strerror(nRet) << '"');
      78             : 
      79           0 :         nRet = pthread_cond_destroy(&pCond->m_Condition);
      80             :         SAL_WARN_IF(
      81             :             nRet != 0, "sal.osl",
      82             :             "pthread_cond_destroy failed, errno " << nRet << ", \""
      83             :                 << strerror(nRet) << '"');
      84             : 
      85           0 :         free(pCond);
      86           0 :         pCond = 0;
      87             :     }
      88             : 
      89       31323 :     return (oslCondition)pCond;
      90             : }
      91             : 
      92             : /*****************************************************************************/
      93             : /* osl_destroyCondition */
      94             : /*****************************************************************************/
      95       29037 : void SAL_CALL osl_destroyCondition(oslCondition Condition)
      96             : {
      97             :     oslConditionImpl* pCond;
      98       29037 :     int nRet = 0;
      99             : 
     100       29037 :     if ( Condition )
     101             :     {
     102       29037 :         pCond = (oslConditionImpl*)Condition;
     103             : 
     104       29037 :         nRet = pthread_cond_destroy(&pCond->m_Condition);
     105             :         SAL_WARN_IF(
     106             :             nRet != 0, "sal.osl",
     107             :             "pthread_cond_destroy failed, errno " << nRet << ", \""
     108             :                 << strerror(nRet) << '"');
     109       29037 :         nRet = pthread_mutex_destroy(&pCond->m_Lock);
     110             :         SAL_WARN_IF(
     111             :             nRet != 0, "sal.osl",
     112             :             "pthread_mutex_destroy failed, errno " << nRet << ", \""
     113             :                 << strerror(nRet) << '"');
     114             : 
     115       29037 :         free(Condition);
     116             :     }
     117             : 
     118       29037 :     return;
     119             : }
     120             : 
     121             : /*****************************************************************************/
     122             : /* osl_setCondition */
     123             : /*****************************************************************************/
     124      177622 : sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
     125             : {
     126             :    oslConditionImpl* pCond;
     127      177622 :    int nRet=0;
     128             : 
     129             :    assert(Condition);
     130      177622 :    pCond = (oslConditionImpl*)Condition;
     131             : 
     132      177622 :    if ( pCond == 0 )
     133             :    {
     134           0 :        return sal_False;
     135             :    }
     136             : 
     137      177622 :    nRet = pthread_mutex_lock(&pCond->m_Lock);
     138      177622 :    if ( nRet != 0 )
     139             :    {
     140             :        SAL_WARN(
     141             :            "sal.osl",
     142             :            "pthread_mutex_lock failed, errno " << nRet << ", \""
     143             :                << strerror(nRet) << '"');
     144           0 :        return sal_False;
     145             :    }
     146             : 
     147      177622 :    pCond->m_State = sal_True;
     148      177622 :    nRet = pthread_cond_broadcast(&pCond->m_Condition);
     149      177622 :    if ( nRet != 0 )
     150             :    {
     151             :        SAL_WARN(
     152             :            "sal.osl",
     153             :            "pthread_cond_broadcast failed, errno " << nRet << ", \""
     154             :                << strerror(nRet) << '"');
     155           0 :        return sal_False;
     156             :    }
     157             : 
     158      177622 :    nRet = pthread_mutex_unlock(&pCond->m_Lock);
     159      177622 :    if ( nRet != 0 )
     160             :    {
     161             :        SAL_WARN(
     162             :            "sal.osl",
     163             :            "pthread_mutex_unlock failed, errno " << nRet << ", \""
     164             :                << strerror(nRet) << '"');
     165           0 :        return sal_False;
     166             :    }
     167             : 
     168      177622 :    return sal_True;
     169             : 
     170             : }
     171             : 
     172             : /*****************************************************************************/
     173             : /* osl_resetCondition */
     174             : /*****************************************************************************/
     175      148006 : sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
     176             : {
     177             :     oslConditionImpl* pCond;
     178      148006 :     int nRet=0;
     179             : 
     180             :     assert(Condition);
     181             : 
     182      148006 :     pCond = (oslConditionImpl*)Condition;
     183             : 
     184      148006 :     if ( pCond == 0 )
     185             :     {
     186           0 :         return sal_False;
     187             :     }
     188             : 
     189      148006 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     190      148006 :     if ( nRet != 0 )
     191             :     {
     192             :         SAL_WARN(
     193             :             "sal.osl",
     194             :             "pthread_mutex_lock failed, errno " << nRet << ", \""
     195             :                 << strerror(nRet) << '"');
     196           0 :         return sal_False;
     197             :     }
     198             : 
     199      148006 :     pCond->m_State = sal_False;
     200             : 
     201      148006 :     nRet = pthread_mutex_unlock(&pCond->m_Lock);
     202      148006 :     if ( nRet != 0 )
     203             :     {
     204             :        SAL_WARN(
     205             :            "sal.osl", "pthread_mutex_unlock failed, errno " << nRet <<", \""
     206             :                << strerror(nRet) << '"');
     207           0 :         return sal_False;
     208             :     }
     209             : 
     210      148006 :     return sal_True;
     211             : }
     212             : 
     213             : /*****************************************************************************/
     214             : /* osl_waitCondition */
     215             : /*****************************************************************************/
     216       72315 : oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
     217             : {
     218             :     oslConditionImpl* pCond;
     219       72315 :     int nRet=0;
     220       72315 :     oslConditionResult Result = osl_cond_result_ok;
     221             : 
     222             :     assert(Condition);
     223       72315 :     pCond = (oslConditionImpl*)Condition;
     224             : 
     225       72315 :     if ( pCond == 0 )
     226             :     {
     227           0 :         return osl_cond_result_error;
     228             :     }
     229             : 
     230       72315 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     231       72315 :     if ( nRet != 0 )
     232             :     {
     233             :         SAL_WARN(
     234             :             "sal.osl", "pthread_mutex_lock failed, errno " << nRet <<", \""
     235             :                 << strerror(nRet) << '"');
     236           0 :         return osl_cond_result_error;
     237             :     }
     238             : 
     239       72315 :     if ( pTimeout )
     240             :     {
     241       10894 :         if ( ! pCond->m_State )
     242             :         {
     243             :             int                 ret;
     244             :             struct timeval      tp;
     245             :             struct timespec     to;
     246             : 
     247       10827 :             gettimeofday(&tp, NULL);
     248             : 
     249       10827 :             SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
     250             :                               tp.tv_usec * 1000 + pTimeout->Nanosec );
     251             : 
     252             :             /* spurious wake up prevention */
     253        2278 :             do
     254             :             {
     255       10827 :                 ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
     256       10827 :                 if ( ret != 0 )
     257             :                 {
     258        8549 :                     if ( ret == ETIME || ret == ETIMEDOUT )
     259             :                     {
     260        8549 :                         Result = osl_cond_result_timeout;
     261        8549 :                         nRet = pthread_mutex_unlock(&pCond->m_Lock);
     262             :                         SAL_WARN_IF(
     263             :                             nRet != 0, "sal.osl",
     264             :                             "pthread_mutex_unlock failed, errno " << nRet
     265             :                                 << ", \"" << strerror(nRet) << '"');
     266             : 
     267        8549 :                         return Result;
     268             :                     }
     269           0 :                     else if ( ret != EINTR )
     270             :                     {
     271           0 :                         Result = osl_cond_result_error;
     272           0 :                         nRet = pthread_mutex_unlock(&pCond->m_Lock);
     273             :                         SAL_WARN_IF(
     274             :                             nRet != 0, "sal.osl",
     275             :                             "pthread_mutex_unlock failed, errno " << nRet
     276             :                                 << ", \"" << strerror(nRet) << '"');
     277           0 :                         return Result;
     278             :                     }
     279             :                 }
     280             :             }
     281        2278 :             while ( !pCond->m_State );
     282             :         }
     283             :     }
     284             :     else
     285             :     {
     286      125531 :         while ( !pCond->m_State )
     287             :         {
     288        2689 :             nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
     289        2689 :             if ( nRet != 0 )
     290             :             {
     291             :                 SAL_WARN(
     292             :                     "sal.osl",
     293             :                     "pthread_cond_wait failed, errno " << nRet << ", \""
     294             :                         << strerror(nRet) << '"');
     295           0 :                 Result = osl_cond_result_error;
     296           0 :                 nRet = pthread_mutex_unlock(&pCond->m_Lock);
     297             :                 SAL_WARN_IF(
     298             :                     nRet != 0, "sal.osl",
     299             :                     "pthread_mutex_unlock failed, errno " << nRet << ", \""
     300             :                         << strerror(nRet) << '"');
     301             : 
     302           0 :                 return Result;
     303             :             }
     304             :         }
     305             :     }
     306             : 
     307       63766 :     nRet = pthread_mutex_unlock(&pCond->m_Lock);
     308             :     SAL_WARN_IF(
     309             :         nRet != 0, "sal.osl",
     310             :         "pthread_mutex_unlock failed, errno " << nRet << ", \""
     311             :             << strerror(nRet) << '"');
     312             : 
     313       63766 :     return Result;
     314             : }
     315             : 
     316             : /*****************************************************************************/
     317             : /* osl_checkCondition */
     318             : /*****************************************************************************/
     319      256275 : sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
     320             : {
     321             :     sal_Bool State;
     322             :     oslConditionImpl* pCond;
     323      256275 :     int nRet=0;
     324             : 
     325             :     assert(Condition);
     326      256275 :     pCond = (oslConditionImpl*)Condition;
     327             : 
     328      256275 :     if ( pCond == 0 )
     329             :     {
     330           0 :         return sal_False;
     331             :     }
     332             : 
     333      256275 :     nRet = pthread_mutex_lock(&pCond->m_Lock);
     334             :     SAL_WARN_IF(
     335             :         nRet != 0, "sal.osl",
     336             :         "pthread_mutex_lock failed, errno " << nRet << ", \"" << strerror(nRet)
     337             :             << '"');
     338             : 
     339      256275 :     State = pCond->m_State;
     340             : 
     341      256275 :     nRet = pthread_mutex_unlock(&pCond->m_Lock);
     342             :     SAL_WARN_IF(
     343             :         nRet != 0, "sal.osl",
     344             :         "pthread_mutex_unlock failed, errno " << nRet << ", \""
     345             :             << strerror(nRet) << '"');
     346             : 
     347      256275 :     return State;
     348             : }
     349             : 
     350             : 
     351             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10