thread_mosq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright (c) 2011-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #ifndef WIN32
  16. #include <time.h>
  17. #endif
  18. #if defined(WITH_THREADING)
  19. #if defined(__linux__) || defined(__NetBSD__)
  20. # include <pthread.h>
  21. #elif defined(__FreeBSD__) || defined(__OpenBSD__)
  22. # include <pthread_np.h>
  23. #endif
  24. #endif
  25. #include "mosquitto_internal.h"
  26. #include "net_mosq.h"
  27. #include "util_mosq.h"
  28. void *mosquitto__thread_main(void *obj);
  29. int mosquitto_loop_start(struct mosquitto *mosq)
  30. {
  31. #if defined(WITH_THREADING)
  32. if(!mosq || mosq->threaded != mosq_ts_none) return MOSQ_ERR_INVAL;
  33. mosq->threaded = mosq_ts_self;
  34. if(!pthread_create(&mosq->thread_id, NULL, mosquitto__thread_main, mosq)){
  35. #if defined(__linux__)
  36. pthread_setname_np(mosq->thread_id, "mosquitto loop");
  37. #elif defined(__NetBSD__)
  38. pthread_setname_np(mosq->thread_id, "%s", "mosquitto loop");
  39. #elif defined(__FreeBSD__) || defined(__OpenBSD__)
  40. pthread_set_name_np(mosq->thread_id, "mosquitto loop");
  41. #endif
  42. return MOSQ_ERR_SUCCESS;
  43. }else{
  44. return MOSQ_ERR_ERRNO;
  45. }
  46. #else
  47. UNUSED(mosq);
  48. return MOSQ_ERR_NOT_SUPPORTED;
  49. #endif
  50. }
  51. int mosquitto_loop_stop(struct mosquitto *mosq, bool force)
  52. {
  53. #if defined(WITH_THREADING)
  54. # ifndef WITH_BROKER
  55. char sockpair_data = 0;
  56. # endif
  57. if(!mosq || mosq->threaded != mosq_ts_self) return MOSQ_ERR_INVAL;
  58. /* Write a single byte to sockpairW (connected to sockpairR) to break out
  59. * of select() if in threaded mode. */
  60. if(mosq->sockpairW != INVALID_SOCKET){
  61. #ifndef WIN32
  62. if(write(mosq->sockpairW, &sockpair_data, 1)){
  63. }
  64. #else
  65. send(mosq->sockpairW, &sockpair_data, 1, 0);
  66. #endif
  67. }
  68. #ifdef HAVE_PTHREAD_CANCEL
  69. if(force){
  70. pthread_cancel(mosq->thread_id);
  71. }
  72. #endif
  73. pthread_join(mosq->thread_id, NULL);
  74. mosq->thread_id = pthread_self();
  75. mosq->threaded = mosq_ts_none;
  76. return MOSQ_ERR_SUCCESS;
  77. #else
  78. UNUSED(mosq);
  79. UNUSED(force);
  80. return MOSQ_ERR_NOT_SUPPORTED;
  81. #endif
  82. }
  83. #ifdef WITH_THREADING
  84. void *mosquitto__thread_main(void *obj)
  85. {
  86. struct mosquitto *mosq = obj;
  87. #ifndef WIN32
  88. struct timespec ts;
  89. ts.tv_sec = 0;
  90. ts.tv_nsec = 10000000;
  91. #endif
  92. if(!mosq) return NULL;
  93. do{
  94. if(mosquitto__get_state(mosq) == mosq_cs_new){
  95. #ifdef WIN32
  96. Sleep(10);
  97. #else
  98. nanosleep(&ts, NULL);
  99. #endif
  100. }else{
  101. break;
  102. }
  103. }while(1);
  104. if(!mosq->keepalive){
  105. /* Sleep for a day if keepalive disabled. */
  106. mosquitto_loop_forever(mosq, 1000*86400, 1);
  107. }else{
  108. /* Sleep for our keepalive value. publish() etc. will wake us up. */
  109. mosquitto_loop_forever(mosq, mosq->keepalive*1000, 1);
  110. }
  111. if(mosq->threaded == mosq_ts_self){
  112. mosq->threaded = mosq_ts_none;
  113. }
  114. return obj;
  115. }
  116. #endif
  117. int mosquitto_threaded_set(struct mosquitto *mosq, bool threaded)
  118. {
  119. if(!mosq) return MOSQ_ERR_INVAL;
  120. if(threaded){
  121. mosq->threaded = mosq_ts_external;
  122. }else{
  123. mosq->threaded = mosq_ts_none;
  124. }
  125. return MOSQ_ERR_SUCCESS;
  126. }