#define _OVM_DEBUG_H
#include "bh.h"
-#include <semaphore.h>
#include <stdbool.h>
+#if defined(_BH_LINUX)
+ #include <semaphore.h>
+
+ typedef dispatch_semaphore_t semaphore;
+
+ static inline semaphore* semaphore_create(const char *name, int oflag, mode_t mode, unsigned int value) {
+ return sem_open(name, flags, mode, value);
+ }
+
+ static inline void semaphore_wait(semaphore* sem) {
+ sem_wait(sem);
+ }
+
+ static inline void semaphore_post(semaphore* sem) {
+ sem_post(sem);
+ }
+#elif defined(_BH_DARWIN)
+ #include <dispatch/dispatch.h>
+
+ typedef dispatch_semaphore_t semaphore;
+
+ static inline semaphore* semaphore_create(const char *name, int oflag, mode_t mode, unsigned int value) {
+ semaphore* sem = bh_alloc(bh_heap_allocator(), sizeof(semaphore));
+ *sem = dispatch_semaphore_create(value);
+ return sem;
+ }
+
+ static inline void semaphore_wait(semaphore* sem) {
+ dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
+ }
+
+ static inline void semaphore_post(semaphore* sem) {
+ dispatch_semaphore_signal(*sem);
+ }
+#else
+ #error "Unsupported platform"
+#endif
+
typedef struct debug_loc_info_t {
u32 file_id;
u32 line;
b32 started;
i32 run_count;
- sem_t* wait_semaphore;
+ semaphore* wait_semaphore;
bool pause_at_next_line;
i32 pause_within;
char name_buf[256];
bh_bprintf(name_buf, 256, "/ovm_thread_%d", new_thread->id);
- new_thread->wait_semaphore = sem_open(name_buf, O_CREAT, 0664, 0);
+ new_thread->wait_semaphore = semaphore_create(name_buf, O_CREAT, 0664, 0);
new_thread->state_change_write_fd = debug->state_change_pipes[1];
static void resume_thread(debug_thread_state_t *thread) {
thread->run_count = -1;
- sem_post(thread->wait_semaphore);
+ semaphore_post(thread->wait_semaphore);
}
static void resume_thread_slow(debug_thread_state_t *thread) {
- sem_post(thread->wait_semaphore);
+ semaphore_post(thread->wait_semaphore);
}
static u32 get_stack_frame_instruction_pointer(debug_state_t *debug, debug_thread_state_t *thread, ovm_stack_frame_t *frame) {
#include <sys/mman.h>
#include <signal.h>
+
#if defined(__arm64__)
#include <arm_neon.h>
#elif defined(__x86_64__)
state->debug->pause_reason = debug_pause_exception;
assert(write(state->debug->state_change_write_fd, "1", 1));
- sem_wait(state->debug->wait_semaphore);
+ semaphore_wait(state->debug->wait_semaphore);
}
}
should_wait:
assert(write(state->debug->state_change_write_fd, "1", 1));
- sem_wait(state->debug->wait_semaphore);
+ semaphore_wait(state->debug->wait_semaphore);
state->debug->state = debug_state_running;
shouldnt_wait: