forked from awslabs/aws-c-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.h
106 lines (88 loc) · 2.92 KB
/
thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef AWS_COMMON_THREAD_H_
#define AWS_COMMON_THREAD_H_
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <aws/common/common.h>
#include <stdint.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <pthread.h>
#endif
typedef enum aws_thread_detach_state {
AWS_THREAD_NOT_CREATED = 1,
AWS_THREAD_JOINABLE,
AWS_THREAD_JOIN_COMPLETED
} aws_thread_detach_state;
struct aws_thread_options {
size_t stack_size;
};
struct aws_thread {
struct aws_allocator *allocator;
aws_thread_detach_state detach_state;
#ifdef _WIN32
HANDLE thread_handle;
DWORD thread_id;
#else
pthread_t thread_id;
#endif
};
#ifdef __cplusplus
extern "C" {
#endif
/**
* Returns an instance of system default thread options.
*/
AWS_COMMON_API const struct aws_thread_options *aws_default_thread_options(void);
/**
* Initializes a new platform specific thread object struct (not the os-level thread itself).
*/
AWS_COMMON_API int aws_thread_init(struct aws_thread *thread, struct aws_allocator *allocator);
/**
* Creates an OS level thread and associates it with func. context will be passed to func when it is executed.
* options will be applied to the thread if they are applicable for the platform.
* You must either call join or detach after creating the thread and before calling clean_up.
*/
AWS_COMMON_API int aws_thread_launch(struct aws_thread *thread, void(*func)(void *arg),
void *arg, struct aws_thread_options *options);
/**
* Gets the id of thread
*/
AWS_COMMON_API uint64_t aws_thread_get_id(struct aws_thread *thread);
/**
* Gets the detach state of the thread. For example, is it safe to call join on this thread? Has it been detached()?
*/
AWS_COMMON_API aws_thread_detach_state aws_thread_get_detach_state(struct aws_thread *thread);
/**
* Joins the calling thread to a thread instance. Returns when thread is finished.
*/
AWS_COMMON_API int aws_thread_join(struct aws_thread *thread);
/**
* Cleans up the thread handle. Either detach or join must be called
* before calling this function.
*/
AWS_COMMON_API void aws_thread_clean_up(struct aws_thread *thread);
/**
* returns the thread id of the calling thread.
*/
AWS_COMMON_API uint64_t aws_thread_current_thread_id();
/**
* Sleeps the current thread by nanos.
*/
AWS_COMMON_API void aws_thread_current_sleep(uint64_t nanos);
#ifdef __cplusplus
}
#endif
#endif /* AWS_COMMON_THREAD_H_ */