Windows threading support for the library.

pull/2386/head
Roger Light 4 years ago
parent dbd0d6f0d8
commit 88dfac8e88

@ -67,12 +67,8 @@ option(WITH_THREADING "Include client library threading support?" ON)
if(WITH_THREADING)
add_definitions("-DWITH_THREADING")
if(WIN32)
if(CMAKE_CL_64)
set (PTHREAD_LIBRARIES C:\\pthreads\\Pre-built.2\\lib\\x64\\pthreadVC2.lib)
else()
set (PTHREAD_LIBRARIES C:\\pthreads\\Pre-built.2\\lib\\x86\\pthreadVC2.lib)
endif()
set (PTHREAD_INCLUDE_DIR C:\\pthreads\\Pre-built.2\\include)
set (PTHREAD_LIBRARIES "")
set (PTHREAD_INCLUDE_DIR "")
elseif(ANDROID)
set (PTHREAD_LIBRARIES "")
set (PTHREAD_INCLUDE_DIR "")

@ -84,6 +84,7 @@ Client library:
filter (a subscription) against another topic filter (an ACL), with `%c` and
`%u` patterns for client id / username substitution.
- Performance: reduce memory allocations when sending packets.
- Reintroduce threading support for Windows.
2.0.12 - 2021-08-31

@ -10,27 +10,7 @@ Capabilities
Some versions of Windows have limitations on the number of concurrent
connections due to the Windows API being used. In modern versions of Windows,
e.g. Windows 10 or Windows Server 2019, this is approximately 8192 connections.
In earlier versions of Windows, t his limit is 2048 connections.
Websockets
----------
The broker executables provided in the installers have Websockets support
through a statically compiled version of libwebsockets and is being distributed
under the Static Linking Exception (Section 2) of the License. As a result, the
content is not subject to the LGPL 2.1.
Library Thread Support
----------------------
libmosquitto on Windows is currently compiled without thread support, so
neither of mosquitto_loop_start() nor "mosquitto_pub -l" are available.
A better solution that the old pthreads-win32 is being looked into, so support
will return in the future. If you need thread support, the code still supports
it just fine. Support has been dropped to simplify installation.
In earlier versions of Windows, this limit is 2048 connections.
Windows Service

@ -241,7 +241,10 @@ static int pub_stdin_line_loop(struct mosquitto *mosq)
int read_len;
bool stdin_finished = false;
mosquitto_loop_start(mosq);
rc = mosquitto_loop_start(mosq);
if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
stdin_finished = false;
do{
if(status == STATUS_CONNECTING){

@ -78,7 +78,7 @@
#define UNUSED(A) (void)(A)
/* Android Bionic libpthread implementation doesn't have pthread_cancel */
#ifndef ANDROID
#if !defined(ANDROID) && !defined(WIN32)
# define HAVE_PTHREAD_CANCEL
#endif

@ -50,6 +50,10 @@ set(C_SRC
util_mosq.c util_topic.c util_mosq.h
will_mosq.c will_mosq.h)
if (WIN32)
list(APPEND C_SRC "winthread_mosq.c" "winthread_mosq.h")
endif()
set (LIBRARIES ${OPENSSL_LIBRARIES} ${PTHREAD_LIBRARIES})
if(UNIX AND NOT APPLE AND NOT ANDROID)

@ -34,7 +34,11 @@ Contributors:
#include <stdlib.h>
#if defined(WITH_THREADING) && !defined(WITH_BROKER)
# include <pthread.h>
# ifdef WIN32
# include "winthread_mosq.h"
# else
# include <pthread.h>
# endif
#else
# include <dummypthread.h>
#endif
@ -290,7 +294,11 @@ struct mosquitto {
pthread_mutex_t out_packet_mutex;
pthread_mutex_t state_mutex;
pthread_mutex_t mid_mutex;
#ifdef WIN32
int thread_id;
#else
pthread_t thread_id;
#endif
#endif
bool clean_start;
time_t session_expiry_time;

@ -0,0 +1,102 @@
/*
Copyright (c) 2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#if defined(WIN32) && defined(WITH_THREADING)
#include "winthread_mosq.h"
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*thread_main) (void *), void *arg)
{
if(thread == NULL || thread_main == NULL){
return 1;
}
*thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_main, arg, 0, NULL);
if(*thread){
return 0;
}else{
return 1;
}
}
int pthread_join(pthread_t thread, void **retval)
{
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
return 0;
}
int pthread_self(void)
{
return GetCurrentThreadId();
}
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
if(mutex == NULL) return 1;
InitializeCriticalSection(mutex);
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
if(mutex == NULL) return 1;
DeleteCriticalSection(mutex);
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
if(mutex == NULL) return 1;
EnterCriticalSection(mutex);
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
if(mutex == NULL) return 1;
LeaveCriticalSection(mutex);
return 0;
}
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
{
if(cond == NULL) return 1;
InitializeConditionVariable(cond);
return 0;
}
int pthread_cond_destroy(pthread_cond_t *cond)
{
return 0;
}
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
if(cond == NULL || mutex == NULL) return 1;
return !SleepConditionVariableCS(cond, mutex, INFINITE);
}
int pthread_cond_signal(pthread_cond_t *cond)
{
if(cond == NULL) return 1;
WakeConditionVariable(cond);
return 0;
}
#endif

@ -0,0 +1,46 @@
/*
Copyright (c) 2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#ifndef WINTHREAD_MOSQ_H
#define WINTHREAD_MOSQ_H
#if defined(WIN32) && defined(WITH_THREADING)
#include <windows.h>
#include <synchapi.h>
typedef HANDLE pthread_t;
typedef void pthread_attr_t;
typedef CRITICAL_SECTION pthread_mutex_t;
typedef void pthread_mutexattr_t;
typedef CONDITION_VARIABLE pthread_cond_t;
typedef void pthread_condattr_t;
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
int pthread_join(pthread_t thread, void **retval);
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_signal(pthread_cond_t *cond);
#endif
#endif
Loading…
Cancel
Save