00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "JackWinSemaphore.h"
00021 #include "JackTools.h"
00022 #include "JackError.h"
00023 #include <stdio.h>
00024
00025 namespace Jack
00026 {
00027
00028 void JackWinSemaphore::BuildName(const char* name, const char* server_name, char* res)
00029 {
00030 sprintf(res, "jack_pipe.%s_%s", server_name, name);
00031 }
00032
00033 bool JackWinSemaphore::Signal()
00034 {
00035 BOOL res;
00036 assert(fSemaphore);
00037
00038 if (fFlush)
00039 return true;
00040
00041 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
00042 jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
00043 }
00044
00045 return res;
00046 }
00047
00048 bool JackWinSemaphore::SignalAll()
00049 {
00050 BOOL res;
00051 assert(fSemaphore);
00052
00053 if (fFlush)
00054 return true;
00055
00056 if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
00057 jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
00058 }
00059
00060 return res;
00061 }
00062
00063 bool JackWinSemaphore::Wait()
00064 {
00065 DWORD res;
00066
00067 if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
00068 jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
00069 }
00070
00071 return (res == WAIT_OBJECT_0);
00072 }
00073
00074 bool JackWinSemaphore::TimedWait(long usec)
00075 {
00076 DWORD res;
00077
00078 if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
00079 jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
00080 }
00081
00082 return (res == WAIT_OBJECT_0);
00083 }
00084
00085
00086 bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name)
00087 {
00088 BuildName(name, server_name, fName);
00089 jack_log("JackWinSemaphore::Connect %s", fName);
00090
00091
00092 if (fSemaphore) {
00093 jack_log("Already connected name = %s", name);
00094 return true;
00095 }
00096
00097 if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) {
00098 jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
00099 return false;
00100 } else {
00101 return true;
00102 }
00103 }
00104
00105 bool JackWinSemaphore::Connect(const char* name, const char* server_name)
00106 {
00107 return ConnectInput(name, server_name);
00108 }
00109
00110 bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name)
00111 {
00112 return ConnectInput(name, server_name);
00113 }
00114
00115 bool JackWinSemaphore::Disconnect()
00116 {
00117 if (fSemaphore) {
00118 jack_log("JackWinSemaphore::Disconnect %s", fName);
00119 CloseHandle(fSemaphore);
00120 fSemaphore = NULL;
00121 return true;
00122 } else {
00123 return false;
00124 }
00125 }
00126
00127 bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value)
00128 {
00129 BuildName(name, server_name, fName);
00130 jack_log("JackWinSemaphore::Allocate name = %s val = %ld", fName, value);
00131
00132 if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) {
00133 jack_error("Allocate: can't check in named semaphore name = %s err = %ld", fName, GetLastError());
00134 return false;
00135 } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
00136 jack_error("Allocate: named semaphore already exist name = %s", fName);
00137 CloseHandle(fSemaphore);
00138 fSemaphore = NULL;
00139 return false;
00140 } else {
00141 return true;
00142 }
00143 }
00144
00145 void JackWinSemaphore::Destroy()
00146 {
00147 if (fSemaphore != NULL) {
00148 jack_log("JackWinSemaphore::Destroy %s", fName);
00149 CloseHandle(fSemaphore);
00150 fSemaphore = NULL;
00151 } else {
00152 jack_error("JackWinSemaphore::Destroy synchro == NULL");
00153 }
00154 }
00155
00156
00157 }
00158