Example #1
0
static int detach_thrd(void* arg) {
    BEGIN_HELPER;
    thrd_t* thrd = (thrd_t*) arg;
    EXPECT_EQ(thrd_detach(*thrd), 0, "");
    free(thrd);
    END_HELPER;
}
Example #2
0
main()
{
    thrd_t thrd1;
    printf("%d\n", mtx_init(&mtx1, mtx_plain));
    thrd_create(&thrd1, thread1, 0);
    thrd_join(thrd1, NULL);
    mtx_destroy(&mtx1);

    printf("\n%d\n", mtx_init(&mtx1, mtx_plain | mtx_recursive));
    thrd_create(&thrd1, thread1, 0);
    thrd_join(thrd1, NULL);
    mtx_destroy(&mtx1);

    mtx_init(&mtx1, mtx_try);
    thrd_create(&thrd1, thread3, 0); 
    Sleep(500);
    printf("%d\n", mtx_trylock(&mtx1));
    xtime xt;
    xtime_get(&xt, TIME_UTC);
    xt.nsec += 200000000;
    printf("%d\n", mtx_timedlock(&mtx1, &xt));
    xt.nsec += 500000000;
    printf("%d\n", mtx_timedlock(&mtx1, &xt));
    mtx_unlock(&mtx1);
    printf("%d\n", mtx_trylock(&mtx1));
    mtx_unlock(&mtx1);
    
    thrd_create(&thrd1, thread4, 0);
    thrd_detach(thrd1);
    thrd_create(&thrd1, thread5, 0);
    thrd_join(thrd1, NULL);
        
    thrd_create(&thrd1, thread2, 0);
    thrd_detach(thrd1);
    thrd_create(&thrd1, thread2, (void *)1);
    thrd_detach(thrd1);
    Sleep(4000);

    thrd_create(&thrd1, thread2, (void *)2);
    thrd_detach(thrd1);
    thrd_create(&thrd1, thread2, (void *)3);
    thrd_detach(thrd1);
    Sleep(4000);
    
    mtx_destroy(&mtx1);

}
Example #3
0
zx_status_t Device::Bind() {
    zx_status_t status = DdkAdd("tpm", DEVICE_ADD_INVISIBLE);
    if (status != ZX_OK) {
        return status;
    }

    thrd_t thread;
    int ret = thrd_create_with_name(&thread, Init, this, "tpm:slow_bind");
    if (ret != thrd_success) {
        DdkRemove();
        return ZX_ERR_INTERNAL;
    }
    thrd_detach(thread);
    return ZX_OK;
}
Example #4
0
static int run_test_from_thread (void *arg) {
        struct run_args *run_args = arg;

	thrd_detach(thrd_current());

	run_test0(run_args);

        TEST_LOCK();
        tests_running_cnt--;
        TEST_UNLOCK();

        free(run_args);

        return 0;
}
Example #5
0
bool c11_thread_test(void) {
    BEGIN_TEST;

    thrd_t thread;
    int return_value = 99;

    unittest_printf("Welcome to thread test!\n");

    memset((void*)threads_done, 0, sizeof(threads_done));
    for (int i = 0; i != 4; ++i) {
        int return_value = 99;
        int ret = thrd_create_with_name(&thread, thread_entry, (void*)(intptr_t)i, "c11 thread test");
        ASSERT_EQ(ret, thrd_success, "Error while creating thread");

        ret = thrd_join(thread, &return_value);
        ASSERT_EQ(ret, thrd_success, "Error while thread join");
        ASSERT_EQ(return_value, i, "Incorrect return from thread");
    }

    unittest_printf("Attempting to create thread with a null name. This should succeed\n");
    int ret = thrd_create_with_name(&thread, thread_entry, (void*)(intptr_t)4, NULL);
    ASSERT_EQ(ret, thrd_success, "Error returned from thread creation");
    zx_handle_t handle = thrd_get_zx_handle(thread);
    ASSERT_NE(handle, ZX_HANDLE_INVALID, "got invalid thread handle");
    // Prove this is a valid handle by duplicating it.
    zx_handle_t dup_handle;
    zx_status_t status = zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, &dup_handle);
    ASSERT_EQ(status, 0, "failed to duplicate thread handle");

    ret = thrd_join(thread, &return_value);
    ASSERT_EQ(ret, thrd_success, "Error while thread join");
    ASSERT_EQ(zx_handle_close(dup_handle), ZX_OK, "failed to close duplicate handle");
    ASSERT_EQ(return_value, 4, "Incorrect return from thread");

    ret = thrd_create_with_name(&thread, thread_entry, (void*)(intptr_t)5, NULL);
    ASSERT_EQ(ret, thrd_success, "Error returned from thread creation");
    ret = thrd_detach(thread);
    ASSERT_EQ(ret, thrd_success, "Error while thread detach");

    while (!threads_done[5])
        zx_nanosleep(zx_deadline_after(ZX_MSEC(100)));

    thread_entry((void*)(intptr_t)6);
    ASSERT_TRUE(threads_done[6], "All threads should have completed");

    END_TEST;
}
Example #6
0
void skRequest_send(skRequest* request, int async)
{
    if (request->isRunning)
    {
        return;
    }
    
    request->state = SK_IN_PROGRESS;
    
    if (request->headerFieldList)
    {
        curl_easy_setopt(request->curl, CURLOPT_HTTPHEADER, request->headerFieldList);
    }
    
    curl_easy_setopt(request->curl, CURLOPT_CONNECTTIMEOUT_MS, SK_REQUEST_CONNECT_TIMEOUT_MS);
    
    curl_easy_setopt(request->curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(request->curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(request->curl, CURLOPT_WRITEHEADER, request);
    curl_easy_setopt(request->curl, CURLOPT_WRITEFUNCTION, responseBytesReceivedCallback);
    curl_easy_setopt(request->curl, CURLOPT_WRITEDATA, request);
    
    if (request->log)
    {
        curl_easy_setopt(request->curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(request->curl, CURLOPT_DEBUGFUNCTION, my_trace);
    }
    
    const char* requestBody = skMutableString_getString(&request->requestBody);
    
    if (strlen(requestBody))
    {
        curl_easy_setopt(request->curl, CURLOPT_POST, 1);
        curl_easy_setopt(request->curl, CURLOPT_POSTFIELDS, requestBody);
    }
    
    if (request->method == SK_HTTP_DELETE)
    {
        curl_easy_setopt(request->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
    }
    else if (request->method == SK_HTTP_PATCH)
    {
        curl_easy_setopt(request->curl, CURLOPT_CUSTOMREQUEST, "PATCH");
    }
    else if (request->method == SK_HTTP_PUT)
    {
        curl_easy_setopt(request->curl, CURLOPT_CUSTOMREQUEST, "PUT");
    }
    
    curl_easy_setopt(request->curl, CURLOPT_URL, skMutableString_getString(&request->url));
    
    skResponse_deinit(&request->response);
    http_parser_init(&request->httpParser, HTTP_RESPONSE);
    
    request->isRunning = 1;
    request->async = async;
    
    if (async)
    {
        const int res = thrd_create(&request->thread, requestThreadEntryPoint, request);
        if (res == thrd_success) {
            thrd_detach(request->thread);
        } else {
            // TODO: handle this case
        }
    }
    else
    {
        requestThreadEntryPoint(request);
        request->isRunning = 0;
        
        if (request->state == SK_SUCCEEDED)
        {
            if (request->responseCallback)
            {
                request->responseCallback(request, &request->response);
            }
        }
        else if (request->errorCode != SK_NO_ERROR)
        {
            if (request->errorCallback)
            {
                request->errorCallback(request, request->errorCode);
            }
        }
    }
}