static void perform_thread_post_mortem(struct thread_post_mortem *post_mortem) { #ifdef CREATE_POST_MORTEM_THREAD pthread_detach(pthread_self()); #endif int result; if (post_mortem) { /* The thread may exit before pthread_create() has finished initialization and it may write into already unmapped memory. This lock doesn't actually need to protect anything, just to make sure that at least one call to pthread_create() has finished. Possible improvements: stash the address of the thread struct for which a pthread is being created and don't lock here if it's not the one being terminated. */ result = pthread_mutex_lock(&create_thread_lock); gc_assert(result == 0); result = pthread_mutex_unlock(&create_thread_lock); gc_assert(result == 0); if ((result = pthread_join(post_mortem->os_thread, NULL))) { lose("Error calling pthread_join in perform_thread_post_mortem:\n%s", strerror(result)); } if ((result = pthread_attr_destroy(post_mortem->os_attr))) { lose("Error calling pthread_attr_destroy in perform_thread_post_mortem:\n%s", strerror(result)); } os_invalidate(post_mortem->os_address, THREAD_STRUCT_SIZE); free(post_mortem); } }
bool GameScene24::onContactBegin(PhysicsContact& contact) { auto nodeA = (Sprite*)contact.getShapeA()->getBody()->getNode(); auto nodeB = (Sprite*)contact.getShapeB()->getBody()->getNode(); if (nodeA->getTag() == 13 && nodeB->getTag() == 14) { success(); } if (nodeB->getTag() == 13 && nodeA->getTag() == 14) { success(); } if (nodeB->getTag() == 14 && nodeA->getTag() != 14) { lose(); } if (nodeA->getTag() == 14 && nodeB->getTag() != 14) { lose(); } return true; }
static void lose_gerror (const char *prefix, GError *error) { if (error) { lose ("%s: %s", prefix, error->message); } else { lose ("%s", prefix); } }
void Gameplay::initialize() { reachable = new bool[MAX_SUM]; currentLevel = new Level1(); disconnect(&nextNumberTimer, SIGNAL(timeout()), this, SLOT(addNumber())); disconnect(&levelCountdownTimer, SIGNAL(timeout()), this, SLOT(lose())); connect(&nextNumberTimer, SIGNAL(timeout()), this, SLOT(addNumber())); connect(&levelCountdownTimer, SIGNAL(timeout()), this, SLOT(lose())); prepare(); }
/* This function does file handle black magic. Here, we create pipes, duplicate the ones we're going to pass on to the child process, and set the current process's stdin and stdout to be those pipes. (### I'm not sure duplicating them is necessary, but it doesn't hurt...) inpipes, outpipes, and old_handles are 2 element arrays. */ static void pipe_setup (STARTUPINFO *siStartInfo, int inpipes[], int outpipes[], HANDLE old_handles[]) { const int pipe_size = 2000; HANDLE new_stdin, new_stdout; HANDLE parent = GetCurrentProcess(); /* Create new file handles--in binary mode. _pipe sticks the read then the write handle in {in,out}pipes, and returns 0 on success and -1 on failure */ if (_pipe(inpipes, pipe_size, O_BINARY) != 0 || _pipe(outpipes, pipe_size, O_BINARY) != 0 /* Duplicate the stdin and stdout handles. False on failure. */ || !DuplicateHandle(parent, /* source process */ /* next, handle to dup */ (HANDLE) _get_osfhandle(inpipes[0]), parent, /* Proc to give new handles to */ &new_stdin, /* Where new handle is stored */ 0, /* Parameter ignored */ true, /* Make new handle inheritable */ DUPLICATE_SAME_ACCESS) || !DuplicateHandle(parent, /* source process */ /* next, handle to dup */ (HANDLE)_get_osfhandle(outpipes[1]), parent, /* Proc to give new handles to */ &new_stdout, /* Where new handle is stored */ 0, /* Parameter ignored */ true, /* Make new handle inheritable */ DUPLICATE_SAME_ACCESS)) { lose("Failed while doing pipe stuff for fd_exec"); } /* Save the old stdin and stdout handles to some place we can remember */ old_handles[0] = GetStdHandle(STD_INPUT_HANDLE); old_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE); /* Set stdin and stdout to the new handles */ if (!SetStdHandle(STD_INPUT_HANDLE, new_stdin) || !SetStdHandle(STD_OUTPUT_HANDLE, new_stdout)) { lose("Failed while doing pipe stuff for fd_exec"); } /* Now tell the StartInfo to use the handles we just created. By default, child processes don't inherit the stdin and stdout of their parents. */ siStartInfo->dwFlags = STARTF_USESTDHANDLES; siStartInfo->hStdInput = new_stdin; siStartInfo->hStdOutput = new_stdout; /* nothing funny with stderr, but we still have to initialize the field anyway */ siStartInfo->hStdError = GetStdHandle(STD_ERROR_HANDLE); }
/* * Waitable timer implementation for the safepoint-based (SIGALRM-free) * timer facility using timerfd_create(). */ int os_create_wtimer() { int fd = timerfd_create(CLOCK_MONOTONIC, 0); if (fd == -1) lose("os_create_wtimer: timerfd_create"); /* Cannot count on TFD_CLOEXEC availability, so do it manually: */ if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) lose("os_create_wtimer: fcntl"); return fd; }
int os_wait_for_wtimer(int fd) { unsigned char buf[8]; int n = read(fd, buf, sizeof(buf)); if (n == -1) { if (errno == EINTR) return -1; lose("os_wtimer_listen failed"); } if (n != sizeof(buf)) lose("os_wtimer_listen read too little"); return 0; }
static const char * get_prefix (bool running_suid, const char *user_prefix) { if (!running_suid && user_prefix == NULL) lose ("Not using a shared game directory, and no prefix given."); if (running_suid) { #ifdef HAVE_SHARED_GAME_DIR return HAVE_SHARED_GAME_DIR; #else lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid."); #endif } return user_prefix; }
/* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the exception port (which is being listened to do by the mach exception handling thread). */ kern_return_t mach_thread_init(mach_port_t thread_exception_port) { kern_return_t ret; mach_port_t current_mach_thread; /* allocate a named port for the thread */ FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port)); ret = mach_port_allocate_name(current_mach_task, MACH_PORT_RIGHT_RECEIVE, thread_exception_port); if (ret) { lose("mach_port_allocate_name failed with return_code %d\n", ret); } /* establish the right for the thread_exception_port to send messages */ ret = mach_port_insert_right(current_mach_task, thread_exception_port, thread_exception_port, MACH_MSG_TYPE_MAKE_SEND); if (ret) { lose("mach_port_insert_right failed with return_code %d\n", ret); } current_mach_thread = mach_thread_self(); ret = thread_set_exception_ports(current_mach_thread, EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION, thread_exception_port, EXCEPTION_DEFAULT, THREAD_STATE_NONE); if (ret) { lose("thread_set_exception_ports failed with return_code %d\n", ret); } ret = mach_port_deallocate (current_mach_task, current_mach_thread); if (ret) { lose("mach_port_deallocate failed with return_code %d\n", ret); } ret = mach_port_move_member(current_mach_task, thread_exception_port, mach_exception_handler_port_set); if (ret) { lose("mach_port_move_member failed with return_code %d\n", ret); } return ret; }
void os_sem_post(os_sem_t *sem, char *what) { if (KERN_SUCCESS!=semaphore_signal(*sem)) lose("%s: os_sem_post(%p): %s", what, sem, strerror(errno)); FSHOW((stderr, "%s: os_sem_post(%p) ok\n", what, sem)); }
void sigtrap_handler(int signal, siginfo_t *info, os_context_t *context) { unsigned int trap; if (single_stepping) { restore_breakpoint_from_single_step(context); return; } /* This is just for info in case the monitor wants to print an * approximation. */ current_control_stack_pointer = (lispobj *)*os_context_sp_addr(context); #ifdef LISP_FEATURE_SUNOS /* For some reason the breakpoints that :ENCAPSULATE NIL tracing sets up * cause a trace trap (i.e. processor single-stepping trap) on the following * instruction on Solaris 10/x86. -- JES, 2006-04-07 */ if (info->si_code == TRAP_TRACE) { lose("foo"); return; } #endif /* On entry %eip points just after the INT3 byte and aims at the * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a * number of bytes will follow, the first is the length of the byte * arguments to follow. */ trap = *(unsigned char *)(*os_context_pc_addr(context)); handle_trap(context, trap); }
// Main function with switch that controls the flow of the game. int main(){ int state = START; while(1){ switch(state){ case START: start(); state = GAME; break; case GAME: state = game(); break; case WIN: win(); state = START; break; case LOSE: lose(); state = START; break; default: break; } } return 0; }
void mapseq() { switch(next) { case 0:menu();sidemenu();break; case 98:levelskip();break; case 1:level1();break; case 2:level2();break; case 3:level3();break; case 4:level4();break; case 5:level5();break; case 101:levelskip();break; case 6:level6();break; case 7:level7();break; case 8:level8();break; case 9:level9();break; case 10:level10();break; case 102:levelskip();break; case 11:level11();break; case 12:level12();break; case 13:level13();break; case 14:level14();break; case 15:level15();break; case 103:levelskip();break; case 16:win();break; case 99:help();break; case 100:lose();elapsedTime=0;break; } }
void wait_for_thread_state_change(struct thread *thread, lispobj state) { sigset_t old; os_sem_t *wait_sem; block_blockable_signals(&old); start: os_sem_wait(thread->state_sem, "wait_for_thread_state_change"); if (thread->state == state) { switch (state) { case STATE_RUNNING: wait_sem = thread->state_not_running_sem; thread->state_not_running_waitcount++; break; case STATE_STOPPED: wait_sem = thread->state_not_stopped_sem; thread->state_not_stopped_waitcount++; break; default: lose("Invalid state in wait_for_thread_state_change: "OBJ_FMTX"\n", state); } } else { wait_sem = NULL; } os_sem_post(thread->state_sem, "wait_for_thread_state_change"); if (wait_sem) { os_sem_wait(wait_sem, "wait_for_thread_state_change"); goto start; } thread_sigmask(SIG_SETMASK, &old, NULL); }
static void dump_list_literal(struct list_literal *literal) { struct literal *part; int length; int i; length = 0; for (part = literal->first; part != NULL; part = part->next) length++; part = literal->first; while (length > 255+9) { dump_op(fop_DOTTED_LISTN); dump_byte(255); for (i = 0; i < 255+9; i++) { dump_literal(part); part = part->next; } length -= 255+9; } if (literal->tail) switch (length) { case 0: lose("Zero element dotted list?\n"); case 1: dump_op(fop_DOTTED_LIST1); break; case 2: dump_op(fop_DOTTED_LIST2); break; case 3: dump_op(fop_DOTTED_LIST3); break; case 4: dump_op(fop_DOTTED_LIST4); break; case 5: dump_op(fop_DOTTED_LIST5); break; case 6: dump_op(fop_DOTTED_LIST6); break; case 7: dump_op(fop_DOTTED_LIST7); break; case 8: dump_op(fop_DOTTED_LIST8); break; default: dump_op(fop_DOTTED_LISTN); dump_byte(length - 9); break; } else switch (length) { case 0: dump_op(fop_NIL); break; case 1: dump_op(fop_LIST1); break; case 2: dump_op(fop_LIST2); break; case 3: dump_op(fop_LIST3); break; case 4: dump_op(fop_LIST4); break; case 5: dump_op(fop_LIST5); break; case 6: dump_op(fop_LIST6); break; case 7: dump_op(fop_LIST7); break; case 8: dump_op(fop_LIST8); break; default: dump_op(fop_LISTN); dump_byte(length - 9); break; } while (part != NULL) { dump_literal(part); part = part->next; } if (literal->tail) dump_literal(literal->tail); }
void Cell::open() { emit generate(m_x, m_y); if (isOpen()) { return; } else if (!isSuspect() & !isMined()) { if(minesAround() != 0) { m_open = true; } else { m_open = true; for (Cell *cell : m_neighbors) { if (!cell->isMined() & !cell->isSuspect() &cell->minesAround() == 0) { cell->open(); } else if (!cell->isMined() & !cell->isSuspect()) { cell->m_open = true; } } } emit checkWin(); } if(this->haveMine() & !isSuspect() & !isMined()) { emit lose(); } }
inline void os_sem_wait(os_sem_t *sem, char *what) { kern_return_t ret; restart: FSHOW((stderr, "%s: os_sem_wait(%p)\n", what, sem)); ret = semaphore_wait(*sem); FSHOW((stderr, "%s: os_sem_wait(%p) => %s\n", what, sem, KERN_SUCCESS==ret ? "ok" : strerror(errno))); switch (ret) { case KERN_SUCCESS: return; /* It is unclear just when we can get this, but a sufficiently * long wait seems to do that, at least sometimes. * * However, a wait that long is definitely abnormal for the * GC, so we complain before retrying. */ case KERN_OPERATION_TIMED_OUT: fprintf(stderr, "%s: os_sem_wait(%p): %s", what, sem, strerror(errno)); /* This is analogous to POSIX EINTR. */ case KERN_ABORTED: goto restart; default: lose("%s: os_sem_wait(%p): %lu, %s", what, sem, ret, strerror(errno)); } }
int main() { int seed = 0; int state = SPLASH; while(1) { switch(state) { case SPLASH: seed = splash(); state = GAME; break; case GAME: state = game(seed); break; case WIN: win(); state = SPLASH; break; case LOSE: lose(); state = SPLASH; break; } } }
static const char * get_prefix (bool privileged, const char *user_prefix) { if (privileged) { #ifdef HAVE_SHARED_GAME_DIR return HAVE_SHARED_GAME_DIR; #else lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n" "and should not run with elevated privileges."); #endif } if (user_prefix == NULL) lose ("Not using a shared game directory, and no prefix given."); return user_prefix; }
void os_sem_post(sem_t *sem, char *what) { if (-1 == sem_post(sem)) lose("%s: os_sem_post(%p): %s", what, sem, strerror(errno)); FSHOW((stderr, "%s: os_sem_post(%p)\n", what, sem)); }
void os_zero(os_vm_address_t addr, os_vm_size_t length) { os_vm_address_t block_start; os_vm_size_t block_size; #ifdef DEBUG fprintf(stderr,";;; os_zero: addr: 0x%08x, len: 0x%08x\n",addr,length); #endif block_start = os_round_up_to_page(addr); length -= block_start-addr; block_size = os_trunc_size_to_page(length); if (block_start > addr) bzero((char *)addr, block_start-addr); if (block_size < length) bzero((char *)block_start+block_size, length-block_size); if (block_size != 0) { /* Now deallocate and allocate the block so that it faults in * zero-filled. */ os_invalidate(block_start, block_size); addr = os_validate(NOT_MOVABLE, block_start, block_size); if (addr == NULL || addr != block_start) lose("os_zero: block moved! 0x%08x ==> 0x%08x\n", block_start, addr); } }
void do_byte_return(struct thread *thread, obj_t *old_sp, obj_t *vals) { int opcode = ((unsigned char *)(thread->component))[thread->pc - 1]; if (opcode == op_BREAKPOINT) opcode = original_byte(thread->component, thread->pc - 1); if ((opcode&0xf0) == op_CALL_FOR_SINGLE || opcode == op_DOT_FOR_SINGLE || opcode >= op_PLUS) { if (vals == thread->sp) *old_sp = obj_False; else if (vals != old_sp) *old_sp = vals[0]; thread->sp = old_sp + 1; } else if ((opcode&0xf0) == op_CALL_FOR_MANY || opcode == op_DOT_FOR_MANY) canonicalize_values(thread, old_sp, vals); else lose("Strange call opcode: 0x%02x", opcode); #ifdef MINDY_SLOW_FUNCTION_POINTERS thread->advance = NULL; #else thread->advance = interpret_next_byte; #endif }
void os_sem_init(os_sem_t *sem, unsigned int value) { if (-1==sem_init(sem, 0, value)) lose("os_sem_init(%p, %u): %s", sem, value, strerror(errno)); FSHOW((stderr, "os_sem_init(%p, %u)\n", sem, value)); }
int main(){ int n; scanf("%d", &n); std::vector<int> cnt(n, 0); std::vector<std::set<int> > win(n); std::vector<std::set<int> > lose(n); for(int p = 1; p < n * (n - 1) / 2; p++){ long x, y; scanf("%ld %ld", &x, &y); --x; --y; win[x].insert(y); lose[y].insert(x); } int a(-1), b(-1); for(int p = 0; p < n; p++){ if(win[p].size() + lose[p].size() >= n - 1){continue;} if(a < 0){a = p;} else if(b < 0){b = p;} } for(long p = 0; p < n; p++){ if(win[a].count(p) && lose[b].count(p)){break;} else if(lose[a].count(p) && win[b].count(p)){int t = a; a = b; b = t; break;} } printf("%d %d\n", a + 1, b + 1); return 0; }
void *check_realloc(void *ptr, size_t sz, char *file, int line) { void *ret = realloc(ptr, sz); if (ret == 0) lose("realloc failed -- out of memory in %s line %d", file, line); return ret; }
static void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context) { unsigned int code = *((unsigned char *)(4+*os_context_pc_addr(context))); u32 trap_instruction = *((u32 *)*os_context_pc_addr(context)); int condition_bits = (trap_instruction >> 28) & 0x0f; /* Make sure that we're looking at an SWI instruction or that one * undefined instruction that the kernel recognizes as an explicit * trap. */ if ((condition_bits == 15) || (((trap_instruction & 0x0f000000) != 0x0f000000) && (trap_instruction != 0xe7f001f0))) { lose("Unrecognized trap instruction %08lx in sigtrap_handler()", trap_instruction); } if (trap_instruction == 0xe7f001f0) { handle_trap(context, code); } else { arch_clear_pseudo_atomic_interrupted(context); arch_skip_instruction(context); interrupt_handle_pending(context); } }
void *check_malloc(size_t sz, const char *file, int line) { void *ret = malloc(sz); if (ret == 0) lose("malloc failed -- out of memory in %s line %d", file, line); return ret; }
void *check_calloc(size_t nobj, size_t sz, char *file, int line) { void *ret = calloc(nobj, sz); if (ret == 0) lose("calloc failed -- out of memory in %s line %d", file, line); return ret; }
/* checks for input. Although it seems like there should be a simpler way, this does a full producer/consumer thing with mindy_read. */ static DWORD input_checker (LPDWORD param) { int fd = (int) param; /* so what if we cast a pointer to an int? */ HANDLE handle = (HANDLE) _get_osfhandle(fd); int read_from = 0; for (;;) { char small_buffer; /* Ignore the contents of the next two vars */ int bytes_read; DWORD the_error = 0; /* Wait until someone invalidates our last answer */ WaitForSingleObject(update_input_available[fd], INFINITE); /* Now loop until we complete a read without getting a broken pipe error */ /* read 0 bytes, block if not available */ if (_fileno(stdin) == fd) { if (!PeekConsoleInput(handle, &small_buffer, 0, &bytes_read)) { the_error = GetLastError(); /* handle broken pipes later */ if (the_error != ERROR_BROKEN_PIPE) { lose("Read error type %d on fd %d (aka handle #x%x)", the_error, fd, handle); } } } else { if (!PeekNamedPipe(handle, NULL, 0, NULL, NULL, NULL)) { the_error = GetLastError(); /* handle broken pipes later */ if (the_error != ERROR_BROKEN_PIPE && the_error != ERROR_INVALID_FUNCTION) { lose("Read error type %d on fd %d (aka handle #x%x)", the_error, fd, handle); } } } input_available_array[fd] = true; SetEvent(input_available_is_updated[fd]); if (the_error == ERROR_BROKEN_PIPE) { /* All further read attempts will return this error. Go to sleep; we'll soon be killed off by mindy_close */ SuspendThread(fd_threads[fd]); } read_from = 1; } lose("This is not supposed to be reached!"); return 0; }
void os_init(char *argv[], char *envp[]) { struct utsname name; int major_version; int minor_version; uname(&name); major_version = atoi(name.release); if (major_version != 5) { lose("sunos major version=%d (which isn't 5!)\n", major_version); } minor_version = atoi(name.release+2); if ((minor_version < 8)) { kludge_mmap_fd = open("/dev/zero",O_RDONLY); if (kludge_mmap_fd < 0) { perror("open"); lose("Error in open(..)\n"); } } else if (minor_version > 11) { FSHOW((stderr, "os_init: Solaris version greater than 11?\nUnknown MAP_ANON behaviour.\n")); lose("Unknown mmap() interaction with MAP_ANON\n"); } else { /* Versions 8-11*/ KLUDGE_MAYBE_MAP_ANON = 0x100; } /* I do not understand this at all. FIXME. */ os_vm_page_size = os_real_page_size = sysconf(_SC_PAGESIZE); if(os_vm_page_size>OS_VM_DEFAULT_PAGESIZE){ fprintf(stderr,"os_init: Pagesize too large (%d > %d)\n", os_vm_page_size,OS_VM_DEFAULT_PAGESIZE); exit(1); } else { /* * we do this because there are apparently dependencies on * the pagesize being OS_VM_DEFAULT_PAGESIZE somewhere... * but since the OS doesn't know we're using this restriction, * we have to grovel around a bit to enforce it, thus anything * that uses real_page_size_difference. */ /* FIXME: Is this still true? */ real_page_size_difference=OS_VM_DEFAULT_PAGESIZE-os_vm_page_size; os_vm_page_size=OS_VM_DEFAULT_PAGESIZE; } }