JackWinSemaphore.cpp

00001 /*
00002 Copyright (C) 2004-2005 Grame  
00003 
00004 This program is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008 
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details.
00013 
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #include "JackWinSemaphore.h"
00021 #include "JackError.h"
00022 
00023 namespace Jack
00024 {
00025 
00026 void JackWinSemaphore::BuildName(const char* name, const char* server_name, char* res)
00027 {
00028     sprintf(res, "jack_pipe.%s_%s", server_name, name);
00029 }
00030 
00031 bool JackWinSemaphore::Signal()
00032 {
00033     BOOL res;
00034     assert(fSemaphore);
00035 
00036     if (fFlush)
00037         return true;
00038         
00039     if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
00040         jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
00041     }
00042         
00043     return res;
00044 }
00045 
00046 bool JackWinSemaphore::SignalAll()
00047 {
00048     BOOL res;
00049     assert(fSemaphore);
00050 
00051     if (fFlush)
00052         return true;
00053 
00054         if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
00055         jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
00056     }
00057         
00058     return res;
00059 }
00060 
00061 bool JackWinSemaphore::Wait()
00062 {
00063     DWORD res;
00064 
00065     if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
00066         jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
00067     }
00068 
00069     return (res == WAIT_OBJECT_0);
00070 }
00071 
00072 bool JackWinSemaphore::TimedWait(long usec)
00073 {
00074     DWORD res;
00075 
00076     if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
00077                 jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
00078     }
00079 
00080     return (res == WAIT_OBJECT_0);
00081 }
00082 
00083 // Client side : get the published semaphore from server
00084 bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name)
00085 {
00086     BuildName(name, server_name, fName);
00087     JackLog("JackWinSemaphore::Connect %s\n", fName);
00088 
00089     // Temporary...
00090     if (fSemaphore) {
00091         JackLog("Already connected name = %s\n", name);
00092         return true;
00093     }
00094 
00095         if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) {
00096                 jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
00097         return false;
00098     } else {
00099         return true;
00100     }
00101 }
00102 
00103 bool JackWinSemaphore::Connect(const char* name, const char* server_name)
00104 {
00105     return ConnectInput(name, server_name);
00106 }
00107 
00108 bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name)
00109 {
00110     return ConnectInput(name, server_name);
00111 }
00112 
00113 bool JackWinSemaphore::Disconnect()
00114 {
00115     if (fSemaphore) {
00116         JackLog("JackWinSemaphore::Disconnect %s\n", fName);
00117         CloseHandle(fSemaphore);
00118         fSemaphore = NULL;
00119         return true;
00120     } else {
00121         return false;
00122     }
00123 }
00124 
00125 bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value)
00126 {
00127     BuildName(name, server_name, fName);
00128     JackLog("JackWinSemaphore::Allocate name = %s val = %ld\n", fName, value);
00129 
00130         if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) {
00131         jack_error("Allocate: can't check in named semaphore name = %s err = %ld", fName, GetLastError());
00132         return false;
00133     } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
00134                 jack_error("Allocate: named semaphore already exist name = %s", fName);
00135                 CloseHandle(fSemaphore);
00136                 fSemaphore = NULL;
00137                 return false;
00138         } else {
00139                 return true;
00140     }
00141 }
00142 
00143 void JackWinSemaphore::Destroy()
00144 {
00145     if (fSemaphore != NULL) {
00146                 JackLog("JackWinSemaphore::Destroy %s\n", fName);
00147         CloseHandle(fSemaphore);
00148         fSemaphore = NULL;
00149     } else {
00150         jack_error("JackWinSemaphore::Destroy synchro == NULL");
00151     }
00152 }
00153 
00154 
00155 } // end of namespace
00156 

Generated on Thu Feb 14 11:16:02 2008 for Jackdmp by  doxygen 1.5.1