113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include <curl/curl.h>
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#if defined(USE_THREADS_POSIX)
3013498266Sopenharmony_ci#  ifdef HAVE_PTHREAD_H
3113498266Sopenharmony_ci#    include <pthread.h>
3213498266Sopenharmony_ci#  endif
3313498266Sopenharmony_ci#elif defined(USE_THREADS_WIN32)
3413498266Sopenharmony_ci#  include <process.h>
3513498266Sopenharmony_ci#endif
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#include "curl_threads.h"
3813498266Sopenharmony_ci#include "curl_memory.h"
3913498266Sopenharmony_ci/* The last #include file should be: */
4013498266Sopenharmony_ci#include "memdebug.h"
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci#if defined(USE_THREADS_POSIX)
4313498266Sopenharmony_ci
4413498266Sopenharmony_cistruct Curl_actual_call {
4513498266Sopenharmony_ci  unsigned int (*func)(void *);
4613498266Sopenharmony_ci  void *arg;
4713498266Sopenharmony_ci};
4813498266Sopenharmony_ci
4913498266Sopenharmony_cistatic void *curl_thread_create_thunk(void *arg)
5013498266Sopenharmony_ci{
5113498266Sopenharmony_ci  struct Curl_actual_call *ac = arg;
5213498266Sopenharmony_ci  unsigned int (*func)(void *) = ac->func;
5313498266Sopenharmony_ci  void *real_arg = ac->arg;
5413498266Sopenharmony_ci
5513498266Sopenharmony_ci  free(ac);
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci  (*func)(real_arg);
5813498266Sopenharmony_ci
5913498266Sopenharmony_ci  return 0;
6013498266Sopenharmony_ci}
6113498266Sopenharmony_ci
6213498266Sopenharmony_cicurl_thread_t Curl_thread_create(unsigned int (*func) (void *), void *arg)
6313498266Sopenharmony_ci{
6413498266Sopenharmony_ci  curl_thread_t t = malloc(sizeof(pthread_t));
6513498266Sopenharmony_ci  struct Curl_actual_call *ac = malloc(sizeof(struct Curl_actual_call));
6613498266Sopenharmony_ci  if(!(ac && t))
6713498266Sopenharmony_ci    goto err;
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci  ac->func = func;
7013498266Sopenharmony_ci  ac->arg = arg;
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
7313498266Sopenharmony_ci    goto err;
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci  return t;
7613498266Sopenharmony_ci
7713498266Sopenharmony_cierr:
7813498266Sopenharmony_ci  free(t);
7913498266Sopenharmony_ci  free(ac);
8013498266Sopenharmony_ci  return curl_thread_t_null;
8113498266Sopenharmony_ci}
8213498266Sopenharmony_ci
8313498266Sopenharmony_civoid Curl_thread_destroy(curl_thread_t hnd)
8413498266Sopenharmony_ci{
8513498266Sopenharmony_ci  if(hnd != curl_thread_t_null) {
8613498266Sopenharmony_ci    pthread_detach(*hnd);
8713498266Sopenharmony_ci    free(hnd);
8813498266Sopenharmony_ci  }
8913498266Sopenharmony_ci}
9013498266Sopenharmony_ci
9113498266Sopenharmony_ciint Curl_thread_join(curl_thread_t *hnd)
9213498266Sopenharmony_ci{
9313498266Sopenharmony_ci  int ret = (pthread_join(**hnd, NULL) == 0);
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci  free(*hnd);
9613498266Sopenharmony_ci  *hnd = curl_thread_t_null;
9713498266Sopenharmony_ci
9813498266Sopenharmony_ci  return ret;
9913498266Sopenharmony_ci}
10013498266Sopenharmony_ci
10113498266Sopenharmony_ci#elif defined(USE_THREADS_WIN32)
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci/* !checksrc! disable SPACEBEFOREPAREN 1 */
10413498266Sopenharmony_cicurl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void *),
10513498266Sopenharmony_ci                                 void *arg)
10613498266Sopenharmony_ci{
10713498266Sopenharmony_ci#ifdef _WIN32_WCE
10813498266Sopenharmony_ci  typedef HANDLE curl_win_thread_handle_t;
10913498266Sopenharmony_ci#else
11013498266Sopenharmony_ci  typedef uintptr_t curl_win_thread_handle_t;
11113498266Sopenharmony_ci#endif
11213498266Sopenharmony_ci  curl_thread_t t;
11313498266Sopenharmony_ci  curl_win_thread_handle_t thread_handle;
11413498266Sopenharmony_ci#ifdef _WIN32_WCE
11513498266Sopenharmony_ci  thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
11613498266Sopenharmony_ci#else
11713498266Sopenharmony_ci  thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
11813498266Sopenharmony_ci#endif
11913498266Sopenharmony_ci  t = (curl_thread_t)thread_handle;
12013498266Sopenharmony_ci  if((t == 0) || (t == LongToHandle(-1L))) {
12113498266Sopenharmony_ci#ifdef _WIN32_WCE
12213498266Sopenharmony_ci    DWORD gle = GetLastError();
12313498266Sopenharmony_ci    errno = ((gle == ERROR_ACCESS_DENIED ||
12413498266Sopenharmony_ci              gle == ERROR_NOT_ENOUGH_MEMORY) ?
12513498266Sopenharmony_ci             EACCES : EINVAL);
12613498266Sopenharmony_ci#endif
12713498266Sopenharmony_ci    return curl_thread_t_null;
12813498266Sopenharmony_ci  }
12913498266Sopenharmony_ci  return t;
13013498266Sopenharmony_ci}
13113498266Sopenharmony_ci
13213498266Sopenharmony_civoid Curl_thread_destroy(curl_thread_t hnd)
13313498266Sopenharmony_ci{
13413498266Sopenharmony_ci  CloseHandle(hnd);
13513498266Sopenharmony_ci}
13613498266Sopenharmony_ci
13713498266Sopenharmony_ciint Curl_thread_join(curl_thread_t *hnd)
13813498266Sopenharmony_ci{
13913498266Sopenharmony_ci#if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
14013498266Sopenharmony_ci    (_WIN32_WINNT < _WIN32_WINNT_VISTA)
14113498266Sopenharmony_ci  int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
14213498266Sopenharmony_ci#else
14313498266Sopenharmony_ci  int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
14413498266Sopenharmony_ci#endif
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci  Curl_thread_destroy(*hnd);
14713498266Sopenharmony_ci
14813498266Sopenharmony_ci  *hnd = curl_thread_t_null;
14913498266Sopenharmony_ci
15013498266Sopenharmony_ci  return ret;
15113498266Sopenharmony_ci}
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci#endif /* USE_THREADS_* */
154