Commit | Line | Data |
---|---|---|
f00ff009 LOK |
1 | #pragma once |
2 | /* | |
3 | * This file is part of the libCEC(R) library. | |
4 | * | |
16f47961 | 5 | * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved. |
f00ff009 LOK |
6 | * libCEC(R) is an original work, containing original code. |
7 | * | |
8 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
9 | * | |
10 | * This program is dual-licensed; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
23 | * | |
24 | * | |
25 | * Alternatively, you can license this library under a commercial license, | |
26 | * please contact Pulse-Eight Licensing for more information. | |
27 | * | |
28 | * For more information contact: | |
29 | * Pulse-Eight Licensing <license@pulse-eight.com> | |
30 | * http://www.pulse-eight.com/ | |
31 | * http://www.pulse-eight.net/ | |
32 | */ | |
33 | ||
34 | namespace PLATFORM | |
35 | { | |
36 | inline pthread_mutexattr_t *GetRecursiveMutexAttribute(void) | |
37 | { | |
38 | static pthread_mutexattr_t g_mutexAttr; | |
39 | static bool bAttributeInitialised = false; | |
40 | if (!bAttributeInitialised) | |
41 | { | |
42 | pthread_mutexattr_init(&g_mutexAttr); | |
43 | pthread_mutexattr_settype(&g_mutexAttr, PTHREAD_MUTEX_RECURSIVE); | |
44 | bAttributeInitialised = true; | |
45 | } | |
46 | return &g_mutexAttr; | |
47 | } | |
48 | ||
49 | inline struct timespec GetAbsTime(uint64_t iIncreaseBy = 0) | |
50 | { | |
1ea419fa | 51 | struct timespec now; |
28b0f9fb LOK |
52 | #ifdef __APPLE__ |
53 | struct timeval tv; | |
54 | gettimeofday(&tv, NULL); | |
1b469124 BL |
55 | now.tv_sec = tv.tv_sec; |
56 | now.tv_nsec = tv.tv_usec * 1000; | |
28b0f9fb | 57 | #else |
1ea419fa | 58 | clock_gettime(CLOCK_REALTIME, &now); |
28b0f9fb | 59 | #endif |
1ea419fa BL |
60 | now.tv_nsec += iIncreaseBy % 1000 * 1000000; |
61 | now.tv_sec += iIncreaseBy / 1000 + now.tv_nsec / 1000000000; | |
62 | now.tv_nsec %= 1000000000; | |
63 | return now; | |
f00ff009 LOK |
64 | } |
65 | ||
66 | typedef pthread_t thread_t; | |
622f7c33 | 67 | #define INVALID_THREAD_VALUE 0 |
f00ff009 LOK |
68 | |
69 | #define ThreadsCreate(thread, func, arg) (pthread_create(&thread, NULL, (void *(*) (void *))func, (void *)arg) == 0) | |
3a99ce7f | 70 | #define ThreadsWait(thread, retval) (thread ? pthread_join(thread, retval) == 0 : true) |
f00ff009 LOK |
71 | |
72 | typedef pthread_mutex_t mutex_t; | |
73 | #define MutexCreate(mutex) pthread_mutex_init(&mutex, GetRecursiveMutexAttribute()); | |
74 | #define MutexDelete(mutex) pthread_mutex_destroy(&mutex); | |
75 | #define MutexLock(mutex) (pthread_mutex_lock(&mutex) == 0) | |
76 | #define MutexTryLock(mutex) (pthread_mutex_trylock(&mutex) == 0) | |
77 | #define MutexUnlock(mutex) pthread_mutex_unlock(&mutex) | |
78 | ||
3a590d6a | 79 | class CConditionImpl |
f00ff009 | 80 | { |
3a590d6a LOK |
81 | public: |
82 | CConditionImpl(void) | |
f00ff009 | 83 | { |
3a590d6a | 84 | pthread_cond_init(&m_condition, NULL); |
f00ff009 | 85 | } |
3a590d6a LOK |
86 | |
87 | virtual ~CConditionImpl(void) | |
88 | { | |
89 | pthread_cond_destroy(&m_condition); | |
90 | } | |
91 | ||
92 | void Signal(void) | |
93 | { | |
94 | pthread_cond_signal(&m_condition); | |
95 | } | |
96 | ||
97 | void Broadcast(void) | |
98 | { | |
99 | pthread_cond_broadcast(&m_condition); | |
100 | } | |
101 | ||
960f33c6 | 102 | bool Wait(mutex_t &mutex) |
3a590d6a LOK |
103 | { |
104 | sched_yield(); | |
97cd34d8 | 105 | return (pthread_cond_wait(&m_condition, &mutex) == 0); |
3a590d6a LOK |
106 | } |
107 | ||
960f33c6 LOK |
108 | bool Wait(mutex_t &mutex, uint32_t iTimeoutMs) |
109 | { | |
110 | if (iTimeoutMs == 0) | |
111 | return Wait(mutex); | |
112 | ||
113 | sched_yield(); | |
114 | struct timespec timeout = GetAbsTime(iTimeoutMs); | |
115 | return (pthread_cond_timedwait(&m_condition, &mutex, &timeout) == 0); | |
116 | } | |
117 | ||
3a590d6a LOK |
118 | pthread_cond_t m_condition; |
119 | }; | |
f00ff009 | 120 | } |