LCOV - code coverage report
Current view: top level - pulsecore - aupdate.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 34 42 81.0 %
Date: 2012-07-17 Functions: 6 7 85.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 9 20 45.0 %

           Branch data     Line data    Source code
       1                 :            : /***
       2                 :            :   This file is part of PulseAudio.
       3                 :            : 
       4                 :            :   Copyright 2009 Lennart Poettering
       5                 :            : 
       6                 :            :   PulseAudio is free software; you can redistribute it and/or modify
       7                 :            :   it under the terms of the GNU Lesser General Public License as
       8                 :            :   published by the Free Software Foundation; either version 2.1 of the
       9                 :            :   License, or (at your option) any later version.
      10                 :            : 
      11                 :            :   PulseAudio is distributed in the hope that it will be useful, but
      12                 :            :   WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      14                 :            :   General Public License for more details.
      15                 :            : 
      16                 :            :   You should have received a copy of the GNU Lesser General Public
      17                 :            :   License along with PulseAudio; if not, write to the Free Software
      18                 :            :   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
      19                 :            :   USA.
      20                 :            : ***/
      21                 :            : 
      22                 :            : #ifdef HAVE_CONFIG_H
      23                 :            : #include <config.h>
      24                 :            : #endif
      25                 :            : 
      26                 :            : #include <pulse/xmalloc.h>
      27                 :            : 
      28                 :            : #include <pulsecore/semaphore.h>
      29                 :            : #include <pulsecore/macro.h>
      30                 :            : #include <pulsecore/mutex.h>
      31                 :            : 
      32                 :            : #include "aupdate.h"
      33                 :            : 
      34                 :            : #define MSB (1U << (sizeof(unsigned)*8U-1))
      35                 :            : #define WHICH(n) (!!((n) & MSB))
      36                 :            : #define COUNTER(n) ((n) & ~MSB)
      37                 :            : 
      38                 :            : struct pa_aupdate {
      39                 :            :     pa_atomic_t read_lock;
      40                 :            :     pa_mutex *write_lock;
      41                 :            :     pa_semaphore *semaphore;
      42                 :            :     pa_bool_t swapped;
      43                 :            : };
      44                 :            : 
      45                 :          2 : pa_aupdate *pa_aupdate_new(void) {
      46                 :            :     pa_aupdate *a;
      47                 :            : 
      48                 :          2 :     a = pa_xnew(pa_aupdate, 1);
      49                 :          2 :     pa_atomic_store(&a->read_lock, 0);
      50                 :          2 :     a->write_lock = pa_mutex_new(FALSE, FALSE);
      51                 :          2 :     a->semaphore = pa_semaphore_new(0);
      52                 :            : 
      53                 :          2 :     return a;
      54                 :            : }
      55                 :            : 
      56                 :          0 : void pa_aupdate_free(pa_aupdate *a) {
      57         [ #  # ]:          0 :     pa_assert(a);
      58                 :            : 
      59                 :          0 :     pa_mutex_free(a->write_lock);
      60                 :          0 :     pa_semaphore_free(a->semaphore);
      61                 :            : 
      62                 :          0 :     pa_xfree(a);
      63                 :          0 : }
      64                 :            : 
      65                 :          1 : unsigned pa_aupdate_read_begin(pa_aupdate *a) {
      66                 :            :     unsigned n;
      67                 :            : 
      68         [ -  + ]:          1 :     pa_assert(a);
      69                 :            : 
      70                 :            :     /* Increase the lock counter */
      71                 :          2 :     n = (unsigned) pa_atomic_inc(&a->read_lock);
      72                 :            : 
      73                 :            :     /* When n is 0 we have about 2^31 threads running that all try to
      74                 :            :      * access the data at the same time, oh my! */
      75                 :            :     pa_assert(COUNTER(n)+1 > 0);
      76                 :            : 
      77                 :            :     /* The uppermost bit tells us which data to look at */
      78                 :          1 :     return WHICH(n);
      79                 :            : }
      80                 :            : 
      81                 :          1 : void pa_aupdate_read_end(pa_aupdate *a) {
      82                 :            :     unsigned n;
      83                 :            : 
      84         [ -  + ]:          1 :     pa_assert(a);
      85                 :            : 
      86                 :            :     /* Decrease the lock counter */
      87                 :          2 :     n = (unsigned) pa_atomic_dec(&a->read_lock);
      88                 :            : 
      89                 :            :     /* Make sure the counter was valid */
      90         [ -  + ]:          1 :     pa_assert(COUNTER(n) > 0);
      91                 :            : 
      92                 :            :     /* Post the semaphore */
      93                 :          1 :     pa_semaphore_post(a->semaphore);
      94                 :          1 : }
      95                 :            : 
      96                 :         18 : unsigned pa_aupdate_write_begin(pa_aupdate *a) {
      97                 :            :     unsigned n;
      98                 :            : 
      99         [ -  + ]:         18 :     pa_assert(a);
     100                 :            : 
     101                 :         18 :     pa_mutex_lock(a->write_lock);
     102                 :            : 
     103                 :         36 :     n = (unsigned) pa_atomic_load(&a->read_lock);
     104                 :            : 
     105                 :         18 :     a->swapped = FALSE;
     106                 :            : 
     107                 :         18 :     return !WHICH(n);
     108                 :            : }
     109                 :            : 
     110                 :         18 : unsigned pa_aupdate_write_swap(pa_aupdate *a) {
     111                 :            :     unsigned n;
     112                 :            : 
     113         [ +  - ]:         18 :     pa_assert(a);
     114                 :            : 
     115                 :            :     for (;;) {
     116                 :         36 :         n = (unsigned) pa_atomic_load(&a->read_lock);
     117                 :            : 
     118                 :            :         /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
     119         [ -  + ]:         18 :         if (COUNTER(n) > 0)
     120                 :          0 :             pa_semaphore_wait(a->semaphore);
     121         [ -  + ]:         18 :         else if (pa_atomic_cmpxchg(&a->read_lock, (int) n, (int) (n ^ MSB)))
     122                 :            :             break;
     123                 :            :     }
     124                 :            : 
     125                 :         18 :     a->swapped = TRUE;
     126                 :            : 
     127                 :         18 :     return WHICH(n);
     128                 :            : }
     129                 :            : 
     130                 :         18 : void pa_aupdate_write_end(pa_aupdate *a) {
     131         [ -  + ]:         18 :     pa_assert(a);
     132                 :            : 
     133         [ -  + ]:         18 :     if (!a->swapped)
     134                 :          0 :         pa_aupdate_write_swap(a);
     135                 :            : 
     136                 :         18 :     pa_mutex_unlock(a->write_lock);
     137                 :         18 : }

Generated by: LCOV version 1.9