int main(int argc, char* argv[]) { NaClHandle handle; UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); /* Check not executable shared memory. */ handle = NaClCreateMemoryObject(MEMORY_SIZE, 0); if (NACL_INVALID_HANDLE == handle) { failWithErrno("NaClCreateMemoryObject(size, 0)"); } if (0 != NaClClose(handle)) { failWithErrno("NaClClose(0)"); } /* Check executable shared memory. */ handle = NaClCreateMemoryObject(MEMORY_SIZE, 1); #ifdef __native_client__ if (NACL_INVALID_HANDLE != handle) { fprintf(stderr, "NaClCreateMemoryObject(size, 1) returned a valid shm. " "It must return -1\n"); exit(EXIT_FAILURE); } #else if (NACL_INVALID_HANDLE == handle) { failWithErrno("NaClCreateMemoryObject(size, 1)"); } if (0 != NaClClose(handle)) { failWithErrno("NaClClose(1)"); } #endif return 0; }
bool TicketHolder::waitForTicketUntil(OperationContext* opCtx, Date_t until) { const Milliseconds intervalMs(500); struct timespec ts; // To support interrupting ticket acquisition while still benefiting from semaphores, we do a // timed wait on an interval to periodically check for interrupts. // The wait period interval is the smaller of the default interval and the provided // deadline. Date_t deadline = std::min(until, Date_t::now() + intervalMs); tsFromDate(deadline, ts); while (0 != sem_timedwait(&_sem, &ts)) { if (errno == ETIMEDOUT) { // If we reached the deadline without being interrupted, we have completely timed out. if (deadline == until) return false; deadline = std::min(until, Date_t::now() + intervalMs); tsFromDate(deadline, ts); } else if (errno != EINTR) { failWithErrno(errno); } // To correctly handle errors from sem_timedwait, we should check for interrupts last. // It is possible to unset 'errno' after a call to checkForInterrupt(). if (opCtx) opCtx->checkForInterrupt(); } return true; }
bool TicketHolder::tryAcquire() { while (0 != sem_trywait(&_sem)) { if (errno == EAGAIN) return false; if (errno != EINTR) failWithErrno(errno); } return true; }
int main(int argc, char* argv[]) { int num = TEST_NUM; struct NaClThread thr; UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); if (!NaClThreadCreateJoinable(&thr, myThread, &num, 128*1024)) { failWithErrno("NaClThreadCreateJoinable"); } /* This test check that the thread actually starts. It checks that this does * not hang. */ NaClThreadJoin(&thr); return 0; }
void WINAPI serviceThread(void* arg) { struct ServiceThreadArgs* typedArg; NaClSrpcImcDescType desc; NaClSrpcHandlerDesc handlers[] = { { "getNum::i", handleGetNum }, { NULL, NULL } }; typedArg = (struct ServiceThreadArgs*) arg; desc = typedArg->desc; free(typedArg); if (!NaClSrpcServerLoop(desc, handlers, 0)) { failWithErrno("NaClSrpcServerLoop"); exit(EXIT_FAILURE); } #ifdef __native_client__ close(desc); #else NaClDescUnref(desc); #endif NaClThreadExit(0); }
int main(int argc, char* argv[]) { int result; NaClHandle pair[2]; #ifdef __native_client__ int imc_desc[2]; #else struct NaClDescImcDesc* imc_desc[2]; #endif struct NaClThread thr; NaClSrpcChannel channel; NaClSrpcError error; struct ServiceThreadArgs* threadArg; UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); NaClSrpcModuleInit(); if (0 != NaClSocketPair(pair)) { failWithErrno("SocketPair"); } #ifdef __native_client__ imc_desc[0] = pair[0]; imc_desc[1] = pair[1]; #else NaClNrdAllModulesInit(); imc_desc[0] = (struct NaClDescImcDesc*) calloc(1, sizeof(*imc_desc[0])); if (0 == imc_desc[0]) { failWithErrno("calloc"); } imc_desc[1] = (struct NaClDescImcDesc*) calloc(1, sizeof(*imc_desc[1])); if (0 == imc_desc[1]) { failWithErrno("calloc"); } if (!NaClDescImcDescCtor(imc_desc[0], pair[0])) { failWithErrno("NaClDescImcDescCtor"); } if (!NaClDescImcDescCtor(imc_desc[1], pair[1])) { failWithErrno("NaClDescImcDescCtor"); } #endif threadArg = (struct ServiceThreadArgs*) calloc(1, sizeof(*threadArg)); if (NULL == threadArg) { failWithErrno("calloc for threadArg"); } threadArg->desc = (NaClSrpcImcDescType)imc_desc[0]; if (!NaClThreadCreateJoinable(&thr, serviceThread, threadArg, 128*1024)) { failWithErrno("NaClThreadCtor"); } if (!NaClSrpcClientCtor(&channel, (NaClSrpcImcDescType) imc_desc[1])) { failWithErrno("NaClSrpcClientCtor"); } error = NaClSrpcInvokeBySignature(&channel, "getNum::i", &result); if (NACL_SRPC_RESULT_OK != error) { fprintf(stderr, "SRPC call to getNum::i failed: %s\n", NaClSrpcErrorString(error)); exit(EXIT_FAILURE); } if (TEST_NUM != result) { fprintf(stderr, "expected: %d, got: %d\n", TEST_NUM, result); exit(EXIT_FAILURE); } NaClSrpcDtor(&channel); #ifdef __native_client__ close(imc_desc[1]); #else NaClDescUnref((NaClSrpcImcDescType)imc_desc[1]); #endif NaClThreadJoin(&thr); NaClSrpcModuleFini(); return 0; }
int main(int argc, char* argv[]) { nacl_abi_size_t char_array_count; char* char_array_carr; NaClHandle pair[2]; #ifdef __native_client__ int imc_desc[2]; #else struct NaClDescImcDesc* imc_desc[2]; #endif struct NaClThread thr; NaClSrpcChannel channel; NaClSrpcError error; struct ServiceThreadArgs* threadArg; UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); NaClSrpcModuleInit(); if (0 != NaClSocketPair(pair)) { failWithErrno("SocketPair"); } #ifdef __native_client__ imc_desc[0] = pair[0]; imc_desc[1] = pair[1]; #else NaClNrdAllModulesInit(); imc_desc[0] = (struct NaClDescImcDesc*) calloc(1, sizeof(*imc_desc[0])); if (0 == imc_desc[0]) { failWithErrno("calloc"); } imc_desc[1] = (struct NaClDescImcDesc*) calloc(1, sizeof(*imc_desc[1])); if (0 == imc_desc[1]) { failWithErrno("calloc"); } if (!NaClDescImcDescCtor(imc_desc[0], pair[0])) { failWithErrno("NaClDescImcDescCtor"); } if (!NaClDescImcDescCtor(imc_desc[1], pair[1])) { failWithErrno("NaClDescImcDescCtor"); } #endif threadArg = (struct ServiceThreadArgs*) calloc(1, sizeof(*threadArg)); if (NULL == threadArg) { failWithErrno("calloc for threadArg"); } threadArg->desc = (NaClSrpcImcDescType)imc_desc[0]; if (!NaClThreadCreateJoinable(&thr, serviceThread, threadArg, 128*1024)) { failWithErrno("NaClThreadCtor"); } if (!NaClSrpcClientCtor(&channel, (NaClSrpcImcDescType) imc_desc[1])) { failWithErrno("NaClSrpcClientCtor"); } char_array_count = TEST_ARRAY_LENGTH; char_array_carr = (char*) calloc(TEST_ARRAY_LENGTH, sizeof(char)); if (!char_array_carr) { failWithErrno("calloc for char_array"); } memset(char_array_carr, TEST_NUM, TEST_ARRAY_LENGTH); error = NaClSrpcInvokeBySignature(&channel, "putArr:C:", char_array_count, char_array_carr); if (NACL_SRPC_RESULT_OK != error) { fprintf(stderr, "SRPC call to putArr:C: failed: %s\n", NaClSrpcErrorString(error)); exit(EXIT_FAILURE); } free(char_array_carr); NaClSrpcDtor(&channel); #ifdef __native_client__ close(imc_desc[1]); #else NaClDescUnref((NaClSrpcImcDescType)imc_desc[1]); #endif NaClThreadJoin(&thr); NaClSrpcModuleFini(); return 0; }