An environment variable acquisition method and related device
By identifying the target thread and obtaining its proprietary environment variables in a multi-threaded environment, the program exception problem caused by different threads sharing environment variables is solved, improving program stability and the accuracy of environment variables, and simplifying the operation process.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- 武汉斗鱼鱼乐网络科技有限公司
- Filing Date
- 2021-03-10
- Publication Date
- 2026-04-24
AI Technical Summary
In a multi-threaded environment, existing technologies often cause program exceptions by having different threads share the same environment variable, affecting program stability.
By determining the target thread corresponding to the execution function, the target environment variables are obtained from the preset variable set and input into the execution function. The system platform-specific abstraction layer interface function and the inter-platform interface function are used to obtain and transmit the environment variables, ensuring that each thread uses its own environment variables.
It improves program stability, avoids conflicts between environment variables between multiple threads, simplifies the operation process, ensures the accuracy and consistency of environment variables, and reduces the workload of programmers.
Smart Images

Figure CN115080256B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and related apparatus for obtaining environmental variables. Background Technology
[0002] When developing features for the Android platform, different programming languages are often used to design different functionalities, which are then encapsulated into corresponding containers to form functional modules. Furthermore, when these functional modules are called by certain threads, they often need to be invoked at different programming language levels. For example, functionalities developed using C++ are typically encapsulated into a container module, which is then provided to the Java layer for invocation. The interaction between the C++ and Java layers requires obtaining the necessary environment variables to execute the corresponding functions.
[0003] Currently, in practical applications, different threads typically require different environment variables to ensure normal program operation. However, in real-world applications, when thread A calls a certain function, and thread B also calls the same function at the same time, it is very likely that the function will still use the environment variables used by thread A. This results in different threads sharing the same environment variables, which can lead to program exceptions. Summary of the Invention
[0004] The purpose of this invention is to provide a method for obtaining environment variables, which can determine the corresponding target thread when the execution function is executed, determine the target environment variable from a preset variable set, and input the obtained environment variable into the execution function. This ensures that the environment variables in the execution function meet the requirements of its thread, avoids program exceptions caused by multiple threads sharing the same environment variable, and ensures program stability.
[0005] In a first aspect, the present invention provides a method for obtaining environmental variables, comprising the following steps:
[0006] Determine the target thread corresponding to the execution function, wherein the execution function is the function called by the target thread;
[0007] The target environment variables are determined from a preset set of variables based on the target thread;
[0008] Obtain the target environment variable and input the target environment variable into the execution function.
[0009] Optionally, obtaining the target environment variable and inputting the target environment variable into the execution function includes:
[0010] Determine the system platform category of the executed function; the system platform category includes Windows platform and Linux platform.
[0011] The corresponding abstraction layer interface function is invoked according to the system platform category, and the environment variables of the target thread are obtained according to the abstraction layer interface function. The abstraction layer interface function is a special interface function for different system platforms built based on a preset interface abstraction layer.
[0012] The inter-platform interface function is invoked, and the environment variables are input into the execution function according to the inter-platform interface function, wherein the inter-platform interface function is a variable transfer function defined based on the JNI inter-platform transfer interface program.
[0013] Optionally, before invoking the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0014] A preset interface abstraction layer is set up, which consists of basic functions used to implement the corresponding abstraction layer functions of different system platforms;
[0015] The abstraction layer interface functions for each system platform are set according to the corresponding interface functions for each system platform and the system interface abstraction layer.
[0016] Optionally, before invoking the inter-platform interface function, the method further includes:
[0017] Obtain the virtual machine of the target thread and the environment variable interface of the target thread, wherein the virtual machine contains environment variable parameters, and the environment variable interface is obtained based on the virtual machine and a preset global variable pointer;
[0018] The inter-platform interface function is constructed based on the virtual machine and the environment variable interface.
[0019] Optionally, after calling the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0020] Check whether the return value of the abstract layer interface function is normal;
[0021] If an anomaly is detected, it is determined that the initial environment variables have been obtained. The initial environment variables are the environment variables obtained before the aforementioned environment variables were obtained.
[0022] Optionally, a preset interface abstraction layer class is defined, and a preset virtual destructor is set in the preset interface abstraction layer class, wherein the preset virtual destructor is a function used to pass data content to the corresponding thread based on the handle identifier key value of the thread.
[0023] Optionally, setting the abstraction layer interface function for each system platform based on the interface function corresponding to each system platform and the system interface abstraction layer includes:
[0024] When the system platform is a Windows platform, the TlsGetValue function is obtained, and the abstraction layer interface function is set according to the TlsGetValue function and the system interface abstraction layer, wherein the TlsGetValue function is a system-preset built-in API function in the Windows platform;
[0025] When the system platform is a Linux platform, the thread_key_create function is obtained, and the abstraction layer interface function is set according to the thread_key_create function and the system interface abstraction layer, wherein the thread_key_create function is a system-preset built-in API function in the Linux platform.
[0026] Secondly, the present invention also provides an environmental variable acquisition device, comprising:
[0027] The first determining unit is used to determine the target thread corresponding to the execution function, wherein the execution function is a function called by the target thread;
[0028] The second determining unit is used to determine the required target environment variables from a preset variable set based on the target thread;
[0029] An input unit is used to obtain the target environment variable and input the target environment variable into the execution function.
[0030] This invention provides a method and related apparatus for obtaining environment variables. The method involves determining the target thread corresponding to an execution function, where the execution function is a function called by the target thread; then, determining the required target environment variables from a preset variable set based on the target thread; finally, obtaining the target environment variables and inputting them into the execution function, thereby achieving the function of obtaining environment variables. In this scheme, since the environment variables ultimately input into the execution function are obtained from the preset variable set based on the target thread corresponding to the execution function, it ensures that the obtained environment variables are consistent with the corresponding thread, avoiding program exceptions caused by multiple threads using the same environment variables when calling execution functions, thus improving program stability. Furthermore, this scheme allows for the acquisition of corresponding environment variables for different threads based on the method of this invention. That is, when different execution functions are executed or called, the method can be used to obtain the environment variables for the corresponding thread, thus avoiding the conventional process of passing environment variables between execution functions when multiple execution functions are called within a single thread cycle. This simplifies the operation process, and since the environment variables are obtained based on the target thread each time, rather than being passed between functions, the accuracy of the environment variables is also ensured. Furthermore, using the above solution, only one set of code corresponding to the above functions is needed to achieve the process of obtaining the corresponding environment variables when any type of execution function is called, thus eliminating the need to set the corresponding environment variables for multiple functions corresponding to a thread and passing code separately, reducing the workload of programmers. Attached Figure Description
[0031] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0032] Figure 1 A flowchart illustrating the steps of an environmental variable acquisition method provided in an embodiment of the present invention;
[0033] Figure 2 This is a schematic diagram of an embodiment of an environmental variable acquisition device provided by the present invention;
[0034] Figure 3 A schematic diagram illustrating an embodiment of an electronic device provided in this application;
[0035] Figure 4 This is a schematic diagram illustrating an embodiment of a computer-readable storage medium provided in this application. Detailed Implementation
[0036] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0037] The terms "comprising" and "having," and any variations thereof, used in the embodiments of this invention are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or device that includes a series of steps or units is not limited to the steps or units listed, but may optionally include other steps or units not listed, or may optionally include other steps or units inherent to such processes, methods, products, or devices.
[0038] like Figure 1 As shown, an embodiment of the present invention provides a method for obtaining environmental variables, which includes the following steps:
[0039] 101. Determine the target thread corresponding to the function to be executed.
[0040] The execution function is the function called by the target thread.
[0041] In this embodiment, to ensure that the environment variables used by the subsequently executed function are consistent with the target thread, it is necessary to determine which thread the current execution function corresponds to before obtaining the environment variables, i.e., to determine the target thread. Specifically, this determination can be achieved by analyzing the execution code during the function call process to identify the identifier corresponding to the target thread that calls and uses the execution function, thereby identifying the target thread. Alternatively, in this embodiment, the determination process can be based on preset monitoring code. This monitoring code can be a built-in global monitoring function within the system; it is not limited here and can be selected based on the user's actual situation.
[0042] Therefore, identifying the target thread of the execution function in this step lays the foundation for subsequently obtaining the corresponding environment variables based on that target thread. Compared with conventional environment variables, this embodiment can obtain environment variables based on the target thread, avoiding the program exceptions caused by the execution function blindly using the same environment variable to serve different threads, thus improving program stability.
[0043] 102. Determine the required target environment variables from the preset variable set according to the target thread.
[0044] In this embodiment, the preset variable set can be understood as multiple regions that can determine environment variables for the target thread, which may include the virtual machine code part in the target thread. Of course, in actual applications, there may be multiple environment variables. In this case, the specific environment variable required can be determined based on the user's instructions, that is, the target environment variable required by the target thread can be determined.
[0045] Based on the above method, it can be determined that the actual target environment variable is determined from the preset variable set before the process of obtaining environment variables is performed, thereby ensuring the accuracy of obtaining the target environment variable in subsequent steps.
[0046] 103. Obtain the target environment variable and input the target environment variable into the execution function.
[0047] Once the target thread and required target environment variables have been determined in the preceding steps, the target to be acquired is essentially defined. This allows the operation of acquiring the target environment variables to be performed and input into the execution function. Specifically, during execution, the method described in this step can be executed according to the following aspects based on different functional requirements:
[0048] Firstly, in some examples, since there may be multiple platforms composed of different programming codes within the system, based on the compatibility between different platforms, obtaining the target environment variable in this step and inputting the target environment variable into the execution function can specifically include:
[0049] First, determine the system platform category of the executed function; the system platform category includes Windows platform and Linux platform.
[0050] Then, the corresponding abstraction layer interface function is called according to the system platform category, and the environment variables of the target thread are obtained according to the abstraction layer interface function. The abstraction layer interface function is a special interface function for different system platforms built based on a preset interface abstraction layer.
[0051] Finally, the inter-platform interface function is called, and the environment variables are input into the execution function according to the inter-platform interface function, wherein the inter-platform interface function is a variable transfer function defined based on the JNI inter-platform transfer interface program.
[0052] As can be seen from the steps described in the first aspect, since the corresponding abstraction layer interface functions can be called based on different platform categories during the execution process, compatibility between multiple platforms can be ensured, making the method described in this embodiment more adaptable.
[0053] Secondly, in some embodiments, before invoking the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0054] A preset interface abstraction layer is set up, which consists of basic functions used to implement the corresponding abstraction layer functions of different system platforms;
[0055] The abstraction layer interface functions for each system platform are set according to the corresponding interface functions for each system platform and the system interface abstraction layer.
[0056] As described in the preceding steps, since the code functions and types differ between different platforms, it is necessary to design a preset interface abstraction layer that can interface with each platform. This ensures that the environment variables can be obtained in a way that is compatible with different platforms. The specific platforms mentioned above are not limited to Windows, Android, Linux, etc.
[0057] Since interfaces are system-dependent and different platforms have different implementation methods, we can pre-set the interface abstraction layer to provide abstraction layer interface functions to the outside world for different platforms. This not only achieves compatibility with various platforms, but also allows for direct calls after a single setup, facilitating subsequent operations.
[0058] Thirdly, regarding the above steps, setting the execution of the preset interface abstraction layer specifically involves: first, defining a preset abstraction layer class, then constructing a preset virtual destructor function and making it inherit from the preset abstraction layer class, while setting the main statements of the virtual destructor function to implement a function that passes data content to the corresponding thread based on the thread handle identifier key value.
[0059] The specific setup process is as follows:
[0060] template<typename K, D>
[0061] Where K represents the thread handle identifier key, and D represents the corresponding data content (i.e., variable).
[0062] Then, first define a class as: Class JniData{
[0063] Then set the virtual destructor virtual ~JniData() {}
[0064] Next, we will set up three main function statements.
[0065] 1. The first one is virtual K makeKeyData() = 0; This statement is used to determine the target thread based on the thread's handle identifier key;
[0066] 2. The second statement is virtual D* getData(K key) = 0; This statement is used to retrieve the data corresponding to the thread based on the handle identifier key, i.e., the variable. The data D* can be stored using a templated pointer.
[0067] 3. The third statement is `virtual Void setData(K key, D* data) = 0;`. This statement is used to assign the data obtained in the second statement to the thread corresponding to the key, that is, to add `data` to the corresponding key.
[0068] In this way, the above code enables the definition and setting of a preset abstraction layer. The functions described above include: `virtual K makeKeyData() = 0` retrieves the key value of the data stored in the handle of the target thread; `virtual D* getData(K key) = 0` retrieves the data stored in the thread based on the key value; and `virtual Void setData(K key, D* data) = 0` stores the data in the target thread based on the key value. This achieves a function that allows data retrieval and storage within a thread based on the key value, after obtaining the target thread from the handle. This lays the foundation for building abstraction layer interface functions for different platforms based on this functionality.
[0069] Fourthly, in some embodiments, setting the abstraction layer interface function for each system platform according to the interface function corresponding to each system platform and the system interface abstraction layer includes:
[0070] When the system platform is a Windows platform, the TlsGetValue function is obtained, and the abstraction layer interface function is set according to the TlsGetValue function and the system interface abstraction layer, wherein the TlsGetValue function is a system-preset built-in API function in the Windows platform;
[0071] When the system platform is a Linux platform, the thread_key_create function is obtained, and the abstraction layer interface function is set according to the thread_key_create function and the system interface abstraction layer, wherein the thread_key_create function is a system-preset built-in API function in the Linux platform.
[0072] By utilizing built-in functions for different platforms, such as the TlsGetValue and thread_key_create functions, the system implements thread data retrieval functionality based on built-in API functions for each platform. This eliminates the need to set up specific interface functions during the implementation process; only internal system calls are required, simplifying the coding process and improving setup efficiency.
[0073] Furthermore, the process of setting up the abstraction layer interface functions for each system platform specifically for different platforms can be as follows:
[0074] As described above, the process of constructing the abstraction layer interface functions for different platforms also differs. This paper implements two platforms, Windows and Linux, as shown in sections 1) and 2) respectively:
[0075] 1) The implementation process based on the Windows platform is as follows:
[0076] For the Windows platform, the interface functions of this abstraction layer can be implemented using the aforementioned pre-defined interface abstraction layer combined with the built-in system API functions provided by the Windows platform. Therefore, we first write a class that inherits from the abstraction layer class JniData;
[0077] template <typename k="DWORD," d>
[0078] Class WindwosJniData : public JniData{This step is used to implement the abstract layer interfaces defined above one by one.
[0079] K makeKeyData(){This step is used to retrieve the key-value pairs of data stored in the thread.
[0080] K key = TlsAlloc(); This step is used to call the system API function TlsAlloc to obtain a key value.
[0081] Return key; This step is used to return the obtained key value.
[0082] } D* getData(K key){ This step is used to implement the data retrieval interface.
[0083] LPVOID raw_data = TlsGetValue(key); This step is used to retrieve the raw_data stored in the thread based on the key value by calling the system function TlsGetValue.
[0084] return reinterpret_cast<D*> (raw_data); This step is used to perform data transformation.
[0085] }Void setData(K key, D* data) { This implements the interface functionality.
[0086] In practical applications, the code `BOOL ret = TlsSetValue(key, data);` can be used to store the retrieved data in a thread and index it using the key. The return value is used to determine if any exceptions have occurred. A `ret` value of `false` indicates that a value has already been stored, allowing for error detection in the retrieved results.
[0087] Therefore, by using the built-in API function TlsGetValue, the thread data acquisition function of the Windows system platform can be implemented based on the built-in API function. This eliminates the need to set up a corresponding API function during the implementation process, and only requires calling the internal Windows system functions, simplifying the coding process and improving the setup efficiency. At the same time, the code based on BOOL ret = TlsSetValue(key, data) implements the function of error detection for the acquired thread data, further ensuring the accuracy of the acquired data results and laying the foundation for the accuracy of subsequent acquisition of environment variables.
[0088] 2) The implementation process based on the Linux platform is as follows:
[0089] On the Linux platform, all the aforementioned interfaces can be implemented using the pre-defined interface abstraction layer combined with the system API functions provided by the Linux platform. Similar to the Windows platform, a class can be written that inherits from the JniData class in the abstraction layer.
[0090] template <typename k="pthread_key_t," d>Class LinuxJniData : publicJniData{This step is used to implement the abstract layer interfaces defined above one by one.
[0091] K makeKeyData(){This step is used to retrieve the key-value pairs of the data stored in the target thread.}
[0092] `pthread_key_t key; pthread_key_create(&key, null);` This step calls the system API function `thread_key_create` to obtain the key value.
[0093] `Return key;` is used to return the obtained key value based on the retrieved result.
[0094] } D* getData(K key){ This implements the interface function for retrieving data from the target thread.
[0095] In addition, to ensure the accuracy of the data obtained, in practical applications, the code can be set as: LPVOIDraw_data = pthread_getspecific(key); and the system's built-in function TlsGetValue can be called to retrieve the data raw_data stored in the thread based on the key value.
[0096] return reinterpret_cast<D*> (raw_data); This step is used to perform data transformation.
[0097] `Void setData(K key, D* data) {` This implements an interface for retrieving data within the target thread. Alternatively, `BOOL ret = pthread_setspecific(key, data)` can store the data in the thread and use the key for subsequent indexing. The return value indicates whether an exception has occurred. If `ret` is false, it means that a data entry has already been stored; otherwise, an error message can be displayed.}
[0098] Therefore, by using the built-in API function `thread_key_create`, the thread data acquisition function of the Linux system platform can be implemented based on the built-in API function. This eliminates the need to set up a specific API function during the implementation process; only internal calls within the Linux system are required, simplifying the coding process and improving setup efficiency. In addition, the code `BOOL ret = TlsSetValue(key, data)` implements error detection for the acquired thread data, further ensuring the accuracy of the acquired data results and laying the foundation for the accuracy of subsequent environment variable acquisition.
[0099] Fifthly, in some examples, when a container module is loaded, the virtual machine of the current program or thread can be obtained, and the virtual machine contains all parameters and code during execution. To ensure smooth execution of parameters or content for a specific thread in a subsequent manner, the obtained virtual machine can be saved. An interface can be defined that uses the saved virtual machine as a basis for retrieving environment variables. This interface can then be called when needed to implement the cross-platform environment variable input function. Based on this, before calling the cross-platform interface function, the method further includes:
[0100] Obtain the virtual machine of the target thread and the environment variable interface of the target thread, wherein the virtual machine contains environment variable parameters, and the environment variable interface is obtained based on the virtual machine and a preset global variable pointer;
[0101] The inter-platform interface function is constructed based on the virtual machine and the environment variable interface.
[0102] In this way, by setting the inter-platform interface function through the virtual machine and environment variable interface, it can be ensured that the set inter-platform interface function can obtain the corresponding environment variables based on the code in the virtual machine, thereby ensuring the accuracy of the obtained results.
[0103] The specific execution of the above process can be as follows:
[0104] First, define a global virtual machine pointer.
[0105] The step `javaVM* g_vm = nullptr;` defines a global virtual machine pointer and initializes it to a null pointer.
[0106] Then, during function loading, the pointer is saved to a global variable, resulting in a global variable pointer. The specific code is: `JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {`
[0107] g_vm = vm; store it as a global variable.
[0108] Next, we need to set up an interface for retrieving environment variables based on the virtual machine. This requires defining an interface that allows retrieving an environment variable from the virtual machine. This includes:
[0109] int getEnv(JNIEnv** env) { This step passes a pointer to JNIEnv to the interface, ensuring that the parameter is also the return value. It's advisable to initialize the parameter to null initially, so that if the retrieval fails, a null pointer is returned: *env = nullptr
[0110] jint ret = g_vm->GetEnv((void**)env, JNI_VERSION_1_4);
[0111] Finally, the corresponding environment variable `env` is retrieved via the virtual machine's `GetEnv` interface, and the result is stored in the parameter `env`. If the return value `ret == JNI_OK || ret == JNI_EDETACHED`, the creation was successful; otherwise, it failed.
[0112] Once the inter-platform interface function is determined based on the above steps, it can be called, and the environment variables can be input into the execution function according to the inter-platform interface function. The specific execution operation can be as follows:
[0113] JNIEnv* current() { This step is used to obtain environment variables.
[0114] JniData* jni = new WindwosJniData();
[0115] This step determines whether the current platform is Windows and calls the corresponding abstraction layer interface function for the Windows system platform.
[0116] JniData* jni = new LinuxJniData ();
[0117] This step is used to determine if the current platform is Linux, and then call the abstraction layer interface function corresponding to the Linux system platform.
[0118] `key = jni->makeKeyData();` This step is used to obtain the thread's key value; then, the thread's data is retrieved based on the key.
[0119] Pdata = jni->getData(key);
[0120] Next, we check if Pdata is empty. If it is empty, it means it hasn't been stored yet; otherwise, it has been stored before, and it represents a JNI environment variable. In this case, we can return it directly and use it. Specifically:
[0121] If (pdata != NULL){
[0122] Return pdata; This step is used to determine if the data has been stored previously, in which case it can be used directly.
[0123] }else {If it has not been stored.}
[0124] JNIEnv* env = null; This step is used to define an environment variable pointer.
[0125] getEnv(&env); then call the previous interface to create an environment variable pointer.
[0126] `Jni->setData(key, env);` uses the previously provided interface to store environment variables in thread-local data. It also returns the newly retrieved environment variable: `return env;`
[0127] Based on the above specific execution process, it can be seen that the environment variables obtained by the method described in this embodiment are created when each thread is called. After creation, they can be stored in the local storage variable of that thread. However, when the function is executed or called again, they can be directly obtained from the local storage variable of the thread, thus preventing the environment variable resources from being leaked due to creation every time.
[0128] Furthermore, the above process can be designed so that each thread obtains or creates a new environment variable from its own thread-local variable each time it is called, thus ensuring that each thread has its own environment variable.
[0129] In addition, when each thread accesses the JNI platform inter-transfer interface, it can only use its own environment variables and cannot use the environment variables of other threads, otherwise, mutual use will cause program exceptions.
[0130] Sixthly, in some embodiments, after calling the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0131] Check whether the return value of the abstract layer interface function is normal;
[0132] If an anomaly is detected, it is determined that the initial environment variables have been obtained. The initial environment variables are the environment variables obtained before the aforementioned environment variables were obtained.
[0133] Specifically, the above steps can be performed as follows:
[0134] Taking the Linux system platform as an example, in order to ensure the accuracy of the obtained data results, in practical applications, the code can be set as: LPVOID raw_data = pthread_getspecific(key); and the system's built-in function TlsGetValue can be called to retrieve the data raw_data stored in the thread based on the key value.
[0135] return reinterpret_cast<D*> (raw_data); This step is used to perform data transformation.
[0136] `Void setData(K key, D* data) {` This implements an interface for retrieving data within the target thread. Alternatively, `BOOL ret = pthread_setspecific(key, data)` can store the data in the thread and use the key for subsequent indexing. Here, the return value serves as the basis for determining whether an initial environment variable has already been stored. For example, if the return value `ret` is false, it means that an initial environment variable has already been stored, allowing for error reporting.
[0137] Thus, this embodiment of the invention also implements the function of error detection for the obtained data within the thread based on the code BOOL ret = TlsSetValue(key, data), which ensures the accuracy of the obtained data results at the code level and lays the foundation for the accuracy of the subsequent acquisition of environment variables.
[0138] According to the method described above, the target thread corresponding to the execution function is determined, wherein the execution function is the function called by the target thread; then, the required target environment variable is determined from a preset variable set based on the target thread; finally, the target environment variable is obtained and input into the execution function, thereby realizing the function of obtaining environment variables. In the above scheme, since the environment variable finally input into the execution function is obtained from the preset variable set based on the target thread corresponding to the execution function, it is ensured that the obtained environment variable is consistent with the corresponding thread, avoiding program exceptions caused by multiple threads using the same environment variable when calling the execution function, thus improving program stability. In addition, the above scheme can realize the function of obtaining the corresponding environment variable for different threads based on the method of the present invention. That is to say, when different execution functions are executed or called, the function of obtaining the environment variable of the corresponding thread can be performed based on this method, thereby avoiding the conventional process of passing environment variables between execution functions when multiple execution functions are called within a single thread cycle, simplifying the operation process, and ensuring the accuracy of the environment variable by obtaining the corresponding environment variable based on the target thread each time rather than passing it between functions. Furthermore, using the above solution, only one set of code corresponding to the above functions is needed to achieve the process of obtaining the corresponding environment variables when any type of execution function is called, thus eliminating the need to set the corresponding environment variables for multiple functions corresponding to a thread and passing code separately, reducing the workload of programmers.
[0139] The environmental variable acquisition method in the embodiments of this application has been described above. The environmental variable acquisition device in the embodiments of this application is described below.
[0140] Please see Figure 2 One embodiment of the environmental variable acquisition device in this application includes:
[0141] The first determining unit 21 can be used to determine the target thread corresponding to the execution function, wherein the execution function is a function called by the target thread;
[0142] The second determining unit 22 can be used to determine the required target environment variables from a preset variable set according to the target thread;
[0143] The input unit 23 can be used to obtain the target environment variable and input the target environment variable into the execution function.
[0144] Please see Figure 3 , Figure 3 A schematic diagram illustrating an embodiment of the electronic device provided in this application.
[0145] like Figure 3 As shown, this application embodiment provides an electronic device, including a memory 310, a processor 320, and a computer program 311 stored in the memory 310 and executable on the processor 320. When the processor 320 executes the computer program 311, it performs the following steps: determining the target thread corresponding to the execution function, wherein the execution function is a function called by the target thread; determining the required target environment variable from a preset variable set according to the target thread; obtaining the target environment variable and inputting the target environment variable into the execution function.
[0146] Optionally, in some embodiments, the aforementioned processor 320 is specifically used for: obtaining the target environment variable and inputting the target environment variable into the execution function, including:
[0147] Determine the system platform category of the executed function; the system platform category includes Windows platform and Linux platform.
[0148] The corresponding abstraction layer interface function is invoked according to the system platform category, and the environment variables of the target thread are obtained according to the abstraction layer interface function. The abstraction layer interface function is a special interface function for different system platforms built based on a preset interface abstraction layer.
[0149] The inter-platform interface function is invoked, and the environment variables are input into the execution function according to the inter-platform interface function, wherein the inter-platform interface function is a variable transfer function defined based on the JNI inter-platform transfer interface program.
[0150] Optionally, in some embodiments, the aforementioned processor 320 is specifically configured to: before invoking the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0151] A preset interface abstraction layer is set up, which consists of basic functions used to implement the corresponding abstraction layer functions of different system platforms;
[0152] The abstraction layer interface functions for each system platform are set according to the corresponding interface functions for each system platform and the system interface abstraction layer.
[0153] Optionally, in some embodiments, the aforementioned processor 320 is specifically configured to: before invoking the inter-platform interface function, the method further includes:
[0154] Obtain the virtual machine of the target thread and the environment variable interface of the target thread, wherein the virtual machine contains environment variable parameters, and the environment variable interface is obtained based on the virtual machine and a preset global variable pointer;
[0155] The inter-platform interface function is constructed based on the virtual machine and the environment variable interface.
[0156] Optionally, in some embodiments, the aforementioned processor 320 is specifically configured to: after invoking the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0157] Check whether the return value of the abstract layer interface function is normal;
[0158] If an anomaly is detected, it is determined that the initial environment variables have been obtained. The initial environment variables are the environment variables obtained before the aforementioned environment variables were obtained.
[0159] Optionally, in some embodiments, the aforementioned processor 320 is specifically used to: define a preset interface abstraction layer class and set a preset virtual destructor function in the preset interface abstraction layer class, wherein the preset virtual destructor function is a function used to transfer data content to the corresponding thread based on the handle identifier key value of the thread.
[0160] Optionally, in some embodiments, the aforementioned processor 320 is specifically used for: setting the abstraction layer interface function for each system platform according to the interface function corresponding to each system platform and the system interface abstraction layer, including:
[0161] When the system platform is a Windows platform, the TlsGetValue function is obtained, and the abstraction layer interface function is set according to the TlsGetValue function and the system interface abstraction layer, wherein the TlsGetValue function is a system-preset built-in API function in the Windows platform;
[0162] When the system platform is a Linux platform, the thread_key_create function is obtained, and the abstraction layer interface function is set according to the thread_key_create function and the system interface abstraction layer, wherein the thread_key_create function is a system-preset built-in API function in the Linux platform.
[0163] like Figure 4 As shown, this embodiment provides a computer-readable storage medium 400, on which a computer program 411 is stored. When the computer program 411 is executed by a processor, it performs the following steps: determining the target thread corresponding to the execution function, wherein the execution function is a function called by the target thread; determining the required target environment variable from a preset variable set according to the target thread; obtaining the target environment variable and inputting the target environment variable into the execution function.
[0164] Optionally, in one possible embodiment, when the computer program 411 is executed by the processor, it is specifically used for: obtaining the target environment variable and inputting the target environment variable into the execution function, including:
[0165] Determine the system platform category of the executed function; the system platform category includes Windows platform and Linux platform.
[0166] The corresponding abstraction layer interface function is invoked according to the system platform category, and the environment variables of the target thread are obtained according to the abstraction layer interface function. The abstraction layer interface function is a special interface function for different system platforms built based on a preset interface abstraction layer.
[0167] The inter-platform interface function is invoked, and the environment variables are input into the execution function according to the inter-platform interface function, wherein the inter-platform interface function is a variable transfer function defined based on the JNI inter-platform transfer interface program.
[0168] Optionally, in one possible embodiment, when the computer program 411 is executed by the processor, it is specifically configured to: before invoking the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0169] A preset interface abstraction layer is set up, which consists of basic functions used to implement the corresponding abstraction layer functions of different system platforms;
[0170] The abstraction layer interface functions for each system platform are set according to the corresponding interface functions for each system platform and the system interface abstraction layer.
[0171] Optionally, in one possible embodiment, when the computer program 411 is executed by the processor, the method further includes, prior to the invocation of the inter-platform interface function:
[0172] Obtain the virtual machine of the target thread and the environment variable interface of the target thread, wherein the virtual machine contains environment variable parameters, and the environment variable interface is obtained based on the virtual machine and a preset global variable pointer;
[0173] The inter-platform interface function is constructed based on the virtual machine and the environment variable interface.
[0174] Optionally, in one possible embodiment, when the computer program 411 is executed by the processor, it is specifically configured to: after calling the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes:
[0175] Check whether the return value of the abstract layer interface function is normal;
[0176] If an anomaly is detected, it is determined that the initial environment variables have been obtained. The initial environment variables are the environment variables obtained before the aforementioned environment variables were obtained.
[0177] Optionally, in one possible embodiment, when the computer program 411 is executed by the processor, it is specifically used for:
[0178] Define a preset interface abstraction layer class and set a preset virtual destructor function in the preset interface abstraction layer class. The preset virtual destructor function is a function used to pass data content to the corresponding thread based on the handle identifier key value.
[0179] Optionally, in one possible embodiment, when the computer program 411 is executed by the processor, it is specifically used for: setting the abstraction layer interface function for each system platform according to the interface function corresponding to each system platform and the system interface abstraction layer, including:
[0180] When the system platform is a Windows platform, the TlsGetValue function is obtained, and the abstraction layer interface function is set according to the TlsGetValue function and the system interface abstraction layer, wherein the TlsGetValue function is a system-preset built-in API function in the Windows platform;
[0181] When the system platform is a Linux platform, the thread_key_create function is obtained, and the abstraction layer interface function is set according to the thread_key_create function and the system interface abstraction layer, wherein the thread_key_create function is a system-preset built-in API function in the Linux platform.
[0182] It should be noted that the steps of the environmental variable acquisition method performed by the above-mentioned device, processor, and electronic device are all the same as those in the aforementioned method embodiments. Figure 1 The corresponding methods and steps are the same, and will not be repeated here.
[0183] In the several embodiments provided by this invention, it should be understood that the disclosed apparatus and methods can also be implemented in other ways. The apparatus embodiments described above are merely illustrative; for example, the flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram and / or flowchart, and combinations of blocks in block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
[0184] For example, the above division of units is merely a logical functional division; in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Another point is that the displayed or discussed mutual couplings, direct couplings, or communication connections may be indirect couplings or communication connections through some communication interfaces, devices, or units, and may be electrical, mechanical, or other forms.
[0185] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0186] In addition, the functional units in the embodiments provided by the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0187] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0188] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures. In addition, the terms "first", "second", "third", etc. are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.
[0189] Finally, it should be noted that the above embodiments are merely specific implementations of the present invention, used to illustrate the technical solutions of the present invention, and not to limit it. The scope of protection of the present invention is not limited thereto. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments within the scope of the technology disclosed in the present invention, or make equivalent substitutions for some of the technical features; and these modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention. All should be covered within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.< / typename> < / typename>
Claims
1. A method for obtaining environmental variables, characterized in that, Includes the following steps: Determine the target thread corresponding to the execution function, wherein the execution function is the function called by the target thread; The target environment variables are determined from a preset set of variables based on the target thread; Obtain the target environment variable and input the target environment variable into the execution function; The step of obtaining the target environment variable and inputting the target environment variable into the execution function includes: Determine the system platform category of the executed function; the system platform category includes Windows platform and Linux platform. The corresponding abstraction layer interface function is invoked according to the system platform category, and the environment variables of the target thread are obtained according to the abstraction layer interface function. The abstraction layer interface function is a special interface function for different system platforms built based on a preset interface abstraction layer. The system calls an inter-platform interface function and inputs the environment variables into the execution function according to the inter-platform interface function, wherein the inter-platform interface function is a variable transfer function defined based on the JNI inter-platform transfer interface program; Before invoking the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes: A preset interface abstraction layer is set up, which consists of basic functions used to implement the corresponding abstraction layer functions of different system platforms; The abstraction layer interface functions for each system platform are set according to the interface functions corresponding to each system platform and the system interface abstraction layer. Before invoking the inter-platform interface function, the method further includes: Obtain the virtual machine of the target thread and the environment variable interface of the target thread, wherein the virtual machine contains environment variable parameters, and the environment variable interface is obtained based on the virtual machine and a preset global variable pointer; The inter-platform interface function is constructed based on the virtual machine and the environment variable interface.
2. The method for obtaining environmental variables according to claim 1, characterized in that, After invoking the corresponding abstraction layer interface function according to the system platform category and obtaining the environment variables of the target thread according to the abstraction layer interface function, the method further includes: Check whether the return value of the abstract layer interface function is normal; If an anomaly is detected, it is determined that the initial environment variables have been obtained. The initial environment variables are the environment variables obtained before the aforementioned environment variables were obtained.
3. The method for obtaining environmental variables according to claim 2, characterized in that, The setting of the preset interface abstraction layer includes: defining a preset interface abstraction layer class and setting a preset virtual destructor function in the preset interface abstraction layer class, wherein the preset virtual destructor function is a function used to pass data content to the corresponding thread based on the handle identifier key value of the thread.
4. The method for obtaining environmental variables according to claim 3, characterized in that, The step of setting the abstraction layer interface function for each system platform according to the interface function corresponding to each system platform and the system interface abstraction layer includes: When the system platform is a Windows platform, the TlsGetValue function is obtained, and the abstraction layer interface function is set according to the TlsGetValue function and the system interface abstraction layer, wherein the TlsGetValue function is a system-preset built-in API function in the Windows platform; When the system platform is a Linux platform, the thread_key_create function is obtained, and the abstraction layer interface function is set according to the thread_key_create function and the system interface abstraction layer, wherein the thread_key_create function is a system-preset built-in API function in the Linux platform.
5. An electronic device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor, when executing the computer program stored in the memory, implements the steps of the environmental variable acquisition method as described in any one of claims 1-4.
6. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by the processor, it implements the steps of the environment variable acquisition method as described in any one of claims 1-4.
Citation Information
Patent Citations
Method and device for calling Java layer API (Application Program Interface) by native layer in Android system
CN104778075A