Robot Control Library
pthread.h
Go to the documentation of this file.
1 /**
2  * <rc/pthread.h>
3  *
4  * @brief manage pthreads and process niceness
5  *
6  * @author James Strawson
7  * @date 1/19/2018
8  *
9  * @addtogroup pthread
10  * @{
11  */
12 
13 
14 #ifndef RC_PTHREAD_H
15 #define RC_PTHREAD_H
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <pthread.h>
22 
23 /**
24  * @brief starts a pthread with specified policy and priority
25  *
26  * Note that using anything other than SCHED_OTHER with priority 0 is only
27  * available to root users or after giving special permission to the executable
28  * with the setcap command line utility.
29  *
30  * If a policy is selected which requires privaldges the user does not have,
31  * then a friendly warning will be displayed and the thread will still be set
32  * with the priority and scheduler inherited from the calling process.
33  *
34  * @param thread pointer to user's pthread_t handle
35  * @param func function pointer for thread to start
36  * @param arg argument to pass to thread function when it starts
37  * @param[in] policy SCHED_FIFO SCHED_RR or SCHED_OTHER
38  * @param[in] priority between 1-99 for FIFO and RR, defualt 0 for SCHED_OTHER
39  *
40  * @return 0 on success or -1 on error
41  */
42 int rc_pthread_create(pthread_t *thread, void*(*func)(void*), void *arg, int policy, int priority);
43 
44 
45 /**
46  * @brief Joins a thread with timeout given in seconds.
47  *
48  * If no timeout is necessary, just use the standard system pthread_join
49  * function.
50  *
51  * @param[in] thread pthread_t handle
52  * @param retval place to put the exit status of target thread, see
53  * pthread_join
54  * @param[in] timeout_sec floating point timeout in seconds
55  *
56  * @return Returns 0 if the thread joined within the timeout period, 1 if
57  * the thread timed out and was forced to close, -1 on error.
58  */
59 int rc_pthread_timed_join(pthread_t thread, void** retval, float timeout_sec);
60 
61 
62 /**
63  * @brief Prints human-readable scheduler and priority for a pthread_t
64  * handle
65  *
66  * @param[in] thread pthread_t handle
67  *
68  * @return 0 on success or -1 on failure
69  */
70 int rc_pthread_print_properties(pthread_t thread);
71 
72 
73 /**
74  * @brief Lets a thread change it's own scheduler policy and priority while
75  * running
76  *
77  * @param[in] policy SCHED_FIFO SCHED_RR or SCHED_OTHER
78  * @param[in] priority between 1-99 for FIFO and RR, defualt 0 for SCHED_OTHER
79  *
80  * @return 0 on success or -1 on failure
81  */
82 int rc_pthread_set_properties_self(int policy, int priority);
83 
84 
85 /**
86  * @brief fetches the niceness value of the executing process
87  *
88  * This is just a helpful wrapper for the system getpriority function and
89  * returns the same value.
90  *
91  * @return niceness (-20 to 20) or -1 on failure and sets errno
92  */
94 
95 
96 /**
97  * @brief Lets a process change its own niceness value
98  *
99  * Note that this requires special privaledges and will print an error if run by
100  * a normal user. This is just a helpful wrapper for the system setpriority
101  * funtion and returns the same thing.
102  *
103  * @param[in] niceness new niceness (-20 to 20)
104  *
105  * @return 0 on success, -1 on failure
106  */
107 int rc_pthread_set_process_niceness(int niceness);
108 
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 #endif // RC_PTHREAD_HELPERS_H
115 
116 ///@} end group pthread_helpers
int rc_pthread_set_properties_self(int policy, int priority)
Lets a thread change it&#39;s own scheduler policy and priority while running.
int rc_pthread_create(pthread_t *thread, void *(*func)(void *), void *arg, int policy, int priority)
starts a pthread with specified policy and priority
int rc_pthread_set_process_niceness(int niceness)
Lets a process change its own niceness value.
int rc_pthread_get_process_niceness(void)
fetches the niceness value of the executing process
int rc_pthread_print_properties(pthread_t thread)
Prints human-readable scheduler and priority for a pthread_t handle.
int rc_pthread_timed_join(pthread_t thread, void **retval, float timeout_sec)
Joins a thread with timeout given in seconds.