Skip to content

Commit

Permalink
Fix NetBSD build
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Oct 12, 2024
1 parent c3dc7a6 commit 88b7bb3
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 61 deletions.
5 changes: 4 additions & 1 deletion ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2.0.20 - 2024-10-11
2.0.20 - 2024-10-xx
===================

Broker:
Expand All @@ -9,6 +9,9 @@ Broker:
Client library:
- Fix cmake build on OS X. Closes #3125.

Build:
- Fix build on NetBSD


2.0.19 - 2024-10-02
===================
Expand Down
14 changes: 0 additions & 14 deletions lib/dummypthread.h

This file was deleted.

4 changes: 2 additions & 2 deletions lib/handle_pubackcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ int handle__pubackcomp(struct mosquitto *mosq, const char *type)
}
}

pthread_mutex_lock(&mosq->msgs_out.mutex);
COMPAT_pthread_mutex_lock(&mosq->msgs_out.mutex);
util__increment_send_quota(mosq);
pthread_mutex_unlock(&mosq->msgs_out.mutex);
COMPAT_pthread_mutex_unlock(&mosq->msgs_out.mutex);

rc = packet__read_uint16(&mosq->in_packet, &mid);
if(rc) return rc;
Expand Down
6 changes: 1 addition & 5 deletions lib/mosquitto_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#endif
#include <stdlib.h>

#if defined(WITH_THREADING) && !defined(WITH_BROKER)
# include <pthread.h>
#else
# include <dummypthread.h>
#endif
#include <pthread_compat.h>

#ifdef WITH_SRV
# include <ares.h>
Expand Down
46 changes: 23 additions & 23 deletions lib/packet_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ void packet__cleanup_all_no_locks(struct mosquitto *mosq)

void packet__cleanup_all(struct mosquitto *mosq)
{
pthread_mutex_lock(&mosq->current_out_packet_mutex);
pthread_mutex_lock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_lock(&mosq->current_out_packet_mutex);
COMPAT_pthread_mutex_lock(&mosq->out_packet_mutex);

packet__cleanup_all_no_locks(mosq);

pthread_mutex_unlock(&mosq->out_packet_mutex);
pthread_mutex_unlock(&mosq->current_out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->current_out_packet_mutex);
}


Expand All @@ -151,7 +151,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet)
packet->to_process = packet->packet_length;

packet->next = NULL;
pthread_mutex_lock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_lock(&mosq->out_packet_mutex);

#ifdef WITH_BROKER
if(db.config->max_queued_messages > 0 && mosq->out_packet_count >= db.config->max_queued_messages){
Expand All @@ -174,7 +174,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet)
}
mosq->out_packet_last = packet;
mosq->out_packet_count++;
pthread_mutex_unlock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->out_packet_mutex);
#ifdef WITH_BROKER
# ifdef WITH_WEBSOCKETS
if(mosq->wsi){
Expand Down Expand Up @@ -232,8 +232,8 @@ int packet__write(struct mosquitto *mosq)
if(!mosq) return MOSQ_ERR_INVAL;
if(mosq->sock == INVALID_SOCKET) return MOSQ_ERR_NO_CONN;

pthread_mutex_lock(&mosq->current_out_packet_mutex);
pthread_mutex_lock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_lock(&mosq->current_out_packet_mutex);
COMPAT_pthread_mutex_lock(&mosq->out_packet_mutex);
if(mosq->out_packet && !mosq->current_out_packet){
mosq->current_out_packet = mosq->out_packet;
mosq->out_packet = mosq->out_packet->next;
Expand All @@ -242,7 +242,7 @@ int packet__write(struct mosquitto *mosq)
}
mosq->out_packet_count--;
}
pthread_mutex_unlock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->out_packet_mutex);

