1 /*
2 Copyright (c) 2005-2021 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 /*
18 The original source for this example is
19 Copyright (c) 1994-2008 John E. Stone
20 All rights reserved.
21
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions
24 are met:
25 1. Redistributions of source code must retain the above copyright
26 notice, this list of conditions and the following disclaimer.
27 2. Redistributions in binary form must reproduce the above copyright
28 notice, this list of conditions and the following disclaimer in the
29 documentation and/or other materials provided with the distribution.
30 3. The name of the author may not be used to endorse or promote products
31 derived from this software without specific prior written permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 SUCH DAMAGE.
44 */
45
46 #ifdef EMULATE_PTHREADS
47
48 #include <assert.h>
49 #include "pthread_w.hpp"
50
51 /*
52 Basics
53 */
54
pthread_create(pthread_t * thread,pthread_attr_t * attr,void * (* start_routine)(void *),void * arg)55 int pthread_create(pthread_t *thread,
56 pthread_attr_t *attr,
57 void *(*start_routine)(void *),
58 void *arg) {
59 pthread_t th;
60
61 if (thread == nullptr)
62 return EINVAL;
63 *thread = nullptr;
64
65 if (start_routine == nullptr)
66 return EINVAL;
67
68 th = (pthread_t)malloc(sizeof(pthread_s));
69 memset(th, 0, sizeof(pthread_s));
70
71 th->winthread_handle =
72 CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, &th->winthread_id);
73 if (th->winthread_handle == nullptr)
74 return EAGAIN; /* GetLastError() */
75
76 *thread = th;
77 return 0;
78 }
79
pthread_join(pthread_t th,void ** thread_return)80 int pthread_join(pthread_t th, void **thread_return) {
81 BOOL b_ret;
82 DWORD dw_ret;
83
84 if (thread_return)
85 *thread_return = nullptr;
86
87 if ((th == nullptr) || (th->winthread_handle == nullptr))
88 return EINVAL;
89
90 dw_ret = WaitForSingleObject(th->winthread_handle, INFINITE);
91 if (dw_ret != WAIT_OBJECT_0)
92 return ERROR_PTHREAD; /* dw_ret == WAIT_FAILED; GetLastError() */
93
94 if (thread_return) {
95 BOOL e_ret;
96 DWORD exit_val;
97 e_ret = GetExitCodeThread(th->winthread_handle, &exit_val);
98 if (!e_ret)
99 return ERROR_PTHREAD; /* GetLastError() */
100 *thread_return = (void *)(std::size_t)exit_val;
101 }
102
103 b_ret = CloseHandle(th->winthread_handle);
104 if (!b_ret)
105 return ERROR_PTHREAD; /* GetLastError() */
106 memset(th, 0, sizeof(pthread_s));
107 free(th);
108 th = nullptr;
109
110 return 0;
111 }
112
pthread_exit(void * retval)113 void pthread_exit(void *retval) {
114 /* specific to PTHREAD_TO_WINTHREAD */
115
116 ExitThread((DWORD)(
117 (std::size_t)retval)); /* thread becomes signalled so its death can be waited upon */
118 /*NOTREACHED*/
119 assert(0);
120 return; /* void fnc; can't return an error code */
121 }
122
123 /*
124 Mutex
125 */
126
pthread_mutex_init(pthread_mutex_t * mutex,pthread_mutexattr_t * mutex_attr)127 int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *mutex_attr) {
128 InitializeCriticalSection(&mutex->critsec);
129 return 0;
130 }
131
pthread_mutex_destroy(pthread_mutex_t * mutex)132 int pthread_mutex_destroy(pthread_mutex_t *mutex) {
133 return 0;
134 }
135
pthread_mutex_lock(pthread_mutex_t * mutex)136 int pthread_mutex_lock(pthread_mutex_t *mutex) {
137 EnterCriticalSection(&mutex->critsec);
138 return 0;
139 }
140
pthread_mutex_unlock(pthread_mutex_t * mutex)141 int pthread_mutex_unlock(pthread_mutex_t *mutex) {
142 LeaveCriticalSection(&mutex->critsec);
143 return 0;
144 }
145
146 #endif /* EMULATE_PTHREADS */
147