1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
25 #include <sal/log.hxx>
26 #include <sal/types.h>
28 #include <osl/conditn.h>
32 typedef struct _oslConditionImpl
34 pthread_cond_t m_Condition
;
35 pthread_mutex_t m_Lock
;
40 oslCondition SAL_CALL
osl_createCondition()
42 oslConditionImpl
* pCond
;
45 pCond
= (oslConditionImpl
*) malloc(sizeof(oslConditionImpl
));
49 SAL_WARN("sal.osl", "std::bad_alloc in C");
53 pCond
->m_State
= sal_False
;
55 /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
56 nRet
= pthread_cond_init(&pCond
->m_Condition
, PTHREAD_CONDATTR_DEFAULT
);
61 "pthread_cond_init failed, errno " << nRet
<< ", \""
62 << strerror(nRet
) << '"');
68 nRet
= pthread_mutex_init(&pCond
->m_Lock
, PTHREAD_MUTEXATTR_DEFAULT
);
73 "pthread_mutex_init failed, errno " << nRet
<< ", \""
74 << strerror(nRet
) << '"');
76 nRet
= pthread_cond_destroy(&pCond
->m_Condition
);
79 "pthread_cond_destroy failed, errno " << nRet
<< ", \""
80 << strerror(nRet
) << '"');
86 return (oslCondition
)pCond
;
89 void SAL_CALL
osl_destroyCondition(oslCondition Condition
)
91 oslConditionImpl
* pCond
;
96 pCond
= (oslConditionImpl
*)Condition
;
98 nRet
= pthread_cond_destroy(&pCond
->m_Condition
);
100 nRet
!= 0, "sal.osl",
101 "pthread_cond_destroy failed, errno " << nRet
<< ", \""
102 << strerror(nRet
) << '"');
103 nRet
= pthread_mutex_destroy(&pCond
->m_Lock
);
105 nRet
!= 0, "sal.osl",
106 "pthread_mutex_destroy failed, errno " << nRet
<< ", \""
107 << strerror(nRet
) << '"');
115 sal_Bool SAL_CALL
osl_setCondition(oslCondition Condition
)
117 oslConditionImpl
* pCond
;
121 pCond
= (oslConditionImpl
*)Condition
;
128 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
133 "pthread_mutex_lock failed, errno " << nRet
<< ", \""
134 << strerror(nRet
) << '"');
138 pCond
->m_State
= sal_True
;
139 nRet
= pthread_cond_broadcast(&pCond
->m_Condition
);
144 "pthread_cond_broadcast failed, errno " << nRet
<< ", \""
145 << strerror(nRet
) << '"');
146 // try to unlock the mutex
147 pthread_mutex_unlock(&pCond
->m_Lock
);
151 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
156 "pthread_mutex_unlock failed, errno " << nRet
<< ", \""
157 << strerror(nRet
) << '"');
165 sal_Bool SAL_CALL
osl_resetCondition(oslCondition Condition
)
167 oslConditionImpl
* pCond
;
172 pCond
= (oslConditionImpl
*)Condition
;
179 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
184 "pthread_mutex_lock failed, errno " << nRet
<< ", \""
185 << strerror(nRet
) << '"');
189 pCond
->m_State
= sal_False
;
191 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
195 "sal.osl", "pthread_mutex_unlock failed, errno " << nRet
<<", \""
196 << strerror(nRet
) << '"');
203 oslConditionResult SAL_CALL
osl_waitCondition(oslCondition Condition
, const TimeValue
* pTimeout
)
205 oslConditionImpl
* pCond
;
207 oslConditionResult Result
= osl_cond_result_ok
;
210 pCond
= (oslConditionImpl
*)Condition
;
214 return osl_cond_result_error
;
217 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
221 "sal.osl", "pthread_mutex_lock failed, errno " << nRet
<<", \""
222 << strerror(nRet
) << '"');
223 return osl_cond_result_error
;
228 if ( ! pCond
->m_State
)
234 gettimeofday(&tp
, NULL
);
236 SET_TIMESPEC( to
, tp
.tv_sec
+ pTimeout
->Seconds
,
237 tp
.tv_usec
* 1000 + pTimeout
->Nanosec
);
239 /* spurious wake up prevention */
242 ret
= pthread_cond_timedwait(&pCond
->m_Condition
, &pCond
->m_Lock
, &to
);
245 if ( ret
== ETIME
|| ret
== ETIMEDOUT
)
247 Result
= osl_cond_result_timeout
;
248 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
250 nRet
!= 0, "sal.osl",
251 "pthread_mutex_unlock failed, errno " << nRet
252 << ", \"" << strerror(nRet
) << '"');
256 else if ( ret
!= EINTR
)
258 Result
= osl_cond_result_error
;
259 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
261 nRet
!= 0, "sal.osl",
262 "pthread_mutex_unlock failed, errno " << nRet
263 << ", \"" << strerror(nRet
) << '"');
268 while ( !pCond
->m_State
);
273 while ( !pCond
->m_State
)
275 nRet
= pthread_cond_wait(&pCond
->m_Condition
, &pCond
->m_Lock
);
280 "pthread_cond_wait failed, errno " << nRet
<< ", \""
281 << strerror(nRet
) << '"');
282 Result
= osl_cond_result_error
;
283 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
285 nRet
!= 0, "sal.osl",
286 "pthread_mutex_unlock failed, errno " << nRet
<< ", \""
287 << strerror(nRet
) << '"');
294 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
296 nRet
!= 0, "sal.osl",
297 "pthread_mutex_unlock failed, errno " << nRet
<< ", \""
298 << strerror(nRet
) << '"');
303 sal_Bool SAL_CALL
osl_checkCondition(oslCondition Condition
)
306 oslConditionImpl
* pCond
;
310 pCond
= (oslConditionImpl
*)Condition
;
317 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
319 nRet
!= 0, "sal.osl",
320 "pthread_mutex_lock failed, errno " << nRet
<< ", \"" << strerror(nRet
)
323 State
= pCond
->m_State
;
325 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
327 nRet
!= 0, "sal.osl",
328 "pthread_mutex_unlock failed, errno " << nRet
<< ", \""
329 << strerror(nRet
) << '"');
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */