2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / liboffloadmic / runtime / offload_util.h
blob2cffd82f70a458a9254af06deb1009594c18d9ef
1 /*
2 Copyright (c) 2014 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 <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
38 #ifdef TARGET_WINNT
39 #include <windows.h>
40 #include <process.h>
41 #else // TARGET_WINNT
42 #include <dlfcn.h>
43 #include <pthread.h>
44 #endif // TARGET_WINNT
46 #ifdef TARGET_WINNT
47 typedef unsigned pthread_key_t;
48 typedef int pid_t;
50 #define __func__ __FUNCTION__
51 #define strtok_r(s,d,p) strtok_s(s,d,p)
52 #define strcasecmp(a,b) stricmp(a,b)
54 #define thread_key_create(key, destructor) \
55 (((*key = TlsAlloc()) > 0) ? 0 : GetLastError())
56 #define thread_key_delete(key) TlsFree(key)
58 #ifndef S_ISREG
59 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
60 #endif
62 void* thread_getspecific(pthread_key_t key);
63 int thread_setspecific(pthread_key_t key, const void *value);
64 #else
65 #define thread_key_create(key, destructor) \
66 pthread_key_create((key), (destructor))
67 #define thread_key_delete(key) pthread_key_delete(key)
68 #define thread_getspecific(key) pthread_getspecific(key)
69 #define thread_setspecific(key, value) pthread_setspecific(key, value)
70 #endif // TARGET_WINNT
72 // Mutex implementation
73 struct mutex_t {
74 mutex_t() {
75 #ifdef TARGET_WINNT
76 InitializeCriticalSection(&m_lock);
77 #else // TARGET_WINNT
78 pthread_mutex_init(&m_lock, 0);
79 #endif // TARGET_WINNT
82 ~mutex_t() {
83 #ifdef TARGET_WINNT
84 DeleteCriticalSection(&m_lock);
85 #else // TARGET_WINNT
86 pthread_mutex_destroy(&m_lock);
87 #endif // TARGET_WINNT
90 void lock() {
91 #ifdef TARGET_WINNT
92 EnterCriticalSection(&m_lock);
93 #else // TARGET_WINNT
94 pthread_mutex_lock(&m_lock);
95 #endif // TARGET_WINNT
98 void unlock() {
99 #ifdef TARGET_WINNT
100 LeaveCriticalSection(&m_lock);
101 #else // TARGET_WINNT
102 pthread_mutex_unlock(&m_lock);
103 #endif // TARGET_WINNT
106 private:
107 #ifdef TARGET_WINNT
108 CRITICAL_SECTION m_lock;
109 #else
110 pthread_mutex_t m_lock;
111 #endif
114 struct mutex_locker_t {
115 mutex_locker_t(mutex_t &mutex) : m_mutex(mutex) {
116 m_mutex.lock();
119 ~mutex_locker_t() {
120 m_mutex.unlock();
123 private:
124 mutex_t &m_mutex;
127 // Dynamic loader interface
128 #ifdef TARGET_WINNT
129 struct Dl_info
131 char dli_fname[MAX_PATH];
132 void *dli_fbase;
133 char dli_sname[MAX_PATH];
134 const void *dli_saddr;
137 void* DL_open(const char *path);
138 #define DL_close(handle) FreeLibrary((HMODULE) (handle))
139 int DL_addr(const void *addr, Dl_info *info);
140 #else
141 #define DL_open(path) dlopen((path), RTLD_NOW)
142 #define DL_close(handle) dlclose(handle)
143 #define DL_addr(addr, info) dladdr((addr), (info))
144 #endif // TARGET_WINNT
146 extern void* DL_sym(void *handle, const char *name, const char *version);
148 // One-time initialization API
149 #ifdef TARGET_WINNT
150 typedef INIT_ONCE OffloadOnceControl;
151 #define OFFLOAD_ONCE_CONTROL_INIT INIT_ONCE_STATIC_INIT
153 extern void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void));
154 #else
155 typedef pthread_once_t OffloadOnceControl;
156 #define OFFLOAD_ONCE_CONTROL_INIT PTHREAD_ONCE_INIT
158 #define __offload_run_once(ctrl, func) pthread_once(ctrl, func)
159 #endif // TARGET_WINNT
161 // Parses size specification string.
162 extern bool __offload_parse_size_string(const char *str, uint64_t &new_size);
164 // Parses string with integer value
165 extern bool __offload_parse_int_string(const char *str, int64_t &value);
167 // get value by its base, offset and size
168 int64_t get_el_value(
169 char *base,
170 int64_t offset,
171 int64_t size
173 #endif // OFFLOAD_UTIL_H_INCLUDED