Lumiera  0.pre.03
»edit your freedom«
recmutex.c
Go to the documentation of this file.
1 /*
2  recmutex.c - recursive mutex
3 
4  Copyright (C) Lumiera.org
5  2008, 2009, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (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 
22 #include "lib/recmutex.h"
23 
30 static pthread_once_t recmutexattr_once = PTHREAD_ONCE_INIT;
31 static pthread_mutexattr_t recmutexattr;
32 
33 static void recmutexattr_init()
34 {
35  pthread_mutexattr_init (&recmutexattr);
36  pthread_mutexattr_settype (&recmutexattr, PTHREAD_MUTEX_RECURSIVE);
37 }
38 
39 
40 LumieraRecmutex
41 lumiera_recmutex_init (LumieraRecmutex self,
42  const char* purpose,
43  struct nobug_flag* flag,
44  const struct nobug_context ctx)
45 {
46  if (self)
47  {
48  pthread_once (&recmutexattr_once, recmutexattr_init);
49 
50  NOBUG_RESOURCE_HANDLE_INIT (self->rh);
51  NOBUG_RESOURCE_ANNOUNCE_RAW_CTX (flag, "recmutex", purpose, self, self->rh, ctx)
52  {
53  pthread_mutex_init (&self->recmutex, &recmutexattr);
54  }
55  }
56 
57  return self;
58 }
59 
60 
61 LumieraRecmutex
62 lumiera_recmutex_destroy (LumieraRecmutex self,
63  struct nobug_flag* flag,
64  const struct nobug_context ctx)
65 {
66  if (self)
67  {
68  NOBUG_RESOURCE_FORGET_RAW_CTX (flag, self->rh, ctx)
69  {
70  if (pthread_mutex_destroy (&self->recmutex))
71  LUMIERA_DIE (LOCK_DESTROY);
72  }
73  }
74  return self;
75 }
76 
77 /*
78 // Local Variables:
79 // mode: C
80 // c-file-style: "gnu"
81 // indent-tabs-mode: nil
82 // End:
83 */
LumieraRecmutex lumiera_recmutex_destroy(LumieraRecmutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a recursive mutex variable.
Definition: recmutex.c:62
Mutual exclusion locking, header.
LumieraRecmutex lumiera_recmutex_init(LumieraRecmutex self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
Initialise a recursive mutex variable Initialises a &#39;recursive&#39; mutex which might be locked by the sa...
Definition: recmutex.c:41
#define LUMIERA_DIE(err)
Abort unconditionally with a &#39;Fatal Error!&#39; message.
Definition: error.h:63