1 /*
2  * Copyright (C) 2011 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 libcore.io;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import java.util.Objects;
21 
22 /** @hide */
23 public final class Libcore {
Libcore()24     private Libcore() { }
25 
26     /**
27      * Direct access to syscalls. Code should strongly prefer using {@link #os}
28      * unless it has a strong reason to bypass the helpful checks/guards that it
29      * provides.
30      */
31     public static final Os rawOs = new Linux();
32 
33     /**
34      * Access to syscalls with helpful checks/guards.
35      * For read access only; the only supported way to update this field is via
36      * {@link #compareAndSetOs}.
37      */
38     @UnsupportedAppUsage
39     public static volatile Os os = new BlockGuardOs(rawOs);
40 
getOs()41     public static Os getOs() {
42         return os;
43     }
44 
45     /**
46      * Updates {@link #os} if {@code os == expect}. The update is atomic with
47      * respect to other invocations of this method.
48      */
compareAndSetOs(Os expect, Os update)49     public static boolean compareAndSetOs(Os expect, Os update) {
50         Objects.requireNonNull(update);
51         if (os != expect) {
52             return false;
53         }
54         synchronized (Libcore.class) {
55             boolean result = (os == expect);
56             if (result) {
57                 os = update;
58             }
59             return result;
60         }
61     }
62 }
63