#ifdef WITH_BROKER
if(mosq->current_out_packet){
Expand All @@ -252,7 +252,7 @@ int packet__write(struct mosquitto *mosq)

state = mosquitto__get_state(mosq);
if(state == mosq_cs_connect_pending){
pthread_mutex_unlock(&mosq->current_out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->current_out_packet_mutex);
return MOSQ_ERR_SUCCESS;
}

Expand All @@ -274,10 +274,10 @@ int packet__write(struct mosquitto *mosq)
|| errno == WSAENOTCONN
#endif
){
pthread_mutex_unlock(&mosq->current_out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->current_out_packet_mutex);
return MOSQ_ERR_SUCCESS;
}else{
pthread_mutex_unlock(&mosq->current_out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->current_out_packet_mutex);
switch(errno){
case COMPAT_ECONNRESET:
return MOSQ_ERR_CONN_LOST;
Expand All @@ -296,7 +296,7 @@ int packet__write(struct mosquitto *mosq)
if(((packet->command)&0xF6) == CMD_PUBLISH){
G_PUB_MSGS_SENT_INC(1);
#ifndef WITH_BROKER
pthread_mutex_lock(&mosq->callback_mutex);
COMPAT_pthread_mutex_lock(&mosq->callback_mutex);
if(mosq->on_publish){
/* This is a QoS=0 message */
mosq->in_callback = true;
Expand All @@ -309,7 +309,7 @@ int packet__write(struct mosquitto *mosq)
mosq->on_publish_v5(mosq, mosq->userdata, packet->mid, 0, NULL);
mosq->in_callback = false;
}
pthread_mutex_unlock(&mosq->callback_mutex);
COMPAT_pthread_mutex_unlock(&mosq->callback_mutex);
}else if(((packet->command)&0xF0) == CMD_DISCONNECT){
do_client_disconnect(mosq, MOSQ_ERR_SUCCESS, NULL);
packet__cleanup(packet);
Expand All @@ -321,7 +321,7 @@ int packet__write(struct mosquitto *mosq)
}

/* Free data and reset values */
pthread_mutex_lock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_lock(&mosq->out_packet_mutex);
mosq->current_out_packet = mosq->out_packet;
if(mosq->out_packet){
mosq->out_packet = mosq->out_packet->next;
Expand All @@ -330,25 +330,25 @@ int packet__write(struct mosquitto *mosq)
}
mosq->out_packet_count--;
}
pthread_mutex_unlock(&mosq->out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->out_packet_mutex);

packet__cleanup(packet);
mosquitto__free(packet);

#ifdef WITH_BROKER
mosq->next_msg_out = db.now_s + mosq->keepalive;
#else
pthread_mutex_lock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_lock(&mosq->msgtime_mutex);
mosq->next_msg_out = mosquitto_time() + mosq->keepalive;
pthread_mutex_unlock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_unlock(&mosq->msgtime_mutex);
#endif
}
#ifdef WITH_BROKER
if (mosq->current_out_packet == NULL) {
mux__remove_out(mosq);
}
#endif
pthread_mutex_unlock(&mosq->current_out_packet_mutex);
COMPAT_pthread_mutex_unlock(&mosq->current_out_packet_mutex);
return MOSQ_ERR_SUCCESS;
}

Expand Down Expand Up @@ -536,9 +536,9 @@ int packet__read(struct mosquitto *mosq)
#ifdef WITH_BROKER
keepalive__update(mosq);
#else
pthread_mutex_lock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_lock(&mosq->msgtime_mutex);
mosq->last_msg_in = mosquitto_time();
pthread_mutex_unlock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_unlock(&mosq->msgtime_mutex);
#endif
}
return MOSQ_ERR_SUCCESS;
Expand Down Expand Up @@ -571,9 +571,9 @@ int packet__read(struct mosquitto *mosq)
#ifdef WITH_BROKER
keepalive__update(mosq);
#else
pthread_mutex_lock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_lock(&mosq->msgtime_mutex);
mosq->last_msg_in = mosquitto_time();
pthread_mutex_unlock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_unlock(&mosq->msgtime_mutex);
#endif
return rc;
}
28 changes: 28 additions & 0 deletions lib/pthread_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef PTHREAD_COMPAT_
#define PTHREAD_COMPAT_

#if defined(WITH_THREADING) && !defined(WITH_BROKER)
# include <pthread.h>

# define COMPAT_pthread_create(A, B, C, D) pthread_create((A), (B), (C), (D))
# define COMPAT_pthread_join(A, B) pthread_join((A), (B))
# define COMPAT_pthread_cancel(A) pthread_cancel((A))
# define COMPAT_pthread_testcancel() pthread_testcancel()

# define COMPAT_pthread_mutex_init(A, B) pthread_mutex_init((A), (B))
# define COMPAT_pthread_mutex_destroy(A) pthread_mutex_init((A))
# define COMPAT_pthread_mutex_lock(A) pthread_mutex_lock((A))
# define COMPAT_pthread_mutex_unlock(A) pthread_mutex_unlock((A))
#else
# define COMPAT_pthread_create(A, B, C, D)
# define COMPAT_pthread_join(A, B)
# define COMPAT_pthread_cancel(A)
# define COMPAT_pthread_testcancel()

# define COMPAT_pthread_mutex_init(A, B)
# define COMPAT_pthread_mutex_destroy(A)
# define COMPAT_pthread_mutex_lock(A)
# define COMPAT_pthread_mutex_unlock(A)
#endif

