Просмотр исходного кода

Every thread must have a ssl_id (unsigned long), even if not created by civetweb

See #200
bel 9 лет назад
Родитель
Сommit
617a16ee39
1 измененных файлов с 20 добавлено и 5 удалено
  1. 20 5
      src/civetweb.c

+ 20 - 5
src/civetweb.c

@@ -9635,11 +9635,26 @@ ssl_locking_callback(int mode, int mutex_num, const char *file, int line)
 /* Must be set if sizeof(pthread_t) > sizeof(unsigned long) */
 static unsigned long ssl_id_callback(void)
 {
-	struct mg_workerTLS *tls =
-	    (struct mg_workerTLS *)pthread_getspecific(sTlsKey);
-	/* Do not check for tls == NULL here. If tls == NULL, something is wrong
-     * and there is no way to fix it here. In particular, do not return 0. */
-	return tls->thread_idx;
+#ifdef _WIN32
+	return GetCurrentThreadId();
+#else
+	if (sizeof(pthread_t) > sizeof(unsigned long)) {
+		struct mg_workerTLS *tls =
+		    (struct mg_workerTLS *)pthread_getspecific(sTlsKey);
+		if (tls == NULL) {
+			/* SSL called from an unknown thread: Create some thread index.
+			 * This will cause a minimal memory leak when the thread exits.
+			 * TODO (low): Use a destructor function. */
+			tls = calloc(1, sizeof(struct mg_workerTLS));
+			tls.is_master = -2;
+			tls.thread_idx = (unsigned)mg_atomic_inc(&thread_idx_max);
+			pthread_setspecific(sTlsKey, tls);
+		}
+		return tls->thread_idx;
+	} else {
+		return (unsigned long)pthread_self();
+	}
+#endif
 }