1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#include "condition.h"
5
6#if defined(__WIN32__) && !defined(PTHREADS_WIN32)
7
8#define WIN32_LEAN_AND_MEAN
9#include <windows.h>
10
11namespace embree
12{
13 struct ConditionImplementation
14 {
15 __forceinline ConditionImplementation () {
16 InitializeConditionVariable(&cond);
17 }
18
19 __forceinline ~ConditionImplementation () {
20 }
21
22 __forceinline void wait(MutexSys& mutex_in) {
23 SleepConditionVariableCS(&cond, (LPCRITICAL_SECTION)mutex_in.mutex, INFINITE);
24 }
25
26 __forceinline void notify_all() {
27 WakeAllConditionVariable(&cond);
28 }
29
30 public:
31 CONDITION_VARIABLE cond;
32 };
33}
34#endif
35
36#if defined(__UNIX__) || defined(PTHREADS_WIN32)
37#include <pthread.h>
38namespace embree
39{
40 struct ConditionImplementation
41 {
42 __forceinline ConditionImplementation () {
43 if (pthread_cond_init(&cond,nullptr) != 0)
44 THROW_RUNTIME_ERROR("pthread_cond_init failed");
45 }
46
47 __forceinline ~ConditionImplementation() {
48 MAYBE_UNUSED bool ok = pthread_cond_destroy(&cond) == 0;
49 assert(ok);
50 }
51
52 __forceinline void wait(MutexSys& mutex) {
53 if (pthread_cond_wait(&cond, (pthread_mutex_t*)mutex.mutex) != 0)
54 THROW_RUNTIME_ERROR("pthread_cond_wait failed");
55 }
56
57 __forceinline void notify_all() {
58 if (pthread_cond_broadcast(&cond) != 0)
59 THROW_RUNTIME_ERROR("pthread_cond_broadcast failed");
60 }
61
62 public:
63 pthread_cond_t cond;
64 };
65}
66#endif
67
68namespace embree
69{
70 ConditionSys::ConditionSys () {
71 cond = new ConditionImplementation;
72 }
73
74 ConditionSys::~ConditionSys() {
75 delete (ConditionImplementation*) cond;
76 }
77
78 void ConditionSys::wait(MutexSys& mutex) {
79 ((ConditionImplementation*) cond)->wait(mutex);
80 }
81
82 void ConditionSys::notify_all() {
83 ((ConditionImplementation*) cond)->notify_all();
84 }
85}
86