2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __MSG_MUTEX_H__
18 #define __MSG_MUTEX_H__
20 /*==================================================================================================
22 ==================================================================================================*/
31 void WaitMsgReady(int sec);
37 pthread_mutexattr_t mattr;
38 pthread_mutexattr_init(&mattr);
39 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
40 pthread_mutex_init(&m, &mattr);
41 pthread_mutexattr_destroy(&mattr);
43 ~MsgMutex() { pthread_mutex_destroy(&m); }
44 void lock() { pthread_mutex_lock(&m); }
47 struct timespec abs_time;
48 clock_gettime(CLOCK_REALTIME, &abs_time);
50 return pthread_mutex_timedlock(&m, &abs_time);
53 void unlock() { pthread_mutex_unlock(&m); }
54 pthread_mutex_t* pMsgMutex() { return &m; }
63 MsgMutexLocker(MsgMutex& mx) {
79 MsgCndVar() { pthread_cond_init(&c, 0); }
80 ~MsgCndVar() { pthread_cond_destroy(&c); }
81 void wait(pthread_mutex_t* m) { pthread_cond_wait(&c, m); }
82 int timedwait(pthread_mutex_t* m, int sec) {
83 struct timeval now = {0};
84 struct timespec timeout = {0};
85 gettimeofday(&now, NULL);
86 timeout.tv_sec = now.tv_sec+sec;
87 timeout.tv_nsec = now.tv_usec;
88 int retcode = pthread_cond_timedwait(&c, m, &timeout);
91 void signal() { pthread_cond_signal(&c); }
92 void broadcast() { pthread_cond_broadcast(&c); }
98 #endif /* __MUTEX_H__ */