源码分析 --- Context

1.1 Context是什么?

    1) Context是一个抽象类,其通用实现在ContextImpl类中。

    2) Context:是一个访问application环境全局信息的接口,通过它可以访问application的资源和相关的类,其主要功能如下:

        • 启动Activity
        • 启动和停止Service
        • 发送广播消息(Intent)
        • 注册广播消息(Intent)接收者
        • 可以访问APK中各种资源(如Resources和AssetManager等)
        • 可以访问Package的相关信息
        • APK的各种权限管理

        从以上分析可以看出,Context就是一个对APK包无所不知的大管家,大家需要什么,直接问它就可以了。

1.1.1 Context与View的关系

      View与Context(或Activity)的关系类似于明星与经纪人的关系,所以创建View时,必须明确指定其Context(即经纪人或大管家),否则View就成不了明星。


1.2 Context家族关系







Context类中基本都是抽象的方法

ContextImpl类是Context的实现类,实现了Context中定义的抽象的方法

ContextWrapper只是简单的一个代理类,包含一个ContextImpl的对象,也实现了Context中定义的抽象的方法,但实际上调用的是ContextImpl中的方法实现

1.3 Context关键函数

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. public abstract class Context {  
  2.   
  3.     // 获取应用程序包的AssetManager实例  
  4.     public abstract AssetManager getAssets();  
  5.    
  6.     // 获取应用程序包的Resources实例  
  7.     public abstract Resources getResources();  
  8.   
  9.     // 获取PackageManager实例,以查看全局package信息      
  10.     public abstract PackageManager getPackageManager();  
  11.   
  12.     // 获取应用程序包的ContentResolver实例  
  13.     public abstract ContentResolver getContentResolver();  
  14.       
  15.     // 它返回当前进程的主线程的Looper,此线程分发调用给应用组件(activities, services等)  
  16.     public abstract Looper getMainLooper();  
  17.   
  18.     // 返回当前进程的单实例全局Application对象的Context       
  19.     public abstract Context getApplicationContext();  
  20.   
  21.     // 从string表中获取本地化的、格式化的字符序列  
  22.     public final CharSequence getText(int resId) {  
  23.         return getResources().getText(resId);  
  24.     }  
  25.   
  26.     // 从string表中获取本地化的字符串  
  27.     public final String getString(int resId) {  
  28.         return getResources().getString(resId);  
  29.     }  
  30.   
  31.     public final String getString(int resId, Object... formatArgs) {  
  32.         return getResources().getString(resId, formatArgs);  
  33.     }  
  34.   
  35.     // 返回一个可用于获取包中类信息的class loader  
  36.     public abstract ClassLoader getClassLoader();  
  37.   
  38.     // 返回应用程序包名  
  39.     public abstract String getPackageName();  
  40.   
  41.     // 返回应用程序信息  
  42.     public abstract ApplicationInfo getApplicationInfo();  
  43.   
  44.     // 根据文件名获取SharedPreferences  
  45.     public abstract SharedPreferences getSharedPreferences(String name,  
  46.             int mode);  
  47.   
  48.     // 其根目录为: Environment.getExternalStorageDirectory()  
  49.    
  50.     public abstract File getExternalFilesDir(String type);  
  51.   
  52.     // 返回应用程序obb文件路径  
  53.     public abstract File getObbDir();  
  54.   
  55.     // 启动一个新的activity   
  56.     public abstract void startActivity(Intent intent);  
  57.   
  58.     // 启动一个新的activity   
  59.     public void startActivityAsUser(Intent intent, UserHandle user) {  
  60.         throw new RuntimeException("Not implemented. Must override in a subclass.");  
  61.     }  
  62.   
  63.     // 启动一个新的activity   
  64.     // intent: 将被启动的activity的描述信息  
  65.     // options: 描述activity将如何被启动  
  66.     public abstract void startActivity(Intent intent, Bundle options);  
  67.   
  68.     // 启动多个新的activity  
  69.     public abstract void startActivities(Intent[] intents);  
  70.   
  71.     // 启动多个新的activity  
  72.     public abstract void startActivities(Intent[] intents, Bundle options);  
  73.   
  74.     // 广播一个intent给所有感兴趣的接收者,异步机制   
  75.     public abstract void sendBroadcast(Intent intent);  
  76.   
  77.     // 广播一个intent给所有感兴趣的接收者,异步机制   
  78.     public abstract void sendBroadcast(Intent intent,String receiverPermission);  
  79.   
  80.     public abstract void sendOrderedBroadcast(Intent intent,String receiverPermission);  
  81.    
  82.     public abstract void sendOrderedBroadcast(Intent intent,  
  83.             String receiverPermission, BroadcastReceiver resultReceiver,  
  84.             Handler scheduler, int initialCode, String initialData,  
  85.             Bundle initialExtras);  
  86.   
  87.     public abstract void sendBroadcastAsUser(Intent intent, UserHandle user);  
  88.   
  89.     public abstract void sendBroadcastAsUser(Intent intent, UserHandle user,  
  90.             String receiverPermission);  
  91.     
  92.     // 注册一个BroadcastReceiver,且它将在主activity线程中运行  
  93.     public abstract Intent registerReceiver(BroadcastReceiver receiver,  
  94.                                             IntentFilter filter);  
  95.   
  96.     public abstract Intent registerReceiver(BroadcastReceiver receiver,  
  97.             IntentFilter filter, String broadcastPermission, Handler scheduler);  
  98.   
  99.     public abstract void unregisterReceiver(BroadcastReceiver receiver);  
  100.    
  101.     // 请求启动一个application service  
  102.     public abstract ComponentName startService(Intent service);  
  103.   
  104.     // 请求停止一个application service  
  105.     public abstract boolean stopService(Intent service);  
  106.    
  107.     // 连接一个应用服务,它定义了application和service间的依赖关系  
  108.     public abstract boolean bindService(Intent service, ServiceConnection conn,  
  109.             int flags);  
  110.   
  111.     // 断开一个应用服务,当服务重新开始时,将不再接收到调用,   
  112.     // 且服务允许随时停止  
  113.     public abstract void unbindService(ServiceConnection conn);  
  114.     public abstract Object getSystemService(String name);  
  115.  
  116.     public abstract int checkPermission(String permission, int pid, int uid);  
  117.    
  118.     // 返回一个新的与application name对应的Context对象  
  119.     public abstract Context createPackageContext(String packageName,  
  120.             int flags) throws PackageManager.NameNotFoundException;  
  121.       
  122.     // 返回基于当前Context对象的新对象,其资源与display相匹配  
  123.     public abstract Context createDisplayContext(Display display);  
  124.  }    

