1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_BASE_C_INTERNAL_PLATFORM_H_
28 #define EMBB_BASE_C_INTERNAL_PLATFORM_H_
29 
30 #include <embb/base/c/internal/config.h>
31 #include <limits.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define EMBB_TIME_MAX_SECONDS ULLONG_MAX
38 #define EMBB_DURATION_MAX_SECONDS 60 * 60 * 24 * 7
39 
40 #ifdef EMBB_PLATFORM_THREADING_WINTHREADS
41 
42 #ifdef EMBB_PLATFORM_COMPILER_MSVC
43 // Suppress virtual functions but non-virtual constructor warning
44 // in windows headers
45 #pragma warning(push)
46 #pragma warning(disable : 4265)
47 #endif
48 
49 #define NOMINMAX
50 #include <windows.h>
51 
52 #ifdef EMBB_PLATFORM_COMPILER_MSVC
53 #pragma warning(pop) // Reset warning 4640
54 #endif
55 
56 struct embb_internal_thread_arg_t;
57 
58 /**
59  * Opaque handle for a thread.
60  */
61 typedef struct embb_thread_t {
62   HANDLE embb_internal_handle;
63   struct embb_internal_thread_arg_t* embb_internal_arg;
64 } embb_thread_t;
65 
66 typedef DWORD embb_thread_id_t;
67 typedef CRITICAL_SECTION embb_mutex_t;
68 typedef CONDITION_VARIABLE embb_condition_t;
69 
70 #define EMBB_DURATION_MIN_NANOSECONDS 1000
71 
72 #define EMBB_THREAD_SPECIFIC static __declspec(thread)
73 
74 #elif defined EMBB_PLATFORM_THREADING_POSIXTHREADS
75 
76 #include <pthread.h>
77 #include <errno.h>
78 #include <time.h>
79 
80 struct embb_internal_thread_arg_t;
81 
82 /**
83  * Opaque handle for a thread.
84  */
85 typedef struct embb_thread_t {
86   pthread_t embb_internal_handle;
87   struct embb_internal_thread_arg_t* embb_internal_arg;
88 } embb_thread_t;
89 
90 typedef pthread_t embb_thread_id_t;
91 typedef pthread_mutex_t embb_mutex_t;
92 typedef pthread_cond_t embb_condition_t;
93 
94 #define EMBB_DURATION_MIN_NANOSECONDS 1
95 
96 #define EMBB_THREAD_SPECIFIC __thread
97 
98 #else /* EMBB_PLATFORM_THREADING_POSIXTHREADS */
99 
100 #error "No threading platform defined!"
101 
102 #endif /* else */
103 
104 #ifdef __cplusplus
105 } /* Close extern "C" { */
106 #endif
107 
108 #endif  // EMBB_BASE_C_INTERNAL_PLATFORM_H_
109