?? bsp_file.c
字號:
/****************************************************************************** Product: QDPP example, 80x86, Win32, Visual C++ 6.0* Last Updated for Version: 3.4.00* Date of the Last Update: Sep 03, 2007** Q u a n t u m L e a P s* ---------------------------* innovating embedded systems** Copyright (C) 2002-2007 Quantum Leaps, LLC. All rights reserved.** This software 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 under the* terms of Quantum Leaps commercial licenses, which expressly supersede* the GPL and are specifically designed for licensees interested in* retaining the proprietary status of their code.** Contact information:* Quantum Leaps Web site: http://www.quantum-leaps.com* e-mail: info@quantum-leaps.com*****************************************************************************/#include "qf_port.h"#include "qs_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/*--------------------------------------------------------------------------*/#ifdef Q_SPYstatic FILE *l_qsFile;enum QSQHsmTstRecords { QS_QDPP_DISPLAY = QS_USER};uint8_t QS_init(void const *arg) { static uint8_t qsBuf[1024]; /* 1K buffer for Quantum Spy */ QS_initBuf(qsBuf, sizeof(qsBuf)); l_qsFile = fopen((char const *)arg, "wb"); /* write binary */ return (uint8_t)(l_qsFile != (FILE *)0);}/*..........................................................................*/void QS_exit(void) { if (l_qsFile != (FILE *)0) { fclose(l_qsFile); }}/*..........................................................................*/#define QS_DUMP() do { \ uint16_t nBytes = 1000; \ uint8_t const *block = QS_getBlock(&nBytes); \ if (block != (uint8_t *)0) { \ fwrite(block, 1, nBytes, l_qsFile); \ } \} while (0)/*..........................................................................*/void QS_flush(void) { uint32_t nBytes = 1000; uint8_t const *block; while ((block = QS_getBlock(&nBytes)) != (uint8_t *)0) { fwrite(block, 1, nBytes, l_qsFile); nBytes = 1000; }}/*..........................................................................*/QSTimeCtr QS_getTime(void) { return (QSTimeCtr)QF_getTime();}#endif /* Q_SPY *//*--------------------------------------------------------------------------*/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)); } } QS_DUMP(); /* dump a chunk of QS trace data */ } return (void *)0; /* return success */}/*..........................................................................*/void BSP_init(int argc, char *argv[]) { pthread_attr_t attr; struct sched_param param; pthread_t idle; char const *hostAndPort = "qs.bin"; if (argc > 1) { /* port specified? */ hostAndPort = argv[1]; } if (!QS_INIT(hostAndPort)) { printf("\nUnable to open QSpy socket\n"); exit(-1); } 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); QS_BEGIN(QS_QDPP_DISPLAY, QS_apObj_); QS_U8(1, n); QS_STR(stat); QS_END();}/*..........................................................................*/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 + -