1.5 ContextWrapper

   它只是对Context类的一种封装,它的构造函数包含了一个真正的Context引用,即ContextImpl对象。

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Proxying implementation of Context that simply delegates all of its calls to 
  3.  * another Context.  Can be subclassed to modify behavior without changing 
  4.  * the original Context. 
  5.  */  
  6. public class ContextWrapper extends Context {  
  7.     Context mBase;     //该属性指向一个ContextIml实例  
  8.   
  9.     public ContextWrapper(Context base) {  
  10.         mBase = base;  
  11.     }  
  12.   
  13.     /** 
  14.      * Set the base context for this ContextWrapper.  All calls will then be 
  15.      * delegated to the base context.  Throws 
  16.      * IllegalStateException if a base context has already been set. 
  17.      *  
  18.      * @param base The new base context for this wrapper. 
  19.      * 创建Application、Service、Activity,会调用该方法给mBase属性赋值 
  20.      */  
  21.     protected void attachBaseContext(Context base) {  
  22.         if (mBase != null) {  
  23.             throw new IllegalStateException("Base context already set");  
  24.         }  
  25.         mBase = base;  
  26.     }  
  27.   
  28.     @Override  
  29.     public Looper getMainLooper() {  
  30.         return mBase.getMainLooper();  
  31.     }  
  32.   
  33.     @Override  
  34.     public Object getSystemService(String name) {  
  35.         return mBase.getSystemService(name);  
  36.     }  
  37.   
  38.     @Override  
  39.     public void startActivity(Intent intent) {  
  40.         mBase.startActivity(intent);  
  41.     } 

                    //   也实现了Context的抽象方法getResources,但是只是一层代理,实际上调用的是ContextIml中实现的方法

  1.    @Override

                    public Resources getResources()
                   {
                           return mBase.getResources();
                    }

  1. }  

