File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ CXXFLAGS := -O2 -Wall -std=c++17
3636# ------------------------------------------------------------------------------
3737LDFLAGS := -Llibatmega -latmega_static
3838CXXFLAGS += -Ilibatmega
39- LDFLAGS += -lm
39+ LDFLAGS += -lm -lpthread
4040ifeq ($(OS ) ,Linux)
4141 LDFLAGS += -ludev
4242else ifeq ($(OS),Darwin)
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments