annotate liboffloadmic/runtime/offload_util.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /*
kono
parents:
diff changeset
2 Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
5 modification, are permitted provided that the following conditions
kono
parents:
diff changeset
6 are met:
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 * Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
9 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
10 * Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
11 notice, this list of conditions and the following disclaimer in the
kono
parents:
diff changeset
12 documentation and/or other materials provided with the distribution.
kono
parents:
diff changeset
13 * Neither the name of Intel Corporation nor the names of its
kono
parents:
diff changeset
14 contributors may be used to endorse or promote products derived
kono
parents:
diff changeset
15 from this software without specific prior written permission.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
kono
parents:
diff changeset
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
kono
parents:
diff changeset
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
kono
parents:
diff changeset
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
kono
parents:
diff changeset
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
kono
parents:
diff changeset
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
kono
parents:
diff changeset
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
kono
parents:
diff changeset
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
kono
parents:
diff changeset
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
kono
parents:
diff changeset
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
kono
parents:
diff changeset
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kono
parents:
diff changeset
28 */
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #ifndef OFFLOAD_UTIL_H_INCLUDED
kono
parents:
diff changeset
32 #define OFFLOAD_UTIL_H_INCLUDED
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 #include <stdlib.h>
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 #ifdef TARGET_WINNT
kono
parents:
diff changeset
37 #define DLL_LOCAL
kono
parents:
diff changeset
38 #else
kono
parents:
diff changeset
39 #define DLL_LOCAL __attribute__((visibility("hidden")))
kono
parents:
diff changeset
40 #endif
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 #ifdef TARGET_WINNT
kono
parents:
diff changeset
43 // Don't use <stdint.h> as compiling with VS2010 makes ofldbegin.obj
kono
parents:
diff changeset
44 // incompatible with STL library of versions older than VS2010.
kono
parents:
diff changeset
45 typedef unsigned long long int uint64_t;
kono
parents:
diff changeset
46 typedef signed long long int int64_t;
kono
parents:
diff changeset
47 #include <windows.h>
kono
parents:
diff changeset
48 #include <process.h>
kono
parents:
diff changeset
49 #else // TARGET_WINNT
kono
parents:
diff changeset
50 #include <stdint.h>
kono
parents:
diff changeset
51 #include <dlfcn.h>
kono
parents:
diff changeset
52 #include <pthread.h>
kono
parents:
diff changeset
53 #endif // TARGET_WINNT
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 #ifdef TARGET_WINNT
kono
parents:
diff changeset
56 typedef unsigned pthread_key_t;
kono
parents:
diff changeset
57 typedef int pid_t;
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 #define __func__ __FUNCTION__
kono
parents:
diff changeset
60 #define strtok_r(s,d,p) strtok_s(s,d,p)
kono
parents:
diff changeset
61 #define strcasecmp(a,b) stricmp(a,b)
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 #define thread_key_create(key, destructor) \
kono
parents:
diff changeset
64 (((*key = TlsAlloc()) > 0) ? 0 : GetLastError())
kono
parents:
diff changeset
65 #define thread_key_delete(key) TlsFree(key)
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 #ifndef S_ISREG
kono
parents:
diff changeset
68 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
kono
parents:
diff changeset
69 #endif
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 void* thread_getspecific(pthread_key_t key);
kono
parents:
diff changeset
72 int thread_setspecific(pthread_key_t key, const void *value);
kono
parents:
diff changeset
73 #else
kono
parents:
diff changeset
74 #define thread_key_create(key, destructor) \
kono
parents:
diff changeset
75 pthread_key_create((key), (destructor))
kono
parents:
diff changeset
76 #define thread_key_delete(key) pthread_key_delete(key)
kono
parents:
diff changeset
77 #define thread_getspecific(key) pthread_getspecific(key)
kono
parents:
diff changeset
78 #define thread_setspecific(key, value) pthread_setspecific(key, value)
kono
parents:
diff changeset
79 #endif // TARGET_WINNT
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 // Mutex implementation
kono
parents:
diff changeset
82 struct mutex_t {
kono
parents:
diff changeset
83 mutex_t() {
kono
parents:
diff changeset
84 #ifdef TARGET_WINNT
kono
parents:
diff changeset
85 InitializeCriticalSection(&m_lock);
kono
parents:
diff changeset
86 #else // TARGET_WINNT
kono
parents:
diff changeset
87 pthread_mutex_init(&m_lock, 0);
kono
parents:
diff changeset
88 #endif // TARGET_WINNT
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 ~mutex_t() {
kono
parents:
diff changeset
92 #ifdef TARGET_WINNT
kono
parents:
diff changeset
93 DeleteCriticalSection(&m_lock);
kono
parents:
diff changeset
94 #else // TARGET_WINNT
kono
parents:
diff changeset
95 pthread_mutex_destroy(&m_lock);
kono
parents:
diff changeset
96 #endif // TARGET_WINNT
kono
parents:
diff changeset
97 }
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 void lock() {
kono
parents:
diff changeset
100 #ifdef TARGET_WINNT
kono
parents:
diff changeset
101 EnterCriticalSection(&m_lock);
kono
parents:
diff changeset
102 #else // TARGET_WINNT
kono
parents:
diff changeset
103 pthread_mutex_lock(&m_lock);
kono
parents:
diff changeset
104 #endif // TARGET_WINNT
kono
parents:
diff changeset
105 }
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 void unlock() {
kono
parents:
diff changeset
108 #ifdef TARGET_WINNT
kono
parents:
diff changeset
109 LeaveCriticalSection(&m_lock);
kono
parents:
diff changeset
110 #else // TARGET_WINNT
kono
parents:
diff changeset
111 pthread_mutex_unlock(&m_lock);
kono
parents:
diff changeset
112 #endif // TARGET_WINNT
kono
parents:
diff changeset
113 }
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 private:
kono
parents:
diff changeset
116 #ifdef TARGET_WINNT
kono
parents:
diff changeset
117 CRITICAL_SECTION m_lock;
kono
parents:
diff changeset
118 #else
kono
parents:
diff changeset
119 pthread_mutex_t m_lock;
kono
parents:
diff changeset
120 #endif
kono
parents:
diff changeset
121 };
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 struct mutex_locker_t {
kono
parents:
diff changeset
124 mutex_locker_t(mutex_t &mutex) : m_mutex(mutex) {
kono
parents:
diff changeset
125 m_mutex.lock();
kono
parents:
diff changeset
126 }
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 ~mutex_locker_t() {
kono
parents:
diff changeset
129 m_mutex.unlock();
kono
parents:
diff changeset
130 }
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 private:
kono
parents:
diff changeset
133 mutex_t &m_mutex;
kono
parents:
diff changeset
134 };
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 // Dynamic loader interface
kono
parents:
diff changeset
137 #ifdef TARGET_WINNT
kono
parents:
diff changeset
138 struct Dl_info
kono
parents:
diff changeset
139 {
kono
parents:
diff changeset
140 char dli_fname[MAX_PATH];
kono
parents:
diff changeset
141 void *dli_fbase;
kono
parents:
diff changeset
142 char dli_sname[MAX_PATH];
kono
parents:
diff changeset
143 const void *dli_saddr;
kono
parents:
diff changeset
144 };
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 void* DL_open(const char *path);
kono
parents:
diff changeset
147 #define DL_close(handle) FreeLibrary((HMODULE) (handle))
kono
parents:
diff changeset
148 int DL_addr(const void *addr, Dl_info *info);
kono
parents:
diff changeset
149 #else
kono
parents:
diff changeset
150 #define DL_open(path) dlopen((path), RTLD_NOW)
kono
parents:
diff changeset
151 #define DL_close(handle) dlclose(handle)
kono
parents:
diff changeset
152 #define DL_addr(addr, info) dladdr((addr), (info))
kono
parents:
diff changeset
153 #endif // TARGET_WINNT
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 DLL_LOCAL extern void* DL_sym(void *handle, const char *name, const char *version);
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 // One-time initialization API
kono
parents:
diff changeset
158 #ifdef TARGET_WINNT
kono
parents:
diff changeset
159 typedef INIT_ONCE OffloadOnceControl;
kono
parents:
diff changeset
160 #define OFFLOAD_ONCE_CONTROL_INIT INIT_ONCE_STATIC_INIT
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 extern void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void));
kono
parents:
diff changeset
163 #else
kono
parents:
diff changeset
164 typedef pthread_once_t OffloadOnceControl;
kono
parents:
diff changeset
165 #define OFFLOAD_ONCE_CONTROL_INIT PTHREAD_ONCE_INIT
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 #define __offload_run_once(ctrl, func) pthread_once(ctrl, func)
kono
parents:
diff changeset
168 #endif // TARGET_WINNT
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 // Parses size specification string.
kono
parents:
diff changeset
171 DLL_LOCAL extern bool __offload_parse_size_string(const char *str, uint64_t &new_size);
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 // Parses string with integer value
kono
parents:
diff changeset
174 DLL_LOCAL extern bool __offload_parse_int_string(const char *str, int64_t &value);
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 // get value by its base, offset and size
kono
parents:
diff changeset
177 DLL_LOCAL int64_t get_el_value(
kono
parents:
diff changeset
178 char *base,
kono
parents:
diff changeset
179 int64_t offset,
kono
parents:
diff changeset
180 int64_t size
kono
parents:
diff changeset
181 );
kono
parents:
diff changeset
182 #endif // OFFLOAD_UTIL_H_INCLUDED