1.6 ContextThemeWrapper

   该类内部包含了主题(Theme)相关的接口,即Android:theme属性指定的。只有Activity需要主题,Service不需要主题,所以Service直接继承于ContextWrapper类。

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * A ContextWrapper that allows you to modify the theme from what is in the  
  3.  * wrapped context.  
  4.  */  
  5. public class ContextThemeWrapper extends ContextWrapper {  
  6.     private Context mBase;  
  7.     private int mThemeResource;  
  8.     private Resources.Theme mTheme;  
  9.     private LayoutInflater mInflater;  
  10.     private Configuration mOverrideConfiguration;  
  11.     private Resources mResources;  
  12.   
  13.     public ContextThemeWrapper() {  
  14.         super(null);  
  15.     }  
  16.       
  17.     public ContextThemeWrapper(Context base, int themeres) {  
  18.         super(base);  
  19.         mBase = base;  
  20.         mThemeResource = themeres;  
  21.     }  
  22.   
  23.     @Override protected void attachBaseContext(Context newBase) {  
  24.         super.attachBaseContext(newBase);  
  25.         mBase = newBase;  
  26.     }  
  27.   
  28.     @Override public void setTheme(int resid) {  
  29.         mThemeResource = resid;  
  30.         initializeTheme();  
  31.     }  
  32.   
  33.     @Override public Resources.Theme getTheme() {  
  34.         if (mTheme != null) {  
  35.             return mTheme;  
  36.         }  
  37.   
  38.         mThemeResource = Resources.selectDefaultTheme(mThemeResource,  
  39.                 getApplicationInfo().targetSdkVersion);  
  40.         initializeTheme();  
  41.   
  42.         return mTheme;  
  43.     }  
  44. }  

2. 何时创建Context

    应用程序在以下几种情况下创建Context实例:
      1) 创建Application对象时, 而且整个App共一个Application对象
      2) 创建Service对象时
      3) 创建Activity对象时
 
    因此应用程序App共有的Context数目公式为:

         总Context实例个数 = Service个数 + Activity个数 + 1(Application对应的Context实例)

 

    ActivityThread类中(就是UI线程也是主线程)消息处理函数与本节相关的内容如下:

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. public void handleMessage(Message msg) {  
  2.     if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));  
  3.     switch (msg.what) {  
  4.         case LAUNCH_ACTIVITY: { // 创建Activity对象  
  5.             Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");  
  6.             ActivityClientRecord r = (ActivityClientRecord)msg.obj;  
  7.   
  8.             r.packageInfo = getPackageInfoNoCheck(  
  9.                     r.activityInfo.applicationInfo, r.compatInfo);  
  10.             handleLaunchActivity(r, null);  
  11.             Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);  
  12.         } break;  
  13.   
  14.         case BIND_APPLICATION: // 创建Application对象  
  15.             Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");  
  16.             AppBindData data = (AppBindData)msg.obj;  
  17.             handleBindApplication(data);  
  18.             Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);  
  19.             break;  
  20.   
  21.         case CREATE_SERVICE: // 创建Service对象  
  22.             Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");  
  23.             handleCreateService((CreateServiceData)msg.obj);  
  24.             Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);  
  25.             break;  
  26.               
  27.         case BIND_SERVICE:  // Bind Service对象  
  28.             Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");  
  29.             handleBindService((BindServiceData)msg.obj);  
  30.             Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);  
  31.             break;  
  32.     }  
  33. }  


