<commit_before>/****************************************************************************
 *
 * Copyright 2016 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/
/*
Copyright (c) 2011-2014 <NAME> <<EMAIL>>

All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.

The Eclipse Public License is available at
   http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
  http://www.eclipse.org/org/documents/edl-v10.php.

Contributors:
   <NAME> - initial implementation and documentation.
*/

#include <config.h>

#ifndef WIN32
#include <unistd.h>
#endif

#include <mosquitto_internal.h>
#include <net_mosq.h>

#if defined(__TINYARA__)
#define TINYARA_MQTT_PTHREAD_STACK_SIZE		(8 * 1024)
#endif

void *_mosquitto_thread_main(void *obj);

int mosquitto_loop_start(struct mosquitto *mosq)
{
#ifdef WITH_THREADING
	if (!mosq || mosq->threaded != mosq_ts_none) {
		return MOSQ_ERR_INVAL;
	}

#if defined(__TINYARA__)
	char threadname[40];
	pthread_attr_t attr;

	mosq->threaded = mosq_ts_self;
	pthread_attr_init(&attr);
	pthread_attr_setstacksize(&attr, TINYARA_MQTT_PTHREAD_STACK_SIZE);

	if (pthread_create(&mosq->thread_id, &attr, _mosquitto_thread_main, mosq) != 0) {
		return MOSQ_ERR_ERRNO;
	}

	snprintf(threadname, sizeof(threadname), "%s-%s", "MQTT", mosq->id);
	pthread_setname_np(mosq->thread_id, threadname);
	pthread_detach(mosq->thread_id);

	return MOSQ_ERR_SUCCESS;

#else
	mosq->threaded = mosq_ts_self;
	if (!pthread_create(&mosq->thread_id, NULL, _mosquitto_thread_main, mosq)) {
		return MOSQ_ERR_SUCCESS;
	} else {
		return MOSQ_ERR_ERRNO;
	}
#endif

#else
	return MOSQ_ERR_NOT_SUPPORTED;
#endif
}

int mosquitto_loop_stop(struct mosquitto *mosq, bool force)
{
#ifdef WITH_THREADING
#	ifndef WITH_BROKER<commit_msg>netutils/mqtt: add exception handling code<commit_after>/****************************************************************************
 *
 * Copyright 2016 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/
/*
Copyright (c) 2011-2014 <NAME> <<EMAIL>>

All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.

The Eclipse Public License is available at
   http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
  http://www.eclipse.org/org/documents/edl-v10.php.

Contributors:
   <NAME> - initial implementation and documentation.
*/

#include <config.h>

#ifndef WIN32
#include <unistd.h>
#endif

#include <mosquitto_internal.h>
#include <net_mosq.h>

#if defined(__TINYARA__)
#define TINYARA_MQTT_PTHREAD_STACK_SIZE		(8 * 1024)
#endif

void *_mosquitto_thread_main(void *obj);

int mosquitto_loop_start(struct mosquitto *mosq)
{
#ifdef WITH_THREADING
	if (!mosq || mosq->threaded != mosq_ts_none) {
		return MOSQ_ERR_INVAL;
	}

#if defined(__TINYARA__)
	char threadname[40];
	pthread_attr_t attr;

	mosq->threaded = mosq_ts_self;
	if (pthread_attr_init(&attr) != 0) {
		return MOSQ_ERR_ERRNO;
	}

	if (pthread_attr_setstacksize(&attr, TINYARA_MQTT_PTHREAD_STACK_SIZE) != 0) {
		return MOSQ_ERR_ERRNO;
	}

	if (pthread_create(&mosq->thread_id, &attr, _mosquitto_thread_main, mosq) != 0) {
		return MOSQ_ERR_ERRNO;
	}

	snprintf(threadname, sizeof(threadname), "%s-%s", "MQTT", mosq->id);
	pthread_setname_np(mosq->thread_id, threadname);
	pthread_detach(mosq->thread_id);

	return MOSQ_ERR_SUCCESS;

#else
	mosq->threaded = mosq_ts_self;
	if (!pthread_create(&mosq->thread_id, NULL, _mosquitto_thread_main, mosq)) {
		return MOSQ_ERR_SUCCESS;
	} else {
		return MOSQ_ERR_ERRNO;
	}
#endif

#else
	return MOSQ_ERR_NOT_SUPPORTED;
#endif
}

int mosquitto_loop_stop(struct mosquitto *mosq, bool force)
{
#ifdef WITH_THREADING
#	ifndef WITH_BROKER
{'fraction_non_alphanumeric': 0.15124760076775431, 'fraction_numerical': 0.013435700575815739, 'mean_word_length': 5.409594095940959, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5508355', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1671, 'n_words': 479}