QT_BEGIN_NAMESPACE void QLocalSocketPrivate::init() { Q_Q(QLocalSocket); pipeReader = new QWindowsPipeReader(q); q->connect(pipeReader, SIGNAL(readyRead()), SIGNAL(readyRead())); q->connect(pipeReader, SIGNAL(pipeClosed()), SLOT(_q_pipeClosed()), Qt::QueuedConnection); q->connect(pipeReader, SIGNAL(winError(ulong,QString)), SLOT(_q_winError(ulong,QString))); }
/* * Convert X cursor to Windows cursor * FIXME: Perhaps there are more smart code */ HCURSOR winXCursorToHCURSOR(WMUTIL_CURSOR *pCursor) { HCURSOR hCursor = NULL; unsigned char *pAnd; unsigned char *pXor; int nCX, nCY; int nBytes; double dForeY, dBackY; BOOL fReverse; HBITMAP hAnd, hXor; ICONINFO ii; unsigned char *pCur; unsigned char bit; HDC hDC; BITMAPV4HEADER bi; BITMAPINFO *pbmi; uint32_t *lpBits; int sm_cx = GetSystemMetrics(SM_CXCURSOR); int sm_cy = GetSystemMetrics(SM_CYCURSOR); WIN_DEBUG_MSG("winXCursorToHCURSOR: Win32 size: %dx%d X11 size: %dx%d hotspot: %d,%d\n", sm_cx, sm_cy, pCursor->width, pCursor->height, pCursor->xhot, pCursor->yhot); /* We can use only White and Black, so calc brightness of color * Also check if the cursor is inverted */ dForeY = BRIGHTNESS(pCursor->fore); dBackY = BRIGHTNESS(pCursor->back); fReverse = dForeY < dBackY; /* Check whether the X11 cursor is bigger than the win32 cursor */ if (sm_cx < pCursor->width || sm_cy < pCursor->height) { winError("winXCursorToHCURSOR - Windows requires %dx%d cursor but X requires %dx%d\n", sm_cx, sm_cy, pCursor->width, pCursor->height); } /* Get the number of bytes required to store the whole cursor image * This is roughly (sm_cx * sm_cy) / 8 * round up to 8 pixel boundary so we can convert whole bytes */ nBytes = bits_to_bytes(sm_cx) * sm_cy; /* Get the effective width and height */ nCX = min(sm_cx, pCursor->width); nCY = min(sm_cy, pCursor->height); /* Allocate memory for the bitmaps */ pAnd = malloc(nBytes); memset(pAnd, 0xFF, nBytes); pXor = calloc(1, nBytes); memset(pXor, 0x00, nBytes); /* prepare the pointers */ hCursor = NULL; lpBits = NULL; /* We have a truecolor alpha-blended cursor and can use it! */ if (pCursor->argb) { WIN_DEBUG_MSG("winXCursorToHCURSOR: Trying truecolor alphablended cursor\n"); memset(&bi, 0, sizeof(BITMAPV4HEADER)); bi.bV4Size = sizeof(BITMAPV4HEADER); bi.bV4Width = sm_cx; bi.bV4Height = -(sm_cy); /* right-side up */ bi.bV4Planes = 1; bi.bV4BitCount = 32; bi.bV4V4Compression = BI_BITFIELDS; bi.bV4RedMask = 0x00FF0000; bi.bV4GreenMask = 0x0000FF00; bi.bV4BlueMask = 0x000000FF; bi.bV4AlphaMask = 0xFF000000; lpBits = (uint32_t *) calloc(sm_cx * sm_cy, sizeof(uint32_t)); if (lpBits) { int y; for (y = 0; y < nCY; y++) { void *src, *dst; src = &(pCursor->argb[y * pCursor->width]); dst = &(lpBits[y * sm_cx]); memcpy(dst, src, 4 * nCX); } } } /* End if-truecolor-icon */ else { /* Convert the X11 bitmap to a win32 bitmap * The first is for an empty mask */ if (pCursor->emptyMask) { int x, y, xmax = bits_to_bytes(nCX); for (y = 0; y < nCY; ++y) for (x = 0; x < xmax; ++x) { int nWinPix = bits_to_bytes(sm_cx) * y + x; int nXPix = BitmapBytePad(pCursor->width) * y + x; pAnd[nWinPix] = 0; if (fReverse) pXor[nWinPix] = reverse(~pCursor->source[nXPix]); else pXor[nWinPix] = reverse(pCursor->source[nXPix]); } } else { int x, y, xmax = bits_to_bytes(nCX); for (y = 0; y < nCY; ++y) for (x = 0; x < xmax; ++x) { int nWinPix = bits_to_bytes(sm_cx) * y + x; int nXPix = BitmapBytePad(pCursor->width) * y + x; unsigned char mask = pCursor->mask[nXPix]; pAnd[nWinPix] = reverse(~mask); if (fReverse) pXor[nWinPix] = reverse(~pCursor->source[nXPix] & mask); else pXor[nWinPix] = reverse(pCursor->source[nXPix] & mask); } } } if (!lpBits) { RGBQUAD *pbmiColors; /* Bicolor, use a palettized DIB */ WIN_DEBUG_MSG("winXCursorToHCURSOR: Trying two color cursor\n"); pbmi = (BITMAPINFO *) &bi; pbmiColors = &(pbmi->bmiColors[0]); memset(pbmi, 0, sizeof(BITMAPINFOHEADER)); pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->bmiHeader.biWidth = sm_cx; pbmi->bmiHeader.biHeight = -abs(sm_cy); /* right-side up */ pbmi->bmiHeader.biPlanes = 1; pbmi->bmiHeader.biBitCount = 8; pbmi->bmiHeader.biCompression = BI_RGB; pbmi->bmiHeader.biSizeImage = 0; pbmi->bmiHeader.biClrUsed = 3; pbmi->bmiHeader.biClrImportant = 3; pbmiColors[0].rgbRed = 0; /* Empty */ pbmiColors[0].rgbGreen = 0; pbmiColors[0].rgbBlue = 0; pbmiColors[0].rgbReserved = 0; pbmiColors[1].rgbRed = pCursor->backRed >> 8; /* Background */ pbmiColors[1].rgbGreen = pCursor->backGreen >> 8; pbmiColors[1].rgbBlue = pCursor->backBlue >> 8; pbmiColors[1].rgbReserved = 0; pbmiColors[2].rgbRed = pCursor->foreRed >> 8; /* Foreground */ pbmiColors[2].rgbGreen = pCursor->foreGreen >> 8; pbmiColors[2].rgbBlue = pCursor->foreBlue >> 8; pbmiColors[2].rgbReserved = 0; lpBits = (uint32_t *) calloc(sm_cx * sm_cy, sizeof(char)); pCur = (unsigned char *) lpBits; if (lpBits) { int x, y; for (y = 0; y < sm_cy; y++) { for (x = 0; x < sm_cx; x++) { if (x >= nCX || y >= nCY) /* Outside of X11 icon bounds */ (*pCur++) = 0; else { /* Within X11 icon bounds */ int nWinPix = bits_to_bytes(sm_cx) * y + (x / 8); bit = pAnd[nWinPix]; bit = bit & (1 << (7 - (x & 7))); if (!bit) { /* Within the cursor mask? */ int nXPix = BitmapBytePad(pCursor->width) * y + (x / 8); bit = ~reverse(~pCursor-> source[nXPix] & pCursor-> mask[nXPix]); bit = bit & (1 << (7 - (x & 7))); if (bit) /* Draw foreground */ (*pCur++) = 2; else /* Draw background */ (*pCur++) = 1; } else /* Outside the cursor mask */ (*pCur++) = 0; } } /* end for (x) */ } /* end for (y) */ } /* end if (lpbits) */ }
int main( int argc, char *argv[] ) { int i; HANDLE bgThread; DWORD exitCode; SECURITY_ATTRIBUTES semSec; MSEmul_UseSharedMemory(true); #if 1 { char test[256], *head; test[0] = '\0'; head = test; fprintf( stderr, "appending to test[%lu] with snprintf:\n", sizeof(test) ); do{ int len = strlen(test), n; size_t ni = strlen("0123456789"); fprintf( stderr, "len(test[%lu])=%d, rem. %lu added", sizeof(test), len, (size_t)(sizeof(test) - len) ); fflush(stderr); n = snprintf( &test[len], (size_t)(sizeof(test) - len), "0123456789" ); head += n; fprintf( stderr, " %d (head[%lu]=", n, head - test ); fflush(stderr); if( len + n < sizeof(test) ){ fprintf( stderr, "%c) -> %lu\n", head[0], strlen(test) ); } else{ fprintf( stderr, "!$@#) -> %lu\n", strlen(test) ); } } while( strlen(test) < sizeof(test)-1 ); fprintf( stderr, "test = %s\n", test ); } #endif init_HRTime(); tStart = HRTime_Time(); HRTime_tic(); { double t0; DWORD ret; HANDLE hh; if( (hh = CreateSemaphore( NULL, 1, 0x7FFFFFFF, NULL )) ){ t0 = HRTime_Time(); ret = WaitForSingleObject(hh, (DWORD)(SLEEPTIMEFG*1000)); t0 = HRTime_Time() - t0; fprintf( stderr, "WaitForSingleObject(hh,%lu)==%lu took %g seconds\n", (DWORD)(SLEEPTIMEFG*1000), ret, t0 ); t0 = HRTime_Time(); ret = WaitForSingleObject(hh, (DWORD)(SLEEPTIMEFG*1000)); t0 = HRTime_Time() - t0; fprintf( stderr, "WaitForSingleObject(hh,%lu)==%lu took %g seconds\n", (DWORD)(SLEEPTIMEFG*1000), ret, t0 ); t0 = HRTime_Time(); ret = WaitForSingleObject(hh, 500); t0 = HRTime_Time() - t0; fprintf( stderr, "WaitForSingleObject(hh,500)==%lu took %g seconds\n", ret, t0 ); ReleaseSemaphore(hh, 1, NULL); t0 = HRTime_Time(); ret = WaitForSingleObject(hh, 500); t0 = HRTime_Time() - t0; fprintf( stderr, "WaitForSingleObject(hh,500)==%lu took %g seconds after ReleaseSemaphore()\n", ret, t0 ); CloseHandle(hh); } else{ fprintf( stderr, "Error creating semaphore: %s\n", winError(GetLastError()) ); } ret = 0; YieldProcessor(); fprintf( stderr, "sizeof(long)=%lu\n", sizeof(long) ); _WriteBarrier(); { long oval, lbool; void *ptr = NULL, *optr; oval = _InterlockedCompareExchange( (long*) &ret, 10L, 0L ); fprintf( stderr, "_InterlockedCompareExchange(&ret==0, 10, 0) == %lu, ret==%lu\n", oval, ret ); optr = InterlockedCompareExchangePointer( &ptr, (void*) fprintf, NULL ); fprintf( stderr, "InterlockedCompareExchangePointer(&ptr==NULL, fprintf==%p, NULL) == %p, ret==%p\n", fprintf, optr, ptr ); _InterlockedIncrement( (long*) &ret ); fprintf( stderr, "_InterlockedIncrement(&ret) ret=%lu\n", ret ); _InterlockedDecrement( (long*) &ret ); fprintf( stderr, "_InterlockedDecrement(&ret) ret=%lu\n", ret ); _ReadWriteBarrier(); lbool = false; _InterlockedSetTrue(&lbool); fprintf( stderr, "lbool = %ld\n", lbool ); _InterlockedSetTrue(&lbool); fprintf( stderr, "lbool = %ld\n", lbool ); _InterlockedSetFalse(&lbool); fprintf( stderr, "lbool = %ld\n", lbool ); } } #ifdef DEBUG { CSEScopedLock *scope = ObtainCSEScopedLock(NULL); fprintf( stderr, "NULL testscope %p:locked==%u\n", scope, IsCSEScopeLocked(scope) ); scope = ReleaseCSEScopedLock(scope); } #endif csex = CreateCSEHandle(4000); if( !csex ){ fprintf( stderr, "Failure creating CSEHandle\n" ); exit(1); } else{ fprintf( stderr, "Created a '%s' CSEHandle with spinMax==%lu\n", CSEHandleInfo(csex), csex->spinMax ); } fprintf( stderr, "GetCurrentThread() = 0x%p\n", GetCurrentThread() ); Sleep(5); fprintf( stderr, "GetCurrentThread() = 0x%p\n", GetCurrentThread() ); Sleep(5); fprintf( stderr, "GetCurrentThread() = 0x%p\n", GetCurrentThread() ); Sleep(5); fputs( "\n", stderr ); if( (bgThread = CreateThread( NULL, 0, bgThreadSleeper, NULL, CREATE_SUSPENDED, NULL )) ){ unsigned long ret; double tEnd; fprintf( stderr, ">%lx t=%g will start %lx and WaitForSingleObject on it (should take 5s)\n", GetCurrentThreadId(), HRTime_Time() - tStart, GetThreadId(bgThread) ); ResumeThread(bgThread); // Sleep(1); ret = WaitForSingleObject( bgThread, 1500 ); tEnd = HRTime_Time(); GetExitCodeThread( bgThread, &exitCode ); fprintf( stderr, ">%lx WaitForSingleObject(bgThread,1500)=%lu exitCode=%ld at t=%g\n", GetCurrentThreadId(), ret, exitCode, tEnd - tStart ); ret = WaitForSingleObject( bgThread, 10000 ); tEnd = HRTime_Time(); GetExitCodeThread( bgThread, &exitCode ); fprintf( stderr, ">%lx WaitForSingleObject(bgThread,10000)=%lu exitCode=%ld at t=%g\n", GetCurrentThreadId(), ret, exitCode, tEnd - tStart ); CloseHandle(bgThread); } fputs( "\n", stderr ); if( (nudgeEvent = CreateEvent( NULL, false, false, NULL )) ){ if( (bgThread = CreateThread( NULL, 0, bgThread2Nudge, NULL, 0, NULL )) ){ unsigned long ret; double tEnd; Sleep(1000); fprintf( stderr, ">%lx t=%g SetEvent(nudgeEvent) = %d; sleep(1ms) and then wait for return nudge\n", GetCurrentThreadId(), HRTime_Time() - tStart, SetEvent(nudgeEvent) ); Sleep(1); ret = WaitForSingleObject( nudgeEvent, INFINITE ); tEnd = HRTime_Time(); fprintf( stderr, ">%lx WaitForSingleObject( nudgeEvent, INFINITE ) = %lu at t=%g\n", GetCurrentThreadId(), ret, tEnd - tStart ); ret = WaitForSingleObject( bgThread, 5000 ); tEnd = HRTime_Time(); GetExitCodeThread( bgThread, &exitCode ); fprintf( stderr, ">%lx WaitForSingleObject(bgThread,5000)=%lu exitCode=%ld at t=%g\n", GetCurrentThreadId(), ret, exitCode, tEnd - tStart ); CloseHandle(bgThread); } CloseHandle(nudgeEvent); } fputs( "\n", stderr ); semSec.nLength = sizeof(SECURITY_ATTRIBUTES); semSec.lpSecurityDescriptor = NULL; semSec.bInheritHandle = true; // semSec does not actually need to be used: if( (nudgeEvent = CreateSemaphore( &semSec, 0, 0x7FFFFFFF, (char*) "cseSem" )) ){ if( (bgThread = CreateThread( NULL, 0, bgThread4SemTest, NULL, 0, NULL )) ){ unsigned long ret; double tEnd; Sleep(1000); fprintf( stderr, ">%lx t=%g ReleaseSemaphore(nudgeEvent,1,NULL) = %d; sleep(1ms) and then wait for return nudge\n", GetCurrentThreadId(), HRTime_Time() - tStart, ReleaseSemaphore(nudgeEvent, 1, NULL) ); Sleep(1); ret = WaitForSingleObject( nudgeEvent, INFINITE ); tEnd = HRTime_Time(); fprintf( stderr, ">%lx WaitForSingleObject( nudgeEvent, INFINITE ) = %lu at t=%g\n", GetCurrentThreadId(), ret, tEnd - tStart ); ret = WaitForSingleObject( bgThread, 5000 ); tEnd = HRTime_Time(); GetExitCodeThread( bgThread, &exitCode ); fprintf( stderr, ">%lx WaitForSingleObject(bgThread,5000)=%lu exitCode=%ld at t=%g\n", GetCurrentThreadId(), ret, exitCode, tEnd - tStart ); CloseHandle(bgThread); } CloseHandle(nudgeEvent); } fputs( "\n", stderr ); if( (bgThread = CreateThread( NULL, 0, bgCSEXaccess, NULL, CREATE_SUSPENDED, NULL )) ){ fprintf( stderr, "csex is %slocked\n", (IsCSEHandleLocked(csex))? "" : "not " ); SetThreadPriority( bgThread, GetThreadPriority(GetCurrentThread()) ); fprintf( stderr, "GetThreadPriority(GetCurrentThread()) = %d\n", GetThreadPriority(GetCurrentThread()) ); ResumeThread(bgThread); i = 0; fprintf( stderr, "entering main csex locking loop at t=%gs\n", HRTime_toc() ); while( i < 5 ){ double t0, t1; if( IsCSEHandleLocked(csex) ){ fprintf( stderr, "\tmain loop waiting for csex lock\n" ); } t0 = HRTime_toc(); { #ifdef LOCKSCOPEFG CSEScopedLock *scope = ObtainCSEScopedLock(csex); #else unsigned char unlock = LockCSEHandle(csex); #endif t1 = HRTime_toc(); i += 1; fprintf( stderr, "> got csex lock #%d=%d at t=%g after %gs; starting %g s wait\n", i, IsCSEHandleLocked(csex), t1, t1-t0, SLEEPTIMEFG ); fflush(stderr); #ifdef BUSYSLEEPING MMSleep(SLEEPTIMEFG); #else do{ t1 = HRTime_toc(); } while (t1-t0 < SLEEPTIMEFG); #endif fprintf( stderr, "\tmain loop wait #%d ended at t=%gs; csex lock=%d\n", i, HRTime_toc(), IsCSEHandleLocked(csex) ); fflush(stderr); #ifndef LOCKSCOPEFG UnlockCSEHandle( csex, unlock ); #else scope = ReleaseCSEScopedLock(scope); #endif } // just to give the other thread a chance to get a lock: Sleep(1); } fprintf( stderr, "exiting main csex locking loop at t=%gs\n", HRTime_toc() ); _InterlockedSetFalse(&bgRun); WaitForSingleObject( bgThread, 5000 ); CloseHandle(bgThread); fprintf( stderr, "Background loop finished at t=%gs\n", HRTime_toc() ); } else{ fprintf( stderr, "Failure creating bgCSEXaccess thread\n" ); } DeleteCSEHandle(csex); ASSERT(1); ASSERT(0); exit(0); }