lock.c
1 /*
2  shm-arena shared memory arena
3  Copyright (C) 2006-2008 Lance Arsenault (LGPL v3)
4 
5 
6  This file is part of shm-arena.
7 
8  shm-arena is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12 
13  shm-arena is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this program. If not, see
20  <http://www.gnu.org/licenses/>.
21 */
22 
37 #include "config.h"
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdint.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 
48 #include <pthread.h>
49 #include "spew.h"
50 #include "assert.h"
51 #include "debug_lock.h"
52 #include "shm_arena.h"
53 #include "arena.h"
54 #include "arena_lock.h"
55 
56 
81 {
82  int err;
83  //SPEW(_DEBUG, "%s(arena=%p)", __func__, arena);
84 
85  arena = get_arena_and_autolock(arena, 2, &err IF_SPEW(, __func__));
86  if(!arena)
87  {
88  return (errno = err);
89  }
90 
91  return 0;
92 }
93 
115 {
116  int err;
117  //SPEW(_DEBUG, "%s(arena=%p)", __func__, arena);
118 
119  arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
120  if(!arena)
121  {
122  return (errno = err);
123  }
124 
125  return 0;
126 }
127 
146 {
147  int err;
148  //SPEW(_DEBUG, "%s(arena=%p)", __func__, arena);
149 
150  arena = get_arena_and_autolock(arena, 0, &err IF_SPEW(, __func__));
151  if(!arena)
152  return (errno = err);
153 
154  return 0;
155 }
156 
177 int shm_wrlock(shm_arena_t arena, const void *ptr)
178 {
179  int err = 0;
180  pthread_rwlock_t *l;
181 
182  //SPEW(_DEBUG, "%s(arena=%p, ptr=%p)", __func__, arena, ptr);
183 
184  arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
185  if(!arena)
186  {
187  return err;
188  }
189  if((l = ptr_rwlock(ptr)))
190  {
191  err = pthread_rwlock_wrlock(l);
192  if(err)
193  {
194  errno = err;
195  SPEW_SYS(_WARN,
196  "%s() failed: pthread_rwlock_wrlock() failed",
197  __func__);
198  }
199  }
200 
201  return err;
202 }
203 
204 
225 int shm_rdlock(shm_arena_t arena, const void *ptr)
226 {
227  int err = 0;
228  pthread_rwlock_t *l;
229 
230  //SPEW(_DEBUG, "%s(arena=%p, ptr=%p)", __func__, arena, ptr);
231 
232  arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
233  if(!arena)
234  {
235  return err;
236  }
237 
238  if((l = ptr_rwlock(ptr)))
239  {
240  err = pthread_rwlock_rdlock(l);
241  if(err)
242  {
243  errno = err;
244  SPEW_SYS(_WARN,
245  "%s() failed: pthread_rwlock_rdlock() failed",
246  __func__);
247  }
248  }
249 
250  return err;
251 }
252 
268 int shm_unlock(shm_arena_t arena, const void *ptr)
269 {
270  int err = 0;
271  pthread_rwlock_t *l;
272 
273  //SPEW(_DEBUG, "%s(arena=%p, ptr=%p)", __func__, arena, ptr);
274 
275  arena = get_arena(arena, 1, &err IF_SPEW(, __func__));
276  if(!arena)
277  {
278  return err;
279  }
280 
281  if((l = ptr_rwlock(ptr)))
282  {
283  err = pthread_rwlock_unlock(l);
284  if(err)
285  {
286  errno = err;
287  SPEW_SYS(_WARN,
288  "%s() failed: pthread_rwlock_unlock() failed",
289  __func__);
290  }
291  }
292 
293  if(!err)
294  err = arena_autounlock(arena IF_SPEW(, __func__));
295  else
296  arena_autounlock(arena IF_SPEW(, __func__));
297 
298  return err;
299 }
300 
301 
315 int shm_rwlock_reset(shm_arena_t arena, const void *ptr)
316 {
319  int err = 1;
320  /* For now keep gcc from complaining about unused variables: */
321  if(arena) return err;
322  if(ptr) return err;
323 #if 0
324  pthread_rwlock_t *l;
325 
326  //SPEW(_DEBUG, "%s(arena=%p, ptr=%p)", __func__, arena, ptr);
327 
328  arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
329  if(!arena)
330  {
331  return err;
332  }
333  if((l = ptr_rwlock(ptr))
334  {
335  err = pthread_rwlock_rdlock(l);
336  if(err)
337  {
338  errno = err;
339  SPEW_SYS(_WARN,
340  "%s() failed: pthread_rwlock_rdlock() failed",
341  __func__);
342  }
343  }
344 #endif
345 
346  return err;
347 }
348 
359 {
362  /* For now keep gcc from complaining about unused variables: */
363  if(arena) return 1;
364 #if 0
365  int err;
366  SPEW(_DEBUG, "%s(arena=%p)", __func__, arena);
367 
368  arena = get_arena(arena, 2, &err IF_SPEW(, __func__));
369  if(!arena)
370  {
371  return (errno = err);
372  }
373 
374 #endif
375  return 1;
376 }
int shm_rwlock_reset(shm_arena_t arena, const void *ptr)
reset a segment read-write lock
Definition: lock.c:315
int shm_arena_rdlock(shm_arena_t arena)
acquire an arena read lock
Definition: lock.c:114
int shm_unlock(shm_arena_t arena, const void *ptr)
release a shared memory segment read-write lock
Definition: lock.c:268
int shm_arena_wrlock(shm_arena_t arena)
acquire an arena write lock
Definition: lock.c:80
int shm_arena_rwlock_reset(shm_arena_t arena)
reset an arena read-write lock
Definition: lock.c:358
int shm_wrlock(shm_arena_t arena, const void *ptr)
acquire an segment write lock
Definition: lock.c:177
int shm_arena_unlock(shm_arena_t arena)
release an arena read or write lock
Definition: lock.c:145
int shm_rdlock(shm_arena_t arena, const void *ptr)
acquire an segment read lock
Definition: lock.c:225

Shared Memory Arena version RC-0.0.25