BRL-CAD
tc.h
Go to the documentation of this file.
1 /************************************************************************/
2 /* Bits from tinycthread for portable threads
3  *
4  * Copyright (c) 2012 Marcus Geelnard
5  * Copyright (c) 2013-2016 Evan Nemerson
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any damages
9  * arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute it
13  * freely, subject to the following restrictions:
14 
15  * 1. The origin of this software must not be misrepresented; you must not
16  * claim that you wrote the original software. If you use this software
17  * in a product, an acknowledgment in the product documentation would be
18  * appreciated but is not required.
19  *
20  * 2. Altered source versions must be plainly marked as such, and must not be
21  * misrepresented as being the original software.
22  *
23  * 3. This notice may not be removed or altered from any source
24  * distribution.
25  */
26 
27 /* Note - this is a temporary measure for portable compilation of some BRL-CAD
28  * components, and will likely be replaced by standards and/or other API at
29  * some future date.
30  *
31  * This header should be considered as INTERNAL ONLY - not public API. */
32 
33 #ifndef _BU_TCM_H
34 #define _BU_TCM_H
35 
36 #include "common.h"
37 
38 #if !defined(BRLCADBUILD)
39 # error "Warning: included bu/tc.h (compile-time API) without BRLCADBUILD defined"
40 #endif
41 #if !defined(HAVE_CONFIG_H)
42 # error "Warning: included bu/tc.h (compile-time API) without HAVE_CONFIG_H defined"
43 #endif
44 
45 #include "bu/defines.h"
46 
47 #include "bio.h" /* For windows.h */
48 
49 __BEGIN_DECLS
50 
51 #if defined(HAVE_PTHREAD_H)
52 # include <pthread.h>
53 #endif
54 #if defined(HAVE_WINDOWS_H)
55 # include <process.h>
56 # include <sys/timeb.h>
57 #endif
58 
59 #define bu_thrd_error 0 /**< The requested operation failed */
60 #define bu_thrd_success 1 /**< The requested operation succeeded */
61 #define bu_thrd_timedout 2 /**< The time specified in the call was reached without acquiring the requested resource */
62 #define bu_thrd_busy 3 /**< The requested operation failed because a tesource requested by a test and return function is already in use */
63 #define bu_thrd_nomem 4 /**< The requested operation failed because it was unable to allocate memory */
64 
65 typedef int (*bu_thrd_start_t)(void *arg);
66 #if defined(HAVE_WINDOWS_H)
67 typedef HANDLE bu_thrd_t;
68 #else
69 typedef pthread_t bu_thrd_t;
70 #endif
71 
72 /* Mutex */
73 #if defined(HAVE_WINDOWS_H)
74 typedef struct {
75  union {
76  CRITICAL_SECTION cs; /* Critical section handle (used for non-timed mutexes) */
77  HANDLE mut; /* Mutex handle (used for timed mutex) */
78  } mHandle; /* Mutex handle */
79  int mAlreadyLocked; /* TRUE if the mutex is already locked */
80  int mRecursive; /* TRUE if the mutex is recursive */
81  int mTimed; /* TRUE if the mutex is timed */
82 } bu_mtx_t;
83 #else
84 typedef pthread_mutex_t bu_mtx_t;
85 #endif
86 
87 /* Condition variable */
88 #if defined(HAVE_WINDOWS_H)
89 typedef struct {
90  HANDLE mEvents[2]; /* Signal and broadcast event HANDLEs. */
91  unsigned int mWaitersCount; /* Count of the number of waiters. */
92  CRITICAL_SECTION mWaitersCountLock; /* Serialize access to mWaitersCount. */
93 } bu_cnd_t;
94 #else
95 typedef pthread_cond_t bu_cnd_t;
96 #endif
97 
98 BU_EXPORT extern int bu_thrd_create(bu_thrd_t *thr, bu_thrd_start_t func, void *arg);
99 BU_EXPORT extern int bu_thrd_join(bu_thrd_t thr, int *res);
100 BU_EXPORT extern int bu_mtx_init(bu_mtx_t *mtx);
101 BU_EXPORT extern int bu_mtx_lock(bu_mtx_t *mtx);
102 BU_EXPORT extern int bu_mtx_unlock(bu_mtx_t *mtx);
103 BU_EXPORT extern void bu_mtx_destroy(bu_mtx_t *mtx);
104 BU_EXPORT extern int bu_cnd_init(bu_cnd_t *cond);
105 BU_EXPORT extern void bu_cnd_destroy(bu_cnd_t *cond);
106 BU_EXPORT extern int bu_cnd_wait(bu_cnd_t *cond, bu_mtx_t *mtx);
107 BU_EXPORT extern int bu_cnd_broadcast(bu_cnd_t *cond);
108 BU_EXPORT extern int bu_cnd_signal(bu_cnd_t *cond);
109 BU_EXPORT extern int bu_mtx_trylock(bu_mtx_t *mtx);
110 
111 
112 __END_DECLS
113 
114 #endif /* _BU_TCM_H */
115 
116 /************************************************************************/
117 /*
118  * Local Variables:
119  * tab-width: 8
120  * mode: C
121  * indent-tabs-mode: t
122  * c-file-style: "stroustrup"
123  * End:
124  * ex: shiftwidth=4 tabstop=8
125  */
Header file for the BRL-CAD common definitions.
int bu_cnd_wait(bu_cnd_t *cond, bu_mtx_t *mtx)
int bu_cnd_init(bu_cnd_t *cond)
pthread_cond_t bu_cnd_t
Definition: tc.h:95
void bu_mtx_destroy(bu_mtx_t *mtx)
int bu_cnd_signal(bu_cnd_t *cond)
int bu_cnd_broadcast(bu_cnd_t *cond)
int bu_mtx_lock(bu_mtx_t *mtx)
int(* bu_thrd_start_t)(void *arg)
Definition: tc.h:65
int bu_mtx_init(bu_mtx_t *mtx)
int bu_mtx_unlock(bu_mtx_t *mtx)
int bu_mtx_trylock(bu_mtx_t *mtx)
int bu_thrd_join(bu_thrd_t thr, int *res)
void bu_cnd_destroy(bu_cnd_t *cond)
pthread_t bu_thrd_t
Definition: tc.h:69
int bu_thrd_create(bu_thrd_t *thr, bu_thrd_start_t func, void *arg)
pthread_mutex_t bu_mtx_t
Definition: tc.h:84