Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
cppthread.h
Go to the documentation of this file.
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library.
2  Copyright (C) 2013 Jonathan Pelletier, New Paradigm Software
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
25 #ifndef MCR_UTIL_CPPTHREAD_H_
26 #define MCR_UTIL_CPPTHREAD_H_
27 
28 #include "mcr/globals.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
35 MCR_API int thrd_sleep_until(struct tm *time_point);
36 
37 /* */
38 /* Temporary solution for C11 */
39 /* */
40 #ifndef _Noreturn
41 #define _Noreturn
42 #endif
43 
44 /* Error defining key word restrict. __restrict workaround. */
45 #ifndef __restrict
46 #ifdef restrict
47 #define __restrict restrict
48 #else
49 #define __restrict
50 #endif
51 #endif
52 
53 /* */
54 /* enums */
55 /* */
56 enum {
57  thrd_success = 0,
58  thrd_nomem,
59  thrd_timedout,
60  thrd_busy,
61  thrd_error
62 };
63 enum {
64  mtx_plain = 0,
65  mtx_recursive = 1 << 0,
66  mtx_timed = 1 << 1
67 };
68 
69 /* */
70 /* types */
71 /* */
72 typedef int once_flag;
73 
74 typedef int (*thrd_start_t)(void *);
75 
76 /* */
77 /* Temporary solution. Until threading is implemented in C we have */
78 /* to use C++. */
79 /* */
80 /* Handles to dynamically allocated C++ objects. ( Make sure to */
81 /* destruct on exit. ) */
82 typedef void *thrd_t;
83 typedef struct {
84  void *mtx;
85  int type;
86 } mtx_t;
87 typedef void *cnd_t;
88 
89 /* */
90 /* C standard thread. */
91 /* */
92 MCR_API int thrd_create(thrd_t * thr, thrd_start_t func, void *arg);
93 MCR_API int thrd_equal(thrd_t lhs, thrd_t rhs);
94 MCR_API thrd_t thrd_current();
95 MCR_API int thrd_sleep(const struct timespec *time_point,
96  struct timespec *remaining);
97 MCR_API void thrd_yield();
98 _Noreturn void thrd_exit(int res);
99 MCR_API int thrd_detach(thrd_t thr);
100 MCR_API int thrd_join(thrd_t thr, int *res);
101 MCR_API void mcr_thrd_delete(thrd_t * thr);
102 
103 /* */
104 /* mutex */
105 /* */
106 MCR_API int mtx_init(mtx_t * mutex, int type);
107 MCR_API int mtx_lock(mtx_t * mutex);
108 MCR_API int mtx_timedlock(mtx_t * __restrict mutex,
109  const struct timespec *__restrict time_point);
110 MCR_API int mtx_trylock(mtx_t * mutex);
111 MCR_API int mtx_unlock(mtx_t * mutex);
112 MCR_API void mtx_destroy(mtx_t * mutex);
113 
114 /* */
115 /* condition */
116 /* */
117 MCR_API int cnd_init(cnd_t * cond);
118 MCR_API int cnd_signal(cnd_t * cond);
119 MCR_API int cnd_broadcast(cnd_t * cond);
121 MCR_API int cnd_wait(cnd_t * cond, mtx_t * mutex);
123 MCR_API int cnd_timedwait(cnd_t * __restrict cond, mtx_t * __restrict mutex,
124  const struct timespec *__restrict time_point);
125 MCR_API void cnd_destroy(cnd_t * cond);
126 
127 /* */
128 /* Do not know how to do thread local storage */
129 /* */
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif
MCR_API int cnd_timedwait(cnd_t *cond, mtx_t *mutex, const struct timespec *time_point)
MCR_API int cnd_wait(cnd_t *cond, mtx_t *mutex)
MCR_API int thrd_sleep_until(struct tm *time_point)