static jint receiveBytes(SharedMemoryConnection *connection, void *bytes, jint length) { Stream *stream = &connection->incoming; SharedStream *shared = stream->shared; jint fragmentStart; jint fragmentLength; jint index = 0; jint maxLength; CHECK_ERROR(enterMutex(stream, connection->shutdown)); while (index < length) { CHECK_ERROR(waitForData(connection, stream)); SHMEM_ASSERT(!EMPTY(stream)); fragmentStart = shared->readOffset; if (fragmentStart < shared->writeOffset) { maxLength = shared->writeOffset - fragmentStart; } else { maxLength = SHARED_BUFFER_SIZE - fragmentStart; } fragmentLength = MIN(maxLength, length - index); memcpy((jbyte *)bytes + index, shared->buffer + fragmentStart, fragmentLength); shared->readOffset = ADD_OFFSET(fragmentStart, fragmentLength); index += fragmentLength; shared->isFull = JNI_FALSE; STREAM_INVARIANT(stream); CHECK_ERROR(signalSpace(stream)); } CHECK_ERROR(leaveMutex(stream)); return SYS_OK; }
jint shmemBase_sendByte(SharedMemoryConnection *connection, jbyte data) { Stream *stream = &connection->outgoing; SharedStream *shared = stream->shared; int offset; CHECK_ERROR(enterMutex(stream, connection->shutdown)); CHECK_ERROR(waitForSpace(connection, stream)); SHMEM_ASSERT(!FULL(stream)); offset = shared->writeOffset; shared->buffer[offset] = data; shared->writeOffset = ADD_OFFSET(offset, 1); shared->isFull = (shared->readOffset == shared->writeOffset); STREAM_INVARIANT(stream); CHECK_ERROR(leaveMutex(stream)); CHECK_ERROR(signalData(stream)); return SYS_OK; }
jint shmemBase_receiveByte(SharedMemoryConnection *connection, jbyte *data) { Stream *stream = &connection->incoming; SharedStream *shared = stream->shared; int offset; CHECK_ERROR(enterMutex(stream, connection->shutdown)); CHECK_ERROR(waitForData(connection, stream)); SHMEM_ASSERT(!EMPTY(stream)); offset = shared->readOffset; *data = shared->buffer[offset]; shared->readOffset = ADD_OFFSET(offset, 1); shared->isFull = JNI_FALSE; STREAM_INVARIANT(stream); CHECK_ERROR(leaveMutex(stream)); CHECK_ERROR(signalSpace(stream)); return SYS_OK; }
static jint sendBytes(SharedMemoryConnection *connection, const void *bytes, jint length) { Stream *stream = &connection->outgoing; SharedStream *shared = stream->shared; jint fragmentStart; jint fragmentLength; jint index = 0; jint maxLength; clearLastError(); CHECK_ERROR(enterMutex(stream, connection->shutdown)); while (index < length) { CHECK_ERROR(waitForSpace(connection, stream)); SHMEM_ASSERT(!FULL(stream)); fragmentStart = shared->writeOffset; if (fragmentStart < shared->readOffset) { maxLength = shared->readOffset - fragmentStart; } else { maxLength = SHARED_BUFFER_SIZE - fragmentStart; } fragmentLength = MIN(maxLength, length - index); memcpy(shared->buffer + fragmentStart, (jbyte *)bytes + index, fragmentLength); shared->writeOffset = ADD_OFFSET(fragmentStart, fragmentLength); index += fragmentLength; shared->isFull = (shared->readOffset == shared->writeOffset); STREAM_INVARIANT(stream); CHECK_ERROR(signalData(stream)); } CHECK_ERROR(leaveMutex(stream)); return SYS_OK; }