LCOV - code coverage report
Current view: top level - tests - lock-autospawn-test.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 39 39 100.0 %
Date: 2012-07-17 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 11 20 55.0 %

           Branch data     Line data    Source code
       1                 :            : /***
       2                 :            :   This file is part of PulseAudio.
       3                 :            : 
       4                 :            :   Copyright 2008 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 published
       8                 :            :   by the Free Software Foundation; either version 2.1 of the License,
       9                 :            :   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 License
      17                 :            :   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 <string.h>
      27                 :            : 
      28                 :            : #include <pulsecore/poll.h>
      29                 :            : #include <pulsecore/macro.h>
      30                 :            : #include <pulsecore/thread.h>
      31                 :            : #include <pulsecore/lock-autospawn.h>
      32                 :            : #include <pulse/util.h>
      33                 :            : 
      34                 :          2 : static void thread_func(void*k) {
      35         [ -  + ]:          2 :     pa_assert_se(pa_autospawn_lock_init() >= 0);
      36                 :            : 
      37                 :          2 :     pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
      38                 :            : 
      39         [ -  + ]:          2 :     pa_assert_se(pa_autospawn_lock_acquire(TRUE) > 0);
      40                 :            : 
      41                 :          2 :     pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
      42                 :            : 
      43                 :          2 :     pa_msleep(5000);
      44                 :            : 
      45                 :          2 :     pa_log("%i, Releasing", PA_PTR_TO_INT(k));
      46                 :            : 
      47                 :          2 :     pa_autospawn_lock_release();
      48                 :            : 
      49                 :          2 :     pa_autospawn_lock_done(FALSE);
      50                 :          2 : }
      51                 :            : 
      52                 :          2 : static void thread_func2(void *k) {
      53                 :            :     int fd;
      54                 :            : 
      55         [ -  + ]:          2 :     pa_assert_se((fd = pa_autospawn_lock_init()) >= 0);
      56                 :            : 
      57                 :          2 :     pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
      58                 :            : 
      59                 :            :     for (;;) {
      60                 :            :         struct pollfd pollfd;
      61                 :            :         int j;
      62                 :            : 
      63         [ +  + ]:          8 :         if ((j = pa_autospawn_lock_acquire(FALSE)) > 0)
      64                 :            :             break;
      65                 :            : 
      66         [ -  + ]:          6 :         pa_assert(j == 0);
      67                 :            : 
      68                 :            :         memset(&pollfd, 0, sizeof(pollfd));
      69                 :          6 :         pollfd.fd = fd;
      70                 :          6 :         pollfd.events = POLLIN;
      71                 :            : 
      72         [ -  + ]:          6 :         pa_assert_se(pa_poll(&pollfd, 1, -1) == 1);
      73                 :            : 
      74                 :          6 :         pa_log("%i, woke up", PA_PTR_TO_INT(k));
      75                 :          6 :     }
      76                 :            : 
      77                 :          2 :     pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
      78                 :            : 
      79                 :          2 :     pa_msleep(5000);
      80                 :            : 
      81                 :          2 :     pa_log("%i, Releasing", PA_PTR_TO_INT(k));
      82                 :            : 
      83                 :          2 :     pa_autospawn_lock_release();
      84                 :            : 
      85                 :          2 :     pa_autospawn_lock_done(FALSE);
      86                 :          2 : }
      87                 :            : 
      88                 :          1 : int main(int argc, char**argv) {
      89                 :            :     pa_thread *a, *b, *c, *d;
      90                 :            : 
      91         [ -  + ]:          1 :     pa_assert_se((a = pa_thread_new("test1", thread_func, PA_INT_TO_PTR(1))));
      92         [ -  + ]:          1 :     pa_assert_se((b = pa_thread_new("test2", thread_func2, PA_INT_TO_PTR(2))));
      93         [ -  + ]:          1 :     pa_assert_se((c = pa_thread_new("test3", thread_func2, PA_INT_TO_PTR(3))));
      94         [ -  + ]:          1 :     pa_assert_se((d = pa_thread_new("test4", thread_func, PA_INT_TO_PTR(4))));
      95                 :            : 
      96                 :          1 :     pa_thread_join(a);
      97                 :          1 :     pa_thread_join(b);
      98                 :          1 :     pa_thread_join(c);
      99                 :          1 :     pa_thread_join(d);
     100                 :            : 
     101                 :          1 :     pa_thread_free(a);
     102                 :          1 :     pa_thread_free(b);
     103                 :          1 :     pa_thread_free(c);
     104                 :          1 :     pa_thread_free(d);
     105                 :            : 
     106                 :            :     return 0;
     107                 :            : }

Generated by: LCOV version 1.9