btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / kernel / kscheduler.h
blobbfc6e0203d472dcb0f9db5bcb816ca42377539ec
1 /*
2 * Copyright 2013, Paweł Dziepak, pdziepak@quarnos.org.
3 * Copyright 2008-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Copyright 2005-2010, Axel Dörfler, axeld@pinc-software.de.
5 * Distributed under the terms of the MIT License.
6 */
7 #ifndef KERNEL_SCHEDULER_H
8 #define KERNEL_SCHEDULER_H
11 #include <cpu.h>
12 #include <int.h>
13 #include <smp.h>
14 #include <thread_types.h>
17 struct scheduling_analysis;
18 struct SchedulerListener;
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
25 /*! Enqueues the thread in the ready-to-run queue.
26 The caller must hold the enqueued thread \c scheduler_lock.
28 void scheduler_enqueue_in_run_queue(Thread* thread);
30 void scheduler_reschedule_ici(void);
32 /*! Selects a thread from the ready-to-run queue and, if that's not the
33 calling thread, switches the current CPU's context to run the selected
34 thread.
35 If it's the same thread, the thread will just continue to run.
36 In either case, unless the thread is dead or is sleeping/waiting
37 indefinitely, the function will eventually return.
38 The caller must hold the current thread \c scheduler_lock.
40 void scheduler_reschedule(int32 next_state);
42 /*! Sets the given thread's priority.
43 The thread may be running or may be in the ready-to-run queue.
45 int32 scheduler_set_thread_priority(Thread* thread, int32 priority);
47 /*! Called when the Thread structure is first created.
48 Per-thread housekeeping resources can be allocated.
49 Interrupts must be enabled.
51 status_t scheduler_on_thread_create(Thread* thread, bool idleThread);
53 /*! Called when a Thread structure is initialized and made ready for
54 use.
55 The per-thread housekeeping data structures are reset, if needed.
57 void scheduler_on_thread_init(Thread* thread);
59 /*! Called when a Thread structure is freed.
60 Frees up any per-thread resources allocated on the scheduler's part. The
61 function may be called even if on_thread_create() failed.
62 Interrupts must be enabled.
64 void scheduler_on_thread_destroy(Thread* thread);
66 /*! Called in the early boot process to start thread scheduling on the
67 current CPU.
68 The function is called once for each CPU.
70 void scheduler_start(void);
72 /*! Sets scheduler operation mode.
74 status_t scheduler_set_operation_mode(scheduler_mode mode);
76 /*! Dumps scheduler specific thread information.
78 void scheduler_dump_thread_data(Thread* thread);
80 void scheduler_new_thread_entry(Thread* thread);
82 void scheduler_set_cpu_enabled(int32 cpu, bool enabled);
84 void scheduler_add_listener(struct SchedulerListener* listener);
85 void scheduler_remove_listener(struct SchedulerListener* listener);
87 void scheduler_init(void);
88 void scheduler_enable_scheduling(void);
90 bigtime_t _user_estimate_max_scheduling_latency(thread_id thread);
91 status_t _user_analyze_scheduling(bigtime_t from, bigtime_t until, void* buffer,
92 size_t size, struct scheduling_analysis* analysis);
94 status_t _user_set_scheduler_mode(int32 mode);
95 int32 _user_get_scheduler_mode(void);
97 #ifdef __cplusplus
99 #endif
102 /*! Reschedules, if necessary.
103 The caller must hold the scheduler lock (with disabled interrupts).
105 static inline void
106 scheduler_reschedule_if_necessary_locked()
108 if (gCPU[smp_get_current_cpu()].invoke_scheduler)
109 scheduler_reschedule(B_THREAD_READY);
113 /*! Reschedules, if necessary.
114 Is a no-op, if interrupts are disabled.
116 static inline void
117 scheduler_reschedule_if_necessary()
119 if (are_interrupts_enabled()) {
120 cpu_status state = disable_interrupts();
122 Thread* thread = get_cpu_struct()->running_thread;
123 acquire_spinlock(&thread->scheduler_lock);
125 scheduler_reschedule_if_necessary_locked();
127 release_spinlock(&thread->scheduler_lock);
129 restore_interrupts(state);
134 #endif /* KERNEL_SCHEDULER_H */