2.1 创建Application对象时创建Context实例

      每个应用程序在第一次启动时,都会首先创建一个Application对象。从startActivity流程可知,创建Application的时机在handleBindApplication()方法中,该函数位于 ActivityThread.Java类中 ,相关代码如下:

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. // ActivityThread.java  
  2. private void handleBindApplication(AppBindData data) {   
  3.    try {  
  4.          // If the app is being launched for full backup or restore, bring it up in  
  5.          // a restricted environment with the base application class.  
  6.          Application app = data.info.makeApplication(data.restrictedBackupMode, null);  
  7.          mInitialApplication = app;  
  8.          ...  
  9.      } finally {  
  10.          StrictMode.setThreadPolicy(savedPolicy);  
  11.      }  
  12. }  
  13.   
  14. // LoadedApk.java  
  15. public Application makeApplication(boolean forceDefaultAppClass,  
  16.          Instrumentation instrumentation) {  
  17.      if (mApplication != null) {  
  18.          return mApplication;  
  19.      }  
  20.   
  21.      Application app = null;  
  22.   
  23.      String appClass = mApplicationInfo.className;  
  24.      if (forceDefaultAppClass || (appClass == null)) {  
  25.          appClass = "android.app.Application";  
  26.      }  
  27.   
  28.      try {  
  29.          java.lang.ClassLoader cl = getClassLoader();  
  30.          ContextImpl appContext = new ContextImpl(); // 创建ContextImpl实例  
  31.          appContext.init(thisnull, mActivityThread);  
  32.          app = mActivityThread.mInstrumentation.newApplication(  
  33.                  cl, appClass, appContext);  
  34.          appContext.setOuterContext(app); // 将Application实例传递给Context实例  
  35.      } catch (Exception e) {  
  36.          ...  
  37.      }  
  38.      mActivityThread.mAllApplications.add(app);  
  39.      mApplication = app;  
  40.   
  41.      return app;  
  42.  }  
把创建的 ContextImpl实例通过 mActivityThread.mInstrumentation.newApplication传递进去

看下这个函数的实现:


 public Application newApplication(ClassLoader cl, String className, Context context)
            throws InstantiationException, IllegalAccessException,
            ClassNotFoundException {
        return newApplication(cl.loadClass(className), context);
    }

 // 也是通过反射创建Application的实例

 static public Application newApplication(Class<?> clazz, Context context)
            throws InstantiationException, IllegalAccessException,
            ClassNotFoundException {
        Application app = (Application)clazz.newInstance();
        app.attach(context);
        return app;
  }

看下Applicationattach的实现

/* package */ final void attach(Context context) {
          attachBaseContext(context);   // 因为Application也是继承的ContextWrapper这个类。这个方法的实现在ContextWrapper类里
          mLoadedApk = ContextImpl.getImpl(context).mPackageInfo;
    }


