Xenomai API  2.6.5
mutex.h
1 /*
2  * Written by Gilles Chanteperdrix <[email protected]>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef _POSIX_MUTEX_H
20 #define _POSIX_MUTEX_H
21 
22 #include <asm/xenomai/atomic.h>
23 #include <pthread.h>
24 
25 struct pse51_mutex;
26 
27 union __xeno_mutex {
28  pthread_mutex_t native_mutex;
29  struct __shadow_mutex {
30  unsigned magic;
31  unsigned lockcnt;
32  struct pse51_mutex *mutex;
33  xnarch_atomic_t lock;
34 #ifdef CONFIG_XENO_FASTSYNCH
35  union {
36  unsigned owner_offset;
37  xnarch_atomic_t *owner;
38  };
39  struct pse51_mutexattr attr;
40 #endif /* CONFIG_XENO_FASTSYNCH */
41  } shadow_mutex;
42 };
43 
44 #if defined(__KERNEL__) || defined(__XENO_SIM__)
45 
46 #include <posix/internal.h>
47 #include <posix/thread.h>
48 #include <posix/cb_lock.h>
49 
50 typedef struct pse51_mutex {
51  unsigned magic;
52  xnsynch_t synchbase;
53  xnholder_t link; /* Link in pse51_mutexq */
54 
55 #define link2mutex(laddr) \
56  ((pse51_mutex_t *)(((char *)laddr) - offsetof(pse51_mutex_t, link)))
57 
58  pthread_mutexattr_t attr;
59  pse51_kqueues_t *owningq;
60 } pse51_mutex_t;
61 
62 extern pthread_mutexattr_t pse51_default_mutex_attr;
63 
64 void pse51_mutexq_cleanup(pse51_kqueues_t *q);
65 
66 void pse51_mutex_pkg_init(void);
67 
68 void pse51_mutex_pkg_cleanup(void);
69 
70 /* Internal mutex functions, exposed for use by syscall.c. */
71 int pse51_mutex_timedlock_break(struct __shadow_mutex *shadow,
72  int timed, xnticks_t to);
73 
74 int pse51_mutex_check_init(struct __shadow_mutex *shadow,
75  const pthread_mutexattr_t *attr);
76 
77 int pse51_mutex_init_internal(struct __shadow_mutex *shadow,
78  pse51_mutex_t *mutex,
79  xnarch_atomic_t *ownerp,
80  const pthread_mutexattr_t *attr);
81 
82 void pse51_mutex_destroy_internal(pse51_mutex_t *mutex,
83  pse51_kqueues_t *q);
84 
85 /* must be called with nklock locked, interrupts off. */
86 static inline int pse51_mutex_timedlock_internal(xnthread_t *cur,
87  struct __shadow_mutex *shadow,
88  unsigned count,
89  int timed,
90  xnticks_t abs_to)
91 
92 {
93  pse51_mutex_t *mutex = shadow->mutex;
94 
95  if (xnpod_unblockable_p())
96  return -EPERM;
97 
98  if (!pse51_obj_active(shadow, PSE51_MUTEX_MAGIC, struct __shadow_mutex)
99  || !pse51_obj_active(mutex, PSE51_MUTEX_MAGIC, struct pse51_mutex))
100  return -EINVAL;
101 
102 #if XENO_DEBUG(POSIX)
103  if (mutex->owningq != pse51_kqueues(mutex->attr.pshared))
104  return -EPERM;
105 #endif /* XENO_DEBUG(POSIX) */
106 
107  if (xnsynch_owner_check(&mutex->synchbase, cur) == 0)
108  return -EBUSY;
109 
110  if (timed)
111  xnsynch_acquire(&mutex->synchbase, abs_to, XN_REALTIME);
112  else
113  xnsynch_acquire(&mutex->synchbase, XN_INFINITE, XN_RELATIVE);
114 
115  if (unlikely(xnthread_test_info(cur, XNBREAK | XNRMID | XNTIMEO))) {
116  if (xnthread_test_info(cur, XNBREAK))
117  return -EINTR;
118  else if (xnthread_test_info(cur, XNTIMEO))
119  return -ETIMEDOUT;
120  else /* XNRMID */
121  return -EINVAL;
122  }
123 
124  shadow->lockcnt = count;
125 
126  return 0;
127 }
128 
129 #endif /* __KERNEL__ */
130 
131 #endif /* !_POSIX_MUTEX_H */
xnflags_t xnsynch_acquire(struct xnsynch *synch, xnticks_t timeout, xntmode_t timeout_mode)
Acquire the ownership of a synchronization object.
Definition: synch.c:406
#define XNTIMEO
Woken up due to a timeout condition.
Definition: thread.h:108
#define XNRMID
Pending on a removed resource.
Definition: thread.h:109
#define XNBREAK
Forcibly awaken from a wait state.
Definition: thread.h:110