ksrc/skins/posix/mutex.h

00001 /*
00002  * Written by Gilles Chanteperdrix <gilles.chanteperdrix@laposte.net>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifndef _POSIX_MUTEX_H
00020 #define _POSIX_MUTEX_H
00021 
00022 #include <posix/internal.h>
00023 #include <posix/thread.h>
00024 
00025 typedef struct pse51_mutex {
00026     xnsynch_t synchbase;
00027     xnholder_t link;            /* Link in pse51_mutexq */
00028 
00029 #define link2mutex(laddr)                                               \
00030     ((pse51_mutex_t *)(((char *)laddr) - offsetof(pse51_mutex_t, link)))
00031 
00032     pthread_mutexattr_t attr;
00033     unsigned count;             /* lock count. */
00034     unsigned condvars;          /* count of condition variables using this
00035                                    mutex. */
00036 } pse51_mutex_t;
00037 
00038 void pse51_mutexq_cleanup(pse51_kqueues_t *q);
00039 
00040 void pse51_mutex_pkg_init(void);
00041 
00042 void pse51_mutex_pkg_cleanup(void);
00043 
00044 /* Interruptible versions of pthread_mutex_*. Exposed for use by syscall.c. */
00045 int pse51_mutex_timedlock_break(struct __shadow_mutex *shadow, xnticks_t to);
00046 
00047 /* must be called with nklock locked, interrupts off. */
00048 static inline int pse51_mutex_trylock_internal(xnthread_t *cur,
00049                                                struct __shadow_mutex *shadow,
00050                                                unsigned count)
00051 {
00052     pse51_mutex_t *mutex = shadow->mutex;
00053 
00054     if (xnpod_unblockable_p())
00055         return EPERM;
00056 
00057     if (!pse51_obj_active(shadow, PSE51_MUTEX_MAGIC, struct __shadow_mutex))
00058         return EINVAL;
00059 
00060     if (mutex->count)
00061         return EBUSY;
00062 
00063     xnsynch_set_owner(&mutex->synchbase, cur);
00064     mutex->count = count;
00065     return 0;
00066 }
00067 
00068 /* must be called with nklock locked, interrupts off. */
00069 static inline int pse51_mutex_timedlock_internal(xnthread_t *cur,
00070                                                  struct __shadow_mutex *shadow,
00071                                                  unsigned count,
00072                                                  xnticks_t abs_to)
00073 
00074 {
00075     int err;
00076 
00077     err = pse51_mutex_trylock_internal(cur, shadow, count);
00078 
00079     if (err == EBUSY)
00080         {
00081         pse51_mutex_t *mutex = shadow->mutex;
00082 
00083         if (xnsynch_owner(&mutex->synchbase) != cur)
00084             {
00085             xnticks_t to = abs_to;
00086                 
00087             err = clock_adjust_timeout(&to, CLOCK_REALTIME);
00088                 
00089             if (err)
00090                 return err;
00091             
00092             xnsynch_sleep_on(&mutex->synchbase, to);
00093             
00094             if (xnthread_test_info(cur, XNBREAK))
00095                 return EINTR;
00096             
00097             if (xnthread_test_info(cur, XNRMID))
00098                 return EINVAL;
00099             
00100             if (xnthread_test_info(cur, XNTIMEO))
00101                 return ETIMEDOUT;
00102 
00103             mutex->count = count;
00104             }
00105         }
00106 
00107     return err;
00108 }
00109 
00110 #endif /* !_POSIX_MUTEX_H */

Generated on Mon Dec 25 13:57:10 2006 for Xenomai API by  doxygen 1.4.6