#endif
32 changes: 16 additions & 16 deletions lib/util_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ int mosquitto__check_keepalive(struct mosquitto *mosq)
return MOSQ_ERR_SUCCESS;
}
#endif
pthread_mutex_lock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_lock(&mosq->msgtime_mutex);
next_msg_out = mosq->next_msg_out;
last_msg_in = mosq->last_msg_in;
pthread_mutex_unlock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_unlock(&mosq->msgtime_mutex);
if(mosq->keepalive && mosq->sock != INVALID_SOCKET &&
(now >= next_msg_out || now - last_msg_in >= mosq->keepalive)){

state = mosquitto__get_state(mosq);
if(state == mosq_cs_active && mosq->ping_t == 0){
send__pingreq(mosq);
/* Reset last msg times to give the server time to send a pingresp */
pthread_mutex_lock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_lock(&mosq->msgtime_mutex);
mosq->last_msg_in = now;
mosq->next_msg_out = now + mosq->keepalive;
pthread_mutex_unlock(&mosq->msgtime_mutex);
COMPAT_pthread_mutex_unlock(&mosq->msgtime_mutex);
}else{
#ifdef WITH_BROKER
# ifdef WITH_BRIDGE
Expand All @@ -118,7 +118,7 @@ int mosquitto__check_keepalive(struct mosquitto *mosq)
}else{
rc = MOSQ_ERR_KEEPALIVE;
}
pthread_mutex_lock(&mosq->callback_mutex);
COMPAT_pthread_mutex_lock(&mosq->callback_mutex);
if(mosq->on_disconnect){
mosq->in_callback = true;
mosq->on_disconnect(mosq, mosq->userdata, rc);
Expand All @@ -129,7 +129,7 @@ int mosquitto__check_keepalive(struct mosquitto *mosq)
mosq->on_disconnect_v5(mosq, mosq->userdata, rc, NULL);
mosq->in_callback = false;
}
pthread_mutex_unlock(&mosq->callback_mutex);
COMPAT_pthread_mutex_unlock(&mosq->callback_mutex);

return rc;
#endif
Expand All @@ -150,11 +150,11 @@ uint16_t mosquitto__mid_generate(struct mosquitto *mosq)
uint16_t mid;
assert(mosq);

pthread_mutex_lock(&mosq->mid_mutex);
COMPAT_pthread_mutex_lock(&mosq->mid_mutex);
mosq->last_mid++;
if(mosq->last_mid == 0) mosq->last_mid++;
mid = mosq->last_mid;
pthread_mutex_unlock(&mosq->mid_mutex);
COMPAT_pthread_mutex_unlock(&mosq->mid_mutex);

return mid;
}
Expand Down Expand Up @@ -280,14 +280,14 @@ int util__random_bytes(void *bytes, int count)

int mosquitto__set_state(struct mosquitto *mosq, enum mosquitto_client_state state)
{
pthread_mutex_lock(&mosq->state_mutex);
COMPAT_pthread_mutex_lock(&mosq->state_mutex);
#ifdef WITH_BROKER
if(mosq->state != mosq_cs_disused)
#endif
{
mosq->state = state;
}
pthread_mutex_unlock(&mosq->state_mutex);
COMPAT_pthread_mutex_unlock(&mosq->state_mutex);

return MOSQ_ERR_SUCCESS;
}
Expand All @@ -296,28 +296,28 @@ enum mosquitto_client_state mosquitto__get_state(struct mosquitto *mosq)
{
enum mosquitto_client_state state;

pthread_mutex_lock(&mosq->state_mutex);
COMPAT_pthread_mutex_lock(&mosq->state_mutex);
state = mosq->state;
pthread_mutex_unlock(&mosq->state_mutex);
COMPAT_pthread_mutex_unlock(&mosq->state_mutex);

return state;
}

#ifndef WITH_BROKER
void mosquitto__set_request_disconnect(struct mosquitto *mosq, bool request_disconnect)
{
pthread_mutex_lock(&mosq->state_mutex);
COMPAT_pthread_mutex_lock(&mosq->state_mutex);
mosq->request_disconnect = request_disconnect;
pthread_mutex_unlock(&mosq->state_mutex);
COMPAT_pthread_mutex_unlock(&mosq->state_mutex);
}

bool mosquitto__get_request_disconnect(struct mosquitto *mosq)
{
bool request_disconnect;

pthread_mutex_lock(&mosq->state_mutex);
COMPAT_pthread_mutex_lock(&mosq->state_mutex);
request_disconnect = mosq->request_disconnect;
pthread_mutex_unlock(&mosq->state_mutex);
COMPAT_pthread_mutex_unlock(&mosq->state_mutex);

return request_disconnect;
}
Expand Down

0 comments on commit 88b7bb3

Please sign in to comment.