check rvalues from pthread_mutex_ operations in examples

This commit is contained in:
Christian Grothoff
2023-09-25 21:26:31 +02:00
parent e42c783bb8
commit 2212a37256
2 changed files with 251 additions and 250 deletions
+75 -80
View File
@@ -188,7 +188,8 @@
" {\n" \ " {\n" \
" /* Determine the base url (for http:/" "/ this is ws:/" "/ for https:/" \ " /* Determine the base url (for http:/" "/ this is ws:/" "/ for https:/" \
"/ this must be wss:/" "/) */\n" \ "/ this must be wss:/" "/) */\n" \
" baseUrl = 'ws' + (window.location.protocol === 'https:' ? 's' : '') + ':/" "/' + window.location.host + '/ChatServerWebSocket';\n" \ " baseUrl = 'ws' + (window.location.protocol === 'https:' ? 's' : '') + ':/" \
"/' + window.location.host + '/ChatServerWebSocket';\n" \
" chat_generate();\n" \ " chat_generate();\n" \
" chat_connect();\n" \ " chat_connect();\n" \
" }\n" \ " }\n" \
@@ -586,7 +587,7 @@
" let message = [ ];\n" \ " let message = [ ];\n" \
" /* message type */ \n" \ " /* message type */ \n" \
" let j = 0;\n" \ " let j = 0;\n" \
" let i = byteData.indexOf(0x7C, j); /* | = 0x7C;*/ \n"\ " let i = byteData.indexOf(0x7C, j); /* | = 0x7C;*/ \n" \
" if(i < 0)\n" \ " if(i < 0)\n" \
" return;\n" \ " return;\n" \
" message.push(decoder.decode(byteData.slice(0, i)));\n" \ " message.push(decoder.decode(byteData.slice(0, i)));\n" \
@@ -737,8 +738,8 @@ send_all (struct ConnectedUser *cu,
ssize_t ret; ssize_t ret;
size_t off; size_t off;
if (0 == pthread_mutex_lock (&cu->send_mutex)) if (0 != pthread_mutex_lock (&cu->send_mutex))
{ abort ();
for (off = 0; off < len; off += ret) for (off = 0; off < len; off += ret)
{ {
ret = send (cu->fd, ret = send (cu->fd,
@@ -757,8 +758,8 @@ send_all (struct ConnectedUser *cu,
if (0 == ret) if (0 == ret)
break; break;
} }
pthread_mutex_unlock (&cu->send_mutex); if (0 != pthread_mutex_unlock (&cu->send_mutex))
} abort ();
} }
@@ -806,7 +807,7 @@ chat_addmessage (size_t from_user_id,
if (0 != needs_lock) if (0 != needs_lock)
{ {
if (0 != pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
return 1; abort ();
} }
/* add the new message to the global message list */ /* add the new message to the global message list */
@@ -819,7 +820,8 @@ chat_addmessage (size_t from_user_id,
{ {
free (message); free (message);
if (0 != needs_lock) if (0 != needs_lock)
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return 1; return 1;
} }
messages_[message_count] = message; messages_[message_count] = message;
@@ -834,7 +836,8 @@ chat_addmessage (size_t from_user_id,
if (0 != needs_lock) if (0 != needs_lock)
{ {
if (0 != needs_lock) if (0 != needs_lock)
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
} }
return 0; return 0;
} }
@@ -854,7 +857,7 @@ chat_clearmessages (int needs_lock)
if (0 != needs_lock) if (0 != needs_lock)
{ {
if (0 != pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
return 1; abort ();
} }
/* update the clean counter and check whether we need cleaning */ /* update the clean counter and check whether we need cleaning */
@@ -864,7 +867,8 @@ chat_clearmessages (int needs_lock)
/* no cleanup required */ /* no cleanup required */
if (0 != needs_lock) if (0 != needs_lock)
{ {
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
} }
return 0; return 0;
} }
@@ -916,7 +920,8 @@ chat_clearmessages (int needs_lock)
/* unlock the global mutex if needed */ /* unlock the global mutex if needed */
if (0 != needs_lock) if (0 != needs_lock)
{ {
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
} }
return 0; return 0;
} }
@@ -947,10 +952,7 @@ chat_adduser (struct ConnectedUser *cu)
/* lock the mutex */ /* lock the mutex */
if (0 != pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
free (data);
return 1;
}
/* inform the other chat users about the new user */ /* inform the other chat users about the new user */
if (0 != chat_addmessage (0, if (0 != chat_addmessage (0,
0, 0,
@@ -960,7 +962,8 @@ chat_adduser (struct ConnectedUser *cu)
0)) 0))
{ {
free (data); free (data);
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return 1; return 1;
} }
free (data); free (data);
@@ -973,7 +976,8 @@ chat_adduser (struct ConnectedUser *cu)
if (NULL == users_) if (NULL == users_)
{ {
/* realloc failed */ /* realloc failed */
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return 1; return 1;
} }
users_[user_count] = cu; users_[user_count] = cu;
@@ -985,7 +989,8 @@ chat_adduser (struct ConnectedUser *cu)
cu->next_message_index = message_count; cu->next_message_index = message_count;
/* unlock the mutex */ /* unlock the mutex */
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return 0; return 0;
} }
@@ -1014,10 +1019,7 @@ chat_removeuser (struct ConnectedUser *cu)
/* lock the mutex */ /* lock the mutex */
if (0 != pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
free (data);
return 1;
}
/* inform the other chat users that the user is gone */ /* inform the other chat users that the user is gone */
int got_error = 0; int got_error = 0;
if (0 != chat_addmessage (0, 0, data, data_len, 0, 0)) if (0 != chat_addmessage (0, 0, data, data_len, 0, 0))
@@ -1045,7 +1047,8 @@ chat_removeuser (struct ConnectedUser *cu)
got_error = 1; got_error = 1;
/* unlock the mutex */ /* unlock the mutex */
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return got_error; return got_error;
} }
@@ -1066,9 +1069,7 @@ chat_renameuser (struct ConnectedUser *cu,
{ {
/* lock the mutex */ /* lock the mutex */
if (0 != pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
return 1;
}
/* check whether the name is already in use */ /* check whether the name is already in use */
for (size_t i = 0; i < user_count; ++i) for (size_t i = 0; i < user_count; ++i)
@@ -1078,7 +1079,8 @@ chat_renameuser (struct ConnectedUser *cu,
if ((users[i]->user_name_len == new_name_len) && if ((users[i]->user_name_len == new_name_len) &&
(0 == strcasecmp (users[i]->user_name, new_name))) (0 == strcasecmp (users[i]->user_name, new_name)))
{ {
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return 2; return 2;
} }
} }
@@ -1101,7 +1103,8 @@ chat_renameuser (struct ConnectedUser *cu,
if (0 != chat_addmessage (0, 0, data, data_len, 0, 0)) if (0 != chat_addmessage (0, 0, data, data_len, 0, 0))
{ {
free (data); free (data);
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return 1; return 1;
} }
free (data); free (data);
@@ -1112,7 +1115,8 @@ chat_renameuser (struct ConnectedUser *cu,
cu->user_name_len = new_name_len; cu->user_name_len = new_name_len;
/* unlock the mutex */ /* unlock the mutex */
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return 0; return 0;
} }
@@ -1411,8 +1415,8 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser *cu,
case 3: case 3:
/* ping */ /* ping */
{ {
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
/* check whether the to_user exists */ /* check whether the to_user exists */
struct ConnectedUser *ping_user = NULL; struct ConnectedUser *ping_user = NULL;
for (size_t k = 0; k < user_count; ++k) for (size_t k = 0; k < user_count; ++k)
@@ -1442,17 +1446,8 @@ connecteduser_parse_received_websocket_stream (struct ConnectedUser *cu,
pthread_cond_signal (&ping_user->wake_up_sender); pthread_cond_signal (&ping_user->wake_up_sender);
} }
} }
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
} abort ();
else
{
chat_addmessage (0,
from_user_id,
"error||Error while pinging.",
27,
0,
1);
}
} }
break; break;
@@ -1591,8 +1586,8 @@ connecteduser_send_messages (void *cls)
struct ConnectedUser *cu = cls; struct ConnectedUser *cu = cls;
/* the main loop of sending messages requires to lock the mutex */ /* the main loop of sending messages requires to lock the mutex */
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
for (;;) for (;;)
{ {
/* loop while not all messages processed */ /* loop while not all messages processed */
@@ -1611,14 +1606,16 @@ connecteduser_send_messages (void *cls)
MHD_upgrade_action (urh, MHD_upgrade_action (urh,
MHD_UPGRADE_ACTION_CLOSE); MHD_UPGRADE_ACTION_CLOSE);
} }
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return NULL; return NULL;
} }
else if (1 == cu->disconnect) else if (1 == cu->disconnect)
{ {
/* The sender thread shall close. */ /* The sender thread shall close. */
/* This is only requested by the receive thread, so we can just leave. */ /* This is only requested by the receive thread, so we can just leave. */
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
return NULL; return NULL;
} }
else if (1 == cu->ping_status) else if (1 == cu->ping_status)
@@ -1643,14 +1640,13 @@ connecteduser_send_messages (void *cls)
/* send the data via the TCP/IP socket and */ /* send the data via the TCP/IP socket and */
/* unlock the mutex while sending */ /* unlock the mutex while sending */
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
send_all (cu, send_all (cu,
frame_data, frame_data,
frame_len); frame_len);
if (0 != pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
return NULL;
}
} }
MHD_websocket_free (cu->ws, frame_data); MHD_websocket_free (cu->ws, frame_data);
} }
@@ -1691,7 +1687,8 @@ connecteduser_send_messages (void *cls)
/* send the data via the TCP/IP socket and */ /* send the data via the TCP/IP socket and */
/* unlock the mutex while sending */ /* unlock the mutex while sending */
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
if (MHD_WEBSOCKET_STATUS_OK == er) if (MHD_WEBSOCKET_STATUS_OK == er)
{ {
send_all (cu, send_all (cu,
@@ -1701,9 +1698,7 @@ connecteduser_send_messages (void *cls)
MHD_websocket_free (cu->ws, MHD_websocket_free (cu->ws,
frame_data); frame_data);
if (0 != pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
return NULL;
}
/* check whether there are still pending messages */ /* check whether there are still pending messages */
all_messages_read = (cu->next_message_index < message_count) ? 0 : 1; all_messages_read = (cu->next_message_index < message_count) ? 0 : 1;
} }
@@ -1720,7 +1715,6 @@ connecteduser_send_messages (void *cls)
/* lock the mutex after waiting */ /* lock the mutex after waiting */
pthread_cond_wait (&cu->wake_up_sender, &chat_mutex); pthread_cond_wait (&cu->wake_up_sender, &chat_mutex);
} }
}
return NULL; return NULL;
} }
@@ -1817,8 +1811,8 @@ connecteduser_receive_messages (void *cls)
size_t init_users_len = 0; size_t init_users_len = 0;
/* first collect all users without sending (so the mutex isn't locked too long) */ /* first collect all users without sending (so the mutex isn't locked too long) */
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
if (0 < user_count) if (0 < user_count)
{ {
init_users = (struct UserInit *) malloc (user_count * sizeof (struct init_users = (struct UserInit *) malloc (user_count * sizeof (struct
@@ -1846,8 +1840,8 @@ connecteduser_receive_messages (void *cls)
} }
} }
} }
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
} abort ();
/* then send all users to the connected client */ /* then send all users to the connected client */
for (size_t i = 0; i < init_users_len; ++i) for (size_t i = 0; i < init_users_len; ++i)
@@ -1922,13 +1916,14 @@ connecteduser_receive_messages (void *cls)
cu->extra_in_size)) cu->extra_in_size))
{ {
chat_removeuser (cu); chat_removeuser (cu);
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
cu->disconnect = 1; cu->disconnect = 1;
pthread_cond_signal (&cu->wake_up_sender); pthread_cond_signal (&cu->wake_up_sender);
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
pthread_join (pt, NULL); pthread_join (pt, NULL);
}
struct MHD_UpgradeResponseHandle *urh = cu->urh; struct MHD_UpgradeResponseHandle *urh = cu->urh;
if (NULL != urh) if (NULL != urh)
{ {
@@ -1967,13 +1962,13 @@ connecteduser_receive_messages (void *cls)
{ {
/* A websocket protocol error occurred */ /* A websocket protocol error occurred */
chat_removeuser (cu); chat_removeuser (cu);
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
cu->disconnect = 1; cu->disconnect = 1;
pthread_cond_signal (&cu->wake_up_sender); pthread_cond_signal (&cu->wake_up_sender);
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
pthread_join (pt, NULL); pthread_join (pt, NULL);
}
struct MHD_UpgradeResponseHandle *urh = cu->urh; struct MHD_UpgradeResponseHandle *urh = cu->urh;
if (NULL != urh) if (NULL != urh)
{ {
@@ -1993,13 +1988,13 @@ connecteduser_receive_messages (void *cls)
/* cleanup */ /* cleanup */
chat_removeuser (cu); chat_removeuser (cu);
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
cu->disconnect = 1; cu->disconnect = 1;
pthread_cond_signal (&cu->wake_up_sender); pthread_cond_signal (&cu->wake_up_sender);
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
abort ();
pthread_join (pt, NULL); pthread_join (pt, NULL);
}
struct MHD_UpgradeResponseHandle *urh = cu->urh; struct MHD_UpgradeResponseHandle *urh = cu->urh;
if (NULL != urh) if (NULL != urh)
{ {
@@ -2322,16 +2317,16 @@ main (int argc,
return 1; return 1;
(void) getc (stdin); (void) getc (stdin);
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
disconnect_all = 1; disconnect_all = 1;
for (size_t i = 0; i < user_count; ++i) for (size_t i = 0; i < user_count; ++i)
pthread_cond_signal (&users[i]->wake_up_sender); pthread_cond_signal (&users[i]->wake_up_sender);
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
} abort ();
sleep (2); sleep (2);
if (0 == pthread_mutex_lock (&chat_mutex)) if (0 != pthread_mutex_lock (&chat_mutex))
{ abort ();
for (size_t i = 0; i < user_count; ++i) for (size_t i = 0; i < user_count; ++i)
{ {
struct MHD_UpgradeResponseHandle *urh = users[i]->urh; struct MHD_UpgradeResponseHandle *urh = users[i]->urh;
@@ -2342,8 +2337,8 @@ main (int argc,
MHD_UPGRADE_ACTION_CLOSE); MHD_UPGRADE_ACTION_CLOSE);
} }
} }
pthread_mutex_unlock (&chat_mutex); if (0 != pthread_mutex_unlock (&chat_mutex))
} abort ();
sleep (2); sleep (2);
/* usually we should wait here in a safe way for all threads to disconnect, */ /* usually we should wait here in a safe way for all threads to disconnect, */
+12 -6
View File
@@ -625,7 +625,8 @@ ws_send_frame (MHD_socket sock, const char *msg, size_t length)
} }
response[idx_response] = '\0'; response[idx_response] = '\0';
output = 0; output = 0;
pthread_mutex_lock (&MUTEX); if (0 != pthread_mutex_lock (&MUTEX))
abort ();
for (i = 0; i < MAX_CLIENTS; i++) for (i = 0; i < MAX_CLIENTS; i++)
{ {
isock = CLIENT_SOCKS[i]; isock = CLIENT_SOCKS[i];
@@ -634,7 +635,8 @@ ws_send_frame (MHD_socket sock, const char *msg, size_t length)
output += send_all (isock, response, idx_response); output += send_all (isock, response, idx_response);
} }
} }
pthread_mutex_unlock (&MUTEX); if (0 != pthread_mutex_unlock (&MUTEX))
abort ();
free (response); free (response);
return (ssize_t) output; return (ssize_t) output;
} }
@@ -759,7 +761,8 @@ run_usock (void *cls)
} }
} }
} }
pthread_mutex_lock (&MUTEX); if (0 != pthread_mutex_lock (&MUTEX))
abort ();
for (i = 0; i < MAX_CLIENTS; i++) for (i = 0; i < MAX_CLIENTS; i++)
{ {
if (CLIENT_SOCKS[i] == ws->sock) if (CLIENT_SOCKS[i] == ws->sock)
@@ -768,7 +771,8 @@ run_usock (void *cls)
break; break;
} }
} }
pthread_mutex_unlock (&MUTEX); if (0 != pthread_mutex_unlock (&MUTEX))
abort ();
free (ws); free (ws);
MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE); MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE);
return NULL; return NULL;
@@ -798,7 +802,8 @@ uh_cb (void *cls, struct MHD_Connection *con, void *req_cls,
ws->sock = sock; ws->sock = sock;
ws->urh = urh; ws->urh = urh;
sock_overflow = MHD_YES; sock_overflow = MHD_YES;
pthread_mutex_lock (&MUTEX); if (0 != pthread_mutex_lock (&MUTEX))
abort ();
for (i = 0; i < MAX_CLIENTS; i++) for (i = 0; i < MAX_CLIENTS; i++)
{ {
if (MHD_INVALID_SOCKET == CLIENT_SOCKS[i]) if (MHD_INVALID_SOCKET == CLIENT_SOCKS[i])
@@ -808,7 +813,8 @@ uh_cb (void *cls, struct MHD_Connection *con, void *req_cls,
break; break;
} }
} }
pthread_mutex_unlock (&MUTEX); if (0 != pthread_mutex_unlock (&MUTEX))
abort ();
if (sock_overflow) if (sock_overflow)
{ {
free (ws); free (ws);