Jim Cownie | 33f7b24 | 2014-04-09 15:40:23 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.txt for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | #ifndef OFFLOAD_UTIL_H_INCLUDED |
| 12 | #define OFFLOAD_UTIL_H_INCLUDED |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <stdint.h> |
| 17 | |
| 18 | #ifdef TARGET_WINNT |
| 19 | #include <windows.h> |
| 20 | #include <process.h> |
| 21 | #else // TARGET_WINNT |
| 22 | #include <dlfcn.h> |
| 23 | #include <pthread.h> |
| 24 | #endif // TARGET_WINNT |
| 25 | |
| 26 | #ifdef TARGET_WINNT |
| 27 | typedef unsigned pthread_key_t; |
| 28 | typedef int pid_t; |
| 29 | |
| 30 | #define __func__ __FUNCTION__ |
| 31 | #define strtok_r(s,d,p) strtok_s(s,d,p) |
| 32 | #define strcasecmp(a,b) stricmp(a,b) |
| 33 | |
| 34 | #define thread_key_create(key, destructor) \ |
| 35 | (((*key = TlsAlloc()) > 0) ? 0 : GetLastError()) |
| 36 | #define thread_key_delete(key) TlsFree(key) |
| 37 | |
| 38 | #ifndef S_ISREG |
| 39 | #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) |
| 40 | #endif |
| 41 | |
| 42 | void* thread_getspecific(pthread_key_t key); |
| 43 | int thread_setspecific(pthread_key_t key, const void *value); |
| 44 | #else |
| 45 | #define thread_key_create(key, destructor) \ |
| 46 | pthread_key_create((key), (destructor)) |
| 47 | #define thread_key_delete(key) pthread_key_delete(key) |
| 48 | #define thread_getspecific(key) pthread_getspecific(key) |
| 49 | #define thread_setspecific(key, value) pthread_setspecific(key, value) |
| 50 | #endif // TARGET_WINNT |
| 51 | |
| 52 | // Mutex implementation |
| 53 | struct mutex_t { |
| 54 | mutex_t() { |
| 55 | #ifdef TARGET_WINNT |
| 56 | InitializeCriticalSection(&m_lock); |
| 57 | #else // TARGET_WINNT |
| 58 | pthread_mutex_init(&m_lock, 0); |
| 59 | #endif // TARGET_WINNT |
| 60 | } |
| 61 | |
| 62 | ~mutex_t() { |
| 63 | #ifdef TARGET_WINNT |
| 64 | DeleteCriticalSection(&m_lock); |
| 65 | #else // TARGET_WINNT |
| 66 | pthread_mutex_destroy(&m_lock); |
| 67 | #endif // TARGET_WINNT |
| 68 | } |
| 69 | |
| 70 | void lock() { |
| 71 | #ifdef TARGET_WINNT |
| 72 | EnterCriticalSection(&m_lock); |
| 73 | #else // TARGET_WINNT |
| 74 | pthread_mutex_lock(&m_lock); |
| 75 | #endif // TARGET_WINNT |
| 76 | } |
| 77 | |
| 78 | void unlock() { |
| 79 | #ifdef TARGET_WINNT |
| 80 | LeaveCriticalSection(&m_lock); |
| 81 | #else // TARGET_WINNT |
| 82 | pthread_mutex_unlock(&m_lock); |
| 83 | #endif // TARGET_WINNT |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | #ifdef TARGET_WINNT |
| 88 | CRITICAL_SECTION m_lock; |
| 89 | #else |
| 90 | pthread_mutex_t m_lock; |
| 91 | #endif |
| 92 | }; |
| 93 | |
| 94 | struct mutex_locker_t { |
| 95 | mutex_locker_t(mutex_t &mutex) : m_mutex(mutex) { |
| 96 | m_mutex.lock(); |
| 97 | } |
| 98 | |
| 99 | ~mutex_locker_t() { |
| 100 | m_mutex.unlock(); |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | mutex_t &m_mutex; |
| 105 | }; |
| 106 | |
| 107 | // Dynamic loader interface |
| 108 | #ifdef TARGET_WINNT |
| 109 | struct Dl_info |
| 110 | { |
| 111 | char dli_fname[MAX_PATH]; |
| 112 | void *dli_fbase; |
| 113 | char dli_sname[MAX_PATH]; |
| 114 | const void *dli_saddr; |
| 115 | }; |
| 116 | |
| 117 | void* DL_open(const char *path); |
| 118 | #define DL_close(handle) FreeLibrary((HMODULE) (handle)) |
| 119 | int DL_addr(const void *addr, Dl_info *info); |
| 120 | #else |
| 121 | #define DL_open(path) dlopen((path), RTLD_NOW) |
| 122 | #define DL_close(handle) dlclose(handle) |
| 123 | #define DL_addr(addr, info) dladdr((addr), (info)) |
| 124 | #endif // TARGET_WINNT |
| 125 | |
| 126 | extern void* DL_sym(void *handle, const char *name, const char *version); |
| 127 | |
| 128 | // One-time initialization API |
| 129 | #ifdef TARGET_WINNT |
| 130 | typedef INIT_ONCE OffloadOnceControl; |
| 131 | #define OFFLOAD_ONCE_CONTROL_INIT INIT_ONCE_STATIC_INIT |
| 132 | |
| 133 | extern void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void)); |
| 134 | #else |
| 135 | typedef pthread_once_t OffloadOnceControl; |
| 136 | #define OFFLOAD_ONCE_CONTROL_INIT PTHREAD_ONCE_INIT |
| 137 | |
| 138 | #define __offload_run_once(ctrl, func) pthread_once(ctrl, func) |
| 139 | #endif // TARGET_WINNT |
| 140 | |
| 141 | // Parses size specification string. |
| 142 | extern bool __offload_parse_size_string(const char *str, uint64_t &new_size); |
| 143 | |
| 144 | // Parses string with integer value |
| 145 | extern bool __offload_parse_int_string(const char *str, int64_t &value); |
| 146 | |
| 147 | // get value by its base, offset and size |
| 148 | int64_t get_el_value( |
| 149 | char *base, |
| 150 | int64_t offset, |
| 151 | int64_t size |
| 152 | ); |
| 153 | #endif // OFFLOAD_UTIL_H_INCLUDED |