JackWinSemaphore.cpp 4.07 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2004-2005 Grame
sletz's avatar
sletz committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "JackWinSemaphore.h"
21
#include "JackTools.h"
sletz's avatar
sletz committed
22
#include "JackError.h"
sletz's avatar
sletz committed
23
#include <stdio.h>
sletz's avatar
sletz committed
24
25
26
27
28
29

namespace Jack
{

void JackWinSemaphore::BuildName(const char* name, const char* server_name, char* res)
{
30
    sprintf(res, "jack_pipe.%d_%s_%s", JackTools::GetUID(), server_name, name);
sletz's avatar
sletz committed
31
32
33
34
35
36
37
38
39
}

bool JackWinSemaphore::Signal()
{
    BOOL res;
    assert(fSemaphore);

    if (fFlush)
        return true;
sletz's avatar
sletz committed
40

sletz's avatar
sletz committed
41
42
43
    if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
        jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
    }
sletz's avatar
sletz committed
44

sletz's avatar
sletz committed
45
46
47
48
49
50
51
52
53
54
55
    return res;
}

bool JackWinSemaphore::SignalAll()
{
    BOOL res;
    assert(fSemaphore);

    if (fFlush)
        return true;

sletz's avatar
sletz committed
56
    if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
sletz's avatar
sletz committed
57
58
        jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
    }
sletz's avatar
sletz committed
59

sletz's avatar
sletz committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    return res;
}

bool JackWinSemaphore::Wait()
{
    DWORD res;

    if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
        jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
    }

    return (res == WAIT_OBJECT_0);
}

bool JackWinSemaphore::TimedWait(long usec)
{
    DWORD res;

    if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
sletz's avatar
sletz committed
79
        jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
sletz's avatar
sletz committed
80
81
82
83
84
85
86
87
88
    }

    return (res == WAIT_OBJECT_0);
}

// Client side : get the published semaphore from server
bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name)
{
    BuildName(name, server_name, fName);
sletz's avatar
sletz committed
89
    jack_log("JackWinSemaphore::Connect %s", fName);
sletz's avatar
sletz committed
90
91
92

    // Temporary...
    if (fSemaphore) {
sletz's avatar
sletz committed
93
        jack_log("Already connected name = %s", name);
sletz's avatar
sletz committed
94
95
96
        return true;
    }

sletz's avatar
sletz committed
97
98
    if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) {
        jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
sletz's avatar
sletz committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        return false;
    } else {
        return true;
    }
}

bool JackWinSemaphore::Connect(const char* name, const char* server_name)
{
    return ConnectInput(name, server_name);
}

bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name)
{
    return ConnectInput(name, server_name);
}

bool JackWinSemaphore::Disconnect()
{
    if (fSemaphore) {
sletz's avatar
sletz committed
118
        jack_log("JackWinSemaphore::Disconnect %s", fName);
sletz's avatar
sletz committed
119
120
121
122
123
124
125
126
127
128
129
        CloseHandle(fSemaphore);
        fSemaphore = NULL;
        return true;
    } else {
        return false;
    }
}

bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value)
{
    BuildName(name, server_name, fName);
sletz's avatar
sletz committed
130
    jack_log("JackWinSemaphore::Allocate name = %s val = %ld", fName, value);
sletz's avatar
sletz committed
131

sletz's avatar
sletz committed
132
    if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) {
sletz's avatar
Typo    
sletz committed
133
        jack_error("Allocate: can't check in named semaphore name = %s err = %ld", fName, GetLastError());
sletz's avatar
sletz committed
134
135
        return false;
    } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
sletz's avatar
sletz committed
136
137
138
139
140
141
        jack_error("Allocate: named semaphore already exist name = %s", fName);
        CloseHandle(fSemaphore);
        fSemaphore = NULL;
        return false;
    } else {
        return true;
sletz's avatar
sletz committed
142
143
144
145
146
147
    }
}

void JackWinSemaphore::Destroy()
{
    if (fSemaphore != NULL) {
sletz's avatar
sletz committed
148
        jack_log("JackWinSemaphore::Destroy %s", fName);
sletz's avatar
sletz committed
149
150
151
152
153
154
155
156
157
158
        CloseHandle(fSemaphore);
        fSemaphore = NULL;
    } else {
        jack_error("JackWinSemaphore::Destroy synchro == NULL");
    }
}


} // end of namespace