00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <rtt/os/ecos_rec_mutex.h>
00029
00030 void cyg_recursive_mutex_init( cyg_recursive_mutex_t *mx )
00031 {
00032 cyg_mutex_init(&(mx->mutex));
00033 mx->owner=0;
00034 mx->count = 0;
00035 }
00036
00037 bool cyg_recursive_mutex_lock( cyg_recursive_mutex_t *mx )
00038 {
00039 bool result = false;
00040 cyg_scheduler_lock();
00041 {
00042 if( cyg_thread_self() == mx->owner )
00043 {
00044 mx->count++;
00045 result = true;
00046 }
00047 else
00048 {
00049 result = cyg_mutex_lock( &mx->mutex );
00050 mx->count = 1;
00051 mx->owner = cyg_thread_self();
00052 }
00053 }
00054 cyg_scheduler_unlock();
00055 return result;
00056 }
00057
00058 bool cyg_recursive_mutex_trylock( cyg_recursive_mutex_t *mx )
00059 {
00060 bool result=false;
00061 cyg_scheduler_lock();
00062 {
00063 if( cyg_thread_self() == mx->owner ) {
00064 mx->count++;
00065 result = true;
00066 } else {
00067 result = cyg_mutex_trylock( &mx->mutex );
00068 if (result == true) {
00069 mx->count = 1;
00070 mx->owner = cyg_thread_self();
00071 }
00072 }
00073 }
00074 cyg_scheduler_unlock();
00075 return result;
00076 }
00077
00078 void cyg_recursive_mutex_unlock( cyg_recursive_mutex_t *mx )
00079 {
00080 cyg_scheduler_lock();
00081 {
00082 if( cyg_thread_self() == mx->owner )
00083 {
00084
00085 if (mx->count >= 1)
00086 {
00087 mx->count--;
00088 if (mx->count == 0)
00089 {
00090 cyg_mutex_unlock( &mx->mutex );
00091 }
00092 }
00093 else
00094 diag_printf("recursive mutex: not locked\n");
00095 }
00096 else
00097 diag_printf("Error unlocking recursive mutex: you're not the owner!\n");
00098 }
00099 cyg_scheduler_unlock();
00100 }
00101
00102 void cyg_recursive_mutex_destroy( cyg_recursive_mutex_t *mx )
00103 {
00104
00105 cyg_mutex_release(&(mx->mutex));
00106 cyg_mutex_destroy(&(mx->mutex));
00107 }