SheafSystem  0.0.0.0
thread_mutex.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for class THREAD_MUTEX
19 
20 #ifndef THREAD_MUTEX_H
21 #include "SheafSystem/thread_mutex.h"
22 #endif
23 
24 #ifdef _PTHREADS
25 
26 #include "SheafSystem/assert_contract.h"
27 #include "SheafSystem/std_errno.h" // for EBUSY
28 
30 sheaf::thread_mutex::
31 thread_mutex()
32 {
33 
34  // body:
35 
36  int rtncode = pthread_mutex_init(&pthread_mutex,&pthread_mutexattr);
37 
38  // postconditions:
39 
40  ensure(rtncode == 0);
41 }
42 
43 
45 sheaf::thread_mutex::
46 ~thread_mutex()
47 {}
48 
49 bool
50 sheaf::thread_mutex::
51 invariant() const
52 {
53 
54  return true;
55 }
56 
57 
59 
60 
61 void
62 sheaf::thread_mutex::
63 lock()
64 {
65 
66  // body:
67 
68  int rtncode = pthread_mutex_lock(&pthread_mutex);
69 
70  // postconditions:
71 
72  ensure(rtncode == 0);
73 }
74 
75 
77 void
78 sheaf::thread_mutex::
79 unlock()
80 {
81 
82  // body:
83 
84  int rtncode = pthread_mutex_unlock(&pthread_mutex);
85 
86  // postconditions:
87 
88  ensure(rtncode == 0);
89 
90 }
91 
92 
94 bool
95 sheaf::thread_mutex::
96 trylock()
97 {
98 
99  // body:
100 
101  int rtncode = pthread_mutex_trylock(&pthread_mutex);
102 
103  bool result;
104 
105  if (rtncode == EBUSY)
106  result = true;
107  else
108  result = false;
109 
110  return result;
111 }
112 
113 
115 pthread_mutex_t *
116 sheaf::thread_mutex::
117 get_pthread_mutex()
118 {
119 
120  return &pthread_mutex;
121 }
122 
123 #endif // _PTHREADS