ref: 968ac77631ed370269bfd11dec3ba15679585491
parent: c2ea79dba50215eaeefba1c4cffba868fea8c624
author: Bxxh7 <StormBorower@gmail.com>
date: Fri Nov 5 15:09:43 EDT 2021
removed clients_count and its mutex: khar.c
--- a/khar.c
+++ b/khar.c
@@ -15,21 +15,6 @@
#define BUF_SIZE 1024
-
-void atomic_decr(pthread_mutex_t *mutex, int *resource) {
- pthread_mutex_lock(mutex);
- *resource = *resource - 1;
- pthread_mutex_unlock(mutex);
-}
-
-void atomic_incr(pthread_mutex_t *mutex, int *resource) {
- pthread_mutex_lock(mutex);
- *resource = *resource + 1;
- pthread_mutex_unlock(mutex);
-}
-
-
-
typedef struct {
int sockfd;
struct sockaddr_storage claddr;
@@ -37,9 +22,7 @@
} client_t;
client_t clients[MAX_CLIENT];
-int clients_count = 0;
-pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
char *
printable_addr(struct sockaddr * saddr, socklen_t saddr_len) {
@@ -106,7 +89,7 @@
}
void write_all(char *msg, int length) {
- for (int i = 0; i < clients_count; i++) {
+ for (int i = 0; i < MAX_CLIENT; i++) {
if (clients[i].sockfd != -1)
write(clients[i].sockfd, msg, length);
}
@@ -189,8 +172,6 @@
free(all_khar_msg);
- atomic_decr(&count_mutex, &clients_count);
-
free(arg);
pthread_detach(pthread_self());
}
@@ -216,7 +197,14 @@
if (cfd == -1)
continue;
- if (clients_count + 1 >= MAX_CLIENT) {
+ for (int i = 0; i <= MAX_CLIENT; i++) {
+ if (clients[i].sockfd == -1) {
+ free_index = i;
+ break;
+ }
+ }
+
+ if (free_index > MAX_CLIENT) {
char *a;
a = printable_addr((struct sockaddr *)&claddr, claddr_len);
@@ -228,13 +216,6 @@
}
- for (int i = 0; i <= MAX_CLIENT; i++) {
- if (clients[i].sockfd == -1) {
- free_index = i;
- break;
- }
- }
-
clients[free_index].claddr = claddr;
clients[free_index].sockfd = cfd;
@@ -255,6 +236,5 @@
pthread_create(&tid, NULL, handle_client, (void*) copy);
- atomic_incr(&count_mutex, &clients_count);
}
}