diff --git a/lib/mosquitto.c b/lib/mosquitto.c index bad9402f..5009bda3 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -117,7 +117,10 @@ struct mosquitto *mosquitto_new(const char *id, bool clean_start, void *userdata if(mosq){ mosq->sock = INVALID_SOCKET; #ifdef WITH_THREADING +# ifndef WIN32 + /* Windows doesn't have pthread_cancel, so no need to record self */ mosq->thread_id = pthread_self(); +# endif #endif mosq->sockpairR = INVALID_SOCKET; mosq->sockpairW = INVALID_SOCKET; @@ -217,7 +220,11 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st pthread_mutex_init(&mosq->msgs_in.mutex, NULL); pthread_mutex_init(&mosq->msgs_out.mutex, NULL); pthread_mutex_init(&mosq->mid_mutex, NULL); +#ifdef WIN32 + mosq->thread_id = NULL; +#else mosq->thread_id = pthread_self(); +#endif #endif if(mosq->disable_socketpair == false){ /* This must be after pthread_mutex_init(), otherwise the log mutex may be diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 6f99c50f..52916fe9 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -329,11 +329,7 @@ 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; diff --git a/lib/thread_mosq.c b/lib/thread_mosq.c index 32a3e7e0..6b8254a7 100644 --- a/lib/thread_mosq.c +++ b/lib/thread_mosq.c @@ -87,7 +87,11 @@ int mosquitto_loop_stop(struct mosquitto *mosq, bool force) } #endif pthread_join(mosq->thread_id, NULL); +#ifdef WIN32 + mosq->thread_id = NULL; +#else mosq->thread_id = pthread_self(); +#endif mosq->threaded = mosq_ts_none; return MOSQ_ERR_SUCCESS; diff --git a/lib/winthread_mosq.h b/lib/winthread_mosq.h index 78743f90..ae23a133 100644 --- a/lib/winthread_mosq.h +++ b/lib/winthread_mosq.h @@ -33,6 +33,7 @@ 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_self(void); 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);