first public release of samba4 code
[gd/samba-autobuild/.git] / source4 / include / mutex.h
1 #ifndef _MUTEX_H_
2 #define _MUTEX_H_
3 /* 
4    Unix SMB/CIFS implementation.
5    Samba mutex functions
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) James J Myers 2003
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 /* To add a new mutex, add it to enum mutex_id
25  */
26 enum mutex_id { MUTEX_SMBD,             /* global smbd lock */
27                 MUTEX_TALLOC,           /* global talloc.c lock */
28                 MUTEX_DEBUG,            /* global debug.c lock */
29                 MUTEX_TANK,             /* vfs_tank lock */
30
31                 MUTEX_MAX /* this MUST be kept last */
32 };
33
34 /* To add a new read/write lock, add it to enum rwlock_id
35  */
36 enum rwlock_id { RWLOCK_SMBD,           /* global smbd lock */
37
38                 RWLOCK_MAX /* this MUST be kept last */
39 };
40
41 #define MUTEX_LOCK_BY_ID(mutex_index) mutex_lock_by_id(mutex_index, #mutex_index)
42 #define MUTEX_UNLOCK_BY_ID(mutex_index) mutex_unlock_by_id(mutex_index, #mutex_index)
43 #define MUTEX_INIT(mutex, name) mutex_init(mutex, #name)
44 #define MUTEX_DESTROY(mutex, name) mutex_destroy(mutex, #name)
45 #define MUTEX_LOCK(mutex, name) mutex_lock(mutex, #name)
46 #define MUTEX_UNLOCK(mutex, name) mutex_unlock(mutex, #name)
47
48 #define RWLOCK_INIT(rwlock, name) rwlock_init(rwlock, #name)
49 #define RWLOCK_DESTROY(rwlock, name) rwlock_destroy(rwlock, #name)
50 #define RWLOCK_LOCK_WRITE(rwlock, name) rwlock_lock_write(rwlock, #name)
51 #define RWLOCK_LOCK_READ(rwlock, name) rwlock_lock_read(rwlock, #name)
52 #define RWLOCK_UNLOCK(rwlock, name) rwlock_unlock(rwlock, #name)
53
54
55
56 /* this null typedef ensures we get the types right and avoids the
57    pitfalls of void* */
58 typedef struct {
59         void *mutex;
60 } mutex_t;
61 typedef struct {
62         void *rwlock;
63 } rwlock_t;
64
65 /* the mutex model operations structure - contains function pointers to 
66    the model-specific implementations of each operation */
67 struct mutex_ops {
68         int (*mutex_init)(mutex_t *mutex, const char *name);
69         int (*mutex_lock)(mutex_t *mutex, const char *name);
70         int (*mutex_unlock)(mutex_t *mutex, const char *name);
71         int (*mutex_destroy)(mutex_t *mutex, const char *name);
72         int (*rwlock_init)(rwlock_t *rwlock, const char *name);
73         int (*rwlock_lock_write)(rwlock_t *rwlock, const char *name);
74         int (*rwlock_lock_read)(rwlock_t *rwlock, const char *name);
75         int (*rwlock_unlock)(rwlock_t *rwlock, const char *name);
76         int (*rwlock_destroy)(rwlock_t *rwlock, const char *name);
77 };
78
79 #endif /* ndef _MUTEX_H_ */