1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.car.os;
18 
19 import android.annotation.IntDef;
20 import android.annotation.IntRange;
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.os.Parcelable;
24 
25 import com.android.car.internal.util.AnnotationValidations;
26 import com.android.car.internal.util.DataClass;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 
31 /**
32  * Defines the thread scheduling policy and priority.
33  *
34  * <p>This API supports real-time scheduling polices:
35  * ({@code SCHED_FIFO}, {@code SCHED_RR}) with a {@code sched_priority} value in the range within
36  * [{@link PRIORITY_MIN}, {@link PRIORITY_MAX}]. This API also supports the default round-robin
37  * time-sharing scheduling algorithm: {@code SCHED_DEFAULT}.
38  *
39  * @hide
40  */
41 @SystemApi
42 @DataClass(genConstructor = false, genHiddenConstDefs = true)
43 public final class ThreadPolicyWithPriority implements Parcelable {
44 
45     /**
46      * Min supported thread priority.
47      */
48     @Priority
49     public static final int PRIORITY_MIN = 1;
50 
51     /**
52      * Max supported thread priority.
53      */
54     @Priority
55     public static final int PRIORITY_MAX = 99;
56 
57     /** @hide */
58     @IntDef({SCHED_DEFAULT, SCHED_FIFO, SCHED_RR})
59     @Retention(RetentionPolicy.SOURCE)
60     public @interface SchedPolicy {}
61 
62     /**
63      * Default round-robin time-sharing scheduling policy.
64      *
65      * <p> Same as {@code SCHED_OTHER} defined in {@code /include/uapi/linux/sched.h}.
66      */
67     @Sched
68     public static final int SCHED_DEFAULT = 0;
69 
70     /**
71      * First-in-first-out scheduling policy. See definition for Linux {@code sched(7)}.
72      *
73      * <p>Same as {@code SCHED_FIFO} defined in {@code /include/uapi/linux/sched.h}.
74      */
75     @Sched
76     public static final int SCHED_FIFO = 1;
77 
78     /**
79      * Round robin scheduling policy. See definition for Linux {@code sched(7)}.
80      *
81      * <p>Same as {@code SCHED_RR} defined in {@code /include/uapi/linux/sched.h}.
82      */
83     @Sched
84     public static final int SCHED_RR = 2;
85 
86     @SchedPolicy
87     private final int mPolicy;
88 
89     @IntRange(from = 0, to = 99)
90     private final int mPriority;
91 
92     /**
93      * Creates a new thread policy with priority.
94      *
95      * @param policy The scheduling policy, must be one of {@link SchedPolicy}.
96      * @param priority The priority, must be within [{@link PRIORITY_MIN}, {@link PRIORITY_MAX}].
97      */
ThreadPolicyWithPriority( @chedPolicy int policy, @IntRange(from = 0, to = 99) int priority)98     public ThreadPolicyWithPriority(
99             @SchedPolicy int policy, @IntRange(from = 0, to = 99) int priority) {
100         if (policy != SCHED_FIFO && policy != SCHED_RR && policy != SCHED_DEFAULT) {
101             throw new IllegalArgumentException("invalid policy");
102         }
103         // priority is ignored for SCHED_DEFAULT
104         if (policy == SCHED_DEFAULT) {
105             priority = 0;
106         } else if (priority < PRIORITY_MIN || priority > PRIORITY_MAX) {
107             throw new IllegalArgumentException("invalid priority");
108         }
109         mPolicy = policy;
110         mPriority = priority;
111     }
112 
113 
114 
115     // Code below generated by codegen v1.0.23.
116     //
117     // DO NOT MODIFY!
118     // CHECKSTYLE:OFF Generated code
119     //
120     // The generated code is patched with adding "apiRequirements" annotation to all public
121     // methods/interfaces.
122     //
123     // To regenerate run:
124     // $ codegen $ANDROID_BUILD_TOP/packages/services/Car/car-lib/src/android/car/os/ThreadPolicyWithPriority.java
125     // Added AddedInOrBefore or ApiRequirement Annotation manually
126     //
127     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
128     //   Settings > Editor > Code Style > Formatter Control
129     //@formatter:off
130 
131 
132     /** @hide */
133     @IntDef(prefix = "PRIORITY_", value = {
134         PRIORITY_MIN,
135         PRIORITY_MAX
136     })
137     @Retention(RetentionPolicy.SOURCE)
138     @DataClass.Generated.Member
139     public @interface Priority {}
140 
141     /** @hide */
142     @DataClass.Generated.Member
priorityToString(@riority int value)143     public static String priorityToString(@Priority int value) {
144         switch (value) {
145             case PRIORITY_MIN:
146                     return "PRIORITY_MIN";
147             case PRIORITY_MAX:
148                     return "PRIORITY_MAX";
149             default: return Integer.toHexString(value);
150         }
151     }
152 
153     /** @hide */
154     @IntDef(prefix = "SCHED_", value = {
155         SCHED_DEFAULT,
156         SCHED_FIFO,
157         SCHED_RR
158     })
159     @Retention(RetentionPolicy.SOURCE)
160     @DataClass.Generated.Member
161     public @interface Sched {}
162 
163     /** @hide */
164     @DataClass.Generated.Member
schedToString(@ched int value)165     public static String schedToString(@Sched int value) {
166         switch (value) {
167             case SCHED_DEFAULT:
168                     return "SCHED_DEFAULT";
169             case SCHED_FIFO:
170                     return "SCHED_FIFO";
171             case SCHED_RR:
172                     return "SCHED_RR";
173             default: return Integer.toHexString(value);
174         }
175     }
176 
177     @DataClass.Generated.Member
getPolicy()178     public @SchedPolicy int getPolicy() {
179         return mPolicy;
180     }
181 
182     @DataClass.Generated.Member
getPriority()183     public @IntRange(from = 0, to = 99) int getPriority() {
184         return mPriority;
185     }
186 
187     @Override
188     @DataClass.Generated.Member
writeToParcel(@onNull android.os.Parcel dest, int flags)189     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
190         // You can override field parcelling by defining methods like:
191         // void parcelFieldName(Parcel dest, int flags) { ... }
192 
193         dest.writeInt(mPolicy);
194         dest.writeInt(mPriority);
195     }
196 
197     @Override
198     @DataClass.Generated.Member
describeContents()199     public int describeContents() { return 0; }
200 
201     /** @hide */
202     @SuppressWarnings({"unchecked", "RedundantCast"})
203     @DataClass.Generated.Member
ThreadPolicyWithPriority(@onNull android.os.Parcel in)204     /* package-private */ ThreadPolicyWithPriority(@NonNull android.os.Parcel in) {
205         // You can override field unparcelling by defining methods like:
206         // static FieldType unparcelFieldName(Parcel in) { ... }
207 
208         int policy = in.readInt();
209         int priority = in.readInt();
210 
211         this.mPolicy = policy;
212         AnnotationValidations.validate(
213                 SchedPolicy.class, null, mPolicy);
214         this.mPriority = priority;
215         AnnotationValidations.validate(
216                 IntRange.class, null, mPriority,
217                 "from", 0,
218                 "to", 99);
219 
220         // onConstructed(); // You can define this method to get a callback
221     }
222 
223     @DataClass.Generated.Member
224     public static final @NonNull Parcelable.Creator<ThreadPolicyWithPriority> CREATOR
225             = new Parcelable.Creator<ThreadPolicyWithPriority>() {
226         @Override
227         public ThreadPolicyWithPriority[] newArray(int size) {
228             return new ThreadPolicyWithPriority[size];
229         }
230 
231         @Override
232         public ThreadPolicyWithPriority createFromParcel(@NonNull android.os.Parcel in) {
233             return new ThreadPolicyWithPriority(in);
234         }
235     };
236 
237     @DataClass.Generated(
238             time = 1657845707744L,
239             codegenVersion = "1.0.23",
240             sourceFile = "packages/services/Car/car-lib/src/android/car/os/ThreadPolicyWithPriority.java",
241             inputSignatures = "public static final @android.car.os.ThreadPolicyWithPriority.Priority @android.car.annotation.AddedIn int PRIORITY_MIN\npublic static final @android.car.os.ThreadPolicyWithPriority.Priority @android.car.annotation.AddedIn int PRIORITY_MAX\npublic static final @android.car.os.ThreadPolicyWithPriority.Sched @android.car.annotation.AddedIn int SCHED_DEFAULT\npublic static final @android.car.os.ThreadPolicyWithPriority.Sched @android.car.annotation.AddedIn int SCHED_FIFO\npublic static final @android.car.os.ThreadPolicyWithPriority.Sched @android.car.annotation.AddedIn int SCHED_RR\nprivate final @android.car.os.ThreadPolicyWithPriority.SchedPolicy int mPolicy\nprivate final @android.annotation.IntRange int mPriority\nclass ThreadPolicyWithPriority extends java.lang.Object implements [android.os.Parcelable]\n@com.android.car.internal.util.DataClass(genConstructor=false, genHiddenConstDefs=true)")
242     @Deprecated
__metadata()243     private void __metadata() {}
244 
245 
246     //@formatter:on
247     // End of generated code
248 
249 }
250