github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/internal/ccall/jit-thread.h (about) 1 /* 2 * jit-thread.h - Internal thread management routines for libjit. 3 * 4 * Copyright (C) 2004 Southern Storm Software, Pty Ltd. 5 * 6 * This file is part of the libjit library. 7 * 8 * The libjit library is free software: you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public License 10 * as published by the Free Software Foundation, either version 2.1 of 11 * the License, or (at your option) any later version. 12 * 13 * The libjit library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with the libjit library. If not, see 20 * <http://www.gnu.org/licenses/>. 21 */ 22 23 #ifndef _JIT_THREAD_H 24 #define _JIT_THREAD_H 25 26 #include <jit/jit-defs.h> 27 #include "jit-config.h" 28 29 #if defined(JIT_THREADS_PTHREAD) 30 # include <pthread.h> 31 #elif defined(JIT_THREADS_WIN32) 32 # include <windows.h> 33 #endif 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /* 40 * Type that describes a thread's identifier, and the id comparison function. 41 */ 42 #if defined(JIT_THREADS_PTHREAD) 43 typedef pthread_t jit_thread_id_t; 44 #define jit_thread_id_equal(x,y) (pthread_equal((x), (y))) 45 #define jit_thread_self() (pthread_self()) 46 #define jit_thread_release_self(t) do { ; } while (0) 47 #elif defined(JIT_THREADS_WIN32) 48 typedef HANDLE jit_thread_id_t; 49 #define jit_thread_id_equal(x,y) ((x) == (y)) 50 jit_thread_id_t _jit_thread_self(void); 51 #define jit_thread_self() _jit_thread_self() 52 #define jit_thread_release_self(t) CloseHandle((t)) 53 #else 54 typedef int jit_thread_id_t; 55 #define jit_thread_id_equal(x,y) ((x) == (y)) 56 #define jit_thread_self() 1 57 #define jit_thread_release_self(t) do { ; } while (0) 58 #endif 59 60 /* 61 * Control information that is associated with a thread. 62 */ 63 typedef struct jit_thread_control *jit_thread_control_t; 64 65 /* 66 * Initialize the thread routines. Ignored if called multiple times. 67 */ 68 void _jit_thread_init(void); 69 70 /* 71 * Get the JIT control object for the current thread. 72 */ 73 jit_thread_control_t _jit_thread_get_control(void); 74 75 /* 76 * Get the identifier for the current thread. 77 */ 78 jit_thread_id_t _jit_thread_current_id(void); 79 80 /* 81 * Define the primitive mutex operations. 82 */ 83 #if defined(JIT_THREADS_PTHREAD) 84 85 typedef pthread_mutex_t jit_mutex_t; 86 #define jit_mutex_create(mutex) (pthread_mutex_init((mutex), 0)) 87 #define jit_mutex_destroy(mutex) (pthread_mutex_destroy((mutex))) 88 #define jit_mutex_lock(mutex) (pthread_mutex_lock((mutex))) 89 #define jit_mutex_unlock(mutex) (pthread_mutex_unlock((mutex))) 90 91 #elif defined(JIT_THREADS_WIN32) 92 93 typedef CRITICAL_SECTION jit_mutex_t; 94 #define jit_mutex_create(mutex) (InitializeCriticalSection((mutex))) 95 #define jit_mutex_destroy(mutex) (DeleteCriticalSection((mutex))) 96 #define jit_mutex_lock(mutex) (EnterCriticalSection((mutex))) 97 #define jit_mutex_unlock(mutex) (LeaveCriticalSection((mutex))) 98 99 #else 100 101 typedef int jit_mutex_t; 102 #define jit_mutex_create(mutex) do { ; } while (0) 103 #define jit_mutex_destroy(mutex) do { ; } while (0) 104 #define jit_mutex_lock(mutex) do { ; } while (0) 105 #define jit_mutex_unlock(mutex) do { ; } while (0) 106 107 #endif 108 109 /* 110 * Mutex that synchronizes global data initialization. 111 */ 112 extern jit_mutex_t _jit_global_lock; 113 114 /* 115 * Define the primitive monitor operations. 116 */ 117 118 #if defined(JIT_THREADS_PTHREAD) 119 120 typedef struct 121 { 122 pthread_mutex_t _mutex; 123 pthread_cond_t _cond; 124 125 } jit_monitor_t; 126 #define jit_monitor_create(mon) \ 127 do { \ 128 pthread_mutex_init(&((mon)->_mutex), 0); \ 129 pthread_cond_init(&((mon)->_cond), 0); \ 130 } while (0) 131 #define jit_monitor_destroy(mon) \ 132 do { \ 133 pthread_cond_destroy(&((mon)->_cond)); \ 134 pthread_mutex_destroy(&((mon)->_mutex)); \ 135 } while (0) 136 #define jit_monitor_lock(mon) \ 137 do { \ 138 pthread_mutex_lock(&((mon)->_mutex)); \ 139 } while (0) 140 #define jit_monitor_unlock(mon) \ 141 do { \ 142 pthread_mutex_unlock(&((mon)->_mutex)); \ 143 } while (0) 144 #define jit_monitor_signal(mon) \ 145 do { \ 146 pthread_cond_signal(&((mon)->_cond)); \ 147 } while (0) 148 #define jit_monitor_signal_all(mon) \ 149 do { \ 150 pthread_cond_broadcast(&((mon)->_cond)); \ 151 } while (0) 152 153 #elif defined(JIT_THREADS_WIN32) 154 155 typedef struct 156 { 157 HANDLE _mutex; 158 HANDLE _cond; 159 LONG volatile _waiting; 160 } jit_monitor_t; 161 #define jit_monitor_create(mon) \ 162 do { \ 163 (mon)->_mutex = CreateMutex(NULL, FALSE, NULL); \ 164 (mon)->_cond = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL); \ 165 (mon)->_waiting = 0; \ 166 } while (0) 167 #define jit_monitor_destroy(mon) \ 168 do { \ 169 CloseHandle((mon)->_cond); \ 170 CloseHandle((mon)->_mutex); \ 171 } while (0) 172 #define jit_monitor_lock(mon) \ 173 do { \ 174 WaitForSingleObject((mon)->_mutex, INFINITE); \ 175 } while (0) 176 #define jit_monitor_unlock(mon) \ 177 do { \ 178 ReleaseMutex((mon)->_mutex); \ 179 } while (0) 180 #define jit_monitor_signal(mon) \ 181 do { \ 182 if((mon)->_waiting > 0) \ 183 { \ 184 --((mon)->_waiting); \ 185 ReleaseSemaphore((mon)->_cond, 1, NULL); \ 186 } \ 187 } while (0) 188 #define jit_monitor_signal_all(mon) \ 189 do { \ 190 LONG _count = (mon)->_waiting; \ 191 if(_count > 0) \ 192 { \ 193 (mon)->_waiting = 0; \ 194 ReleaseSemaphore((mon)->_cond, _count, NULL); \ 195 } \ 196 } while (0) 197 198 #else 199 200 typedef int jit_monitor_t; 201 #define jit_monitor_create(mon) do { ; } while (0) 202 #define jit_monitor_destroy(mon) do { ; } while (0) 203 #define jit_monitor_lock(mon) do { ; } while (0) 204 #define jit_monitor_unlock(mon) do { ; } while (0) 205 #define jit_monitor_signal(mon) do { ; } while (0) 206 #define jit_monitor_signal_all(mon) do { ; } while (0) 207 208 #endif 209 int _jit_monitor_wait(jit_monitor_t *mon, jit_int timeout); 210 #define jit_monitor_wait(mon,timeout) _jit_monitor_wait((mon), (timeout)) 211 212 #ifdef __cplusplus 213 }; 214 #endif 215 216 #endif /* _JIT_THREAD_H */