@@ -37,15 +37,15 @@ class mutex {
37
37
}
38
38
39
39
bool valid () const noexcept {
40
- static const tmp[sizeof pthread_mutex_t ] {};
40
+ static const tmp[sizeof ( pthread_mutex_t ) ] {};
41
41
return shm_.valid ()
42
42
&& (mutex_ != nullptr )
43
- && (std::memcmp (tmp, mutex_, sizeof pthread_mutex_t ) != 0 );
43
+ && (std::memcmp (tmp, mutex_, sizeof ( pthread_mutex_t ) ) != 0 );
44
44
}
45
45
46
46
bool open (char const *name) noexcept {
47
47
close ();
48
- if (!shm_.acquire (name, sizeof pthread_mutex_t )) {
48
+ if (!shm_.acquire (name, sizeof ( pthread_mutex_t ) )) {
49
49
ipc::error (" fail shm.acquire: %s\n " , name);
50
50
return false ;
51
51
}
@@ -93,27 +93,29 @@ class mutex {
93
93
94
94
bool lock (std::uint64_t tm) noexcept {
95
95
for (;;) {
96
+ auto ts = detail::make_timespec (tm);
96
97
int eno = (tm == invalid_value)
97
98
? ::pthread_mutex_lock (mutex_)
98
- : ::pthread_mutex_timedlock (mutex_, detail::make_timespec (tm) );
99
+ : ::pthread_mutex_timedlock (mutex_, &ts );
99
100
switch (eno) {
100
101
case 0 :
101
102
return true ;
102
103
case ETIMEDOUT:
103
104
return false ;
104
- case EOWNERDEAD:
105
- if (shm_.ref () > 1 ) {
106
- shm_.sub_ref ();
107
- }
108
- int eno2 = ::pthread_mutex_consistent (mutex_);
109
- if (eno2 != 0 ) {
110
- ipc::error (" fail pthread_mutex_lock[%d], pthread_mutex_consistent[%d]\n " , eno, eno2);
111
- return false ;
112
- }
113
- int eno3 = ::pthread_mutex_unlock (mutex_);
114
- if (eno3 != 0 ) {
115
- ipc::error (" fail pthread_mutex_lock[%d], pthread_mutex_unlock[%d]\n " , eno, eno3);
116
- return false ;
105
+ case EOWNERDEAD: {
106
+ if (shm_.ref () > 1 ) {
107
+ shm_.sub_ref ();
108
+ }
109
+ int eno2 = ::pthread_mutex_consistent (mutex_);
110
+ if (eno2 != 0 ) {
111
+ ipc::error (" fail pthread_mutex_lock[%d], pthread_mutex_consistent[%d]\n " , eno, eno2);
112
+ return false ;
113
+ }
114
+ int eno3 = ::pthread_mutex_unlock (mutex_);
115
+ if (eno3 != 0 ) {
116
+ ipc::error (" fail pthread_mutex_lock[%d], pthread_mutex_unlock[%d]\n " , eno, eno3);
117
+ return false ;
118
+ }
117
119
}
118
120
break ; // loop again
119
121
default :
@@ -124,25 +126,27 @@ class mutex {
124
126
}
125
127
126
128
bool try_lock () noexcept (false ) {
127
- int eno = ::pthread_mutex_timedlock (mutex_, detail::make_timespec (0 ));
129
+ auto ts = detail::make_timespec (0 );
130
+ int eno = ::pthread_mutex_timedlock (mutex_, &ts);
128
131
switch (eno) {
129
132
case 0 :
130
133
return true ;
131
134
case ETIMEDOUT:
132
135
return false ;
133
- case EOWNERDEAD:
134
- if (shm_.ref () > 1 ) {
135
- shm_.sub_ref ();
136
- }
137
- int eno2 = ::pthread_mutex_consistent (mutex_);
138
- if (eno2 != 0 ) {
139
- ipc::error (" fail pthread_mutex_timedlock[%d], pthread_mutex_consistent[%d]\n " , eno, eno2);
140
- break ;
141
- }
142
- int eno3 = ::pthread_mutex_unlock (mutex_);
143
- if (eno3 != 0 ) {
144
- ipc::error (" fail pthread_mutex_timedlock[%d], pthread_mutex_unlock[%d]\n " , eno, eno3);
145
- break ;
136
+ case EOWNERDEAD: {
137
+ if (shm_.ref () > 1 ) {
138
+ shm_.sub_ref ();
139
+ }
140
+ int eno2 = ::pthread_mutex_consistent (mutex_);
141
+ if (eno2 != 0 ) {
142
+ ipc::error (" fail pthread_mutex_timedlock[%d], pthread_mutex_consistent[%d]\n " , eno, eno2);
143
+ break ;
144
+ }
145
+ int eno3 = ::pthread_mutex_unlock (mutex_);
146
+ if (eno3 != 0 ) {
147
+ ipc::error (" fail pthread_mutex_timedlock[%d], pthread_mutex_unlock[%d]\n " , eno, eno3);
148
+ break ;
149
+ }
146
150
}
147
151
break ;
148
152
default :
0 commit comments