c-pal/c_pal_ll/interfaces/devdoc/threadapi_and_sleep_require...

3.2 KiB

threadapi and sleep

Overview

This document defines the behavior of the sleep and threadapi adapters.

The sleep and threadapi adapters are related because they share a header file and they both deal with thread functionality, but they are described as distinct adapters in these documents because the sleep adapter is mandatory for correct SDK operation, while the threadapi adapter is optional.

Exposed API

The threadapi adapter uses these types specified in threadapi.h:

typedef void* THREAD_HANDLE;

typedef enum
{
    THREADAPI_OK,
    THREADAPI_INVALID_ARG,
    THREADAPI_NO_MEMORY,
    THREADAPI_ERROR,
} THREADAPI_RESULT;

sleep Adapter

ThreadAPI_Sleep

The sleep adapter is a single exposed function, ThreadAPI_Sleep. This function is required for correct SDK operation.

void ThreadAPI_Sleep(unsigned int milliseconds);

NON_THREADAPI_30_001: [ ThreadAPI_Sleep shall suspend the thread for at least the supplied value of milliseconds. ]

threadapi Adapter

ThreadAPI_Create

Creates a thread with an entry point specified by the func argument. The concrete type of the void* THREAD_HANDLE is at the discretion of the developer.

THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg);

NON_THREADAPI_30_010: [ If the threadapi adapter is not implemented, ThreadAPI_Create shall return THREADAPI_ERROR. ]

NON_THREADAPI_30_011: [ If threadHandle is NULL ThreadAPI_Create shall return THREADAPI_INVALID_ARG. ]

NON_THREADAPI_30_012: [ If func is NULL ThreadAPI_Create shall return THREADAPI_INVALID_ARG. ]

NON_THREADAPI_30_013: [ If ThreadAPI_Create is unable to create a thread it shall return THREADAPI_ERROR or THREADAPI_NO_MEMORY, whichever seems more appropriate. ]

NON_THREADAPI_30_014: [ On success, ThreadAPI_Create shall return the created thread object in threadHandle. ]

NON_THREADAPI_30_015: [ On success, ThreadAPI_Create shall return THREADAPI_OK. ]

ThreadAPI_Join

Waits for the thread identified by the threadHandle argument to complete. When the threadHandle thread completes, all resources associated with the thread must be released and the thread handle will no longer be valid.

THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE threadHandle, int* result);

NON_THREADAPI_30_020: [ If the threadapi adapter is not implemented, ThreadAPI_Join shall return THREADAPI_ERROR. ]

NON_THREADAPI_30_021: [ If threadHandle is NULL ThreadAPI_Join shall return THREADAPI_INVALID_ARG. ]

NON_THREADAPI_30_022: [ If ThreadAPI_Join fails it shall return THREADAPI_ERROR. ]

NON_THREADAPI_30_023: [ On success, ThreadAPI_Join shall wait for the thread identified by the threadHandle argument to complete. ]

NON_THREADAPI_30_024: [ On success, all resources associated with the thread shall be released. ]

NON_THREADAPI_30_025: [ On success, if result is non-NULL then the result of the THREAD_START_FUNC shall be returned in result. ]

NON_THREADAPI_30_026: [ On success, ThreadAPI_Join shall return THREADAPI_OK. ]