Skip to content

Commit ca40fbf

Browse files
posix: sched: Implement set APIs for scheduling parameters
Implement `sched_setparam()` and `sched_setscheduler()` POSIX APIs as a part of PSE53 `_POSIX_PRIORITY_SCHEDULING` option group. signed-off-by: Gaetan Perrot <[email protected]>
1 parent 8bd3471 commit ca40fbf

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

include/zephyr/posix/sched.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ int sched_get_priority_max(int policy);
5151
int sched_getparam(pid_t pid, struct sched_param *param);
5252
int sched_getscheduler(pid_t pid);
5353

54+
int sched_setparam(pid_t pid, const struct sched_param *param);
55+
int sched_setscheduler(pid_t pid, int policy,const struct sched_param *param);
56+
5457
#ifdef __cplusplus
5558
}
5659
#endif

lib/posix/sched.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,67 @@ int sched_getscheduler(pid_t pid)
9696

9797
return (ret == 0) ? t_policy : -1;
9898
}
99+
100+
/**
101+
* @brief Set scheduling parameters
102+
*
103+
* See IEEE 1003.1
104+
*/
105+
int sched_setparam(pid_t pid, const struct sched_param *param)
106+
{
107+
struct sched_param dummy_param = {0};
108+
pthread_t tid = (pthread_t)pid;
109+
int t_policy = -1;
110+
int ret = -1;
111+
112+
if (param == NULL) {
113+
param = &dummy_param;
114+
}
115+
116+
if (tid == 0) {
117+
tid = pthread_self();
118+
}
119+
120+
if (param == NULL) {
121+
ret = -1;
122+
}else{
123+
ret = pthread_getschedparam(tid, &t_policy, &dummy_param);
124+
if(ret != -1){
125+
ret = pthread_setschedparam(tid, t_policy, param);
126+
}
127+
}
128+
129+
errno = ret;
130+
return ret;
131+
}
132+
133+
/**
134+
* @brief Set scheduling policy
135+
*
136+
* See IEEE 1003.1
137+
*/
138+
int sched_setscheduler(pid_t pid, int policy,const struct sched_param *param)
139+
{
140+
struct sched_param dummy_param = {0};
141+
pthread_t tid = (pthread_t)pid;
142+
int ret = -1;
143+
144+
if (param == NULL) {
145+
param = &dummy_param;
146+
}
147+
148+
if (tid == 0) {
149+
tid = pthread_self();
150+
}
151+
152+
if (param == NULL) {
153+
ret = -1;
154+
}else{
155+
if(ret != -1){
156+
ret = pthread_setschedparam(tid, policy, param);
157+
}
158+
}
159+
160+
errno = ret;
161+
return ret;
162+
}

0 commit comments

Comments
 (0)