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