f.Printf(" = 0");
f.Printf(";\n\n");
}
- f.Printf(" incref this;\n");
+ // f.Printf(" incref this;\n");
+ f.Printf(" safeIncRef();\n");
for(param = method.dataType.params.first; param; param = param.next)
{
char type[1024] = "";
}
f.Printf(" }\n");
f.Printf(" __ecereBuffer.Free();\n");
- f.Printf(" delete this;\n");
+ //f.Printf(" delete this;\n");
+ f.Printf(" safeDecRef();\n");
if(method.dataType.returnType.kind != voidType)
{
f.Printf(" return __ecereResult;\n");
#endif
}
+ void LockEx(int count)
+ {
+#if !defined(__EMSCRIPTEN__)
+ int i;
+ for(i = 0; i < count; i++)
+ {
+ lockMutex.Wait();
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
+ if(xGlobalDisplay)
+ XLockDisplay(xGlobalDisplay);
+#endif
+ }
+#endif
+ }
+
+ int UnlockEx(void)
+ {
+ int count = 0;
+#if !defined(__EMSCRIPTEN__)
+ int i;
+ count = lockMutex.owningThread == GetCurrentThreadID() ? lockMutex.lockCount : 0;
+ for(i = 0; i < count; i++)
+ {
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
+ if(xGlobalDisplay)
+ XUnlockDisplay(xGlobalDisplay);
+#endif
+ lockMutex.Release();
+ }
+#endif
+ return count;
+ }
+
Cursor GetCursor(SystemCursor cursor)
{
return systemCursors[cursor];
#if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
if(this)
{
+ safeIncRef();
+ {
SOCKET s = this.s;
int count;
fd_set ws, es;
// This is what was making eCom jam...
// select(s+1, null, &ws, &es, null);
+
+ safeDecRef();
return true;
}
+ }
+ safeDecRef();
}
#endif
return false;
}
virtual bool OnEstablishConnection(int s);
+ dllexport void safeIncRef()
+ {
+ mutex.Wait();
+ incref this;
+ //mutex.Release();
+ }
+
+ dllexport void safeDecRef()
+ {
+ Mutex mutex = this.mutex;
+ //mutex.Wait();
+ delete this;
+ mutex.Release();
+ }
+
private:
Socket()
{
{
bool result = false;
SOCKET s;
+ Mutex mutex = this.mutex;
- incref this;
mutex.Wait();
+ incref this;
// network.mutex.Wait();
s = this.s;
if(FD_ISSET(s, rs) || leftOver)
uint recvCount = OnReceive(recvBuffer + flushCount, recvBytes - flushCount);
if(!recvCount)
{
- leftOver = true;
+ if(recvBytes)
+ leftOver = true;
if(!processAlone)
network.leftOverBytes = true;
break;
_Disconnect(disconnectCode);
}
// network.mutex.Release();
+ delete this;
mutex.Release();
- delete this;
return result;
}
#endif
struct timeval tvTO = {(uint)timeOut, (uint)((timeOut -(uint)timeOut)* 1000000)};
fd_set rs, ws, es;
int selectResult;
+ Mutex mutex;
if(disconnectCode > 0 && !leftOver) return false;
FD_ZERO(&rs);
//FD_SET(s, &ws);
FD_SET(s, &es);
+ mutex = this.mutex;
+ mutex.Wait();
incref this;
+ mutex.Release();
selectResult = select((int)(s+1), &rs, &ws, &es, leftOver ? &tv : (timeOut ? &tvTO : null));
- mutex.Wait();
+
if(s != -1 && _refCount && (leftOver || selectResult))
{
gotEvent |= ProcessSocket(&rs, &ws, &es);
}
- mutex.Release();
+ mutex.Wait();
delete this;
+ mutex.Release();
return gotEvent;
}
if(serverSocket && serverSocket.connected)
{
+ int lockCount;
int64 currentThreadID = GetCurrentThreadID();
int callID = nextCallID++;
DCOMServerSocket socket = serverSocket;
if((ack = VirtualCallAcknowledged(methodID, id, callID)))
break;
- guiApp.Unlock();
+ lockCount = guiApp.UnlockEx();
mutex.Release();
if(processingSocket && processingSocket.connected)
processingSocket.ProcessTimeOut(0.01);
else
ecere::sys::Sleep(0.01);//serverSocket.thread.semaphore.Wait();
- guiApp.Lock();
+ guiApp.LockEx(lockCount);
mutex.Wait();
}
delete ack;
}
- guiApp.Unlock();
+ lockCount = guiApp.UnlockEx();
mutex.Release();
if(socket._refCount > 1)
processingSocket._refCount--;
delete processingSocket;
- guiApp.Lock();
+ guiApp.LockEx(lockCount);
mutex.Wait();
if(_refCount > 1)
bool connected;
unsigned int Main()
{
+ socket.mutex.Wait();
socket._refCount += 2;
+ socket.mutex.Release();
while(connected)
{
socket.ProcessTimeOut(0.01);
semaphore.Release();
}
- if(socket._refCount > 1) socket._refCount--;
+ socket.mutex.Wait();
+ if(socket._refCount > 1)
+ socket._refCount--;
+ socket.mutex.Release();
delete socket;
return 0;
}
bool result = false;
if(Socket::Connect(server, port))
{
+ int lockCount;
int len = (int)(strlen(_class.name) + 4 - strlen("DCOMClient_"));
unsigned int size = sizeof(class CreateInstancePacket) + len;
CreateInstancePacket packet = (CreateInstancePacket)new0 byte[size];
thread.socket = this;
thread.connected = true;
thread.Create();
- guiApp.Unlock();
+ lockCount = guiApp.UnlockEx();
while(!answered && thread && connected)
{
//guiApp.WaitNetworkEvent();
else
thread.semaphore.Wait();
}
- guiApp.Lock();
+ guiApp.LockEx(lockCount);
result = connected;
}
return result;
break;
}
}
- guiApp.Unlock();
}
+ guiApp.Unlock();
}
void OnDisconnect(int code)
while(true)
{
+ int lockCount;
if(!thread || !connected)
break;
if((ack = CallAcknowledged(methodID, objectID, callID)))
break;
- guiApp.Unlock();
+ lockCount = guiApp.UnlockEx();
//guiApp.WaitNetworkEvent();
//guiApp.ProcessNetworkEvents();
//Process();
+ mutex.Release();
if(GetCurrentThreadID() == (int64)thread.id)
ProcessTimeOut(0.01);
else
ecere::sys::Sleep(0.01);//thread.semaphore.Wait();
- guiApp.Lock();
+ mutex.Wait();
+ guiApp.LockEx(lockCount);
}
if(ack)
public:
void Stop()
{
- while(sendingOut) guiApp.Unlock(), ecere::sys::Sleep(0.01), guiApp.Lock();
+ while(sendingOut) { int lockCount = guiApp.UnlockEx(); ecere::sys::Sleep(0.01); guiApp.LockEx(lockCount); }
sendingOut = true;
}
#endif
#endif
-#ifdef _DEBUG
- int64 owningThread;
-#endif
- int lockCount;
+//#ifdef _DEBUG
+ int64 _owningThread;
+//#endif
+ int _lockCount;
Mutex()
{
#endif
#endif
- lockCount = 0;
-#ifdef _DEBUG
- owningThread = 0;
-#endif
+ _lockCount = 0;
+//#ifdef _DEBUG
+ _owningThread = 0;
+//#endif
return true;
}
#endif
-#ifdef _DEBUG
- owningThread = GetCurrentThreadID();
-#endif
- lockCount++;
+//#ifdef _DEBUG
+ _owningThread = GetCurrentThreadID();
+//#endif
+ _lockCount++;
}
}
printf("[%d] Releasing Mutex %x\n", (int)GetCurrentThreadID(), this);
*/
#ifdef _DEBUG
- if(lockCount && owningThread != GetCurrentThreadID())
+ if(_lockCount && _owningThread != GetCurrentThreadID())
PrintLn("WARNING: Not in owning thread!!");
#endif
- if(!--lockCount)
-#ifdef _DEBUG
- owningThread = 0;
-#else
- ;
-#endif
+ if(!--_lockCount)
+ {
+//#ifdef _DEBUG
+ _owningThread = 0;
+//#endif
+ }
#if defined(__WIN32__)
#ifdef _DEBUG
ReleaseMutex(mutex);
#endif
#ifdef _DEBUG
- if(lockCount < 0)
+ if(_lockCount < 0)
PrintLn("WARNING: lockCount < 0");
#endif
}
}
- property int lockCount { get { return lockCount; } }
+ property int lockCount { get { return _lockCount; } }
+ property int64 owningThread { get { return _owningThread; } }
};