source: UsbWattMeter/trunk/curl-7.47.1/lib/curl_threads.c

Last change on this file was 167, checked in by coas-nagasima, 8 years ago

MIMEにSJISを設定

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc; charset=SHIFT_JIS
File size: 3.2 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#if defined(USE_THREADS_POSIX)
26# ifdef HAVE_PTHREAD_H
27# include <pthread.h>
28# endif
29#elif defined(USE_THREADS_WIN32)
30# ifdef HAVE_PROCESS_H
31# include <process.h>
32# endif
33#endif
34
35#include "curl_threads.h"
36#include "curl_memory.h"
37/* The last #include file should be: */
38#include "memdebug.h"
39
40#if defined(USE_THREADS_POSIX)
41
42struct curl_actual_call {
43 unsigned int (*func)(void *);
44 void *arg;
45};
46
47static void *curl_thread_create_thunk(void *arg)
48{
49 struct curl_actual_call * ac = arg;
50 unsigned int (*func)(void *) = ac->func;
51 void *real_arg = ac->arg;
52
53 free(ac);
54
55 (*func)(real_arg);
56
57 return 0;
58}
59
60curl_thread_t Curl_thread_create(unsigned int (*func) (void*), void *arg)
61{
62 curl_thread_t t = malloc(sizeof(pthread_t));
63 struct curl_actual_call *ac = malloc(sizeof(struct curl_actual_call));
64 if(!(ac && t))
65 goto err;
66
67 ac->func = func;
68 ac->arg = arg;
69
70 if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
71 goto err;
72
73 return t;
74
75err:
76 free(t);
77 free(ac);
78 return curl_thread_t_null;
79}
80
81void Curl_thread_destroy(curl_thread_t hnd)
82{
83 if(hnd != curl_thread_t_null) {
84 pthread_detach(*hnd);
85 free(hnd);
86 }
87}
88
89int Curl_thread_join(curl_thread_t *hnd)
90{
91 int ret = (pthread_join(**hnd, NULL) == 0);
92
93 free(*hnd);
94 *hnd = curl_thread_t_null;
95
96 return ret;
97}
98
99#elif defined(USE_THREADS_WIN32)
100
101curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*),
102 void *arg)
103{
104#ifdef _WIN32_WCE
105 return CreateThread(NULL, 0, func, arg, 0, NULL);
106#else
107 curl_thread_t t;
108 t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL);
109 if((t == 0) || (t == (curl_thread_t)-1L))
110 return curl_thread_t_null;
111 return t;
112#endif
113}
114
115void Curl_thread_destroy(curl_thread_t hnd)
116{
117 CloseHandle(hnd);
118}
119
120int Curl_thread_join(curl_thread_t *hnd)
121{
122#if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
123 (_WIN32_WINNT < _WIN32_WINNT_VISTA)
124 int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
125#else
126 int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
127#endif
128
129 Curl_thread_destroy(*hnd);
130
131 *hnd = curl_thread_t_null;
132
133 return ret;
134}
135
136#endif /* USE_THREADS_* */
Note: See TracBrowser for help on using the repository browser.