NCBI C++ ToolKit
mutex1.c
Go to the documentation of this file.

Go to the SVN repository for this file.

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 "tds_sysdep_public.h"
29 #include <freetds/thread.h>
30 
31 #include <common/test_assert.h>
32 
33 #define int2ptr(i) ((void*)(((char*)0)+(i)))
34 #define ptr2int(p) ((int)(((char*)(p))-((char*)0)))
35 
36 #if !defined(TDS_NO_THREADSAFE)
37 
38 static tds_mutex mtx = TDS_MUTEX_INITIALIZER;
39 
40 static TDS_THREAD_PROC_DECLARE(trylock_proc, arg)
41 {
42  tds_mutex *mtx = (tds_mutex *) arg;
43 
44  if (!tds_mutex_trylock(mtx)) {
45  /* got mutex, failure as should be locked */
46 #ifdef _WIN32
47  return 1;
48 #else
49  return int2ptr(1);
50 #endif
51  }
52  /* success */
53  return 0;
54 }
55 
56 static void
57 test(tds_mutex *mtx)
58 {
59  tds_thread th;
60  void *res;
61 
62  if (tds_mutex_trylock(mtx)) {
63  fprintf(stderr, "mutex should be unlocked\n");
64  exit(1);
65  }
66  /* this success on Windows cause mutex are recursive */
67 #ifndef _WIN32
68  if (!tds_mutex_trylock(mtx)) {
69  fprintf(stderr, "mutex should be locked\n");
70  exit(1);
71  }
72 #endif
73 
74  if (tds_thread_create(&th, trylock_proc, mtx) != 0) {
75  fprintf(stderr, "error creating thread\n");
76  exit(1);
77  }
78 
79  if (tds_thread_join(th, &res) != 0) {
80  fprintf(stderr, "error waiting thread\n");
81  exit(1);
82  }
83 
84  if (ptr2int(res) != 0) {
85  fprintf(stderr, "mutex should be locked inside thread\n");
86  exit(1);
87  }
88 
90 }
91 
92 int main(void)
93 {
94  tds_mutex local;
95 
96  test(&mtx);
97 
98  /* try allocating it */
99  if (tds_mutex_init(&local)) {
100  fprintf(stderr, "error creating mutex\n");
101  exit(1);
102  }
103  test(&local);
105 
106  /* try again */
107  if (tds_mutex_init(&local)) {
108  fprintf(stderr, "error creating mutex\n");
109  exit(1);
110  }
111  test(&local);
113 
114  return 0;
115 }
116 
117 #else
118 
119 int main(void)
120 {
121  return 0;
122 }
123 
124 #endif
125 
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n th
exit(2)
#define int2ptr(i)
Definition: mutex1.c:33
static void test(tds_mutex *mtx)
Definition: mutex1.c:57
static TDS_THREAD_PROC_DECLARE(trylock_proc, arg)
Definition: mutex1.c:40
int main(void)
Definition: mutex1.c:92
#define ptr2int(p)
Definition: mutex1.c:34
static tds_mutex mtx
Definition: mutex1.c:38
#define tds_mutex_trylock(x)
Definition: thread.h:422
#define tds_thread_create(ret, proc, arg)
Definition: thread.h:283
#define tds_mutex_free(x)
Definition: thread.h:419
#define tds_mutex_unlock(x)
Definition: thread.h:423
#define tds_mutex_init(x)
Definition: thread.h:420
#define tds_thread_join(th, ret)
Definition: thread.h:289
#define local
Definition: zutil.h:33
Modified on Sat Dec 02 09:19:51 2023 by modify_doxy.py rev. 669887