Skip to content

Commit 0ac92b9

Browse files
committed
Back to basic - removed volatile and atomic
1 parent 658dcbe commit 0ac92b9

File tree

8 files changed

+48
-69
lines changed

8 files changed

+48
-69
lines changed

src/handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static void session_free(const struct ref *refcount)
101101

102102
static void session_init(struct client_session *session, const char *session_id)
103103
{
104-
session->inflights = ATOMIC_VAR_INIT(0);
104+
session->inflights = 0;
105105
session->next_free_mid = 1;
106106
session->subscriptions = list_new(NULL);
107107
session->outgoing_msgs = list_new(NULL);

src/memory.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
*/
2727

2828
#include "memory.h"
29-
#include <stdatomic.h>
3029
#include <stdlib.h>
3130
#include <string.h>
3231

33-
static atomic_size_t memory = ATOMIC_VAR_INIT(0);
32+
static size_t memory = 0;
3433

3534
/*
3635
* Custom malloc function, allocate a defined size of bytes plus

src/mqtt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,6 @@ struct mqtt_packet *mqtt_packet_alloc(u8 byte)
687687
struct mqtt_packet *packet = try_alloc(sizeof(*packet));
688688
packet->header = (union mqtt_header){.byte = byte};
689689
packet->refcount = (struct ref){mqtt_packet_free, 0};
690-
packet->refcount.count = ATOMIC_VAR_INIT(0);
690+
packet->refcount.count = 0;
691691
return packet;
692692
}

src/ref.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@
3535

3636
#pragma once
3737

38-
#include <stdatomic.h>
39-
4038
struct ref {
4139
void (*free)(const struct ref *);
42-
volatile atomic_int count;
40+
int count;
4341
};
4442

4543
static inline void ref_inc(const struct ref *ref)

src/server.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
#include <pthread.h>
4040
#include <unistd.h>
4141

42-
pthread_mutex_t mutex;
43-
4442
/*
4543
* Auxiliary structure to be used as init argument for eventloop, fd is the
4644
* listening socket we want to share between multiple instances, cronjobs is
@@ -49,7 +47,7 @@ pthread_mutex_t mutex;
4947
*/
5048
struct listen_payload {
5149
int fd;
52-
atomic_bool cronjobs;
50+
bool cronjobs;
5351
};
5452

5553
/* Seconds in a Sol, easter egg */
@@ -374,21 +372,20 @@ static void client_init(struct client *client)
374372
client->client_id[0] = '\0';
375373
client->status = WAITING_HEADER;
376374
client->rc = 0;
377-
client->rpos = ATOMIC_VAR_INIT(0);
378-
client->read = ATOMIC_VAR_INIT(0);
379-
client->toread = ATOMIC_VAR_INIT(0);
375+
client->rpos = 0;
376+
client->read = 0;
377+
client->toread = 0;
380378
if (!client->rbuf)
381379
client->rbuf =
382380
try_calloc(conf->max_request_size, sizeof(unsigned char));
383-
client->wrote = ATOMIC_VAR_INIT(0);
384-
client->towrite = ATOMIC_VAR_INIT(0);
381+
client->wrote = 0;
382+
client->towrite = 0;
385383
if (!client->wbuf)
386384
client->wbuf =
387385
try_calloc(conf->max_request_size, sizeof(unsigned char));
388386
client->last_seen = time(NULL);
389387
client->has_lwt = false;
390388
client->session = NULL;
391-
pthread_mutex_init(&client->mutex, NULL);
392389
}
393390

394391
/*
@@ -930,7 +927,6 @@ int start_server(const char *addr, const char *port)
930927
BASE_CLIENTS_NUM);
931928
server.clients_map = NULL;
932929
server.sessions = NULL;
933-
pthread_mutex_init(&mutex, NULL);
934930

935931
if (conf->allow_anonymous == false)
936932
if (!config_read_passwd_file(conf->password_file, &server.auths))
@@ -958,7 +954,7 @@ int start_server(const char *addr, const char *port)
958954
log_info("Server start");
959955
info.start_time = time(NULL);
960956

961-
struct listen_payload loop_start = {sfd, ATOMIC_VAR_INIT(false)};
957+
struct listen_payload loop_start = {sfd, false};
962958

963959
loop_start.cronjobs = true;
964960
// start eventloop, could be spread on multiple threads
@@ -973,8 +969,6 @@ int start_server(const char *addr, const char *port)
973969
SSL_CTX_free(server.ssl_ctx);
974970
openssl_cleanup();
975971
}
976-
pthread_mutex_destroy(&mutex);
977-
978972
log_info("Sol v%s exiting", VERSION);
979973

980974
return SOL_OK;

src/server.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,33 @@ struct io_event {
5858
/* Global informations statistics structure */
5959
struct sol_info {
6060
/* Number of clients currently connected */
61-
atomic_size_t active_connections;
61+
size_t active_connections;
6262
/* Total number of clients connected since the start */
63-
atomic_size_t total_connections;
63+
size_t total_connections;
6464
/* Total number of sent messages */
65-
atomic_size_t messages_sent;
65+
size_t messages_sent;
6666
/* Total number of received messages */
67-
atomic_size_t messages_recv;
67+
size_t messages_recv;
6868
/* Timestamp of the start time */
69-
atomic_size_t start_time;
69+
size_t start_time;
7070
/* Seconds passed since the start */
71-
atomic_size_t uptime;
71+
size_t uptime;
7272
/* Total number of bytes received */
73-
atomic_size_t bytes_sent;
73+
size_t bytes_sent;
7474
/* Total number of bytes sent out */
75-
atomic_size_t bytes_recv;
75+
size_t bytes_recv;
7676
};
7777

