/* * Allocate a new ThreadInfo structure, setup a connection to the * server, allocate/init a packer context, bind this ThreadInfo to * the calling thread with crSetTSD(). * We'll always call this function at least once even if we're not * using threads. */ ThreadInfo *replicatespuNewThread( CRthread id ) { ThreadInfo *thread; #ifdef CHROMIUM_THREADSAFE_notyet crLockMutex(&_ReplicateMutex); #else CRASSERT(replicate_spu.numThreads == 0); #endif CRASSERT(replicate_spu.numThreads < MAX_THREADS); thread = &(replicate_spu.thread[replicate_spu.numThreads]); thread->id = id; thread->currentContext = NULL; /* connect to the server */ thread->server.name = crStrdup( replicate_spu.name ); thread->server.buffer_size = replicate_spu.buffer_size; if (replicate_spu.numThreads == 0) { replicatespuConnectToServer( &(thread->server) ); CRASSERT(thread->server.conn); replicate_spu.swap = thread->server.conn->swap; } else { /* a new pthread */ replicatespuFlushAll( &(replicate_spu.thread[0]) ); crNetNewClient( replicate_spu.thread[0].server.conn, &(thread->server)); CRASSERT(thread->server.conn); } /* packer setup */ CRASSERT(thread->packer == NULL); thread->packer = crPackNewContext( replicate_spu.swap ); CRASSERT(thread->packer); crPackInitBuffer( &(thread->buffer), crNetAlloc(thread->server.conn), thread->server.conn->buffer_size, thread->server.conn->mtu ); thread->buffer.canBarf = thread->server.conn->Barf ? GL_TRUE : GL_FALSE; crPackSetBuffer( thread->packer, &thread->buffer ); crPackFlushFunc( thread->packer, replicatespuFlush ); crPackFlushArg( thread->packer, (void *) thread ); crPackSendHugeFunc( thread->packer, replicatespuHuge ); crPackSetContext( thread->packer ); #ifdef CHROMIUM_THREADSAFE_notyet crSetTSD(&_ReplicateTSD, thread); #endif replicate_spu.numThreads++; #ifdef CHROMIUM_THREADSAFE_notyet crUnlockMutex(&_ReplicateMutex); #endif return thread; }
/* * This is called from either the Pack SPU and the packer library whenever * we need to send a data buffer to the server. */ void packspuFlush(void *arg ) { ThreadInfo *thread = (ThreadInfo *) arg; ContextInfo *ctx; unsigned int len; CRMessageOpcodes *hdr; CRPackBuffer *buf; /* we should _always_ pass a valid <arg> value */ CRASSERT(thread); ctx = thread->currentContext; buf = &(thread->buffer); CRASSERT(buf); /* We're done packing into the current buffer, unbind it */ crPackReleaseBuffer( thread->packer ); /* printf("%s thread=%p thread->id = %d thread->pc=%p t2->id=%d t2->pc=%p packbuf=%p packbuf=%p\n", __FUNCTION__, (void*) thread, (int) thread->id, thread->packer, (int) t2->id, t2->packer, buf->pack, thread->packer->buffer.pack); */ if ( buf->opcode_current == buf->opcode_start ) { /* printf("%s early return\n", __FUNCTION__); */ /* XXX these calls seem to help, but might be appropriate */ crPackSetBuffer( thread->packer, buf ); crPackResetPointers(thread->packer); return; } hdr = __prependHeader( buf, &len, 0 ); CRASSERT( thread->netServer.conn ); if ( buf->holds_BeginEnd ) crNetBarf( thread->netServer.conn, &(buf->pack), hdr, len ); else crNetSend( thread->netServer.conn, &(buf->pack), hdr, len ); buf->pack = crNetAlloc( thread->netServer.conn ); /* The network may have found a new mtu */ buf->mtu = thread->netServer.conn->mtu; crPackSetBuffer( thread->packer, buf ); crPackResetPointers(thread->packer); (void) arg; }
void PACKSPU_APIENTRY packspu_VBoxPackSetInjectThread(void) { crLockMutex(&_PackMutex); { int i; GET_THREAD(thread); CRASSERT(!thread); CRASSERT((pack_spu.numThreads>0) && (pack_spu.numThreads<MAX_THREADS)); for (i=0; i<MAX_THREADS; ++i) { if (!pack_spu.thread[i].inUse) { thread = &pack_spu.thread[i]; break; } } CRASSERT(thread); thread->inUse = GL_TRUE; thread->id = crThreadID(); thread->currentContext = NULL; thread->bInjectThread = GL_TRUE; thread->netServer.name = crStrdup(pack_spu.name); thread->netServer.buffer_size = 64 * 1024; crNetNewClient(pack_spu.thread[pack_spu.idxThreadInUse].netServer.conn, &(thread->netServer)); CRASSERT(thread->netServer.conn); CRASSERT(thread->packer == NULL); thread->packer = crPackNewContext( pack_spu.swap ); CRASSERT(thread->packer); crPackInitBuffer(&(thread->buffer), crNetAlloc(thread->netServer.conn), thread->netServer.conn->buffer_size, thread->netServer.conn->mtu); thread->buffer.canBarf = thread->netServer.conn->Barf ? GL_TRUE : GL_FALSE; crPackSetBuffer( thread->packer, &thread->buffer ); crPackFlushFunc( thread->packer, packspuFlush ); crPackFlushArg( thread->packer, (void *) thread ); crPackSendHugeFunc( thread->packer, packspuHuge ); crPackSetContext( thread->packer ); crSetTSD(&_PackTSD, thread); pack_spu.numThreads++; } crUnlockMutex(&_PackMutex); }
/* * Send a Chromium opcode buffer across the given net connection. * size - size of opcode buffer in bytes. * exitFlag - if true, send 'exit' code to server. */ static void SendOpcodes(CRConnection *conn, int size, int exitFlag) { char *buffer, *firstCode; CRMessageOpcodes *msg; if (size <= MTU) { buffer = crNetAlloc(conn); msg = (CRMessageOpcodes *) buffer; msg->header.type = CR_MESSAGE_OPCODES; msg->numOpcodes = size; firstCode = buffer + sizeof(CRMessageOpcodes); if (exitFlag) *firstCode = 42; else *firstCode = 99; crNetSend(conn, NULL, buffer, MTU); crNetFree(conn, buffer); } else { /* send "huge" buffer */ const int totalSize = size + 8 + sizeof(CRMessageOpcodes); unsigned int *uiptr; buffer = crAlloc(totalSize); msg = (CRMessageOpcodes *) buffer; msg->header.type = CR_MESSAGE_OPCODES; msg->numOpcodes = 1; uiptr = (unsigned int *) (buffer + sizeof(CRMessageOpcodes) + 4); uiptr[0] = size; firstCode = buffer + sizeof(CRMessageOpcodes); if (exitFlag) *firstCode = 42; else *firstCode = 99; crNetSend(conn, NULL, buffer, size); crFree(buffer); } }
void REPLICATESPU_APIENTRY replicatespu_Begin( GLenum mode ) { GET_THREAD(thread); CRPackBuffer *buf = &thread->BeginEndBuffer; CRASSERT( mode >= GL_POINTS && mode <= GL_POLYGON ); if (thread->currentContext->displayListMode != GL_FALSE) { crDLMCompileBegin(mode); } if (replicate_spu.swap) { crPackBeginSWAP( mode ); } else { crPackBegin( mode ); } if ( thread->server.conn->Barf ) { thread->BeginEndMode = mode; thread->BeginEndState = -1; if ( mode == GL_LINES || mode == GL_TRIANGLES || mode == GL_QUADS || mode == GL_POLYGON ) { CRASSERT(!buf->pack); crPackReleaseBuffer( thread->packer ); buf->pack = crNetAlloc( thread->server.conn ); crPackInitBuffer( buf, buf->pack, thread->server.conn->buffer_size, thread->server.conn->mtu ); buf->holds_BeginEnd = 1; buf->in_BeginEnd = 1; crPackSetBuffer( thread->packer, buf ); thread->BeginEndState = 0; } } }
GLint PACKSPU_APIENTRY packspu_VBoxPackSetInjectThread(struct VBOXUHGSMI *pHgsmi) { GLint con = 0; int i; GET_THREAD(thread); CRASSERT(!thread); crLockMutex(&_PackMutex); { CRASSERT(CRPACKSPU_IS_WDDM_CRHGSMI() || (pack_spu.numThreads>0)); CRASSERT(pack_spu.numThreads<MAX_THREADS); for (i=0; i<MAX_THREADS; ++i) { if (!pack_spu.thread[i].inUse) { thread = &pack_spu.thread[i]; break; } } CRASSERT(thread); thread->inUse = GL_TRUE; if (!CRPACKSPU_IS_WDDM_CRHGSMI()) thread->id = crThreadID(); else thread->id = THREAD_OFFSET_MAGIC + i; thread->currentContext = NULL; thread->bInjectThread = GL_TRUE; thread->netServer.name = crStrdup(pack_spu.name); thread->netServer.buffer_size = 64 * 1024; packspuConnectToServer(&(thread->netServer) #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST) , pHgsmi #endif ); CRASSERT(thread->netServer.conn); CRASSERT(thread->packer == NULL); thread->packer = crPackNewContext( pack_spu.swap ); CRASSERT(thread->packer); crPackInitBuffer(&(thread->buffer), crNetAlloc(thread->netServer.conn), thread->netServer.conn->buffer_size, thread->netServer.conn->mtu); thread->buffer.canBarf = thread->netServer.conn->Barf ? GL_TRUE : GL_FALSE; crPackSetBuffer( thread->packer, &thread->buffer ); crPackFlushFunc( thread->packer, packspuFlush ); crPackFlushArg( thread->packer, (void *) thread ); crPackSendHugeFunc( thread->packer, packspuHuge ); crPackSetContext( thread->packer ); crSetTSD(&_PackTSD, thread); pack_spu.numThreads++; } crUnlockMutex(&_PackMutex); if (CRPACKSPU_IS_WDDM_CRHGSMI()) { CRASSERT(thread->id - THREAD_OFFSET_MAGIC < RT_ELEMENTS(pack_spu.thread) && GET_THREAD_VAL_ID(thread->id) == thread); con = thread->id; } return con; }
/* * Allocate a new ThreadInfo structure, setup a connection to the * server, allocate/init a packer context, bind this ThreadInfo to * the calling thread with crSetTSD(). * We'll always call this function at least once even if we're not * using threads. */ ThreadInfo *packspuNewThread( unsigned long id ) { ThreadInfo *thread=NULL; int i; #ifdef CHROMIUM_THREADSAFE crLockMutex(&_PackMutex); #else CRASSERT(pack_spu.numThreads == 0); #endif CRASSERT(pack_spu.numThreads < MAX_THREADS); for (i=0; i<MAX_THREADS; ++i) { if (!pack_spu.thread[i].inUse) { thread = &pack_spu.thread[i]; break; } } CRASSERT(thread); thread->inUse = GL_TRUE; thread->id = id; thread->currentContext = NULL; thread->bInjectThread = GL_FALSE; /* connect to the server */ thread->netServer.name = crStrdup( pack_spu.name ); thread->netServer.buffer_size = pack_spu.buffer_size; if (pack_spu.numThreads == 0) { packspuConnectToServer( &(thread->netServer) ); if (!thread->netServer.conn) { return NULL; } pack_spu.swap = thread->netServer.conn->swap; } else { /* a new pthread */ crNetNewClient(pack_spu.thread[pack_spu.idxThreadInUse].netServer.conn, &(thread->netServer)); CRASSERT(thread->netServer.conn); } /* packer setup */ CRASSERT(thread->packer == NULL); thread->packer = crPackNewContext( pack_spu.swap ); CRASSERT(thread->packer); crPackInitBuffer( &(thread->buffer), crNetAlloc(thread->netServer.conn), thread->netServer.conn->buffer_size, thread->netServer.conn->mtu ); thread->buffer.canBarf = thread->netServer.conn->Barf ? GL_TRUE : GL_FALSE; crPackSetBuffer( thread->packer, &thread->buffer ); crPackFlushFunc( thread->packer, packspuFlush ); crPackFlushArg( thread->packer, (void *) thread ); crPackSendHugeFunc( thread->packer, packspuHuge ); crPackSetContext( thread->packer ); #ifdef CHROMIUM_THREADSAFE crSetTSD(&_PackTSD, thread); #endif pack_spu.numThreads++; #ifdef CHROMIUM_THREADSAFE crUnlockMutex(&_PackMutex); #endif return thread; }
/* * This is called from either the Pack SPU and the packer library whenever * we need to send a data buffer to the server. */ void packspuFlush(void *arg ) { ThreadInfo *thread = (ThreadInfo *) arg; ContextInfo *ctx; unsigned int len; CRMessageOpcodes *hdr; CRPackBuffer *buf; /* we should _always_ pass a valid <arg> value */ CRASSERT(thread && thread->inUse); #ifdef CHROMIUM_THREADSAFE CR_LOCK_PACKER_CONTEXT(thread->packer); #endif ctx = thread->currentContext; buf = &(thread->buffer); CRASSERT(buf); if (ctx && ctx->fCheckZerroVertAttr) crStateCurrentRecoverNew(ctx->clientState, &thread->packer->current); /* We're done packing into the current buffer, unbind it */ crPackReleaseBuffer( thread->packer ); /* printf("%s thread=%p thread->id = %d thread->pc=%p t2->id=%d t2->pc=%p packbuf=%p packbuf=%p\n", __FUNCTION__, (void*) thread, (int) thread->id, thread->packer, (int) t2->id, t2->packer, buf->pack, thread->packer->buffer.pack); */ if ( buf->opcode_current == buf->opcode_start ) { /* printf("%s early return\n", __FUNCTION__); */ /* XXX these calls seem to help, but might be appropriate */ crPackSetBuffer( thread->packer, buf ); crPackResetPointers(thread->packer); #ifdef CHROMIUM_THREADSAFE CR_UNLOCK_PACKER_CONTEXT(thread->packer); #endif return; } hdr = __prependHeader( buf, &len, 0 ); CRASSERT( thread->netServer.conn ); if ( buf->holds_BeginEnd ) { /*crDebug("crNetBarf %d, (%d)", len, buf->size);*/ crNetBarf( thread->netServer.conn, &(buf->pack), hdr, len ); } else { /*crDebug("crNetSend %d, (%d)", len, buf->size);*/ crNetSend( thread->netServer.conn, &(buf->pack), hdr, len ); } buf->pack = crNetAlloc( thread->netServer.conn ); /* The network may have found a new mtu */ buf->mtu = thread->netServer.conn->mtu; crPackSetBuffer( thread->packer, buf ); crPackResetPointers(thread->packer); #ifdef CHROMIUM_THREADSAFE CR_UNLOCK_PACKER_CONTEXT(thread->packer); #endif }