[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] r8551 - in usrp2/trunk/host-ng: apps lib
From: |
jcorgan |
Subject: |
[Commit-gnuradio] r8551 - in usrp2/trunk/host-ng: apps lib |
Date: |
Wed, 4 Jun 2008 12:19:51 -0600 (MDT) |
Author: jcorgan
Date: 2008-06-04 12:19:50 -0600 (Wed, 04 Jun 2008)
New Revision: 8551
Modified:
usrp2/trunk/host-ng/apps/test.sh
usrp2/trunk/host-ng/apps/test_usrp2.cc
usrp2/trunk/host-ng/lib/usrp2.cc
usrp2/trunk/host-ng/lib/usrp2_thread.h
Log:
Refactored background thread.
Modified: usrp2/trunk/host-ng/apps/test.sh
===================================================================
--- usrp2/trunk/host-ng/apps/test.sh 2008-06-03 22:05:51 UTC (rev 8550)
+++ usrp2/trunk/host-ng/apps/test.sh 2008-06-04 18:19:50 UTC (rev 8551)
@@ -2,3 +2,4 @@
sudo ./test_usrp2 2>cerr
+
Modified: usrp2/trunk/host-ng/apps/test_usrp2.cc
===================================================================
--- usrp2/trunk/host-ng/apps/test_usrp2.cc 2008-06-03 22:05:51 UTC (rev
8550)
+++ usrp2/trunk/host-ng/apps/test_usrp2.cc 2008-06-04 18:19:50 UTC (rev
8551)
@@ -33,8 +33,8 @@
usrp2::usrp2::sptr u2 = usrp2::usrp2::make("eth0");
- u2->set_rx_gain(1.0);
- u2->set_rx_freq(0.0, NULL);
+// u2->set_rx_gain(1.0);
+// u2->set_rx_freq(0.0, NULL);
u2->set_rx_decim(5);
u2->start_rx_streaming();
Modified: usrp2/trunk/host-ng/lib/usrp2.cc
===================================================================
--- usrp2/trunk/host-ng/lib/usrp2.cc 2008-06-03 22:05:51 UTC (rev 8550)
+++ usrp2/trunk/host-ng/lib/usrp2.cc 2008-06-04 18:19:50 UTC (rev 8551)
@@ -49,7 +49,9 @@
ethernet *d_ethernet;
pktfilter *d_pf;
std::string d_addr;
- usrp2_thread *d_thread;
+ usrp2_thread *d_bg_thread;
+ volatile bool d_bg_running; // TODO: multistate if needed
+
bool d_tx_active;
int d_rx_decim;
int d_rx_seqno;
@@ -67,7 +69,8 @@
void init_et_hdrs(u2_eth_packet_t *p, const std::string &dst);
void init_etf_hdrs(u2_eth_packet_t *p, const std::string &dst,
int word0_flags, int chan, uint32_t timestamp);
- void loop_body();
+ void bg_loop();
+ void stop_bg();
void rx_frames();
void tx_frames();
bool set_rx_gain(double gain);
@@ -141,8 +144,8 @@
// ----------------------------------------------------------------------
usrp2::impl::impl(const std::string &ifc, const std::string &addr)
- : d_ethernet(new ethernet()), d_pf(0), d_thread(0), d_rx_decim(0),
- d_rx_seqno(-1), d_tx_seqno(0), d_next_rid(0),
+ : d_ethernet(new ethernet()), d_pf(0), d_bg_thread(0), d_bg_running(false),
+ d_rx_decim(0), d_rx_seqno(-1), d_tx_seqno(0), d_next_rid(0),
d_num_rx_frames(0), d_num_rx_lost(0), d_num_rx_bytes(0)
{
props_vector_t u2s = find(ifc, addr);
@@ -161,14 +164,15 @@
if (USRP2_DEBUG)
std::cerr << "usrp2 constructor: using USRP2 at " << d_addr << std::endl;
- d_thread = new usrp2_thread(this);
- d_thread->start();
+ d_bg_thread = new usrp2_thread(this);
+ d_bg_thread->start();
}
usrp2::impl::~impl()
{
- d_thread->stop();
- d_thread = 0; // thread class deletes itself
+
+ stop_bg();
+ d_bg_thread = 0; // thread class deletes itself
delete d_pf;
d_ethernet->close();
delete d_ethernet;
@@ -441,45 +445,49 @@
}
void
-usrp2::impl::loop_body()
+usrp2::impl::bg_loop()
{
- fd_set read_fds, write_fds;
- FD_ZERO(&read_fds);
- FD_ZERO(&write_fds);
+ d_bg_running = true;
+ while(d_bg_running) {
- FD_SET(d_ethernet->fd(), &read_fds);
-
- if (d_tx_active)
- FD_SET(d_ethernet->fd(), &write_fds);
+ fd_set read_fds, write_fds;
+ FD_ZERO(&read_fds);
+ FD_ZERO(&write_fds);
- struct timeval timeout;
- timeout.tv_sec = 0;
- timeout.tv_usec = 100 * 1000; // 100 ms
-
- DEBUG_LOG("S"); // select
- int r = select(d_ethernet->fd()+1, &read_fds, &write_fds, 0, &timeout);
- if (r > 0) { // Socket available for read or for write
- if (FD_ISSET(d_ethernet->fd(), &read_fds)) {
- DEBUG_LOG("r"); // data to read()
- rx_frames();
- }
+ FD_SET(d_ethernet->fd(), &read_fds);
- if (FD_ISSET(d_ethernet->fd(), &write_fds)) {
- DEBUG_LOG("w"); // can write()
- tx_frames();
+ if (d_tx_active)
+ FD_SET(d_ethernet->fd(), &write_fds);
+
+ struct timeval timeout;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 100 * 1000; // 100 ms
+
+ DEBUG_LOG("S"); // select
+ int r = select(d_ethernet->fd()+1, &read_fds, &write_fds, 0, &timeout);
+ if (r > 0) { // Socket available for read or for write
+ if (FD_ISSET(d_ethernet->fd(), &read_fds)) {
+ DEBUG_LOG("r"); // data to read()
+ rx_frames();
+ }
+
+ if (FD_ISSET(d_ethernet->fd(), &write_fds)) {
+ DEBUG_LOG("w"); // can write()
+ tx_frames();
+ }
}
+ else if (r == 0) {
+ DEBUG_LOG("T"); // socket timeout
+ return;
+ }
+ else {
+ if (errno == EINTR)
+ DEBUG_LOG("I"); // interrupted system call
+ else
+ DEBUG_LOG("!"); // error on socket
+ return;
+ }
}
- else if (r == 0) {
- DEBUG_LOG("T"); // socket timeout
- return;
- }
- else {
- if (errno == EINTR)
- DEBUG_LOG("I"); // interrupted system call
- else
- DEBUG_LOG("!"); // error on socket
- return;
- }
}
void
@@ -532,19 +540,31 @@
{
}
+void
+usrp2::impl::stop_bg()
+{
+ if (USRP2_DEBUG)
+ std::cerr << "usrp2_thread::stop: joining background thread "
+ << this << std::endl;
+ d_bg_running = false;
+
+ void *dummy_status;
+ d_bg_thread->join(&dummy_status);
+
+ if (USRP2_DEBUG)
+ std::cerr << "usrp2_thread::stop: join returned" << std::endl;
+}
+
// ----------------------------------------------------------------------
usrp2_thread::usrp2_thread(usrp2::impl *u2) :
omni_thread(NULL, PRIORITY_HIGH),
- d_u2(u2), d_keep_running(false)
+ d_u2(u2)
{
}
usrp2_thread::~usrp2_thread()
{
- if (d_keep_running)
- stop();
-
// we don't own this, just forget it
d_u2 = 0;
}
@@ -555,7 +575,7 @@
if (USRP2_DEBUG)
std::cerr << "usrp2_thread::start() "
<< this << std::endl;
- d_keep_running = true;
+
start_undetached();
}
@@ -568,28 +588,12 @@
if (rt != RT_OK)
std::cerr << "failed to enable realtime scheduling\n";
#endif
-
+
// This is the first code to run in the new thread context.
- while(d_keep_running)
- d_u2->loop_body();
+ d_u2->bg_loop();
return 0;
}
-void
-usrp2_thread::stop()
-{
- if (USRP2_DEBUG)
- std::cerr << "usrp2_thread::stop: joining background thread "
- << this << std::endl;
- d_keep_running = false;
-
- void *dummy_status;
- join(&dummy_status);
-
- if (USRP2_DEBUG)
- std::cerr << "usrp2_thread::stop: join returned" << std::endl;
-}
-
} // namespace usrp2
Modified: usrp2/trunk/host-ng/lib/usrp2_thread.h
===================================================================
--- usrp2/trunk/host-ng/lib/usrp2_thread.h 2008-06-03 22:05:51 UTC (rev
8550)
+++ usrp2/trunk/host-ng/lib/usrp2_thread.h 2008-06-04 18:19:50 UTC (rev
8551)
@@ -33,14 +33,12 @@
{
private:
usrp2::impl *d_u2;
- bool d_keep_running;
public:
usrp2_thread(usrp2::impl *u2);
~usrp2_thread();
void start();
- void stop();
virtual void *run_undetached(void *arg);
};
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Commit-gnuradio] r8551 - in usrp2/trunk/host-ng: apps lib,
jcorgan <=