xref: /minix3/minix/lib/libsys/sched_start.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1 #include "syslib.h"
2 #include <assert.h>
3 #include <machine/archtypes.h>
4 #include <minix/timers.h>
5 #include <string.h>
6 
7 #include "kernel/config.h"
8 #include "kernel/const.h"
9 #include "kernel/type.h"
10 #include "kernel/proc.h"
11 
12 /*===========================================================================*
13  *				sched_inherit				     *
14  *===========================================================================*/
15 int sched_inherit(endpoint_t scheduler_e,
16 	endpoint_t schedulee_e, endpoint_t parent_e, unsigned maxprio,
17 	endpoint_t *newscheduler_e)
18 {
19 	int rv;
20 	message m;
21 
22 	assert(_ENDPOINT_P(scheduler_e) >= 0);
23 	assert(_ENDPOINT_P(schedulee_e) >= 0);
24 	assert(_ENDPOINT_P(parent_e) >= 0);
25 	assert(maxprio < NR_SCHED_QUEUES);
26 	assert(newscheduler_e);
27 
28 	memset(&m, 0, sizeof(m));
29 	m.m_lsys_sched_scheduling_start.endpoint	= schedulee_e;
30 	m.m_lsys_sched_scheduling_start.parent		= parent_e;
31 	m.m_lsys_sched_scheduling_start.maxprio		= maxprio;
32 
33 	/* Send the request to the scheduler */
34 	if ((rv = _taskcall(scheduler_e, SCHEDULING_INHERIT, &m))) {
35 		return rv;
36 	}
37 
38 	/* Store the process' scheduler. Note that this might not be the
39 	 * scheduler we sent the SCHEDULING_INHERIT message to. That scheduler
40 	 * might have forwarded the scheduling message on to another scheduler
41 	 * before returning the message.
42 	 */
43 	*newscheduler_e = m.m_sched_lsys_scheduling_start.scheduler;
44 	return (OK);
45 }
46 
47 /*===========================================================================*
48  *				sched_start				     *
49  *===========================================================================*/
50 int sched_start(endpoint_t scheduler_e,
51 			endpoint_t schedulee_e,
52 			endpoint_t parent_e,
53 			int maxprio,
54 			int quantum,
55 			int cpu,
56 			endpoint_t *newscheduler_e)
57 {
58 	int rv;
59 	message m;
60 
61 	/* No scheduler given? We are done. */
62 	if(scheduler_e == NONE) {
63 		return OK;
64 	}
65 
66 	assert(_ENDPOINT_P(schedulee_e) >= 0);
67 	assert(_ENDPOINT_P(parent_e) >= 0);
68 	assert(maxprio >= 0);
69 	assert(maxprio < NR_SCHED_QUEUES);
70 	assert(quantum > 0);
71 	assert(newscheduler_e);
72 
73 	/* The KERNEL must schedule this process. */
74 	if(scheduler_e == KERNEL) {
75 		if ((rv = sys_schedctl(SCHEDCTL_FLAG_KERNEL,
76 			schedulee_e, maxprio, quantum, cpu)) != OK) {
77 			return rv;
78 		}
79 		*newscheduler_e = scheduler_e;
80 		return OK;
81 	}
82 
83 	/* A user-space scheduler must schedule this process. */
84 	memset(&m, 0, sizeof(m));
85 	m.m_lsys_sched_scheduling_start.endpoint	= schedulee_e;
86 	m.m_lsys_sched_scheduling_start.parent		= parent_e;
87 	m.m_lsys_sched_scheduling_start.maxprio		= maxprio;
88 	m.m_lsys_sched_scheduling_start.quantum		= quantum;
89 
90 	/* Send the request to the scheduler */
91 	if ((rv = _taskcall(scheduler_e, SCHEDULING_START, &m))) {
92 		return rv;
93 	}
94 
95 	/* Store the process' scheduler. Note that this might not be the
96 	 * scheduler we sent the SCHEDULING_START message to. That scheduler
97 	 * might have forwarded the scheduling message on to another scheduler
98 	 * before returning the message.
99 	 */
100 	*newscheduler_e = m.m_sched_lsys_scheduling_start.scheduler;
101 	return (OK);
102 }
103