7878
#define INIT_INFO \
7979
do { \
80-
info.active_connections = ATOMIC_VAR_INIT(0); \
81-
info.total_connections = ATOMIC_VAR_INIT(0); \
82-
info.messages_sent = ATOMIC_VAR_INIT(0); \
83-
info.messages_recv = ATOMIC_VAR_INIT(0); \
84-
info.start_time = ATOMIC_VAR_INIT(0); \
85-
info.uptime = ATOMIC_VAR_INIT(0); \
86-
info.bytes_sent = ATOMIC_VAR_INIT(0); \
87-
info.bytes_recv = ATOMIC_VAR_INIT(0); \
80+
info.active_connections = 0; \
81+
info.total_connections = 0; \
82+
info.messages_sent = 0; \
83+
info.messages_recv = 0; \
84+
info.start_time = 0; \
85+
info.uptime = 0; \
86+
info.bytes_sent = 0; \
87+
info.bytes_recv = 0; \
8888
} while (0)
8989

9090
/*

src/sol_internal.h

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "network.h"
3232
#include "trie.h"
3333
#include "uthash.h"
34-
#include <stdatomic.h>
3534
#include <time.h>
3635

3736
/* Generic return codes without a defined purpose */
@@ -174,22 +173,21 @@ struct client {
174173
*ctx; /* An event context refrence mostly used to fire write events */
175174
int rc; /* Return code of the message just handled */
176175
int status; /* Current status of the client (state machine) */
177-
volatile atomic_int rpos; /* The nr of bytes to skip after a complete
178-
* packet has * been read. This because according
179-
* to MQTT, length is encoded on multiple bytes
180-
* according to it's size, using continuation bit
181-
* as a technique to encode it. We don't want to
182-
* decode the length two times when we already
183-
* know it, so we need an offset to know where
184-
* the actual packet will start
185-
*/
186-
volatile atomic_size_t read; /* The number of bytes already read */
187-
volatile atomic_size_t
188-
toread; /* The number of bytes that have to be read */
189-
unsigned char *rbuf; /* The reading buffer */
190-
volatile atomic_size_t wrote; /* The number of bytes already written */
191-
volatile atomic_size_t towrite; /* The number of bytes we have to write */
192-
unsigned char *wbuf; /* The writing buffer */
176+
int rpos; /* The nr of bytes to skip after a complete
177+
* packet has * been read. This because for
178+
* MQTT, length is encoded on multiple bytes
179+
* according to it's size, using a continuation bit
180+
* as a technique to encode it. We don't want to
181+
* decode the length two times when we already
182+
* know it, so we need an offset to know where
183+
* the actual packet will start
184+
*/
185+
size_t read; /* The number of bytes already read */
186+
size_t toread; /* The number of bytes that have to be read */
187+
unsigned char *rbuf; /* The reading buffer */
188+
size_t wrote; /* The number of bytes already written */
189+
size_t towrite; /* The number of bytes we have to write */
190+
unsigned char *wbuf; /* The writing buffer */
193191
char client_id[MQTT_CLIENT_ID_LEN]; /* The client ID according to MQTT specs
194192
*/
195193
struct connection conn; /* A connection structure, takes care of plain or
@@ -201,11 +199,9 @@ struct client {
201199
bool connected; /* States if the client has already processed a connection
202200
packet */
203201
bool has_lwt; /* States if the connection packet carried a LWT message */
204-
bool clean_session; /* States if the connection packet was set to clean
205-
session */
206-
pthread_mutex_t mutex; /* Inner lock for the client, this avoid
207-
race-conditions on shared parts */
208-
UT_hash_handle hh; /* UTHASH handle, needed to use UTHASH macros */
202+
bool clean_session; /* States if the connection packet was set to clean
203+
session */
204+
UT_hash_handle hh; /* UTHASH handle, needed to use UTHASH macros */
209205
};
210206

211207
/*
@@ -226,9 +222,9 @@ struct client_session {
226222
structs */
227223
List *outgoing_msgs; /* Outgoing messages during disconnection time, stored
228224
as mqtt_packet pointers */
229-
volatile atomic_ushort inflights; /* Just a counter stating the presence of
230-
inflight messages */
231-
bool clean_session; /* Clean session flag */
225+
unsigned short inflights; /* Just a counter stating the presence of
226+
inflight messages */
227+
bool clean_session; /* Clean session flag */
232228
char session_id[MQTT_CLIENT_ID_LEN]; /* The client_id the session refers to
233229
*/
234230
struct mqtt_packet
@@ -241,13 +237,6 @@ struct client_session {
241237
refcount; /* Reference counting struct, to share the struct easily */
242238
};
243239

244-
/*
245-
* Simple mutex for contexted critical areas, mainly used in the handlers
246-
* module, in server the only useful use are when creating and deactivating
247-
* clients
248-
*/
249-
extern pthread_mutex_t mutex;
250-
251240
struct server;
252241

253242
/*

src/util.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
// #include <crypt.h>
3434
#include <ctype.h>
3535
#include <stdarg.h>
36-
#include <stdatomic.h>
3736
#include <stdlib.h>
3837
#include <sys/resource.h>
3938
#include <sys/time.h>

0 commit comments

Comments
 (0)