also updated the copyright years in posix-only files
[deb_libcec.git] / src / lib / platform / posix / os-threads.h
CommitLineData
f00ff009
LOK
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
9b53a148 5 * libCEC(R) is Copyright (C) 2011-2012 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
34namespace 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 {
51 struct timespec abstime;
52 struct timeval now;
53 gettimeofday(&now, NULL);
54 iIncreaseBy += now.tv_usec / 1000;
55 abstime.tv_sec = now.tv_sec + (time_t)(iIncreaseBy / 1000);
56 abstime.tv_nsec = (int32_t)((iIncreaseBy % (uint32_t)1000) * (uint32_t)1000000);
57 return abstime;
58 }
59
60 typedef pthread_t thread_t;
61
62 #define ThreadsCreate(thread, func, arg) (pthread_create(&thread, NULL, (void *(*) (void *))func, (void *)arg) == 0)
63 #define ThreadsWait(thread, retval) (pthread_join(thread, retval) == 0)
64
65 typedef pthread_mutex_t mutex_t;
66 #define MutexCreate(mutex) pthread_mutex_init(&mutex, GetRecursiveMutexAttribute());
67 #define MutexDelete(mutex) pthread_mutex_destroy(&mutex);
68 #define MutexLock(mutex) (pthread_mutex_lock(&mutex) == 0)
69 #define MutexTryLock(mutex) (pthread_mutex_trylock(&mutex) == 0)
70 #define MutexUnlock(mutex) pthread_mutex_unlock(&mutex)
71
72 typedef pthread_cond_t condition_t;
73 #define ConditionCreate(cond) pthread_cond_init(&cond, NULL)
74 #define ConditionDelete(cond) pthread_cond_destroy(&cond)
75 #define ConditionSignal(cond) pthread_cond_signal(&cond)
76 #define ConditionBroadcast(cond) pthread_cond_broadcast(&cond)
77 inline bool ConditionWait(condition_t &cond, mutex_t &mutex, uint32_t iTimeout)
78 {
79 sched_yield();
80 if (iTimeout > 0)
81 {
82 struct timespec timeout = GetAbsTime(iTimeout);
83 return (pthread_cond_timedwait(&cond, &mutex, &timeout) == 0);
84 }
85 return (pthread_cond_wait(&cond, &mutex) == 0);
86 }
87}