blob: df9a0b794893e0a816e43ef6ee265b914299b1ce [file] [log] [blame]
Nicolas Capensd9993092016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capensd9993092016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capensd9993092016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capensd9993092016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "Thread.hpp"
16
17namespace sw
18{
19 Thread::Thread(void (*threadFunction)(void *parameters), void *parameters)
20 {
21 Event init;
22 Entry entry = {threadFunction, parameters, &init};
John Bauman66b8ab22014-05-06 15:57:45 -040023
John Bauman89401822014-05-06 15:04:28 -040024 #if defined(_WIN32)
Nicolas Capens165ac812017-02-02 13:54:09 -050025 handle = CreateThread(NULL, 1024 * 1024, startFunction, &entry, 0, NULL);
John Bauman89401822014-05-06 15:04:28 -040026 #else
Nicolas Capens165ac812017-02-02 13:54:09 -050027 pthread_create(&handle, NULL, startFunction, &entry);
John Bauman89401822014-05-06 15:04:28 -040028 #endif
John Bauman66b8ab22014-05-06 15:57:45 -040029
John Bauman89401822014-05-06 15:04:28 -040030 init.wait();
31 }
32
33 Thread::~Thread()
John Bauman66b8ab22014-05-06 15:57:45 -040034 {
Nicolas Capens7d168c42015-03-20 13:21:57 -070035 join(); // Make threads exit before deleting them to not block here
John Bauman89401822014-05-06 15:04:28 -040036 }
37
38 void Thread::join()
39 {
Nicolas Capens165ac812017-02-02 13:54:09 -050040 if(!hasJoined)
41 {
42 #if defined(_WIN32)
43 WaitForSingleObject(handle, INFINITE);
44 CloseHandle(handle);
45 #else
46 pthread_join(handle, NULL);
47 #endif
48
49 hasJoined = true;
50 }
John Bauman89401822014-05-06 15:04:28 -040051 }
52
53 #if defined(_WIN32)
54 unsigned long __stdcall Thread::startFunction(void *parameters)
55 {
56 Entry entry = *(Entry*)parameters;
57 entry.init->signal();
58 entry.threadFunction(entry.threadParameters);
59 return 0;
60 }
John Bauman66b8ab22014-05-06 15:57:45 -040061 #else
John Bauman89401822014-05-06 15:04:28 -040062 void *Thread::startFunction(void *parameters)
63 {
64 Entry entry = *(Entry*)parameters;
65 entry.init->signal();
66 entry.threadFunction(entry.threadParameters);
Nicolas Capens165ac812017-02-02 13:54:09 -050067 return nullptr;
John Bauman89401822014-05-06 15:04:28 -040068 }
John Bauman89401822014-05-06 15:04:28 -040069 #endif
70
71 Event::Event()
72 {
73 #if defined(_WIN32)
Nicolas Capens165ac812017-02-02 13:54:09 -050074 handle = CreateEvent(NULL, FALSE, FALSE, NULL);
John Bauman66b8ab22014-05-06 15:57:45 -040075 #else
Nicolas Capens165ac812017-02-02 13:54:09 -050076 pthread_cond_init(&handle, NULL);
77 pthread_mutex_init(&mutex, NULL);
John Bauman89401822014-05-06 15:04:28 -040078 signaled = false;
John Bauman89401822014-05-06 15:04:28 -040079 #endif
80 }
81
82 Event::~Event()
83 {
84 #if defined(_WIN32)
85 CloseHandle(handle);
John Bauman66b8ab22014-05-06 15:57:45 -040086 #else
John Bauman89401822014-05-06 15:04:28 -040087 pthread_cond_destroy(&handle);
88 pthread_mutex_destroy(&mutex);
John Bauman89401822014-05-06 15:04:28 -040089 #endif
90 }
91}