//ContextWrapper类的attachBaseContext的实现

      protected void attachBaseContext(Context base) {
        if (mBase != null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base; //传进来的ContextImpl实例保存到了ContextWrappr中的mBase变量中。
    }



2.2 创建Activity对象时创建Context实例

    通过startActivity()或startActivityForResult()请求启动一个Activity时,如果系统检测需要新建一个Activity对象时,就会回调handleLaunchActivity()方法,(UI线程的handleMessage中处理的)该方法继而调用performLaunchActivity()方法,去创建一个Activity实例,并且回调onCreate(),onStart()方法等,函数都位于 ActivityThread.java类 ,相关代码如下:

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1.   private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {  
  2.     ...  
  3.     Activity a = performLaunchActivity(r, customIntent); // 到下一步  
  4.   
  5.     if (a != null) {  
  6.         r.createdConfig = new Configuration(mConfiguration);  
  7.         Bundle oldState = r.state;  
  8.         handleResumeActivity(r.token, false, r.isForward,  
  9.                 !r.activity.mFinished && !r.startsNotResumed);  
  10.         ...  
  11.     }  
  12.     ...  
  13.  }  
  14.   
  15. private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {  
  16.     ...      
  17.     Activity activity = null;  
  18.     try {  
  19.         java.lang.ClassLoader cl = r.packageInfo.getClassLoader(); 
  20.         //    这个方法中反射调用了onCreate方法创建一个activity的实例,具体可以看startActivity的源码分析
  21.         activity = mInstrumentation.newActivity(  
  22.                 cl, component.getClassName(), r.intent);  
  23.         StrictMode.incrementExpectedActivityCount(activity.getClass());  
  24.         r.intent.setExtrasClassLoader(cl);  
  25.         if (r.state != null) {  
  26.             r.state.setClassLoader(cl);  
  27.         }  
  28.     } catch (Exception e) {  
  29.         ...  
  30.     }  
  31.   
  32.     try {  
  33.         Application app = r.packageInfo.makeApplication(false, mInstrumentation);  
  34.   
  35.         if (activity != null) {  
  36.             Context appContext = createBaseContextForActivity(r, activity);  // 创建Activity的Context  
  37.             CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());  
  38.             Configuration config = new Configuration(mCompatConfiguration);  
  39.             if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "  
  40.                     + r.activityInfo.name + " with config " + config); 
  41.             //  和application的实现一样,attach函数里也是调用了ContextWrapper类的attachBaseContext的实现,把创建的Context实例保存到了Activity对象的mBace变量里
  42.             activity.attach(appContextthis, getInstrumentation(), r.token,  
  43.                     r.ident, app, r.intent, r.activityInfo, title, r.parent,  
  44.                     r.embeddedID, r.lastNonConfigurationInstances, config);  
  45.   
  46.             if (customIntent != null) {  
  47.                 activity.mIntent = customIntent;  
  48.             }  
  49.             r.lastNonConfigurationInstances = null;  
  50.             activity.mStartedActivity = false;  
  51.             int theme = r.activityInfo.getThemeResource();  
  52.             if (theme != 0) {  
  53.                 activity.setTheme(theme);  
  54.             }  
  55.   
  56.   
  57.         mActivities.put(r.token, r);  
  58.   
  59.     } catch (SuperNotCalledException e) {  
  60.         ...  
  61.   
  62.     } catch (Exception e) {  
  63.         ...  
  64.     }  
  65.   
  66.     return activity;  
  67. }  
