LCOV - code coverage report
Current view: top level - src/utils/unittests - mutex1.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 22 38 57.9 %
Date: 2024-04-18 20:40:06 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2012  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : 
      22             : #include <stdio.h>
      23             : 
      24             : #if HAVE_STDLIB_H
      25             : #include <stdlib.h>
      26             : #endif /* HAVE_STDLIB_H */
      27             : 
      28             : #include <freetds/thread.h>
      29             : #include <freetds/macros.h>
      30             : 
      31             : #if !defined(TDS_NO_THREADSAFE)
      32             : 
      33             : static tds_mutex mtx = TDS_MUTEX_INITIALIZER;
      34             : 
      35          24 : static TDS_THREAD_PROC_DECLARE(trylock_proc, arg)
      36             : {
      37          24 :         tds_mutex *mtx = (tds_mutex *) arg;
      38             : 
      39          24 :         if (!tds_mutex_trylock(mtx)) {
      40             :                 /* got mutex, failure as should be locked */
      41             :                 return TDS_THREAD_RESULT(1);
      42             :         }
      43             :         /* success */
      44          24 :         return TDS_THREAD_RESULT(0);
      45             : }
      46             : 
      47             : static void
      48          24 : test(tds_mutex *mtx)
      49             : {
      50             :         tds_thread th;
      51             :         void *res;
      52             : 
      53          24 :         if (tds_mutex_trylock(mtx)) {
      54           0 :                 fprintf(stderr, "mutex should be unlocked\n");
      55           0 :                 exit(1);
      56             :         }
      57             :         /* check mutex are not recursive, even on Windows */
      58          24 :         if (!tds_mutex_trylock(mtx)) {
      59           0 :                 fprintf(stderr, "mutex should be locked\n");
      60           0 :                 exit(1);
      61             :         }
      62             : 
      63          24 :         if (tds_thread_create(&th, trylock_proc, mtx) != 0) {
      64           0 :                 fprintf(stderr, "error creating thread\n");
      65           0 :                 exit(1);
      66             :         }
      67             : 
      68          48 :         if (tds_thread_join(th, &res) != 0) {
      69           0 :                 fprintf(stderr, "error waiting thread\n");
      70           0 :                 exit(1);
      71             :         }
      72             : 
      73          24 :         if (TDS_PTR2INT(res) != 0) {
      74           0 :                 fprintf(stderr, "mutex should be locked inside thread\n");
      75           0 :                 exit(1);
      76             :         }
      77             : 
      78          24 :         tds_mutex_unlock(mtx);
      79             : 
      80             :         /* now trylock after the unlock should succeed */
      81          24 :         if (tds_mutex_trylock(mtx)) {
      82           0 :                 fprintf(stderr, "mutex should be unlocked\n");
      83           0 :                 exit(1);
      84             :         }
      85             : 
      86          24 :         tds_mutex_unlock(mtx);
      87          24 : }
      88             : 
      89           8 : int main(void)
      90             : {
      91             :         tds_mutex local;
      92             : 
      93           8 :         test(&mtx);
      94             : 
      95             :         /* try allocating it */
      96           8 :         if (tds_mutex_init(&local)) {
      97           0 :                 fprintf(stderr, "error creating mutex\n");
      98           0 :                 exit(1);
      99             :         }
     100           8 :         test(&local);
     101           8 :         tds_mutex_free(&local);
     102             : 
     103             :         /* try again */
     104           8 :         if (tds_mutex_init(&local)) {
     105           0 :                 fprintf(stderr, "error creating mutex\n");
     106           0 :                 exit(1);
     107             :         }
     108           8 :         test(&local);
     109           8 :         tds_mutex_free(&local);
     110             : 
     111             :         return 0;
     112             : }
     113             : 
     114             : #else
     115             : 
     116             : int main(void)
     117             : {
     118             :         return 0;
     119             : }
     120             : 
     121             : #endif
     122             : 

Generated by: LCOV version 1.13