File: Sync.cpp

package info (click to toggle)
pd-vstplugin 0.6.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,008 kB
  • sloc: cpp: 22,783; lisp: 2,860; makefile: 37; sh: 26
file content (153 lines) | stat: -rw-r--r-- 3,451 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Sync.h"

#if VST_HOST_SYSTEM == VST_WINDOWS
# ifndef NOMINMAX
#  define NOMINMAX
# endif
# include <windows.h>
# include <malloc.h>
#else
# include <stdlib.h>
#endif

#include <climits>

namespace vst {

/*/////////////////// SyncCondition ////////////////////*/

SyncCondition::SyncCondition(){
#if VST_HOST_SYSTEM == VST_WINDOWS
    InitializeConditionVariable((PCONDITION_VARIABLE)&condition_);
    InitializeSRWLock((PSRWLOCK)&mutex_);
#else // pthreads
    pthread_mutex_init(&mutex_, 0);
    pthread_cond_init(&condition_, 0);
#endif
}

SyncCondition::~SyncCondition(){
#if VST_HOST_SYSTEM != VST_WINDOWS
    pthread_mutex_destroy(&mutex_);
    pthread_cond_destroy(&condition_);
#endif
}

void SyncCondition::set(){
#if VST_HOST_SYSTEM == VST_WINDOWS
    AcquireSRWLockExclusive((PSRWLOCK)&mutex_);
    state_ = true;
    ReleaseSRWLockExclusive((PSRWLOCK)&mutex_);

    WakeConditionVariable((PCONDITION_VARIABLE)&condition_);
#else
    pthread_mutex_lock(&mutex_);
    state_ = true;
    pthread_mutex_unlock(&mutex_);

    pthread_cond_signal(&condition_);
#endif
}

void SyncCondition::wait(){
#if VST_HOST_SYSTEM == VST_WINDOWS
    AcquireSRWLockExclusive((PSRWLOCK)&mutex_);
    while (!state_){
        SleepConditionVariableSRW((PCONDITION_VARIABLE)&condition_,
                                  (PSRWLOCK)&mutex_, INFINITE, 0);
    }
    state_ = false;
    ReleaseSRWLockExclusive((PSRWLOCK)&mutex_);
#else
    pthread_mutex_lock(&mutex_);
    while (!state_){
        pthread_cond_wait(&condition_, &mutex_);
    }
    state_ = false;
    pthread_mutex_unlock(&mutex_);
#endif
}

/*/////////////////// Semaphore ////////////////////*/

Semaphore::Semaphore(){
#if VST_HOST_SYSTEM == VST_WINDOWS
    sem_ = CreateSemaphoreA(0, 0, INT_MAX, 0);
#elif VST_HOST_SYSTEM == VST_MACOS
    semaphore_create(mach_task_self(), &sem_, SYNC_POLICY_FIFO, 0);
#else // Linux
    sem_init(&sem_, 0, 0);
#endif
}

Semaphore::~Semaphore(){
#if VST_HOST_SYSTEM == VST_WINDOWS
    CloseHandle(sem_);
#elif VST_HOST_SYSTEM == VST_MACOS
    semaphore_destroy(mach_task_self(), sem_);
#else // Linux
    sem_destroy(&sem_);
#endif
}

void Semaphore::post(){
#if VST_HOST_SYSTEM == VST_WINDOWS
    ReleaseSemaphore(sem_, 1, 0);
#elif VST_HOST_SYSTEM == VST_MACOS
    semaphore_signal(sem_);
#else // Linux
    sem_post(&sem_);
#endif
}

void Semaphore::post(int count){
#if VST_HOST_SYSTEM == VST_WINDOWS
    ReleaseSemaphore(sem_, count, 0);
#else
    for (int i = 0; i < count; ++i) {
        post();
    }
#endif
}

void Semaphore::wait(){
#if VST_HOST_SYSTEM == VST_WINDOWS
    WaitForSingleObject(sem_, INFINITE);
#elif VST_HOST_SYSTEM == VST_MACOS
    semaphore_wait(sem_);
#else // Linux
    while (sem_wait(&sem_) == -1 && errno == EINTR) continue;
#endif
}

/*////////////////////// SharedMutex ///////////////////*/

#if VST_HOST_SYSTEM == VST_WINDOWS

Mutex::Mutex() {
    InitializeSRWLock((PSRWLOCK)& lock_);
}
// exclusive
void Mutex::lock() {
    AcquireSRWLockExclusive((PSRWLOCK)&lock_);
}
bool Mutex::try_lock() {
    return TryAcquireSRWLockExclusive((PSRWLOCK)&lock_);
}
void Mutex::unlock() {
    ReleaseSRWLockExclusive((PSRWLOCK)&lock_);
}
// shared
void SharedMutex::lock_shared() {
    AcquireSRWLockShared((PSRWLOCK)&lock_);
}
bool SharedMutex::try_lock_shared() {
    return TryAcquireSRWLockShared((PSRWLOCK)&lock_);
}
void SharedMutex::unlock_shared() {
    ReleaseSRWLockShared((PSRWLOCK)&lock_);
}

#endif

} // vst