[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. private Context createBaseContextForActivity(ActivityClientRecord r,  
  2.         final Activity activity) {  
  3.     ContextImpl appContext = new ContextImpl();  // 创建ContextImpl实例  
  4.     appContext.init(r.packageInfo, r.token, this);  
  5.     appContext.setOuterContext(activity);  
  6.   
  7.     // For debugging purposes, if the activity's package name contains the value of  
  8.     // the "debug.use-second-display" system property as a substring, then show  
  9.     // its content on a secondary display if there is one.  
  10.     Context baseContext = appContext;  
  11.     String pkgName = SystemProperties.get("debug.second-display.pkg");  
  12.     if (pkgName != null && !pkgName.isEmpty()  
  13.             && r.packageInfo.mPackageName.contains(pkgName)) {  
  14.         DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();  
  15.         for (int displayId : dm.getDisplayIds()) {  
  16.             if (displayId != Display.DEFAULT_DISPLAY) {  
  17.                 Display display = dm.getRealDisplay(displayId);  
  18.                 baseContext = appContext.createDisplayContext(display);  
  19.                 break;  
  20.             }  
  21.         }  
  22.     }  
  23.     return baseContext;  


 所以在动态加载activity的时候,只是反射调用了onCreate方法创建了要启动的activity类的对象,但是没有创建ContextImpl这个对象,也就是没有对activity中的mBase做初始化的流程,所以创建的activity不能直接使用自己的context.因为为null.也就是没有上下文环境。


2.3 创建Service对象时创建Context实例

     通过startService或者bindService时,如果系统检测到需要新创建一个Service实例,就会回调handleCreateService()方法,(也是通过UI线程的handlemessage方法实现的)完成相关数据操作。handleCreateService()函数位于 ActivityThread.java类,如下:

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. private void handleCreateService(CreateServiceData data) {  
  2.     // If we are getting ready to gc after going to the background, well  
  3.     // we are back active so skip it.  
  4.     unscheduleGcIdler();  
  5.   
  6.     LoadedApk packageInfo = getPackageInfoNoCheck(  
  7.             data.info.applicationInfo, data.compatInfo);  
  8.     Service service = null;  
  9.     try {  
  10.         java.lang.ClassLoader cl = packageInfo.getClassLoader(); 
  11.        //  反射创建server类的对象
  12.         service = (Service) cl.loadClass(data.info.name).newInstance();  
  13.     } catch (Exception e) {  
  14.         if (!mInstrumentation.onException(service, e)) {  
  15.             throw new RuntimeException(  
  16.                 "Unable to instantiate service " + data.info.name  
  17.                 + ": " + e.toString(), e);  
  18.         }  
  19.     }  
  20.   
  21.     try {  
  22.         if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);  
  23.        //  也是先创建对象再创建Context实例,并且通过attach方法设置到对象的mBase变量中
  24.         ContextImpl context = new ContextImpl(); // 创建ContextImpl实例  
  25.         context.init(packageInfo, nullthis);  
  26.   
  27.         Application app = packageInfo.makeApplication(false, mInstrumentation);  
  28.         context.setOuterContext(service); 
  29.         service.attach(context, this, data.info.name, data.token, app,  
  30.                 ActivityManagerNative.getDefault());  
  31.         service.onCreate();  
  32.         mServices.put(data.token, service);  
  33.         try {  
  34.             ActivityManagerNative.getDefault().serviceDoneExecuting(  
  35.                     data.token, 000);  
  36.         } catch (RemoteException e) {  
  37.             // nothing to do.  
  38.         }  
  39.     } catch (Exception e) {  
  40.         if (!mInstrumentation.onException(service, e)) {  
  41.             throw new RuntimeException(  
  42.                 "Unable to create service " + data.info.name  
  43.                 + ": " + e.toString(), e);  
  44.         }  
  45.     }  
  46. }  


3. 小结

     通过对ContextImp的分析可知,其方法的大多数操作都是直接调用其属性mPackageInfo(该属性类型为PackageInfo)的相关方法而来。这说明ContextImp是一种轻量级类,而PackageInfo才是真正重量级的类。一个App里的不同的activity和service类对应不同的ContextImpl实例,但是这些不同的ContextImpl实例都对应同一个packageInfo对象。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Context包是Go语言内置的一个标准库,主要用于多个Goroutine之间的上下文传递和控制。它提供了一种机制来传递取消信号、截止时间和一些其他的请求/值,这些请求/值可以跨越多个API边界和Goroutine传递,而不需要显式地传递。 以下是Context包的主要源码分析: 1. Context接口 Context接口定义了一个可取消的上下文,它包括了Deadline截止时间、Done通道和Value键值对数据。 ```go type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} } ``` 2. context.Background() Background函数返回一个空的Context,它没有任何值和截止时间,而且永远不会取消。它被广泛用于Main函数、初始化和测试中。 ```go func Background() Context { return background } var ( background = new(emptyCtx) ) type emptyCtx int func (*emptyCtx) Deadline() (deadline time.Time, ok bool) { return } func (*emptyCtx) Done() <-chan struct{} { return nil } func (*emptyCtx) Err() error { return nil } func (*emptyCtx) Value(key interface{}) interface{} { return nil } ``` 3. context.TODO() TODO函数返回一个非空的Context,它没有任何值和截止时间,而且永远不会取消。它被广泛用于暂时不确定上下文应该是什么的情况。 ```go func TODO() Context { return todo } var ( todo = new(emptyCtx) ) ``` 4. context.WithCancel() WithCancel函数返回一个带有CancelFunc的Context,当CancelFunc调用时,Context的Done通道将被关闭。这个函数可以用来取消长时间运行的操作。 ```go func WithCancel(parent Context) (ctx Context, cancel CancelFunc) { c := newCancelCtx(parent) propagateCancel(parent, &c) return &c, func() { c.cancel(true, Canceled) } } type cancelCtx struct { Context mu sync.Mutex // protects following fields done chan struct{} // created lazily, closed by first cancel call children map[canceler]struct{} // set to nil by the first cancel call err error // set to non-nil by the first cancel call } func newCancelCtx(parent Context) cancelCtx { return cancelCtx{Context: parent} } func (c *cancelCtx) Done() <-chan struct{} { c.mu.Lock() if c.done == nil { c.done = make(chan struct{}) } d := c.done c.mu.Unlock() return d } func (c *cancelCtx) Err() error { c.mu.Lock() defer c.mu.Unlock() return c.err } func (c *cancelCtx) cancel(removeFromParent bool, err error) { if err == nil { panic("context: internal error: missing cancel error") } c.mu.Lock() if c.err != nil { c.mu.Unlock() return // already canceled } c.err = err if c.done == nil { c.done = closedchan } else { close(c.done) } for child := range c.children { // NOTE: acquiring the child's lock while holding parent's lock. child.cancel(false, err) } c.children = nil c.mu.Unlock() if removeFromParent { removeChild(c.Context, c) } } type canceler interface { cancel(removeFromParent bool, err error) } var closedchan = make(chan struct{}) func init() { close(closedchan) } ``` 5. context.WithDeadline() WithDeadline函数返回一个带有截止时间的Context,当截止时间到达或者调用CancelFunc时,Context的Done通道将被关闭。 ```go func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) { if cur, ok := parent.Deadline(); ok && cur.Before(deadline) { // The current deadline is already sooner than the new one. return WithCancel(parent) } c := &timerCtx{ cancelCtx: newCancelCtx(parent), deadline: deadline, } propagateCancel(parent, c) d := c.deadline.Sub(time.Now()) if d <= 0 { c.cancel(true, DeadlineExceeded) // deadline has already passed return c, func() { c.cancel(true, Canceled) } } c.mu.Lock() defer c.mu.Unlock() if c.err == nil { c.timer = time.AfterFunc(d, func() { c.cancel(true, DeadlineExceeded) }) } return c, func() { c.cancel(true, Canceled) } } type timerCtx struct { cancelCtx deadline time.Time mu sync.Mutex // protects timer and err timer *time.Timer err error } func (c *timerCtx) Deadline() (deadline time.Time, ok bool) { return c.deadline, true } func (c *timerCtx) cancel(removeFromParent bool, err error) { c.cancelCtx.cancel(false, err) // propagate the cancel first c.mu.Lock() if c.timer != nil { c.timer.Stop() c.timer = nil } c.err = err c.mu.Unlock() if removeFromParent { removeChild(c.cancelCtx.Context, c) } } ``` 6. context.WithTimeout() WithTimeout函数返回一个带有超时时间的Context,当超时时间到达或者调用CancelFunc时,Context的Done通道将被关闭。 ```go func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { return WithDeadline(parent, time.Now().Add(timeout)) } ``` 7. context.WithValue() WithValue函数返回一个带有键值对数据的Context,这个数据可以跨越多个API边界和Goroutine传递,而不需要显式地传递。 ```go func WithValue(parent Context, key, val interface{}) Context { if key == nil { panic("nil key") } if val == nil { panic("nil value") } return &valueCtx{parent, key, val} } type valueCtx struct { Context key, val interface{} } func (c *valueCtx) Value(key interface{}) interface{} { if c.key == key { return c.val } return c.Context.Value(key) } ``` 以上就是Context包的源码分析Context包提供了一种简单而强大的机制来传递请求/值和取消信号,可以用于管理并发访问、超时控制和错误处理等场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值