@@ -31,6 +31,7 @@ typedef struct {
3131 pthread_mutex_t lock;
3232 pthread_cond_t read_cond;
3333 bool read_waiting;
34+ bool closed;
3435 int read_size;
3536 int write_size;
3637 char buffer[BUFFER_SIZE];
@@ -66,25 +67,45 @@ class xdma_sim {
6667 pthread_condattr_init (&cattr);
6768 pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED);
6869 pthread_cond_init (&shm_ptr->read_cond , &cattr);
70+ shm_ptr->closed = false ;
6971 }
7072 }
7173 ~xdma_sim () {
74+ interrupt ();
7275 if (is_host) {
7376 shm_unlink (path);
7477 }
7578 munmap (shm_ptr, sizeof (xdma_shm));
7679 close (shm_fd);
7780 }
81+ void interrupt () {
82+ if (shm_ptr == nullptr ) {
83+ return ;
84+ }
85+ pthread_mutex_lock (&shm_ptr->lock );
86+ shm_ptr->closed = true ;
87+ shm_ptr->read_waiting = false ;
88+ pthread_cond_broadcast (&shm_ptr->read_cond );
89+ pthread_mutex_unlock (&shm_ptr->lock );
90+ }
7891 int read (char *buf, size_t size) {
7992 assert (size <= BUFFER_SIZE);
8093 pthread_mutex_lock (&shm_ptr->lock );
8194
95+ if (shm_ptr->closed ) {
96+ pthread_mutex_unlock (&shm_ptr->lock );
97+ return 0 ;
98+ }
8299 shm_ptr->read_waiting = true ;
83100 shm_ptr->write_size = 0 ;
84101 shm_ptr->read_size = size;
85- while (shm_ptr->write_size < size) {
102+ while (shm_ptr->write_size < size && !shm_ptr-> closed ) {
86103 pthread_cond_wait (&shm_ptr->read_cond , &shm_ptr->lock );
87104 }
105+ if (shm_ptr->closed ) {
106+ pthread_mutex_unlock (&shm_ptr->lock );
107+ return 0 ;
108+ }
88109 size_t to_copy = size < shm_ptr->write_size ? size : shm_ptr->write_size ;
89110 memcpy (buf, shm_ptr->buffer , to_copy);
90111
@@ -94,10 +115,14 @@ class xdma_sim {
94115 }
95116 int write (const char *buf, unsigned char tlast, size_t size) {
96117 pthread_mutex_lock (&shm_ptr->lock );
97- while (!shm_ptr->read_waiting ) {
118+ while (!shm_ptr->read_waiting && !shm_ptr-> closed ) {
98119 pthread_mutex_unlock (&shm_ptr->lock );
99120 pthread_mutex_lock (&shm_ptr->lock );
100121 }
122+ if (shm_ptr->closed ) {
123+ pthread_mutex_unlock (&shm_ptr->lock );
124+ return 0 ;
125+ }
101126 size_t space = shm_ptr->read_size - shm_ptr->write_size ;
102127 size_t to_write = size < space ? size : space;
103128
@@ -128,6 +153,12 @@ void xdma_sim_close(int channel) {
128153 xsim[channel] = nullptr ;
129154}
130155
156+ void xdma_sim_interrupt (int channel) {
157+ if (xsim[channel] != nullptr ) {
158+ xsim[channel]->interrupt ();
159+ }
160+ }
161+
131162int xdma_sim_read (int channel, char *buf, size_t size) {
132163 return xsim[channel]->read (buf, size);
133164}
0 commit comments