Skip to content

Commit 001445b

Browse files
committed
A few fixes, plus don't wait forever.
1 parent 64f12c3 commit 001445b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

arx_control/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ CXXFLAGS := -O2 -Wall -std=c++17
3636
#------------------------------------------------------------------------------
3737
LDFLAGS := -Llibatmega -latmega_static
3838
CXXFLAGS += -Ilibatmega
39-
LDFLAGS += -lm
39+
LDFLAGS += -lm -lpthread
4040
ifeq ($(OS),Linux)
4141
LDFLAGS += -ludev
4242
else ifeq ($(OS),Darwin)

arx_control/aspCommon.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,25 @@ bool ATmega::open() {
9090

9191
return found;
9292
} else {
93-
_lock = sem_open(_sn.c_str(), O_CREAT);
93+
_lock = sem_open(_sn.c_str(), O_CREAT, 0660, 1);
9494
if( _lock == SEM_FAILED ) {
9595
_lock = NULL;
9696
return false;
9797
}
98-
sem_wait(_lock);
98+
99+
double elapsed_time = 0.0;
100+
auto start_time = std::chrono::steady_clock::now();
101+
while( sem_trywait(_lock) == -1 ) {
102+
std::this_thread::sleep_for(std::chrono::milliseconds(100);
103+
104+
auto current_time = std::chrono::steady_clock::now();
105+
elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time).count();
106+
107+
if( elapsed_time > 10000 ) {
108+
std::cerr << "Failed to acquire lock within 10 s" << std::endl;
109+
return false;
110+
}
111+
}
99112

100113
for(std::string const& dev_name: atmega::find_devices()) {
101114
int open_attempts = 0;

0 commit comments

Comments
 (0)