use smb_rwlock_t instead of rwlock_t to avoid conflicts with system
[samba.git] / source4 / lib / mutex.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba mutex/lock functions
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (C) James J Myers 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 #include "includes.h"
22          
23 static smb_mutex_t mutex_list[MUTEX_MAX];
24
25 /* the registered mutex handlers */
26 static struct {
27         const char *name;
28         struct mutex_ops ops;
29 } mutex_handlers;
30
31 int mutex_lock_by_id(enum mutex_id id, const char *name)
32 {
33         return mutex_lock(&mutex_list[id], name);
34 }
35
36 int mutex_unlock_by_id(enum mutex_id id, const char *name)
37 {
38         return mutex_unlock(&mutex_list[id], name);
39 }
40
41 int mutex_init(smb_mutex_t *mutex, const char *name)
42 {
43         if (mutex_handlers.ops.mutex_init) {
44                 return mutex_handlers.ops.mutex_init(mutex, name);
45         }
46         return 0;
47 }
48
49 int mutex_destroy(smb_mutex_t *mutex, const char *name)
50 {
51         if (mutex_handlers.ops.mutex_destroy) {
52                 return mutex_handlers.ops.mutex_destroy(mutex, name);
53         }
54         return 0;
55 }
56
57 int mutex_lock(smb_mutex_t *mutex, const char *name)
58 {
59         if (mutex_handlers.ops.mutex_lock) {
60                 return mutex_handlers.ops.mutex_lock(mutex, name);
61         }
62         return 0;
63 }
64
65 int mutex_unlock(smb_mutex_t *mutex, const char *name)
66 {
67         if (mutex_handlers.ops.mutex_unlock) {
68                 return mutex_handlers.ops.mutex_unlock(mutex, name);
69         }
70         return 0;
71 }
72
73 /* read/write lock routines */
74
75 int rwlock_init(smb_rwlock_t *rwlock, const char *name)
76 {
77         if (mutex_handlers.ops.rwlock_init) {
78                 return mutex_handlers.ops.rwlock_init(rwlock, name);
79         }
80         return 0;
81 }
82
83 int rwlock_destroy(smb_rwlock_t *rwlock, const char *name)
84 {
85         if (mutex_handlers.ops.rwlock_destroy) {
86                 return mutex_handlers.ops.rwlock_destroy(rwlock, name);
87         }
88         return 0;
89 }
90
91 int rwlock_lock_write(smb_rwlock_t *rwlock, const char *name)
92 {
93         if (mutex_handlers.ops.rwlock_lock_write) {
94                 return mutex_handlers.ops.rwlock_lock_write(rwlock, name);
95         }
96         return 0;
97 }
98
99 int rwlock_lock_read(smb_rwlock_t *rwlock, const char *name)
100 {
101         if (mutex_handlers.ops.rwlock_lock_read) {
102                 return mutex_handlers.ops.rwlock_lock_read(rwlock, name);
103         }
104         return 0;
105 }
106
107 int rwlock_unlock(smb_rwlock_t *rwlock, const char *name)
108 {
109         if (mutex_handlers.ops.rwlock_unlock) {
110                 return mutex_handlers.ops.rwlock_unlock(rwlock, name);
111         }
112         return 0;
113 }
114
115
116 /*
117   register a set of mutex/rwlock handlers. 
118   Should only be called once in the execution of smbd.
119 */
120 BOOL register_mutex_handlers(const char *name, struct mutex_ops *ops)
121 {
122         if (mutex_handlers.name != NULL) {
123                 /* it's already registered! */
124                 DEBUG(2,("mutex handler '%s' already registered - failed '%s'\n", 
125                          mutex_handlers.name, name));
126                 return False;
127         }
128
129         mutex_handlers.name = name;
130         mutex_handlers.ops = *ops;
131
132         if (mutex_handlers.ops.mutex_init) {
133                 enum mutex_id id;
134                 for (id=0; id < MUTEX_MAX; id++) {
135                         mutex_handlers.ops.mutex_init(&mutex_list[id], "mutex_list");
136                 }
137         }
138
139         DEBUG(2,("mutex handler '%s' registered\n", name));
140         return True;
141 }
142