root/branches/windows-client/src/common/gen-locks/gen-locks.h @ 8527

Revision 8527, 6.5 KB (checked in by sampson, 3 years ago)

Working on gen_locks

Line 
1/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7/* this is a header for a customizable set of locking primitives.  They can
8 * be turned on or off through compile time defines.  Application
9 * programmers should use the following interface for portability rather
10 * than directly calling specific lock implementations:
11 *
12 * int gen_mutex_lock(gen_mutex_t* mut);
13 * int gen_mutex_unlock(gen_mutex_t* mut);
14 * int gen_mutex_trylock(gen_mutex_t* mut);
15 * int gen_mutex_destroy(gen_mutex_t* mut);
16 *
17 * See the examples directory for details.
18 */
19
20#ifndef __GEN_LOCKS_H
21#define __GEN_LOCKS_H
22
23#include <stdlib.h>
24
25/* we will make posix locks the default unless turned off for now. */
26/* this is especially important for development in which case the locks
27 * should really be enabled in order to verify proper operation
28 */
29#if !defined(__GEN_NULL_LOCKING__)
30#ifdef WIN32
31#if !defined(__GEN_WIN_LOCKING__)
32#define __GEN_WIN_LOCKING__
33#endif
34#else
35#if !defined(__GEN_POSIX_LOCKING__)
36#define __GEN_POSIX_LOCKING__
37#endif
38#endif
39#endif
40
41#ifdef __GEN_POSIX_LOCKING__
42#include <pthread.h>
43
44        /* function prototypes for specific locking implementations */
45int gen_posix_mutex_lock(pthread_mutex_t * mut);
46int gen_posix_mutex_unlock(pthread_mutex_t * mut);
47int gen_posix_mutex_trylock(pthread_mutex_t * mut);
48pthread_mutex_t *gen_posix_mutex_build(void);
49int gen_posix_mutex_destroy(pthread_mutex_t * mut);
50int gen_posix_mutex_init(pthread_mutex_t * mut);
51pthread_t gen_posix_thread_self(void);
52
53int gen_posix_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
54int gen_posix_cond_destroy(pthread_cond_t *cond);
55int gen_posix_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mut);
56int gen_posix_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mut,
57                             const struct timespec *abstime);
58int gen_posix_cond_signal(pthread_cond_t *cond);
59int gen_posix_cond_broadcast(pthread_cond_t *cond);
60
61typedef pthread_mutex_t gen_mutex_t;
62typedef pthread_t       gen_thread_t;
63typedef pthread_cond_t  gen_cond_t;
64#define GEN_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER;
65#define GEN_COND_INITIALIZER PTHREAD_COND_INITIALIZER;
66#define gen_mutex_lock(m) gen_posix_mutex_lock(m)
67#define gen_mutex_unlock(m) gen_posix_mutex_unlock(m)
68#define gen_mutex_trylock(m) gen_posix_mutex_trylock(m)
69#define gen_mutex_destroy(m) gen_posix_mutex_destroy(m)
70#define gen_mutex_init(m) gen_posix_mutex_init(m)
71#define gen_thread_self() gen_posix_thread_self()
72
73#define gen_cond_init(c) gen_posix_cond_init(c, NULL)
74#define gen_cond_destroy(c) gen_posix_cond_destroy(c)
75#define gen_cond_wait(c, m) gen_posix_cond_wait(c, m)
76#define gen_cond_timedwait(c, m, s) gen_posix_cond_timedwait(c, m, s)
77#define gen_cond_signal(c) gen_posix_cond_signal(c)
78#define gen_cond_broadcast(c) gen_posix_cond_broadcast(c)
79
80#elif defined (__GEN_WIN_LOCKING__)
81
82#include <Windows.h>
83
84typedef HANDLE gen_mutex_t;
85typedef HANDLE gen_thread_t;
86
87/* Implementation based on Pthreads-win32 - POSIX Threads Library for Win32
88 * Copyright (C) 1998 John E. Bossom
89 * Copyright (C) 1999,2005 Pthreads-win32 contributors
90 */
91typedef struct gen_cond_t_ *pgen_cond_t;
92typedef struct gen_cond_t_ {
93    long nWaitersBlocked;    /* Number of threads blocked */
94    long nWaitersGone;       /* Number of threads timed out */
95    long nWaitersToUnblock;  /* Number of threads to unblock */
96    HANDLE semBlockQueue;    /* Queue up threads waiting for the condition to become signalled */
97    HANDLE semBlockLock;     /* Semaphore that guards access to waiters blocked count/block queue */
98    HANDLE mtxUnblockLock;   /* Mutex that guards access to waiters (to)unblock(ed) counts */
99    pgen_cond_t next;        /* Doubly linked list */
100    pgen_cond_t prev;
101} gen_cond_t;
102
103int gen_win_mutex_lock(HANDLE *mut);
104int gen_win_mutex_unlock(HANDLE *mut);
105int gen_win_mutex_trylock(HANDLE *mut);
106HANDLE *gen_win_mutex_build(void);
107int gen_win_mutex_destroy(HANDLE *mut);
108int gen_win_mutex_init(HANDLE *mut);
109HANDLE gen_win_thread_self(void);
110
111#define GEN_MUTEX_INITIALIZER CreateMutex(NULL, false, NULL);
112#define gen_mutex_lock(m) gen_win_mutex_lock(m)
113#define gen_mutex_unlock(m) gen_win_mutex_unlock(m)
114#define gen_mutex_trylock(m) gen_win_mutex_trylock(m)
115#define gen_mutex_destroy(m) gen_win_mutex_destroy(m)
116#define gen_mutex_init(m) gen_win_mutex_init(m)
117
118#define gen_thread_self() gen_win_thread_self()
119
120int gen_win_cond_init(pgen_cond_t *cond);
121int gen_win_cond_destroy(pgen_cond_t *cond);
122int gen_win_cond_wait(pgen_cond_t *cond, HANDLE *mut);
123int gen_win_cond_timedwait(pgen_cond_t *cond, HANDLE *mut,
124                             const struct timespec *abstime);
125int gen_win_cond_signal(pgen_cond_t *cond);
126int gen_win_cond_broadcast(pgen_cond_t *cond);
127
128#define GEN_COND_INITIALIZER ((pgen_cond_t) -1)
129#define gen_cond_init(c) gen_win_cond_init(c)
130#define gen_cond_destroy(c) gen_win_cond_destroy(c)
131#define gen_cond_wait(c, m) gen_win_cond_wait(c, m)
132#define gen_cond_timedwait(c, m, s) gen_win_cond_timedwait(c, m, s)
133#define gen_cond_signal(c) gen_win_cond_signal(c)
134#define gen_cond_broadcast(c) gen_win_cond_broadcast(c)
135
136#elif defined (__GEN_NULL_LOCKING__)
137        /* this stuff messes around just enough to prevent warnings */
138typedef int gen_mutex_t;
139typedef unsigned long gen_thread_t;
140typedef int gen_cond_t;
141
142#define GEN_MUTEX_INITIALIZER 0
143#define GEN_COND_INITIALIZER 0
144
145static inline int gen_mutex_lock(
146    gen_mutex_t * mutex_p)
147{
148    (void) mutex_p;
149    return 0;
150}
151static inline int gen_mutex_unlock(
152    gen_mutex_t * mutex_p)
153{
154    (void) mutex_p;
155    return 0;
156}
157static inline int gen_mutex_trylock(
158    gen_mutex_t * mutex_p)
159{
160    (void) mutex_p;
161    return 0;
162}
163static inline gen_thread_t gen_thread_self(void)
164{
165    return 0;
166}
167#define gen_mutex_init(m) do{}while(0)
168#define gen_mutex_destroy(m) do{}while(0)
169
170#define gen_cond_init(c) do{}while(0)
171#define gen_cond_destroy(c) do{}while(0)
172
173static inline int gen_cond_wait(gen_cond_t *cond, gen_mutex_t *mut)
174{
175    (void) cond;
176    return 0;
177}
178
179static inline int gen_cond_timedwait(gen_cond_t *cond, gen_mutex_t *mut,
180                                     const struct timespec *abstime)
181{
182    (void) cond;
183    return 0;
184}
185
186static inline int gen_cond_signal(gen_cond_t *cond)
187{
188    (void) cond;
189    return 0;
190}
191
192static inline int gen_cond_broadcast(gen_cond_t *cond)
193{
194    (void) cond;
195    return 0;
196}
197
198#else /* __GEN_NULL_LOCKING__ */
199#error "Must define either POSIX, Windows or NULL locking"
200#endif
201
202#endif /* __GEN_LOCKS_H */
203
204/*
205 * Local variables:
206 *  c-indent-level: 4
207 *  c-basic-offset: 4
208 * End:
209 *
210 * vim: ts=8 sts=4 sw=4 expandtab
211 */
Note: See TracBrowser for help on using the browser.