00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <l4/thread/thread.h>
00037 #include <l4/lock/lock.h>
00038 #include <stdlib.h>
00039 #include <mutex.h>
00040
00041
00042 int mutex_init(mutex_t *m)
00043 {
00044 if (m == 0)
00045 return -1;
00046 if (m->l4_mutex == 0) {
00047 if ((m->l4_mutex = malloc(sizeof(l4lock_t))) == NULL)
00048 return -1;
00049 }
00050 *((l4lock_t*)m->l4_mutex) = L4LOCK_UNLOCKED;
00051 return 0;
00052 }
00053
00054
00055 int mutex_lock(mutex_t *m)
00056 {
00057 if (m == 0)
00058 return -1;
00059 l4lock_lock((l4lock_t*)m->l4_mutex);
00060 return 0;
00061 }
00062
00063
00064 int mutex_trylock(mutex_t *m)
00065 {
00066 if (m == 0)
00067 return -1;
00068 return l4lock_try_lock((l4lock_t*)m->l4_mutex);
00069 }
00070
00071
00072 int mutex_unlock(mutex_t *m)
00073 {
00074 if (m == 0)
00075 return -1;
00076 l4lock_unlock((l4lock_t*)m->l4_mutex);
00077 return 0;
00078 }
00079
00080
00081 int mutex_destroy(mutex_t *m)
00082 {
00083 int error = -1;
00084 if (m == 0)
00085 return error;
00086
00087 if (m->l4_mutex != 0 &&
00088 l4thread_equal(l4lock_owner((l4lock_t*)m->l4_mutex),
00089 l4thread_myself())) {
00090 l4lock_unlock((l4lock_t*)m->l4_mutex);
00091 free(m->l4_mutex);
00092 m->l4_mutex = 0;
00093 error = 0;
00094 }
00095 return error;
00096 }