00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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;
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;
00034 unsigned condvars;
00035
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
00045 int pse51_mutex_timedlock_break(struct __shadow_mutex *shadow, xnticks_t to);
00046
00047
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
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