bool next_event_blockcheck(void*){ if(try_take_lock_exclusive(timer_lock)){ bool ret = next_event ? get_msecs() >= next_event->time : false; release_lock(timer_lock); return ret; } return false; }
void reset_timer(isr_regs *regs){ bt_handle_info handle = proc_get_handle(regs->ebx, proc_current_pid); if(handle.open){ timer_info *t = (timer_info*)handle.value; if(!t->active){ t->active = true; timer_event *e = new timer_event(); e->cancel = false; e->pid = proc_current_pid; e->time = get_msecs() + t->duration; e->timer_id = t->timer_id; take_lock_exclusive(timer_lock); events->push_back(e); update_next_event(); release_lock(timer_lock); } } }
void idle(void) { int x = curX - (wbrush/2); int y = h - (curY + (hbrush/2)); int msecs; static int last_msecs = -1; /* do not do this more than 60 times a second. Otherwise it's * to fast for use on high-end systems */ msecs = get_msecs(); if (fabs(last_msecs - msecs) < 1000./60.) { return; } last_msecs = msecs; /* we draw the brush using a drawpixels command. on systems with * hardware-accelerated texture mapping it would be better to use * that. * * we use the bitmap hack to set the rasterpos because we don't * know that the position will be within the window. */ glRasterPos2i(0, 0); glBitmap(0, 0, 0, 0, x, y, 0); glColorMask(0, 0, 0, 1); glDrawBuffer(GL_BACK); glDrawPixels(wbrush, hbrush, GL_ALPHA, GL_UNSIGNED_BYTE, brush); glColorMask(1, 1, 1, 1); glReadBuffer(GL_BACK); glDrawBuffer(GL_FRONT); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glCopyPixels(x, y, wbrush, hbrush, GL_COLOR); glDisable(GL_BLEND); glColorMask(1, 1, 1, 1); }
int main(int argc, char **argv) { int nthreads; int max_nthreads; int exper_n; if (1 == argc) max_nthreads = 4; else if (2 == argc) { max_nthreads = atoi(argv[1]); if (max_nthreads < 1 || max_nthreads > MAX_NTHREADS) { fprintf(stderr, "Invalid max # of threads argument\n"); exit(1); } } else { fprintf(stderr, "Usage: %s [max # of threads]\n", argv[0]); exit(1); } for (exper_n = 0; exper_n < N_EXPERIMENTS; ++ exper_n) for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) { int i; # ifdef USE_WINTHREADS DWORD thread_id; HANDLE thread[MAX_NTHREADS]; # else pthread_t thread[MAX_NTHREADS]; # endif int list_length = nthreads*(nthreads+1)/2; long long start_time; list_element * le; # ifdef VERBOSE printf("Before add_elements: exper_n=%d, nthreads=%d," " max_nthreads=%d, list_length=%d\n", exper_n, nthreads, max_nthreads, list_length); # endif add_elements(list_length); # ifdef VERBOSE printf("Initial list (nthreads = %d):\n", nthreads); print_list(); # endif ops_performed = 0; start_time = get_msecs(); for (i = 1; i < nthreads; ++i) { int code; # ifdef USE_WINTHREADS thread[i] = CreateThread(NULL, 0, run_one_test, (LPVOID)(size_t)i, 0, &thread_id); code = thread[i] != NULL ? 0 : (int)GetLastError(); # else code = pthread_create(&thread[i], 0, run_one_test, (void *)(size_t)i); # endif if (code != 0) { fprintf(stderr, "Thread creation failed %u\n", (unsigned)code); exit(3); } } /* We use the main thread to run one test. This allows gprof */ /* profiling to work, for example. */ run_one_test(0); for (i = 1; i < nthreads; ++i) { int code; # ifdef USE_WINTHREADS code = WaitForSingleObject(thread[i], INFINITE) == WAIT_OBJECT_0 ? 0 : (int)GetLastError(); # else code = pthread_join(thread[i], 0); # endif if (code != 0) { fprintf(stderr, "Thread join failed %u\n", (unsigned)code); abort(); } } times[nthreads][exper_n] = (unsigned long)(get_msecs() - start_time); # ifdef VERBOSE printf("%d %lu\n", nthreads, (unsigned long)(get_msecs() - start_time)); printf("final list (should be reordered initial list):\n"); print_list(); # endif check_list(list_length); while ((le = (list_element *)AO_stack_pop(&the_list)) != 0) free(le); } for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) { # ifndef NO_TIMES unsigned long sum = 0; # endif printf("About %d pushes + %d pops in %d threads:", LIMIT, LIMIT, nthreads); # ifndef NO_TIMES for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n) { # if defined(VERBOSE) printf(" [%lu]", times[nthreads][exper_n]); # endif sum += times[nthreads][exper_n]; } printf(" %lu msecs\n", (sum + N_EXPERIMENTS/2)/N_EXPERIMENTS); # else printf(" completed\n"); # endif } return 0; }
int dhcp_init_ifc(const char *ifname) { dhcp_msg discover_msg; dhcp_msg request_msg; dhcp_msg reply; dhcp_msg *msg; dhcp_info info; int s, r, size; int valid_reply; uint32_t xid; unsigned char hwaddr[6]; struct pollfd pfd; unsigned int state; unsigned int timeout; int if_index; xid = (uint32_t) get_msecs(); if (ifc_get_hwaddr(ifname, hwaddr)) { return fatal("cannot obtain interface address"); } if (ifc_get_ifindex(ifname, &if_index)) { return fatal("cannot obtain interface index"); } s = open_raw_socket(ifname, hwaddr, if_index); timeout = TIMEOUT_INITIAL; state = STATE_SELECTING; info.type = 0; goto transmit; for (;;) { pfd.fd = s; pfd.events = POLLIN; pfd.revents = 0; r = poll(&pfd, 1, timeout); if (r == 0) { #if VERBOSE printerr("TIMEOUT\n"); #endif if (timeout >= TIMEOUT_MAX) { printerr("timed out\n"); if ( info.type == DHCPOFFER ) { printerr("no acknowledgement from DHCP server\nconfiguring %s with offered parameters\n", ifname); return dhcp_configure(ifname, &info); } errno = ETIME; close(s); return -1; } timeout = timeout * 2; transmit: size = 0; msg = NULL; switch(state) { case STATE_SELECTING: msg = &discover_msg; size = init_dhcp_discover_msg(msg, hwaddr, xid); break; case STATE_REQUESTING: msg = &request_msg; size = init_dhcp_request_msg(msg, hwaddr, xid, info.ipaddr, info.serveraddr); break; default: r = 0; } if (size != 0) { r = send_message(s, if_index, msg, size); if (r < 0) { printerr("error sending dhcp msg: %s\n", strerror(errno)); } } continue; } if (r < 0) { if ((errno == EAGAIN) || (errno == EINTR)) { continue; } return fatal("poll failed"); } errno = 0; r = receive_packet(s, &reply); if (r < 0) { if (errno != 0) { LOGD("receive_packet failed (%d): %s", r, strerror(errno)); if (errno == ENETDOWN || errno == ENXIO) { return -1; } } continue; } #if VERBOSE > 1 dump_dhcp_msg(&reply, r); #endif decode_dhcp_msg(&reply, r, &info); if (state == STATE_SELECTING) { valid_reply = is_valid_reply(&discover_msg, &reply, r); } else { valid_reply = is_valid_reply(&request_msg, &reply, r); } if (!valid_reply) { printerr("invalid reply\n"); continue; } if (verbose) dump_dhcp_info(&info); switch(state) { case STATE_SELECTING: if (info.type == DHCPOFFER) { state = STATE_REQUESTING; timeout = TIMEOUT_INITIAL; xid++; goto transmit; } break; case STATE_REQUESTING: if (info.type == DHCPACK) { printerr("configuring %s\n", ifname); close(s); return dhcp_configure(ifname, &info); } else if (info.type == DHCPNAK) { printerr("configuration request denied\n"); close(s); return -1; } else { printerr("ignoring %s message in state %d\n", dhcp_type_to_name(info.type), state); } break; } } close(s); return 0; }
int main(int argc, char **argv) { int nthreads; int max_nthreads; int exper_n; if (1 == argc) max_nthreads = 4; else if (2 == argc) { max_nthreads = atoi(argv[1]); if (max_nthreads < 1 || max_nthreads > MAX_NTHREADS) { fprintf(stderr, "Invalid max # of threads argument\n"); exit(1); } } else { fprintf(stderr, "Usage: %s [max # of threads]\n", argv[0]); exit(1); } for (exper_n = 0; exper_n < N_EXPERIMENTS; ++ exper_n) for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) { int i; pthread_t thread[MAX_NTHREADS]; int list_length = nthreads*(nthreads+1)/2; long long start_time; list_element * le; add_elements(list_length); # ifdef VERBOSE printf("Initial list (nthreads = %d):\n", nthreads); print_list(); # endif ops_performed = 0; start_time = get_msecs(); for (i = 1; i < nthreads; ++i) { int code; if ((code = pthread_create(thread+i, 0, run_one_test, (void *)(long)i)) != 0) { fprintf(stderr, "Thread creation failed %u\n", code); exit(3); } } /* We use the main thread to run one test. This allows gprof */ /* profiling to work, for example. */ run_one_test(0); for (i = 1; i < nthreads; ++i) { int code; if ((code = pthread_join(thread[i], 0)) != 0) { fprintf(stderr, "Thread join failed %u\n", code); abort(); } } times[nthreads][exper_n] = (unsigned long)(get_msecs() - start_time); # ifdef VERBOSE printf("%d %lu\n", nthreads, (unsigned long)(get_msecs() - start_time)); printf("final list (should be reordered initial list):\n"); print_list(); # endif check_list(list_length); while ((le = (list_element *)AO_stack_pop(&the_list)) != 0) free(le); } # ifndef NO_TIMES for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) { unsigned long sum = 0; printf("About %d pushes + %d pops in %d threads:", LIMIT, LIMIT, nthreads); for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n) { # if defined(VERBOSE) printf("[%lu] ", times[nthreads][exper_n]); # endif sum += times[nthreads][exper_n]; } printf(" %lu msecs\n", (sum + N_EXPERIMENTS/2)/N_EXPERIMENTS); } # endif /* !NO_TIMES */ return 0; }
long timer_get_msecs(struct Timer *self) { clock_gettime(CLOCK_MONOTONIC, &self->stop); return get_msecs(&self->start, &self->stop); }