1111#include <unistd.h>
1212#include <stdio.h>
1313#include <stdint.h>
14+ #include <stdbool.h>
1415#include <poll.h>
1516
1617#include <sys/socket.h>
2122
2223#include "../ipc.h"
2324
24- #define TX_BUFFER_SIZE 2048
25- #define RX_BUFFER_SIZE 2048
25+ #define TX_BUFFER_SIZE 32
26+ #define RX_BUFFER_SIZE 32
2627
2728struct client {
2829 int socket ;
@@ -53,16 +54,12 @@ static void *wait_thread(void *arg)
5354 /* Call the actual function */
5455 waiter -> result = ocre_container_wait (waiter -> container , & waiter -> exit_status );
5556
56- fprintf (stderr , "container wait returned\n" );
57-
5857 pthread_mutex_lock (& mutex );
5958
6059 LL_DELETE (waiters , waiter );
6160
6261 pthread_mutex_unlock (& mutex );
6362
64- fprintf (stderr , "removed from waiters\n" );
65-
6663 /* Encode response */
6764 ZCBOR_STATE_E (enc_state , 0 , tx_buf , TX_BUFFER_SIZE , 0 );
6865
@@ -99,8 +96,6 @@ static void *wait_thread(void *arg)
9996 perror ("send" );
10097 }
10198
102- fprintf (stderr , "Response sent from waiter (%d bytes)\n" , response_len );
103-
10499 if (close (client -> socket ) < 0 ) {
105100 perror ("close" );
106101 }
@@ -191,19 +186,17 @@ static void *socket_thread(void *arg)
191186 return NULL ;
192187}
193188
194- static struct waiter * waiter_get_or_new (struct ocre_container * container )
189+ static struct waiter * waiter_get_or_new_locked (struct ocre_container * container )
195190{
196- struct waiter * waiter ;
191+ struct waiter * waiter = NULL ;
192+ pthread_attr_t attr ;
197193
198194 LL_SEARCH_SCALAR (waiters , waiter , container , container );
199195 if (waiter ) {
200196 fprintf (stderr , "Waiter already exists for container %p\n" , container );
201- // pthread_mutex_unlock(&mutex);
202197 return waiter ;
203198 }
204199
205- // pthread_mutex_unlock(&mutex);
206-
207200 waiter = malloc (sizeof (struct waiter ));
208201 if (!waiter ) {
209202 fprintf (stderr , "Failed to allocate memory for waiter\n" );
@@ -245,17 +238,43 @@ static struct waiter *waiter_get_or_new(struct ocre_container *container)
245238 goto error_thread ;
246239 }
247240
248- pthread_mutex_unlock (& waiter -> mutex );
249-
250- // pthread_mutex_lock(&mutex);
251-
252241 LL_APPEND (waiters , waiter );
253242
254243 return waiter ;
244+
245+ error_thread :
246+ rc = pthread_mutex_lock (& waiter -> mutex );
247+ if (rc ) {
248+ fprintf (stderr , "Failed to lock mutex: rc=%d" , rc );
249+ }
250+
251+ waiter -> finished = true;
252+
253+ rc = pthread_mutex_unlock (& waiter -> mutex );
254+ if (rc ) {
255+ fprintf (stderr , "Failed to unlock mutex: rc=%d" , rc );
256+ }
257+
258+ pthread_join (waiter -> socket_thread , NULL );
259+
260+ error_attr :
261+ rc = pthread_attr_destroy (& attr );
262+ if (rc ) {
263+ fprintf (stderr , "Failed to destroy thread attributes: rc=%d" , rc );
264+ }
265+
266+ error_mutex :
267+ pthread_mutex_destroy (& waiter -> mutex );
268+
269+ error_waiter :
270+ free (waiter );
271+ return NULL ;
255272}
256273
257274static int waiter_add_client (struct waiter * waiter , int socket )
258275{
276+ int rc ;
277+
259278 struct client * client = malloc (sizeof (struct client ));
260279 if (!client ) {
261280 fprintf (stderr , "Failed to allocate memory for client\n" );
@@ -281,13 +300,19 @@ static int waiter_add_client(struct waiter *waiter, int socket)
281300 }
282301
283302 return 0 ;
303+
304+ error_client :
305+ free (client );
306+ return -1 ;
284307}
285308
286309int container_waiter_add_client (struct ocre_container * container , int socket )
287310{
288311 int ret = -1 ;
312+
289313 pthread_mutex_lock (& mutex );
290- struct waiter * waiter = waiter_get_or_new (container );
314+
315+ struct waiter * waiter = waiter_get_or_new_locked (container );
291316
292317 if (!waiter ) {
293318 fprintf (stderr , "Failed to get or create waiter for container %p\n" , container );
0 commit comments