static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id) { struct clock_event_device *evt = dev_id; return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt); }
//the interrupt handler static irqreturn_t timer_irq_handler(int irq, void *dev_id) { timer_handler(); // tell the kernel it's handled return IRQ_HANDLED; }
static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id) { struct clock_event_device *evt = dev_id; return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt); }
static struct Interface * main_loop(int sock, struct Interface *ifaces, char const *conf_path) { struct pollfd fds[2]; sigset_t sigmask; sigset_t sigempty; struct sigaction sa; sigemptyset(&sigempty); sigemptyset(&sigmask); sigaddset(&sigmask, SIGHUP); sigaddset(&sigmask, SIGTERM); sigaddset(&sigmask, SIGINT); sigaddset(&sigmask, SIGUSR1); sigprocmask(SIG_BLOCK, &sigmask, NULL); sa.sa_handler = sighup_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGHUP, &sa, 0); sa.sa_handler = sigterm_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGTERM, &sa, 0); sa.sa_handler = sigint_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGINT, &sa, 0); sa.sa_handler = sigusr1_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGUSR1, &sa, 0); memset(fds, 0, sizeof(fds)); fds[0].fd = sock; fds[0].events = POLLIN; #if HAVE_NETLINK fds[1].fd = netlink_socket(); fds[1].events = POLLIN; #else fds[1].fd = -1; #endif for (;;) { struct timespec *tsp = 0; struct Interface *next_iface_to_expire = find_iface_by_time(ifaces); if (next_iface_to_expire) { static struct timespec ts; int timeout = next_time_msec(next_iface_to_expire); ts.tv_sec = timeout / 1000; ts.tv_nsec = (timeout - 1000 * ts.tv_sec) * 1000000; tsp = &ts; dlog(LOG_DEBUG, 1, "polling for %g second(s), next iface is %s", timeout / 1000.0, next_iface_to_expire->props.name); } else { dlog(LOG_DEBUG, 1, "no iface is next. Polling indefinitely"); } #ifdef HAVE_PPOLL int rc = ppoll(fds, sizeof(fds) / sizeof(fds[0]), tsp, &sigempty); #else int rc = poll(fds, sizeof(fds) / sizeof(fds[0]), 1000*tsp->tv_sec); #endif if (rc > 0) { #ifdef HAVE_NETLINK if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { flog(LOG_WARNING, "socket error on fds[1].fd"); } else if (fds[1].revents & POLLIN) { process_netlink_msg(fds[1].fd, ifaces); } #endif if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { flog(LOG_WARNING, "socket error on fds[0].fd"); } else if (fds[0].revents & POLLIN) { int len, hoplimit; struct sockaddr_in6 rcv_addr; struct in6_pktinfo *pkt_info = NULL; unsigned char msg[MSG_SIZE_RECV]; unsigned char chdr[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int))]; len = recv_rs_ra(sock, msg, &rcv_addr, &pkt_info, &hoplimit, chdr); if (len > 0 && pkt_info) { process(sock, ifaces, msg, len, &rcv_addr, pkt_info, hoplimit); } else if (!pkt_info) { dlog(LOG_INFO, 4, "recv_rs_ra returned null pkt_info"); } else if (len <= 0) { dlog(LOG_INFO, 4, "recv_rs_ra returned len <= 0: %d", len); } } } else if (rc == 0) { if (next_iface_to_expire) timer_handler(sock, next_iface_to_expire); } else if (rc == -1) { dlog(LOG_INFO, 3, "poll returned early: %s", strerror(errno)); } if (sigint_received) { flog(LOG_WARNING, "exiting, %d sigint(s) received", sigint_received); break; } if (sigterm_received) { flog(LOG_WARNING, "exiting, %d sigterm(s) received", sigterm_received); break; } if (sighup_received) { dlog(LOG_INFO, 3, "sig hup received"); ifaces = reload_config(sock, ifaces, conf_path); sighup_received = 0; } if (sigusr1_received) { dlog(LOG_INFO, 3, "sig usr1 received"); reset_prefix_lifetimes(ifaces); sigusr1_received = 0; } } return ifaces; }
void main_loop(void) { struct pollfd fds[2]; memset(fds, 0, sizeof(fds)); fds[0].fd = sock; fds[0].events = POLLIN; fds[0].revents = 0; #if HAVE_NETLINK fds[1].fd = netlink_socket(); fds[1].events = POLLIN; fds[1].revents = 0; #else fds[1].fd = -1; fds[1].events = 0; fds[1].revents = 0; #endif for (;;) { struct Interface *next = NULL; struct Interface *iface; int timeout = -1; int rc; if (IfaceList) { timeout = next_time_msec(IfaceList); next = IfaceList; for (iface = IfaceList; iface; iface = iface->next) { int t; t = next_time_msec(iface); if (timeout > t) { timeout = t; next = iface; } } } dlog(LOG_DEBUG, 5, "polling for %g seconds.", timeout/1000.0); rc = poll(fds, sizeof(fds)/sizeof(fds[0]), timeout); if (rc > 0) { if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { flog(LOG_WARNING, "socket error on fds[0].fd"); } else if (fds[0].revents & POLLIN) { int len, hoplimit; struct sockaddr_in6 rcv_addr; struct in6_pktinfo *pkt_info = NULL; unsigned char msg[MSG_SIZE_RECV]; len = recv_rs_ra(msg, &rcv_addr, &pkt_info, &hoplimit); if (len > 0) { process(IfaceList, msg, len, &rcv_addr, pkt_info, hoplimit); } } #ifdef HAVE_NETLINK if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { flog(LOG_WARNING, "socket error on fds[1].fd"); } else if (fds[1].revents & POLLIN) { process_netlink_msg(fds[1].fd); } #endif } else if ( rc == 0 ) { if (next) timer_handler(next); } else if ( rc == -1 && errno != EINTR ) { flog(LOG_ERR, "poll error: %s", strerror(errno)); } if (sigterm_received || sigint_received) { flog(LOG_WARNING, "Exiting, sigterm or sigint received.\n"); break; } if (sighup_received) { reload_config(); sighup_received = 0; } if (sigusr1_received) { reset_prefix_lifetimes(); sigusr1_received = 0; } } }
bool OSystem_PalmBase::pollEvent(Event &event) { ::EventType ev; Boolean handled; UInt32 keyCurrentState; Coord x, y; battery_handler(); timer_handler(); sound_handler(); for(;;) { #if defined(COMPILE_OS5) && defined(PALMOS_ARM) SysEventGet(&ev, evtNoWait); #else EvtGetEvent(&ev, evtNoWait); #endif // check for hardkey repeat for mouse emulation keyCurrentState = KeyCurrentState(); // check_hard_keys(); if (!(keyCurrentState & _keyMouseMask)) { _lastKeyRepeat = 0; } else { if (getMillis() >= (_keyMouseRepeat + _keyMouseDelay)) { _keyMouseRepeat = getMillis(); if (gVars->arrowKeys) { if (keyCurrentState & _keyMouse.bitUp) event.kbd.keycode = 273; else if (keyCurrentState & _keyMouse.bitDown) event.kbd.keycode = 274; else if (keyCurrentState & _keyMouse.bitLeft) event.kbd.keycode = 276; else if (keyCurrentState & _keyMouse.bitRight) event.kbd.keycode = 275; else if (keyCurrentState & _keyMouse.bitButLeft) event.kbd.keycode = chrLineFeed; event.type = EVENT_KEYDOWN; event.kbd.ascii = event.kbd.keycode; event.kbd.flags = 0; } else { Int8 sx = 0; Int8 sy = 0; if (keyCurrentState & _keyMouse.bitUp) sy = -1; else if (keyCurrentState & _keyMouse.bitDown) sy = +1; if (keyCurrentState & _keyMouse.bitLeft) sx = -1; else if (keyCurrentState & _keyMouse.bitRight) sx = +1; if (sx || sy) { simulate_mouse(event, sx, sy, &x, &y); event.type = EVENT_MOUSEMOVE; _lastKey = kKeyMouseMove; } else { x = _mouseCurState.x; y = _mouseCurState.y; if (keyCurrentState & _keyMouse.bitButLeft) { event.type = EVENT_LBUTTONDOWN; _lastKey = kKeyMouseLButton; } else if (_lastKey == kKeyMouseLButton) { event.type = EVENT_LBUTTONUP; _lastKey = kKeyNone; } } event.mouse.x = x; event.mouse.y = y; warpMouse(x, y); // updateCD(); } return true; } } if (ev.eType == keyDownEvent) { switch (ev.data.keyDown.chr) { // ESC key case vchrLaunch: _lastKey = kKeyNone; event.type = EVENT_KEYDOWN; event.kbd.keycode = 27; event.kbd.ascii = 27; event.kbd.flags = 0; return true; // F5 = menu case vchrMenu: _lastKey = kKeyNone; event.type = EVENT_KEYDOWN; event.kbd.keycode = 319; event.kbd.ascii = 319; event.kbd.flags = 0; return true; // if hotsync pressed, etc... case vchrHardCradle: case vchrHardCradle2: case vchrLowBattery: case vchrFind: // case vchrBrightness: // volume control on Zodiac, let other backends disable it case vchrContrast: // do nothing _lastKey = kKeyNone; return true; } } if (check_event(event, &ev)) return true; _lastKey = kKeyNone; // prevent crash when alarm is raised handled = ((ev.eType == keyDownEvent) && (ev.data.keyDown.modifiers & commandKeyMask) && ((ev.data.keyDown.chr == vchrAttnStateChanged) || (ev.data.keyDown.chr == vchrAttnUnsnooze))); // graffiti strokes, auto-off, etc... if (!handled) if (SysHandleEvent(&ev)) continue; switch(ev.eType) { case penMoveEvent: get_coordinates(&ev, x, y); if (y > _screenHeight || y < 0 || x > _screenWidth || x < 0) return false; if (_lastEvent != penMoveEvent && (abs(y - event.mouse.y) <= 2 || abs(x - event.mouse.x) <= 2)) // move only if return false; _lastEvent = penMoveEvent; event.type = EVENT_MOUSEMOVE; event.mouse.x = x; event.mouse.y = y; warpMouse(x, y); return true; case penDownEvent: get_coordinates(&ev, x, y); // indy fight mode if (_useNumPad && !_overlayVisible) { char num = '1'; num += 9 - (3 - (3 * x / _screenWidth )) - (3 * (3 * y / _screenHeight)); event.type = EVENT_KEYDOWN; event.kbd.keycode = num; event.kbd.ascii = num; event.kbd.flags = 0; _lastEvent = keyDownEvent; return true; } _lastEvent = penDownEvent; if (y > _screenHeight || y < 0 || x > _screenWidth || x < 0) return false; event.type = ((gVars->stylusClick || _overlayVisible) ? EVENT_LBUTTONDOWN : EVENT_MOUSEMOVE); event.mouse.x = x; event.mouse.y = y; warpMouse(x, y); return true; case penUpEvent: get_coordinates(&ev, x, y); event.type = ((gVars->stylusClick || _overlayVisible) ? EVENT_LBUTTONUP : EVENT_MOUSEMOVE); if (y > _screenHeight || y < 0 || x > _screenWidth || x < 0) return false; event.mouse.x = x; event.mouse.y = y; warpMouse(x, y); return true; case keyDownEvent: if (ev.data.keyDown.chr == vchrCommand && (ev.data.keyDown.modifiers & commandKeyMask)) { _lastKeyModifier++; _lastKeyModifier %= kModifierCount; if (_lastKeyModifier) draw_osd((kDrawKeyState + _lastKeyModifier - 1), 2, _screenDest.h + 2, true); else draw_osd(kDrawKeyState, 2, _screenDest.h + 2, false); return false; } char mask = 0; UInt16 key = ev.data.keyDown.chr; if (_lastKeyModifier == kModifierNone) { // for keyboard mode if (ev.data.keyDown.modifiers & shiftKeyMask) mask |= KBD_SHIFT; if (ev.data.keyDown.modifiers & controlKeyMask) mask |= KBD_CTRL; if (ev.data.keyDown.modifiers & optionKeyMask) mask |= KBD_ALT; if (ev.data.keyDown.modifiers & commandKeyMask) mask |= KBD_CTRL|KBD_ALT; } else { // for grafiti mode if (_lastKeyModifier == kModifierCommand) mask = KBD_CTRL|KBD_ALT; if (_lastKeyModifier == kModifierAlt) mask = KBD_ALT; if (_lastKeyModifier == kModifierCtrl) mask = KBD_CTRL; } if (_lastKeyModifier) draw_osd(kDrawKeyState, 2, _screenDest.h + 2, false); _lastKeyModifier = kModifierNone; // F1 -> F10 key if (key >= '0' && key <= '9' && mask == (KBD_CTRL|KBD_ALT)) { key = (key == '0') ? 324 : (315 + key - '1'); mask = 0; // exit } else if ((key == 'z' && mask == KBD_CTRL) || (mask == KBD_ALT && key == 'x')) { event.type = EVENT_QUIT; return true; // num pad (indy fight mode) } else if (key == 'n' && mask == (KBD_CTRL|KBD_ALT) && !_overlayVisible) { _useNumPad = !_useNumPad; draw_osd(kDrawFight, _screenDest.w - 34, _screenDest.h + 2, _useNumPad, 1); displayMessageOnOSD(_useNumPad ? "Fight mode on." : "Fight mode off."); return false; } // other keys event.type = EVENT_KEYDOWN; event.kbd.keycode = key; event.kbd.ascii = key; event.kbd.flags = mask; return true; default: return false; }; } }
// graphics thread main-function // NOTE: this should only be called *ONCE* by an application // void xwin_graphics_event_loop() { int restarted = 0; if (debug) fprintf(stderr, "Direct call to xwin_graphics_event_loop()\n"); struct sigaction sa = {0}; memset(&sa, 0, sizeof(sa)); sa.sa_handler = restart_sig; sa.sa_flags = SA_RESTART; // install signal handler to catch abort() from GLUT. Note that // signal handlers are global to ALL threads, so the signal // handler that we record here in &original_signal_handler is the // previous one that applied to BOTH threads if (sigaction(SIGABRT, &sa, &original_signal_handler)) { perror("unable to install signal handler to catch SIGABORT"); } // handle glut's notorious exits().. atexit(restart); // THIS IS THE re-entry point at exit or ABORT in the graphics-thread restarted = setjmp(jbuf); // if we came here from an ABORT-signal thrown in this thread, // we put this thread to sleep // if (restarted == JUMP_ABORT) { while(1) { sleep(1); } } // If using freeglut, check to see if we need to // reinitialize glut and graphics. // We're only resetting flags here, actual reinit function // is called in make_new_window(). // if (glut_is_initialized && glut_is_freeglut) { if (1 == (glut_is_initialized = FREEGLUT_IS_INITIALIZED)) { if (!GLUT_HAVE_WINDOW) { win = 0; } } } if (boinc_is_standalone()) { if (restarted) { while(1) { sleep(1); // assuming glutInit() failed: put graphics-thread to sleep } } else { // open the graphics-window set_mode(MODE_WINDOW); glutTimerFunc(TIMER_INTERVAL_MSEC, timer_handler, 0); } } else { if (!glut_is_initialized) { #ifdef __APPLE__ setMacPList(); #endif set_mode(MODE_HIDE_GRAPHICS); while ( current_graphics_mode == MODE_HIDE_GRAPHICS ) { if (debug) fprintf(stderr, "Graphics-thread now waiting for client-message...\n" ); wait_for_initial_message(); if (debug) fprintf(stderr, "got a graphics-message from client... \n"); timer_handler(0); } } else // here glut has been initialized previously // probably the user pressed window-'close' // set_mode(MODE_HIDE_GRAPHICS); // close any previously open windows } // ok we should be ready & initialized by now to call glutMainLoop() // if (debug) fprintf(stderr, "now calling glutMainLoop()...\n"); glutMainLoop(); if (debug) fprintf(stderr, "glutMainLoop() returned!! This should never happen...\n"); }
int main(int argc, char *argv[]) { bool stop_server = false; client_data *users; bool timeout = false; int epollfd; epoll_event events[MAX_EVENT_NUMBER]; int listenfd; int ret = 0; struct sockaddr_in address; const char *ip; int port; if (argc < 2) { printf("usage: ./main ip_address port_number"); return 1; } ip = argv[1]; port = atoi(argv[2]); bzero(&address, sizeof(address)); address.sin_family = AF_INET; inet_pton(AF_INET, ip, &address.sin_addr); address.sin_port = htons(port); listenfd = socket(PF_INET, SOCK_STREAM, 0); assert(listenfd >= 0); ret = bind(listenfd, (struct sockaddr *)&address, sizeof(address)); assert(ret != -1); ret = listen(listenfd, 5); assert(ret != -1); epollfd = epoll_create(5); assert(epollfd != -1); addfd(epollfd, listenfd); ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd); assert(ret != -1); setnonblocking(pipefd[1]); addfd(epollfd, pipefd[0]); addsig(SIGALRM); addsig(SIGTERM); users = new client_data[FD_LIMIT]; alarm(TIMESLOT); while (!stop_server) { int number = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1); if ((number < 0) && (errno != EINTR)) { printf("epoll failure\n"); break; } for (int i=0; i<number; ++i) { int sockfd = events[i].data.fd; if (sockfd == listenfd) { struct sockaddr_in client_address; socklen_t client_addlength = sizeof(client_address); int connfd = accept(listenfd, (struct sockaddr *)&client_address, &client_addlength); addfd(epollfd, connfd); users[connfd].address = client_address; users[connfd].sockfd = connfd; util_timer *timer = new util_timer; timer->user_data = &users[connfd]; timer->cb_func = cb_func; time_t cur = time(NULL); timer->expire = cur + TIME_INACTIVE; users[connfd].timer = timer; timer_lst.add_timer(timer); } else if ((sockfd == pipefd[0]) && (events[i]).events & EPOLLIN) { char signals[1024]; ret = recv(pipefd[0], signals, sizeof(signals), 0); if (ret == -1) continue; else if (ret == 0) continue; else { for (int i=0; i<ret; ++i) { switch (signals[i]) { case SIGALRM: timeout = true; break; case SIGTERM: stop_server = true; } } } } else if (events[i].events & EPOLLIN) { util_timer *timer = users[sockfd].timer; memset(users[sockfd].buf, '\0', BUFFER_SIZE); ret = recv(sockfd, users[sockfd].buf, BUFFER_SIZE, 0); if (ret < 0) { if (errno != EAGAIN) { cb_func(&users[sockfd]); if (timer) timer_lst.del_timer(timer); } } else { users[sockfd].buf[ret-1] = '\0'; printf("get %d bytes of client data %s from %d\n", ret, users[sockfd].buf, sockfd); if (timer) { time_t cur = time(NULL); timer->expire = cur + TIME_INACTIVE; timer_lst.adjust_timer(timer); } } } } if (timeout) { timer_handler(); timeout = false; } } close(listenfd); close(pipefd[0]); close(pipefd[1]); delete []users; return 0; }
int main() { int port = atoi("2500"); int ret = 0; struct sockaddr_in address; //bzero(&address, sizeof(address)); memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(port); address.sin_addr.s_addr = htonl(INADDR_ANY); int listenfd = socket(AF_INET, SOCK_STREAM, 0); assert(listenfd>=0); ret = bind( listenfd, (struct sockaddr*)&address, sizeof(address)); assert( ret != -1 ); ret = listen( listenfd, 5 ); assert( ret != -1 ); epoll_events events[MAX_EVENT_NUMER]; int epollfd = epoll_create(5); assert(epollfd != -1); addfd( epollfd, listenfd); /**/ ret = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd); assert( ret != -1 ); setnonblocking( pipefd[1]); addfd( epollfd, pipefd[0]); addsig( SIGALRM ); addsig( SIGTERM ); bool stop_server = false; client_data* users = new client_data[FD_LIMIT]; bool timeout = false; alarm(TIMESLOT); while( !stop_server ) { int number = epoll_wait( epollfd, events, MAX_EVENT_NUMER, -1 ); if( (number<0) && (errno != EINTR)) { printf("epoll failure\n"); break; } for( int i=0; i<number; i++) { int sockfd = events[i].data.fd; if( sockfd == listenfd ) { struct sockaddr_in client_address; socklen_t client_addrlen = sizeof( client_addrlen); int connfd = accept( listenfd, (struct sockaddr*) &client_address, &client_addrlen); addfd( epollfd, connfd ); users[connfd].address = client_address; users[connfd].sockfd = connfd; util_timer* timer = new util_timer; timer->user_data = &users[connfd]; timer->cb_func = cb_func; time_t cur = time(NULL); timer->expire = cur + 3*TIMESLOT; users[connfd].timer = timer; timer_list.add_timer(timer); } else if( (sockfd == pipefd[0]) && (events[i].events & EPOLLIN)) { int sig; char signals[1024]; ret = recv( pipefd[0], signals, sizeof(signals), 0); if( ret == -1 || ret == 0 ) { continue; } else { for(int i=0; i<ret; i++) { switch( signals[i]) { case SIGALRM: { timeout = true; break; } case SIGHUP: { continue; } case SIGTERM: { stop_server = true; } case SIGINT: { printf("recv SIGINT\n"); stop_server = true; } } } } } else if( events[i].events & EPOLLIN ) { memset( users[sockfd].buf, '\0', BUFFER_SIZE ); ret = recv( sockfd, users[sockfd].buf, BUFFER_SIZE-1, 0); printf("get %d bytes of client data %s from %d\n", ret, users[sockfd].buf, sockfd); util_timer* timer = users[sockfd].timer; if( ret<0 ) { if( errno != EAGAIN ) { cb_func( &users[sockfd]); if( timer ) { timer_list.del_timer( timer ); } } } else if( ret == 0 ) { cb_func( &users[sockfd]); if(timer) { timer_list.del_timer( timer ); } } else { if( timer ) { time_t cur = time(NULL); timer->expire = cur + 3*TIMESLOT; printf("adjust timer once\n"); timer_list.adjust_timer( timer ); } } if( timeout ) { timer_handler(); timeout = false; } } } } close(listenfd); close(epollfd); close(pipefd[0]); close(pipefd[1]); delete [] users; return 0; }
int test_move(unsigned short xi, unsigned short yi, char *xpm[], unsigned short hor, short delta, unsigned short time) { int ipc_status; message msg; int request; int irq_set1,irq_set2; vg_init(GRAPHIC_MODE); Sprite*sprite = create_sprite(xpm); if(!hor){ sprite->xspeed = ((double)delta/(double)time)/FPS; sprite->yspeed = 0; } else{ sprite->xspeed = 0; sprite->yspeed = ((double)delta/(double)time)/FPS; } sprite->x = xi; sprite->y = yi; draw_sprite(sprite); counter = 0; irq_set1 = subscribe_kbd(); irq_set2 = subscribe_timer(); while (scanCode != EXIT_MAKE_CODE) { request = driver_receive(ANY, &msg, &ipc_status); if (request != 0) { printf("driver_receive failed with: %d", request); continue; } if (is_ipc_notify(ipc_status)) { switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: if (msg.NOTIFY_ARG & irq_set1) { kbd_handler(); } if (msg.NOTIFY_ARG & irq_set2) { if(counter % (int)(60/FPS) == 0){ update_sprite(sprite); draw_sprite(sprite); } if(counter == time * 60){ unsubscribe_timer(); } timer_handler(); } break; default: break; } } else { } } unsubscribe_timer(); unsubscribe_kbd(); vg_exit(); return 0; }