FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tdsthread.h
1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  *
3  * Copyright (C) 2005 Liam Widdowson
4  * Copyright (C) 2010 Frediano Ziglio
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 #ifndef TDSTHREAD_H
23 #define TDSTHREAD_H 1
24 
25 /* $Id: tdsthread.h,v 1.9 2010-09-16 11:12:08 freddy77 Exp $ */
26 
27 #undef TDS_HAVE_MUTEX
28 
29 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX)
30 
31 #include <pthread.h>
32 
33 #define TDS_MUTEX_DEFINE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
34 #define TDS_MUTEX_LOCK(mtx) pthread_mutex_lock(mtx)
35 #define TDS_MUTEX_UNLOCK(mtx) pthread_mutex_unlock(mtx)
36 #define TDS_MUTEX_DECLARE(name) pthread_mutex_t name
37 #define TDS_MUTEX_INIT(mtx) pthread_mutex_init(mtx, NULL)
38 #define TDS_MUTEX_FREE(mtx) pthread_mutex_destroy(mtx)
39 
40 #define TDS_HAVE_MUTEX 1
41 
42 #elif defined(_WIN32)
43 
44 struct ptw32_mcs_node_t_;
45 
46 typedef struct tds_win_mutex_t_ {
47  struct ptw32_mcs_node_t_ *lock;
48  LONG done;
49  CRITICAL_SECTION crit;
50 } tds_win_mutex_t;
51 
52 void tds_win_mutex_lock(tds_win_mutex_t *mutex);
53 static inline int tds_win_mutex_init(tds_win_mutex_t *mtx)
54 {
55  mtx->lock = NULL;
56  mtx->done = 0;
57  return 0;
58 }
59 /* void tds_win_mutex_unlock(tds_win_mutex_t *mutex); */
60 
61 #define TDS_MUTEX_DEFINE(name) tds_win_mutex_t name = { NULL, 0 }
62 #define TDS_MUTEX_LOCK(mtx) \
63  do { if ((mtx)->done) EnterCriticalSection(&(mtx)->crit); else tds_win_mutex_lock(mtx); } while(0)
64 #define TDS_MUTEX_UNLOCK(mtx) LeaveCriticalSection(&(mtx)->crit)
65 #define TDS_MUTEX_DECLARE(name) tds_win_mutex_t name
66 #define TDS_MUTEX_INIT(mtx) tds_win_mutex_init(mtx)
67 #define TDS_MUTEX_FREE(mtx) do { if ((mtx)->done) { DeleteCriticalSection(&(mtx)->crit); (mtx)->done = 0; } } while(0)
68 
69 #define TDS_HAVE_MUTEX 1
70 
71 #else
72 
73 #define TDS_MUTEX_DEFINE(name) int name
74 #define TDS_MUTEX_LOCK(mtx)
75 #define TDS_MUTEX_UNLOCK(mtx)
76 #define TDS_MUTEX_DECLARE(name) int name
77 #define TDS_MUTEX_INIT(mtx)
78 #define TDS_MUTEX_FREE(mtx)
79 
80 #endif
81 
82 #endif