ecos_rec_mutex.c
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag:
00003 
00004                         ecos_rec_mutex.c -  description
00005                            -------------------
00006     begin                : Jan 21 2006
00007     copyright            : (C) 2006 Klaas Gadeyne
00008     email                : firstname lastname at fmtc be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place,                                    *
00024  *   Suite 330, Boston, MA  02111-1307  USA                                *
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         // Only do something if mutex was locked
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   // Fixme: need check here to verify if owner??
00105   cyg_mutex_release(&(mx->mutex));
00106   cyg_mutex_destroy(&(mx->mutex));
00107 }


rtt
Author(s): RTT Developers
autogenerated on Fri Sep 9 2016 04:01:52