ecos_rec_mutex.c
Go to the documentation of this file.
1 /***************************************************************************
2  tag:
3 
4  ecos_rec_mutex.c - description
5  -------------------
6  begin : Jan 21 2006
7  copyright : (C) 2006 Klaas Gadeyne
8  email : firstname lastname at fmtc be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, *
24  * Suite 330, Boston, MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 #include <rtt/os/ecos_rec_mutex.h>
29 
31 {
32  cyg_mutex_init(&(mx->mutex));
33  mx->owner=0;
34  mx->count = 0;
35 }
36 
38 {
39  bool result = false;
40  cyg_scheduler_lock();
41  {
42  if( cyg_thread_self() == mx->owner )
43  {
44  mx->count++;
45  result = true;
46  }
47  else
48  {
49  result = cyg_mutex_lock( &mx->mutex );
50  mx->count = 1;
51  mx->owner = cyg_thread_self();
52  }
53  }
54  cyg_scheduler_unlock();
55  return result;
56 }
57 
59 {
60  bool result=false;
61  cyg_scheduler_lock();
62  {
63  if( cyg_thread_self() == mx->owner ) {
64  mx->count++;
65  result = true;
66  } else {
67  result = cyg_mutex_trylock( &mx->mutex );
68  if (result == true) {
69  mx->count = 1;
70  mx->owner = cyg_thread_self();
71  }
72  }
73  }
74  cyg_scheduler_unlock();
75  return result;
76 }
77 
79 {
80  cyg_scheduler_lock();
81  {
82  if( cyg_thread_self() == mx->owner )
83  {
84  // Only do something if mutex was locked
85  if (mx->count >= 1)
86  {
87  mx->count--;
88  if (mx->count == 0)
89  {
90  cyg_mutex_unlock( &mx->mutex );
91  }
92  }
93  else
94  diag_printf("recursive mutex: not locked\n");
95  }
96  else
97  diag_printf("Error unlocking recursive mutex: you're not the owner!\n");
98  }
99  cyg_scheduler_unlock();
100 }
101 
103 {
104  // Fixme: need check here to verify if owner??
105  cyg_mutex_release(&(mx->mutex));
106  cyg_mutex_destroy(&(mx->mutex));
107 }
void cyg_recursive_mutex_init(cyg_recursive_mutex_t *mx)
Initialize recursive mutex.
void cyg_recursive_mutex_destroy(cyg_recursive_mutex_t *mx)
Destroy.
bool cyg_recursive_mutex_trylock(cyg_recursive_mutex_t *mx)
Trylock.
bool cyg_recursive_mutex_lock(cyg_recursive_mutex_t *mx)
Lock recursive mutex.
void cyg_recursive_mutex_unlock(cyg_recursive_mutex_t *mx)
Unlock.


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:24