📄 bsp.c
字号:
/****************************************************************************** Product: QDPP example* Version: Compatible with QF/C 3.1.yy* Updated: Feb 06, 2006** Copyright (C) 2002-2006 Quantum Leaps, LLC. All rights reserved.** This example is part of the Quantum Leaps QP/C software, and may be* distributed and modified under the terms of the GNU General Public License* version 2 (GPL) as published by the Free Software Foundation and appearing* in the file GPL.TXT included in the packaging of this file. Please note* that GPL Section 2[b] requires that all works based on this software must* also be made publicly available under the terms of the GPL ("Copyleft").** Alternatively, this software may be distributed and modified in conjunction* with a valid QP/C Quantum Leaps commercial license. Quantum Leaps* commercial licenses are designed for users who want to retain proprietary* status of their code. The users who license this software under one of* Quantum Leaps commercial licenses do not use this software under the GPL* and therefore are not subject to any of its terms.** Contact information:* Quantum Leaps Web site: http://www.quantum-leaps.com* Quantum Leaps licensing: http://www.quantum-leaps.com/licensing* Quantum Leaps products: http://www.quantum-leaps.com/products* e-mail: sales@quantum-leaps.com*****************************************************************************/#include "qf_port.h"#include "qassert.h"#include "qdpp.h"#include <stdlib.h>#include <stdio.h>#include <sys/select.h>#include <termios.h>#include <unistd.h>Q_DEFINE_THIS_FILE/*..........................................................................*/static void *idleThread(void *me) { /* the expected P-Thread signature */ struct timeval timeout = { 0 }; /* timeout for select() */ struct termios tio; /* modified terminal attributes */ tcgetattr(0, &tio); /* obtain the current terminal attributes */ tio.c_lflag &= ~(ICANON | ECHO); /* disable the canonical mode & echo */ tcsetattr(0, TCSANOW, &tio); /* set the new attributes */ while (QF_running_) { fd_set con; /* FD set representing the console */ FD_ZERO(&con); FD_SET(0, &con); timeout.tv_usec = 8000; /* sleep for the full tick or until a console input arrives, NOTE01 */ if (0 != select(1, &con, 0, 0, &timeout)) { /* any descriptor set? */ char ch; read(0, &ch, 1); if (ch == '\33') { /* ESC pressed? */ QF_publish(Q_NEW(QEvent, TERMINATE_SIG)); } } } return (void *)0; /* return success */}/*..........................................................................*/void BSP_init(int argc, char *argv[]) { pthread_attr_t attr; struct sched_param param; pthread_t idle; pthread_attr_init(&attr); /* SCHED_FIFO corresponds to real-time preemptive priority-based scheduler * NOTE: This scheduling policy requires the superuser priviledges */ pthread_attr_setschedpolicy(&attr, SCHED_FIFO); param.sched_priority = sched_get_priority_min(SCHED_FIFO); pthread_attr_setschedparam(&attr, ¶m); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (pthread_create(&idle, &attr, &idleThread, 0) != 0) { /* Creating the p-thread with the SCHED_FIFO policy failed. * Most probably this application has no superuser privileges, * so we just fall back to the default SCHED_OTHER policy * and priority 0. */ pthread_attr_setschedpolicy(&attr, SCHED_OTHER); param.sched_priority = 0; pthread_attr_setschedparam(&attr, ¶m); Q_ALLEGE(pthread_create(&idle, &attr, &idleThread, 0) == 0); } pthread_attr_destroy(&attr);}/*..........................................................................*/void displyPhilStat(uint8_t n, char const *stat) { printf("Philosopher %2d is %s\n", (int)n, stat);}/*..........................................................................*/void Q_assert_handler(char const *file, int line) { fprintf(stderr, "Assertion failed in %s, line %d", file, line); QF_exit();}/******************************************************************************* NOTE01:* The select() system call seems to deliver the finest time granularity of* 1 clock tick. The timeout value passed to select() is rounded up to the* nearest tick (10 ms on desktop Linux). The timeout cannot be too short,* because the system might choose to busy-wait for very short timeouts.* An alternative, POSIX nanosleep() system call seems to deliver only 20ms* granularity.** Here the select() call is used not just as a fairly portable way to sleep* with subsecond precision. The select() call is also used to detect any* characters typed on the console.** Also according to man pages, on Linux, the function select() modifies* timeout to reflect the amount of time not slept; most other implementations* do not do this. This causes problems both when Linux code which reads* timeout is ported to other operating systems, and when code is ported to* Linux that reuses a struct timeval for multiple selects in a loop without* reinitializing it. Here the microsecond part of the structure is re-* initialized before each select() call.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -