WO2018133713A1 - 一种线程管理方法及装置 - Google Patents

一种线程管理方法及装置 Download PDF

Info

Publication number
WO2018133713A1
WO2018133713A1 PCT/CN2018/072039 CN2018072039W WO2018133713A1 WO 2018133713 A1 WO2018133713 A1 WO 2018133713A1 CN 2018072039 W CN2018072039 W CN 2018072039W WO 2018133713 A1 WO2018133713 A1 WO 2018133713A1
Authority
WO
WIPO (PCT)
Prior art keywords
thread
instruction
target object
lock
synchronization lock
Prior art date
Application number
PCT/CN2018/072039
Other languages
English (en)
French (fr)
Inventor
徐伟刚
吴哲锋
孙哲
龚凯
李庆岩
李勇彪
廖彬
余超君
王帆
金跃
Original Assignee
阿里巴巴集团控股有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 阿里巴巴集团控股有限公司 filed Critical 阿里巴巴集团控股有限公司
Publication of WO2018133713A1 publication Critical patent/WO2018133713A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs

Definitions

  • the present application relates to the field of computer technologies, and in particular, to a thread management method and apparatus.
  • JavaScript is a dynamic, object-oriented, high-level programming language developed by the European Computer Manufacturers Association (ECMA), used by most websites, and supported by current major browsers.
  • ECMA European Computer Manufacturers Association
  • the execution of a JavaScript language program depends on the JavaScript virtual machine. That is to say, the execution of the JavaScript language program is not directly executed on the operating system, but needs to be executed by the JavaScript virtual machine.
  • the JavaScript virtual machine is responsible for executing JavaScript programs and scheduling resources for JavaScript programs.
  • the embodiment of the present application discloses a thread management method and device.
  • an embodiment of the present application provides a thread management method, including:
  • a create thread object instruction sent by a dynamic language layer where the create thread object instruction includes an interface for creating a thread object, where the thread object corresponds to a specified application;
  • the embodiment of the present application provides a thread management apparatus, including:
  • a receiving module configured to receive a thread object instruction sent by a dynamic language layer, where the thread object instruction includes an interface for creating a thread object, where the thread object belongs to a specified application;
  • a management module configured to: according to the interface for creating a thread object, invoke a method for creating a thread object provided by an object corresponding to the interface in the operating system, create a corresponding thread object; and start the thread object , get the corresponding thread.
  • the embodiment of the present application provides one or more computer readable media, where the readable medium stores instructions, when the instructions are executed by one or more processors, causing the communication device to perform the embodiments of the present application.
  • an apparatus provided by the embodiment of the present application includes: one or more processors; and one or more computer readable media, where the readable medium stores instructions, where the instructions are When the one or more processors are executed, the apparatus is caused to perform the method provided by the embodiments of the present application.
  • the thread object instruction may be created according to the method.
  • the interface invokes a method for creating a thread object provided by an object corresponding to the interface in the operating system, creates a corresponding thread object, and further starts the thread object to obtain a corresponding thread.
  • FIG. 1 is a schematic structural diagram of a dynamic language system in an embodiment of the present application.
  • FIG. 2 is a schematic diagram of a multi-thread architecture in an embodiment of the present application.
  • FIG. 3 is a schematic diagram of a process for creating a thread object according to an embodiment of the present application
  • FIG. 4 is a schematic diagram of a thread object management process in an embodiment of the present application.
  • FIG. 5 is a schematic flowchart of thread synchronization mutual exclusion using a synchronization lock according to an embodiment of the present application
  • FIG. 6 is a schematic flowchart of implementing synchronous mutual exclusion on a method by using a synchronization statement in an embodiment of the present application
  • FIG. 7 is a schematic flowchart of synchronizing a statement by using a synchronization statement in the embodiment of the present application.
  • FIGS. 8a and 8b are schematic diagrams showing the layout of a JavaScript object in the embodiment of the present application.
  • FIG. 9 is a schematic flowchart of synchronization and mutual exclusion for an object according to an embodiment of the present disclosure.
  • FIG. 10 is a schematic structural diagram of a thread management apparatus according to an embodiment of the present application.
  • FIG. 11 is a schematic structural diagram of an apparatus according to an embodiment of the present application.
  • references to "an embodiment”, “an embodiment”, “an illustrative embodiment” or the like in the specification are intended to mean that the described embodiments may include specific features, structures, or characteristics, but each embodiment may or may not necessarily include a particular Feature, structure or characteristic. Moreover, such phrases are not necessarily referring to the same embodiments. Further, it is to be understood that the specific features, structures, or characteristics may be combined with other embodiments, whether explicitly described or not, in conjunction with the embodiments. In addition, it should be understood that items included in the list in the form of "at least one of A, B, and C” may represent (A); (B); (C); (A and B); (A and C) ; (B and C); or (A, B and C). Similarly, items listed in the form of "at least one of A, B or C” may represent (A); (B); (C); (A and B); (A and C); (B and C) or (A, B and C).
  • the disclosed embodiments can be implemented in hardware, firmware, software, or any combination thereof.
  • the disclosed embodiments can also be implemented as instructions carried or stored by one or more transitory or non-transitory machine readable (eg, computer readable) storage media, which can be read by one or more processors And execution.
  • a machine-readable storage medium may be embodied in any storage device, mechanism or other physical structure for storing or transmitting information in a machine readable form (eg, a volatile or nonvolatile memory, media disk or other medium). ).
  • the embodiments of the present application provide a dynamic language multi-thread programming capability at the language level, enabling developers to use the capability of multi-thread programming to increase program performance and application range.
  • Dynamic language is a language category in computer programming languages. It is a type of language that can dynamically change types and structures at runtime. Functions and attributes can be added, modified, and deleted at runtime. For example, JavaScript, Python, Ruby, etc. are all dynamic languages. Dynamic languages can be run without compiling, and need to be supported by the runtime environment at runtime. This environment is called the runtime environment and contains all the elements needed for dynamic language operations, such as Java virtual machines, JavaScript virtual machines, and so on.
  • the JavaScrip virtual machine can also be called a JavaScript engine, which can be interpreted as a managed runtime environment for JavaScript, referred to as a runtime environment, or a virtual Execution Environment. It can ensure the security of JavaScript programs in memory allocation, access and release through forced automatic memory management, with mandatory type system security guarantee, and array out-of-bounds detection. It can also manage the rights of code execution and visible. Sexual restrictions, etc., to ensure that the code is only executed within the permissions of its application.
  • Objects can be defined with properties and methods, and properties and methods (functions) can be encapsulated in objects. In memory, these objects are actually memory blocks that hold data and runnable methods (functions).
  • Thread object An instance object of a thread class. Thread objects encapsulate some information about threads (such as methods (functions) that include thread execution). Methods (functions) in a thread object can be run by other threads. Take JavaScript as an example. Thread objects can be obtained by inheriting the thread class or by implementing the runnable interface. For example, a new class is derived from thread, and attributes and methods (functions) are added to it, and the run() method can be overwritten. A new thread object of a derived class. Among them, the run() method contains the code to be executed by the thread.
  • Thread A thread is the execution path of a method defined in an object. That is, a thread is an execution process of code. During this execution, a thread can execute a method (function) defined by a thread object representing it, or it can execute Methods (functions) in other objects. Taking JavaScript as an example, when a thread object is created by inheriting the thread class, the start() method (function) is executed, thereby starting the thread corresponding to the thread object.
  • the embodiment of the present application enables the dynamic language virtual machine to support multi-threading, and provides a corresponding multi-threaded development language interface at the dynamic language layer. Based on the multi-threaded development capability provided by the developer in the language level of the developer, the developer can use the interface provided by the embodiment of the present application to develop a multi-threaded program, such as thread creation, operation, and communication.
  • the embodiment of the present application broadens the application range of the dynamic language by providing the capability of multi-threading, such as background event processing and foreground page rendering, and the thread processing can more efficiently develop an interactive program; and at the same time, multi-threading can better utilize multi-core processing.
  • the computing resources of the device in the application scenario of a large number of calculations such as concurrent data processing, the embodiment of the present application can improve the running performance of the program.
  • the embodiment of the present application provides a multi-thread management solution to implement multi-thread development capability.
  • the multi-thread management may specifically include: thread creation, thread synchronization, thread destruction, and the like.
  • the embodiment of the present application can be applied to a dynamic language based on a single thread mechanism, so that a dynamic language based on a single thread mechanism implements multithreading capability.
  • a device for implementing a thread management method may be referred to as a thread management device.
  • the dynamic language as the JavaScript language as an example, the device can be a JavaScript virtual machine.
  • a thread management device such as a JavaScript virtual machine
  • a dynamic language layer such as a JavaScript language layer
  • an operating system such as a JavaScript language layer
  • the dynamic language program is implemented by a dynamic language code, corresponding to the dynamic language layer.
  • the multi-thread operation of the dynamic language program runs on the thread management device, and the thread management device invokes the underlying multi-thread library in the operating system to perform corresponding multi-thread operations.
  • the operating system running by the thread management device has a set of underlying thread libraries.
  • the thread management device can manage the dynamic language thread
  • FIG. 2 shows the multi-thread architecture managed by the thread management device provided by the embodiment of the present application. As shown in Figure 2, each thread has a different stack, and multiple threads can share one heap (Heap).
  • Heap heap
  • the stack is the memory space reserved for the execution thread. Each thread has its own separate stack and private data area, which is isolated from other threads. When the function is called, the top of the stack is reserved for data such as local variables. When the function is executed, the block is released and used again in the next function call.
  • a heap is a memory space reserved for dynamic allocation.
  • the stored variable can be an object.
  • the variable is a reference type. That is, the variable actually stored in the variable is a pointer to another location. Every thread has a stack, but each application usually has only one heap (although there are cases where multiple heaps are allocated for different types of memory).
  • the embodiment of the present application provides a multi-thread management scheme based on the above architecture, which may include: creation of a thread object, release of a thread object, synchronization of a thread object, synchronization of objects other than a thread object, and the like. These processes are described in detail below.
  • a method (function) for creating a thread object is defined in the operating system layer, and the thread management device provides the calling interface of the method to the dynamic language layer, so that the developer calls the interface to create the thread object.
  • the interface provided by the thread management device to the dynamic language layer may specifically be an Application Programming Interface (API), and more specifically, the API may include information such as a function name and a parameter.
  • API Application Programming Interface
  • the thread management device can provide a plurality of APIs to the dynamic language layer, and each API corresponds to a corresponding method (function).
  • FIG. 3 shows a process of creating a thread object in the embodiment of the present application. As shown in the figure, the process may include:
  • the thread management apparatus receives a create thread object instruction transmitted by the dynamic language layer, and the create thread object instruction includes an interface for creating a thread object belonging to the specified application.
  • the developer can write dynamic language application code according to a thread object creation interface provided to the dynamic language layer, and when the application code is executed, the thread management device parses the application code to obtain the The thread object's interface, so that the thread object creation method (function) is executed through the subsequent steps, and the thread object is created.
  • the thread management device invokes an interface for creating a thread object (function) provided by an object corresponding to the interface in the operating system according to the interface for creating the thread object, and creates a corresponding thread object.
  • the JavaScript virtual machine may create the object according to an object provided by the operating system for creating a thread object (hereinafter referred to as an obj object for convenience) An instance of an object, created to get a thread object.
  • the obj object is an object provided by an operating system, and the obj object is encapsulated with attributes and methods, such as a new method.
  • a new thread object can be created by executing the new method on the obj object.
  • the properties and methods in the obj object can be inherited by the new thread object, and attributes and methods can be added to the new thread object.
  • the created thread object includes a run method (function), which is a method (function) to be executed when the thread starts, and the run method (function) can be performed by the developer according to the function to be implemented or the operation to be performed. customize.
  • the thread management device creates and registers an internal thread object management structure to manage the resources owned by the corresponding thread.
  • the thread object management structure can be as shown in FIG. 2.
  • the pointer of the newly created thread object may be stored in a heap, and data such as variables of the thread may be stored in a stack of the thread.
  • the thread management device starts the created thread object and obtains the corresponding thread.
  • the thread handle corresponding to the thread object may be provided to the application, and the application program operates the thread corresponding to the call.
  • the JavaScript virtual machine can provide a thread handle to the JavaScript language layer, so that the developer can operate the corresponding thread through the thread handle. Threads are created after the thread object is started. A thread can have one or more handles. Different handles can have different operation permissions.
  • the JavaScript virtual machine can also provide other information that can indicate the thread to the JavaScript language layer for use when the developer manipulates the thread.
  • the corresponding thread can be operated through the thread handle, such as executing the thread, waiting for the thread to end, reclaiming the thread, and the like.
  • the process of starting a thread may include: the thread management device parsing the dynamic language application code, obtaining an instruction for starting a thread object based on the thread handle, and the thread management device, according to the instruction, calling a startup method provided by the corresponding thread object (such as a start method) ) to start the thread object.
  • a startup method provided by the corresponding thread object (such as a start method)
  • the thread management device starts the thread through the operating system.
  • the thread management device invokes the method indicated by the attribute value according to the attribute value of the running attribute (such as the run attribute) provided by the thread object (the attribute value of the attribute is the name of the method).
  • the thread management device when the thread management device obtains a method (function) based on the dynamic language program code encapsulation according to the attribute value of the run attribute, since the operating system does not directly connect the dynamic language layer and cannot parse the method, the thread management device is responsible for interpreting the method. Get the interface provided by the operating system layer and call the interface to execute the corresponding method provided by the operating system.
  • the process of waiting for the end of the thread may include: the thread management device parses the dynamic language application code, and obtains an instruction for waiting for the thread to end based on the thread handle, and the thread management device, according to the instruction, executes the thread object to wait for the thread to end.
  • the method (such as the join method) to wait for the end of the thread.
  • the thread management device interprets the method, obtains an interface provided by the operating system layer, and executes the corresponding join method by calling the interface.
  • the process of stopping the thread may include: the thread management device parsing the dynamic language application code, and obtaining an instruction for stopping the thread based on the thread handle, and the thread management device executes the method for stopping the thread in the thread object according to the instruction (such as the sleep method) to wait for the thread to stop.
  • the sleep method since the operating system does not directly connect to the dynamic language layer, the sleep method cannot be parsed, so the thread management device interprets and executes the method.
  • the JavaScript virtual machine parses the JavaScript application code based on the JavaScript language layer to obtain a program statement for indicating a new instance method (new method) in the call obj object, and the JavaScript virtual machine is based on
  • the API in the program statement (such as the function name of the new method) calls the thread library interface provided by the operating system, and creates an instance of the object based on the obj object provided by the operating system.
  • the JavaScript virtual machine encapsulates the attributes and methods of the obj object, creates a thread object, and returns the thread handle to the JavaScript language layer.
  • the JavaScript virtual machine analyzes the JavaScript application code based on the JavaScript language layer, and obtains a program statement for calling the startup method (start method) of the corresponding thread object (identifying the corresponding thread object by the thread handle).
  • start method the startup method
  • the JavaScript virtual machine calls the thread library interface provided by the operating system according to the API in the program statement (such as the function name of the start method)
  • the corresponding thread is created by the operating system, and then the thread is started.
  • the JavaScript virtual machine executes the method indicated by the attribute value according to the attribute value of the run attribute inherited by the thread object from the obj object (the attribute value of the attribute is the name of the method).
  • the JavaScript virtual machine analyzes the JavaScript application code based on the JavaScript language layer, and obtains a wait thread end method (join method) for indicating the thread object corresponding to the call (the thread object is used to identify the corresponding thread object)
  • the JavaScript virtual machine calls the thread library interface provided by the operating system according to the API in the program statement (such as the function name of the join method), and executes the corresponding method for the thread corresponding to the thread handle through the operating system.
  • the JavaScript virtual machine analyzes the JavaScript application code based on the JavaScript language layer, and obtains a thread end method (sleep method) for indicating the thread object corresponding to the call (the thread object is used to identify the corresponding thread object).
  • the JavaScript virtual machine calls the thread library interface provided by the operating system according to the API in the program statement (such as the function name of the sleep method), and executes the corresponding method for the thread corresponding to the thread handle through the operating system.
  • multiple threads can be created, and each thread can be managed to implement multi-thread management.
  • the thread management device encapsulates the thread's capabilities (including attributes and methods) from the operating system, and encapsulates these capabilities into the thread object, and provides the handle of the thread object to the dynamic language layer.
  • the thread management device creates a thread object through the operating system, and then starts the thread object to get the corresponding thread.
  • the thread management device provides the developer with the ability to manipulate threads by encapsulating thread objects and exposing them to a dynamic language layer.
  • Thread synchronization means that when a thread is reading or writing to the memory, other threads cannot read or write to the memory address until the thread completes the read and write operations, and other threads can read and write the memory address. Operation while other threads are in a wait state.
  • the thread synchronization is implemented by using a mutex object, and only the thread that obtains the synchronization lock has the right to access the public resource. Because there is only one synchronization lock, it is guaranteed that public resources will not be accessed by multiple threads at the same time. Synchronous locks not only enable secure sharing of common resources of the same application, but also enable secure sharing of common resources of different applications.
  • the above-mentioned synchronization lock is a global object provided by the thread management device to the dynamic language layer, and is used for thread synchronization of a certain piece of code.
  • developers can implement mutual exclusion between threads.
  • the creation process of the synchronization lock is similar to the creation process of the thread object, and the thread management device can execute the method for creating the synchronization lock in the operating system through the interface provided by the operating system by calling the interface provided by the dynamic language layer for creating the synchronization lock. , create a sync lock.
  • the created synchronization lock is a global object. Threads can use this synchronization lock for synchronization. For example, when thread 1 reads and writes a set of global data, it can use this synchronization lock if it does not want other threads to read or write.
  • Lock identification attribute This attribute value is used to uniquely identify the synchronization lock, a synchronization lock corresponds to a memory area, and different memory areas correspond to different synchronization locks;
  • Locking method The thread that obtains the thread object can execute the method, and the method can execute the method to lock the memory area indicated by the attribute value of the lock identifier attribute, and the locked memory area can only obtain the thread of the thread object. Read and write operations, other threads do not allow read and write operations on the memory area;
  • Unlock method The thread that obtains the thread object can execute the method, and the method can be executed to unlock the memory area indicated by the attribute value of the lock identifier attribute.
  • FIG. 5 shows a schematic diagram of thread 1 and thread 2 using a synchronization lock to implement a mutually exclusive operation.
  • thread 1 and thread 2 need to calculate global data at the same time, and the developer hopes that two threads do not modify the global data at the same time.
  • Developers can use sync locks for thread synchronization.
  • the thread 1 uses the synchronization lock to protect the memory area to be protected
  • the memory area is locked before the memory area is read and written. After the memory area is read and written, the memory area is unlocked.
  • the thread 2 uses the synchronization lock to protect the memory area to be protected, the memory area is locked before the memory area is read and written. After the memory area is read and written, the memory area is unlocked. .
  • the thread 1 when the thread 1 needs to read and write the global data, the thread 1 requests a thread management device (such as a JavaScript virtual machine) to acquire a lock object of a memory area in which the global data is located, and the thread management device determines the memory. If the lock state of the area is not locked, the thread management device locks the synchronization to the thread 1, and after the thread 1 obtains the synchronization lock, the lock method is performed on the memory area corresponding to the synchronization lock (lock method).
  • a thread management device such as a JavaScript virtual machine
  • the state of the memory area becomes a locked state, and the thread 1 performs a read and write operation on the memory area locked by the thread, and after the read and write operation is completed, an unlocking method is performed on the memory area, and the memory area is The status changes to an unlocked state. If the thread 1 requests the thread management device to acquire the synchronization lock, the thread management device determines that the memory area corresponding to the synchronization lock is currently in a locked state, indicating that the memory area is currently being read and written by another thread, so the memory area is not pasted. The synchronization lock is given to thread 1, and thread 1 is in a wait state. When the state of the memory area becomes unlocked, the thread management device locks the synchronization corresponding to the memory area to the thread 1. Similarly, the process of thread 2 reading and writing data based on the synchronization lock is similar.
  • the thread management device is responsible for the thread created by the operating system. Explain and execute the method.
  • the thread management apparatus is responsible for interpreting and executing the method in the thread created by the operating system.
  • a method or dynamic language program statement may be declared to indicate that the declared method or dynamic language program statement requires synchronous mutual exclusion execution, ie, a thread executes the method or a dynamic language program statement. Other threads cannot execute the same method or dynamic language program statement.
  • the JavaScript virtual machine receives an instruction that thread 1 invokes method a;
  • the JavaScript virtual machine determines whether there are other threads currently executing the method, if any, then proceeds to S603 in Figure 6, otherwise proceeds to S604 in Figure 6;
  • the JavaScript virtual machine temporarily does not execute the method a in the thread 1, and when the other thread (such as the thread 2) executes the completion method a, it proceeds to S604 in FIG. 6:
  • the JavaScript virtual machine executes method a in thread 1.
  • thread 1 and other threads need to execute a statement (such as assigning a global variable)
  • a statement such as assigning a global variable
  • thread 1 and thread 2 need to execute a statement (such as assigning a global variable)
  • the statement is declared to be synchronously mutually exclusive, thus, thread 1 and thread 2
  • the statement cannot be executed at the same time (for example, the global variable cannot be assigned at the same time), and only after one thread executes the statement, another thread can execute the statement.
  • the specific implementation process may be as shown in FIG. 7, and may include:
  • the JavaScript virtual machine receives an instruction that thread 1 executes the statement
  • the way to declare can be either an explicit declaration or an implicit declaration. Among them, in some examples, if you add a synchronization keyword to declare before and after the statement that calls the method, it is called an explicit declaration. Similarly, if you add a synchronization keyword before and after the dynamic language program statement, Also known as an explicit statement.
  • this declaration is called an implicit declaration
  • the thread management device provides the operating system by calling
  • the operating system may perform a synchronous mutual exclusion operation on the execution process of the method according to the synchronization keyword in the program code of the method; likewise, if the dynamic language program statement is placed in the range of the synchronization keyword Inside, it is also called implicit declaration.
  • multiple threads may need to read and write to the same object at the same time.
  • multiple threads write to the same object at the same time, due to problems in CPU time scheduling, writing data will Being overwritten multiple times, causing an error to occur.
  • Synchronous mutex technology is needed to ensure that at most one thread can read and write to a specific JavaScript object at any time.
  • the thread management apparatus provides the language layer with the ability to synchronize objects. It is possible to pre-specify the mutual exclusion of reading and writing of one or some objects.
  • the object synchronization is implemented by the mutex object mechanism. Only the thread that obtains the synchronization lock has the right to read and write the specified object.
  • the creation process of the synchronization lock is similar to the foregoing embodiment, and the thread management device can execute the method for creating a synchronization lock in the operating system through an interface provided by the operating system by calling an interface provided by the dynamic language layer for creating a synchronization lock. Create a sync lock.
  • Lock ID attribute This attribute value is used to uniquely identify the sync lock.
  • a sync lock corresponds to a memory area (here is a memory area corresponding to an object), and different objects correspond to different sync locks;
  • Locking method The thread that obtains the thread object can execute the method.
  • the method can be executed to lock the object indicated by the attribute value of the lock identifier attribute (or the memory area where the object is located), and the locked object can only be locked.
  • the thread that gets the thread object performs read and write operations, and other threads do not allow read and write operations on the object;
  • Unlock method The thread that obtains the thread object can execute the method, and the method can be executed to unlock the object indicated by the attribute value of the lock identifier attribute.
  • Method 1 Objects can be divided into different types.
  • ConcurrentArray, ConcurrentSet, and ConcurrentMap are provided.
  • the three objects have ensured inter-thread security on the internal virtual machine implementation.
  • the operations of the three objects do not require the developer to use synchronous keyword synchronization at the JavaScript layer;
  • Method 2 Synchronize keywords can be used to declare objects that require synchronous mutex operations.
  • the layout of the object is specified by the thread management device.
  • the layout of an object can be understood as the storage structure of the object in memory.
  • the layout of the JavaScript object may include an object header portion and an object instance portion, and the object header portion may store information such as an object name, and the object instance portion may store the object.
  • an information field for identifying the state of the synchronization lock is set in the layout header of the JavaScript object, and the information field may be one or more bits, as shown in FIG. 8b. Different values of the information field can identify the lock state of the JavaScript object. For example, if the value of the information field is 0, indicating that the JavaScript object is not locked, the value of the information field is 1, indicating that the JavaScript object has been locked.
  • the thread management apparatus when the thread management apparatus receives an instruction to read or write an object by the thread 1 (S901 in FIG. 9), it can be determined whether the object is an object designated to perform mutual synchronization (FIG. 9). S902), if yes, request to acquire the synchronization lock (S903 in FIG. 9), if the synchronization lock is acquired, the synchronization lock is given to the thread 1, and after receiving the synchronization lock, the thread 1 locks the object. For example, the lock status flag in the layout of the object is set to 1, and the object is read and written, and after the read and write is completed, the object is unlocked, for example, setting a lock status flag in the layout of the object. The bit value is 0 (S904 in Figure 9).
  • the synchronization lock is not obtained, the current object is being read or written. In this case, after the thread management device waits for the object to be read and written, the synchronization lock is given. Thread 1, the operation after the thread 1 acquires the synchronization lock is the same as previously described (S905 in Fig. 9); if the object is not designated as the synchronization mutual exclusion, the object is read and written (S906 in Fig. 9) ).
  • the thread management device in the process of locking or unlocking an object, is responsible for interpreting the thread created by the operating system because the operating system does not directly connect to the dynamic language layer and cannot resolve the locking method or the unlocking method. And execute the method.
  • the thread management device in the process of reading and writing an object by a thread, since the operating system does not directly interface with the dynamic language layer and cannot read and write the memory area, the thread management device is responsible for interpreting and executing the method in the thread created by the operating system.
  • the embodiment of the present application further provides a thread management device, which may be a dynamic language virtual machine in the foregoing embodiment, and more specifically, may be a JavaScript virtual machine.
  • FIG. 10 is a schematic structural diagram of a thread management apparatus according to an embodiment of the present disclosure.
  • the apparatus may include: a receiving module 1001 and a management module 1002, where:
  • the receiving module 1001 is configured to receive a create thread object instruction sent by a dynamic language layer, where the create thread object instruction includes an interface for creating a thread object, where the thread object belongs to a specified application;
  • the management module 1102 is configured to: according to the interface for creating a thread object, invoke a method for creating a thread object provided by an object corresponding to the interface in the operating system, create a corresponding thread object, and start the thread. Object, get the corresponding thread.
  • the application corresponds to a plurality of threads, each of the plurality of threads has a separate stack and a private data area, and the multiple threads share a heap; wherein, the stack of one thread is The memory space occupied by the thread, the heap is the memory space shared by all threads of the application.
  • the receiving module is further configured to: receive an instruction for reading and writing global data sent by the dynamic language layer; the management module 1002 may be further configured to: request to obtain the first used to read and write the global variable a synchronization lock, the first synchronization lock is a global object; the locking method of the obtained first synchronization lock is used to lock the global variable, the global variable is read and written, and is called after the reading and writing is completed.
  • the unlocking method of the first synchronization lock unlocks the global variable.
  • the receiving module 1001 is further configured to: receive an instruction sent by the dynamic language layer to execute the specified method provided by the specified object.
  • the management module 1002 is further configured to: determine whether the specified method is declared to perform synchronous mutual exclusion; if yes, request to obtain a second synchronization lock corresponding to the specified method, where the second synchronization lock is a global object; The locking method of the second synchronization lock locks the specified method, executes the specified method, and invokes the unlocking method of the second synchronization lock after the execution is completed to unlock the specified method.
  • the receiving module 1001 is further configured to: receive an instruction for reading and writing to the target object.
  • the management module 1002 is further configured to: if the type of the target object belongs to a specified type or is declared to be synchronously mutually exclusive, Then requesting to obtain a third synchronization lock for reading and writing the target object, the third synchronization lock is a global object; and calling the obtained locking method of the third synchronization lock to lock the target object, Reading and writing the target object, and unlocking the target object by calling the unlocking method of the third synchronization lock after the reading and writing is completed.
  • the management module 1002 is specifically configured to: acquire an object status flag bit in a memory area for storing object header information in a memory area where the target object is located, if the value of the object status flag bit indicates If the target object is not locked, a third synchronization lock for reading and writing the target object is obtained.
  • the management module 1002 is specifically configured to: when the target object is locked, set a value of the status flag to indicate that the target object has been locked.
  • the management module 1002 is specifically configured to: when the target object is unlocked, set a value of the status flag to indicate that the target object is not locked.
  • the management module is further configured to: provide a handle corresponding to the created thread object to the dynamic language layer.
  • the management module 1002 is specifically configured to: receive an instruction sent by the dynamic language layer to start the thread object, where the instruction of the startup thread object includes a handle corresponding to the thread object; and according to the instruction of the startup thread object
  • the startup method provided by the thread object corresponding to the handle is invoked by the operating system, and the startup method is configured to execute the method indicated by the attribute value according to the attribute value of the running attribute of the thread object.
  • the receiving module 1001 is further configured to: receive an instruction that is sent by the dynamic language layer and wait for the thread to end, and the instruction that ends the waiting thread includes a handle corresponding to the thread object.
  • the management module 1002 is further configured to wait for the end of the thread operation by calling, by the operating system, a method for waiting for the end of the thread provided by the thread object corresponding to the handle according to the received instruction of the waiting thread.
  • the receiving module 1001 is further configured to: receive an instruction for stopping the thread sent by the dynamic language layer, where the instruction for stopping the thread includes a handle corresponding to the thread object.
  • the management module 1002 is further configured to: stop, according to the received instruction of stopping the thread, a method for stopping the thread provided by the thread object corresponding to the handle by the operating system to stop the thread running.
  • the embodiment of the present application further provides one or more computer readable media having instructions stored thereon, when the instructions are executed by one or more processors, causing the communication device to perform the foregoing The thread management method described in the embodiment.
  • the embodiment of the present application further provides an apparatus 1100, which can implement the process described in the foregoing embodiment.
  • FIG. 11 exemplarily illustrates an example apparatus 1100 that may include one or more processors 1102 coupled to at least one processor 1102, non-volatile memory (non-), in accordance with various embodiments.
  • a volatile memory (NMV)/memory 1104 is coupled to system control logic 1101, and a network interface 1106 is coupled to system control logic 1101.
  • Processor 1102 can include one or more single core processors or multi-core processors.
  • Processor 1102 can comprise any combination of general purpose processors or special purpose processors (eg, image processors, application processor baseband processors, etc.).
  • System control logic 1101 in one embodiment may include any suitable interface controller to provide any suitable interface to at least one of processors 1102, and/or to any suitable communication in communication with system control logic 1101. Any suitable interface to the device or component.
  • System control logic 1101 in one embodiment may include one or more memory controllers to provide an interface to system memory 1103.
  • System memory 1103 is used to load and store data and/or instructions.
  • corresponding device 1100 in one embodiment, system memory 1103 can include any suitable volatile memory.
  • NVM/memory 1104 can include one or more tangible, non-transitory computer readable media for storing data and/or instructions.
  • NVM/memory 1104 can include any suitable non-volatile storage device, such as one or more hard disk devices (HDDs), one or more compact disks (CDs), and/or one or more Digital versatile disk (DVD).
  • HDDs hard disk devices
  • CDs compact disks
  • DVD Digital versatile disk
  • the NVM/memory 1104 can include storage resources that are physically part of the device that the system is installed on or can be accessed, but is not necessarily part of the device.
  • NVM/memory 1104 can be accessed by the network via network interface 1106.
  • System memory 1103 and NVM/memory 1104 can each include a copy of a temporary or persistent instruction 1110.
  • the instructions 1110 can include instructions that, when executed by at least one of the processors 1102, cause the apparatus 1100 to implement one or a combination of the methods described in FIGS. 3-7, 9.
  • instructions 1110 or hardware, firmware, and/or software components may additionally/alternatively be placed in system control logic 1101, network interface 1106, and/or processor 1102.
  • Network interface 1106 can include a receiver to provide device 1100 with a wireless interface to communicate with one or more networks and/or any suitable device.
  • Network interface 1106 can include any suitable hardware and/or firmware.
  • Network interface 1106 can include multiple antennas to provide a multiple input multiple output wireless interface.
  • network interface 1106 can include a network adapter, a wireless network adapter, a telephone modem, and/or a wireless modem.
  • At least one of the processors 1102 can be packaged with logic for one or more controllers of system control logic. In one embodiment, at least one of the processors may be packaged with logic for one or more controllers of system control logic to form a system level package. In one embodiment, at least one of the processors can be integrated on the same die as the logic of one or more controllers for system control logic. In one embodiment, at least one of the processors can be integrated on the same die as the logic of one or more controllers for system control logic to form a system chip.
  • the device 1100 can further include an input/output device 1105.
  • the input/output device 1105 can include a user interface intended to enable a user to interact with the device 1100, can include a peripheral component interface designed to enable peripheral components to interact with the system, and/or can include sensors for determining environmental conditions And/or location information about the device 1100.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Devices For Executing Special Programs (AREA)
  • Stored Programmes (AREA)

Abstract

一种线程管理方法及装置。所述方法包括,线程管理装置接收动态语言层发送的创建线程对象指令,所述创建线程对象指令中包括用于创建线程对象的接口(S301);线程管理装置根据所述用于创建线程对象的接口,调用操作系统中所述接口对应的对象所提供的用于创建线程对象的方法,创建得到对应的线程对象(S302);线程管理装置启动所述线程对象,得到对应的线程(S303)。上述方法基于多层架构提供了通用的线程创建过程,为多线程的创建与管理提供了可能性。

Description

一种线程管理方法及装置
本申请要求2017年01月23日递交的申请号为201710062788.2、发明名称为“一种线程管理方法及装置”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
技术领域
本申请涉及计算机技术领域,尤其涉及一种线程管理方法及装置。
背景技术
JavaScript是一种动态类型、面向对象的高级编程语言,由欧洲电脑制造商协会(ECMA)制定的标准,被大多数网站所使用,也被目前的主流浏览器支持。JavaScript语言程序的执行依赖于JavaScript虚拟机,也就是说,JavaScript语言程序的执行并不是直接在操作系统上运行,而是需要通过JavaScript虚拟机来执行。
JavaScript虚拟机负责执行JavaScript程序以及为JavaScript程序调度资源。
发明内容
本申请实施例公开了一种线程管理方法及装置。
第一方面,本申请实施例提供了一种线程管理方法,包括:
接收动态语言层发送的创建线程对象指令,所述创建线程对象指令中包括用于创建线程对象的接口,所述线程对象对应指定的应用程序;
根据所述用于创建线程对象的接口,调用操作系统中所述接口对应的对象所提供的用于创建线程对象的方法,创建得到对应的线程对象;
启动所述线程对象,得到对应的线程。
第二方面,本申请实施例提供了一种线程管理装置,包括:
接收模块,用于接收动态语言层发送的创建线程对象指令,所述创建线程对象指令中包括用于创建线程对象的接口,所述线程对象属于指定的应用程序;
管理模块,用于根据所述用于创建线程对象的接口,调用操作系统中所述接口对应的对象所提供的用于创建线程对象的方法,创建得到对应的线程对象;并启动所述线程对象,得到对应的线程。
第三方面,本申请实施例提供了一个或多个计算机可读介质,所述可读介质上存储有指令,所述指令被一个或多个处理器执行时,使得通信设备执行本申请实施例提供的 方法。
第四方面,本申请实施例提供的一种装置,包括:一个或多个处理器;以及,一个或多个计算机可读介质,所述可读介质上存储有指令,所述指令被所述一个或多个处理器执行时,使得所述装置执行本申请实施例提供的方法。
本申请的上述实施例中,在接收到动态语言层发送的创建线程对象指令后,由于所述创建线程对象指令中包括用于创建线程对象的接口,因此可以根据所述用于创建线程对象的接口,调用操作系统中所述接口对应的对象所提供的创建线程对象的方法,创建得到对应的线程对象,并进而启动所述线程对象,得到对应的线程。从而基于多层架构提供了通用的线程创建过程,为多线程的创建与管理提供了可能性。
附图说明
图1为本申请实施例中动态语言系统架构示意图;
图2为本申请实施例中多线程架构的示意图;
图3为本申请实施例提供的线程对象的创建流程示意图;
图4为本申请实施例中线程对象管理流程示意图;
图5为本申请实施例中采用同步锁进行线程同步互斥的流程示意图;
图6为本申请实施例中采用同步声明实现对方法进行同步互斥的流程示意图;
图7为本申请实施例中采用同步声明实现对语句进行同步互斥的流程示意图;
图8a和图8b为本申请实施例中的JavaScript对象的布局示意图;
图9为本申请实施例提供的针对对象进行同步互斥的流程示意图;
图10为本申请实施例提供的线程管理装置的结构示意图;
图11为本申请实施例提供的装置的结构示意图。
具体实施方式
虽然本申请的概念易于进行各种修改和替代形式,但是其具体实施例已经通过附图中的示例示出并且将在本文中详细描述。然而,应当理解,没有意图将本申请的概念限制为所公开的特定形式,而是相反,意图是覆盖与本申请以及所附权利要求一致的所有修改、等同物和替代物。
说明书中对“一个实施例”、“实施例”、“说明性实施例”等的引用,指示所描述的实施例可包括特定特征、结构或特性,但是每个实施例可以或可以不必包括特定特 征、结构或特性。此外,这样的短语不一定指的是相同的实施例。进一步地,认为在本领域技术人员的知识范围内,当结合实施例描述特定特征、结构或特性时,结合无论是否明确描述的其它实施例影响这样的特征,结构或特性。另外,应当理解,以“A,B和C中的至少一个”的形式包括在列表中的项目可以表示(A);(B);(C);(A和B);(A和C);(B和C);或(A,B和C)。类似地,以“A,B或C中的至少一个”的形式列出的项目可以表示(A);(B);(C);(A和B);(A和C);(B和C)或(A,B和C)。
在一些情况下,所公开的实施例可以在硬件、固件、软件或其任何组合中实现。所公开的实施例还可以被实现为由一个或多个暂时性或非暂时性机器可读(例如,计算机可读)存储介质携带或存储的指令,其可以由一个或多个处理器读取和执行。机器可读存储介质可以体现为用于以机器可读形式(例如,易失性或非易失性存储器、介质盘或其他介质)存储或传输信息的任何存储设备,机制或其他物理结构的设备)。
在附图中,一些结构或方法特征可以以特定布置和/或顺序示出。然而,应当理解,可能不需要这样的具体布置和/或排序。相反,在一些实施例中,这些特征可以以与说明性附图中所示不同的方式和/或顺序来布置。另外,在特定图中包括结构或方法特征并不意味着暗示这种特征在所有实施例中都是需要的,并且在一些实施例中可以不包括或可以与其他特征组合。
本申请实施例提供了语言层面的动态语言多线程编程能力,使开发者能够使用多线程编程的能力,增加程序的性能以及应用范围。
动态语言是计算机编程语言中的一个语言类别,是一类在运行时可以动态地改变类型、结构的语言,在运行时函数和属性可以被增加、修改和删除。例如JavaScript、Python、Ruby等都属于动态语言。动态语言不需要编译即可运行,在运行时需要运行环境的支撑,这个环境叫做运行时环境,它包含动态语言运行所需要的所有要素,例如Java虚拟机、JavaScript虚拟机等。
其中,JavaScrip虚拟机也可称为JavaScript引擎,可以被解释为JavaScript托管运行时环境(managed runtime environment for JavaScript),简称为运行时环境,或者虚拟可执行环境(Virtual Execution Environment)。它可以通过强制的自动内存管理,配合强制的类型系统安全性保证,以及数组越界检测等功能,保证JavaScript程序在内存分配、访问、释放上的安全性;还可以对代码执行进行权限管理、可见性限制等,保证代码只在其应用的权限内执行。
为了更清楚地理解本申请实施例,首先对与本申请实施例相关的面向对象编程技术所涉及的一些技术术语进行简要说明。
对象:对象中可定义有属性(property)和方法(method),可以将属性和方法(函数)封装在对象中。在内存中,这些对象其实是一些内存块,里面保存了数据和可运行的方法(函数)。
线程对象:是线程类的实例对象。线程对象封装了线程的一些信息(比如包括线程执行的方法(函数)),一个线程对象中的方法(函数)可以被其他线程运行。以JavaScript为例,线程对象可通过继承thread类或者通过实现runnable接口得到,比如从thread派生出一个新类,在其中加入属性和方法(函数),并可覆盖run()方法,即可创建完成一个派生类的新线程对象。其中,run()方法中包含了线程所要执行的代码。
线程:线程是对象中定义的方法的执行路径,也就是说,线程是代码的一次执行过程,在这个执行过程中,线程可以执行代表它的线程对象所定义的方法(函数),也可以执行其它的对象中的方法(函数)。以JavaScript为例,当通过继承thread类创建一个线程对象后执行start()方法(函数),从而启动该线程对象对应的线程。
本申请实施例能够使动态语言虚拟机支持多线程,并在动态语言层提供相应配套的多线程开发语言接口。基于本申请实施例提供给开发者语言级别的多线程开发能力,开发者可以使用本申请实施例提供的接口进行多线程程序的开发,如进行线程创建、运行以及通信。本申请实施例通过提供多线程的能力,拓宽了动态语言的应用范围,比如后台事件处理和前台页面渲染分线程处理可以更高效地开发交互式程序;同时由于多线程能更好地利用多核处理器的计算资源,在并发数据处理等大量计算的应用场景,本申请实施例可提高程序的运行性能。
本申请实施例提供了多线程管理方案以实现多线程开发能力,多线程管理具体可包括:线程的创建、线程的同步、线程的销毁等。
本申请实施例可适用于基于单线程机制的动态语言,使基于单线程机制的动态语言实现多线程能力。其中,用于实现线程管理方法的装置可称为线程管理装置。以动态语言为JavaScript语言为例,该装置可以是为JavaScript虚拟机。
为方便理解,下面首先对本申请实施例的整体框架进行描述。如图1所示,线程管理装置(比如JavaScript虚拟机)位于动态语言层(比如JavaScript语言层)和操作系统之间。动态语言程序由动态语言代码实现,对应于动态语言层,动态语言程序的多线程操作运行于线程管理装置之上,通过线程管理装置调用操作系统中的底层多线程库进行 相应的多线程操作。需要指出的是,线程管理装置运行的操作系统拥有一套底层的线程库。
线程管理装置可对动态语言线程进行管理,图2示出了本申请实施例提供的由线程管理装置管理的多线程架构。如图2所示,每个线程各自拥有不同的栈(Stack),多个线程可共享一个堆(Heap)。
栈是为执行线程留出的内存空间,每个线程有自己独立的栈和私有数据区,与其他线程隔离。当函数被调用的时候,栈顶为局部变量等数据预留块,当函数执行完毕,该块被释放,在下次的函数调用时再被使用。堆是为动态分配预留的内存空间,所存放的变量可以是对象,该变量为引用类型,即,该变量中实际保存的是一个指针,这个指针指向另一个位置。每个线程都有一个栈,但是每一个应用程序通常只有一个堆(尽管为不同类型分配内存使用多个堆的情况也是有的)。
本申请实施例基于上述架构,提供了多线程管理方案,其中可包括:线程对象的创建、线程对象的释放、线程对象的同步以及除线程对象之外的其它对象的同步等。下面分别对这些过程进行详细描述。
(一)线程的创建
本申请实施例中,在操作系统层中定义有用于创建线程对象的方法(函数),线程管理装置将该方法的调用接口提供给动态语言层,以便开发者调用该接口来创建线程对象。线程管理装置提供给动态语言层的接口具体可以是应用程序编程接口(Application Programming Interface,简称API),更具体地,该API可以包括函数名和参数等信息。线程管理装置可向动态语言层提供多个API,每个API对应相应的方法(函数)。
图3示出了本申请实施例中的线程对象的创建过程,如图所示,该流程可包括:
在图3的S301中:线程管理装置接收动态语言层发送的创建线程对象指令,该创建线程对象指令中包括用于创建线程对象的接口,该线程对象属于指定的应用程序。在一个实施例中,开发者可根据提供给动态语言层的线程对象创建接口编写动态语言应用程序代码,当该应用程序代码被执行时,线程管理装置解析该应用程序代码,得到其中用于创建线程对象的接口,从而通过后续步骤执行线程对象创建方法(函数),创建得到线程对象。
在图3的S302中:线程管理装置根据该创建线程对象的接口,调用操作系统中该接口对应的对象所提供的用于创建线程对象方法(函数),创建得到对应的线程对象。在一个实施例中,以线程管理装置为JavaScript虚拟机为例,JavaScript虚拟机可根据操作 系统提供的用于创建线程对象的对象(以下为描述方便将该对象称为obj对象),通过创建该对象的一个实例,创建得到线程对象。该obj对象是操作系统提供的对象,该obj对象中封装有属性和方法,比如其中可包含new方法。通过针对该obj对象执行new方法可创建得到新的线程对象,obj对象中的属性和方法可被新的线程对象所继承,还可向新的线程对象中添加属性和方法。创建得到的线程对象中包括run方法(函数),该方法(函数)为线程启动时所要执行的方法(函数),该run方法(函数)可由开发者根据所要实现的功能或所要执行的操作进行自定义。
在创建线程对象过程中,线程管理装置创建并注册一个内部的线程对象管理结构来管理对应线程所拥有的资源。该线程对象管理结构可如图2所示。其中,新创建得到的线程对象的指针可存储在堆(Heap)中,该线程的变量等数据可存储于该线程的栈(Stack)中。
在图3的S303中:线程管理装置启动所创建的线程对象,得到对应的线程。
进一步地,线程管理装置在创建线程对象后,可将该线程对象所对应的线程句柄提供给应用程序,供应用程序根据该调用操作对应的线程。在一个实施例中,以线程管理装置为JavaScript虚拟机为例,JavaScript虚拟机可将线程句柄提供给JavaScript语言层,以便开发者通过该线程句柄操作对应的线程。线程是在线程对象启动后创建的,一个线程可以有一个或多个句柄,不同的句柄可以具有不同的操作权限。当然,JavaScript虚拟机也可以将其他能够指示线程的信息提供给JavaScript语言层,用于在开发者操作线程时使用。
创建线程对象后,可以通过线程句柄操作对应的线程,如执行线程、等待线程结束、回收线程等。
比如,启动线程的过程可包括:线程管理装置解析动态语言应用程序代码,得到基于线程句柄的启动线程对象的指令,线程管理装置根据该指令,通过调用相应线程对象提供的启动方法(如start方法)来启动该线程对象。当调用线程对象的start方法时,线程管理装置通过操作系统启动该线程。线程管理装置根据该线程对象提供的运行属性(如run属性)的属性值,调用该属性值所指示的方法(该属性的属性值为该方法的名称)。
其中,当线程管理装置根据run属性的属性值得到基于动态语言程序代码封装的方法(函数)后,由于操作系统并不直接对接动态语言层,无法解析该方法,因此线程管理装置负责解释该方法,得到操作系统层提供的接口,并调用该接口以执行操作系统提供的相应方法。
再比如,等待线程结束的过程可包括:线程管理装置解析动态语言应用程序代码,得到基于线程句柄的等待线程结束的指令,线程管理装置根据该指令,通过执行线程对象提供的用于等待线程结束的方法(如join方法)来等待线程结束。其中,由于操作系统并不直接对接动态语言层,无法解析该join方法,因此线程管理装置解释该方法,得到操作系统层提供的接口,并通过调用该接口以执行相应的join方法。
再比如,停止线程的过程可包括:线程管理装置解析动态语言应用程序代码,得到基于线程句柄的停止线程的指令,线程管理装置根据该指令,通过执行线程对象中的用于停止线程的方法(如sleep方法)来等停止线程。其中,由于操作系统并不直接对接动态语言层,无法解析该sleep方法,因此线程管理装置解释并执行该方法。
为了更清楚地说明线程对象的管理过程,下面以JavaScript虚拟机为例,结合图4进行举例说明。
参见图4,在S401至S402中,JavaScript虚拟机对基于JavaScript语言层的JavaScript应用程序代码进行解析,得到用于表示调用obj对象中的新建实例方法(new方法)的程序语句,JavaScript虚拟机根据该程序语句中的API(如new方法的函数名)调用操作系统提供的线程库接口,基于操作系统提供的obj对象创建该对象的实例。此过程中,JavaScript虚拟机将该obj对象的属性、方法进行封装,创建得到线程对象,并将线程句柄返回给JavaScript语言层。
在S403中,JavaScript虚拟机对基于JavaScript语言层的JavaScript应用程序代码进行解析,得到用于表示调用对应的线程对象(通过线程句柄来标识对应的线程对象)的启动方法(start方法)的程序语句时,JavaScript虚拟机根据该程序语句中的API(如start方法的函数名)调用操作系统提供的线程库接口,通过操作系统创建对应的线程,然后启动该线程。该过程中,JavaScript虚拟机根据该线程对象从obj对象继承的run属性的属性值,执行该属性值所指示的方法(该属性的属性值为该方法的名称)。
在S404中,JavaScript虚拟机对基于JavaScript语言层的JavaScript应用程序代码进行解析,得到用于表示调用对应的线程对象(通过线程句柄来标识对应的线程对象)的等待线程结束方法(join方法)时,JavaScript虚拟机根据该程序语句中的API(如join方法的函数名)调用操作系统提供的线程库接口,通过操作系统针对线程句柄所对应的线程执行相应的方法。
在S405中,JavaScript虚拟机对基于JavaScript语言层的JavaScript应用程序代码进行解析,得到用于表示调用对应的线程对象(通过线程句柄来标识对应的线程对象)的 线程结束方法(sleep方法)时,JavaScript虚拟机根据该程序语句中的API(如sleep方法的函数名)调用操作系统提供的线程库接口,通过操作系统针对该线程句柄所对应的线程执行相应的方法。
采用上述实施例提供的方法,可创建多个线程,并可对每个线程进行管理,从而实现多线程管理。
通过以上流程可以看出,线程管理装置从操作系统中封装好线程的能力(包括属性和方法),并将这些能力封装到线程对象中,并将该线程对象的句柄提供给动态语言层。当创建线程时,线程管理装置会通过操作系统创建一个线程对象,然后启动该线程对象得到相应的线程。线程管理装置通过封装线程对象并暴露到动态语言层,从而为开发者提供操作线程的能力。
(二)线程同步
(1)使用同步锁实现线程同步
在多线程系统中,多个线程同时运行时可能调用函数或访问相同的数据,在多个线程同时对同一个内存地址进行写入的情况下,由于CPU时间调度上的问题,写入数据会被多次的覆盖。对于一些敏感数据不允许被多个线程同时读写,此时需要使用同步互斥技术,保证数据在任何时刻,最多有一个线程读写,以保证数据的完整性。
线程同步,是指当有一个线程在对内存进行读写操作时,其他线程都不可以对这个内存地址进行读写操作,直到该线程完成读写操作,其他线程才能对该内存地址进行读写操作,而其他线程又处于等待状态。
在本申请的一些实施例中,采用互斥对象机制实现线程同步,只有获得同步锁的线程才有访问公共资源的权限。因为同步锁只有一个,所以能保证公共资源不会同时被多个线程访问。同步锁不仅能实现同一应用程序的公共资源安全共享,还能实现不同应用程序的公共资源安全共享。
上述同步锁是由线程管理装置对动态语言层提供的全局对象,用来进行某一段代码的线程同步。开发者通过构建同步锁,能够实现线程之间执行互斥操作。同步锁的创建过程与线程对象的创建过程类似,可通过调用动态语言层提供的用于创建同步锁的接口,使线程管理装置通过操作系统提供的接口,执行操作系统中的创建同步锁的方法,创建得到同步锁。所创建得到的同步锁为全局对象。线程可以使用此同步锁进行同步,比如线程1在对一组全局数据进行读写操作时,若不希望其他线程进行读写,则可以使用此同步锁。
同步锁中可包括如下属性和方法:
锁标识属性:该属性值用于唯一标识同步锁,一个同步锁与一个内存区域对应,不同的内存区域对应不同的同步锁;
加锁方法:获得该线程对象的线程可执行该方法,执行该方法可对锁标识属性的属性值所指示的内存区域进行加锁,被加锁的内存区域只能被获得该线程对象的线程进行读写操作,其他线程不允许对该内存区域进行读写操作;
解锁方法:获得该线程对象的线程可执行该方法,执行该方法可对锁标识属性的属性值所指示的内存区域进行解锁。
图5示出了线程1和线程2使用同步锁实现互斥操作的示意图,如图所示,线程1和线程2同时需要计算全局数据,开发者希望两个线程不要同时修改该全局数据。开发者就可以使用同步锁进行线程同步。首先,针对请求读写的全局数据所在的内存区域新建一个全局的同步锁。当线程1用该同步锁对需要保护的内存区域进行保护,对该内存区域进行读写前先对该内存区域加锁,对该内存区域读写完成后,对该内存区域进行解锁。同理,当线程2用该同步锁对需要保护的内存区域进行保护,对该内存区域进行读写前先对该内存区域加锁,对该内存区域读写完成后,对该内存区域进行解锁。
如图5所示,在线程1需要对该全局数据进行读写时,线程1向线程管理装置(如JavaScript虚拟机)请求获取该全局数据所在的内存区域的锁对象,线程管理装置判断该内存区域的锁状态,若为未被加锁状态,则线程管理装置将该同步锁给线程1,线程1获得该同步锁后,对该同步锁所对应的内存区域执行加锁方法(lock方法),该内存区域的状态变为加锁状态,线程1对被该线程加锁的该内存区域进行读写操作,读写操作完成后,对该内存区域执行解锁(unlock)方法,该内存区域的状态变为未加锁状态。若线程1向线程管理装置请求获取同步锁后,线程管理装置判断该同步锁对应的内存区域当前为加锁状态,说明该内存区域当前正在被其他线程读写,因此不糊将该内存区域对应的同步锁给线程1,线程1处于等待状态。当该内存区域的状态变为未被加锁状态时,线程管理装置将该内存区域对应的同步锁给线程1。同理,线程2基于该同步锁进行数据读写的过程与此类似。
需要说明的是,线程对内存区域进行加锁或解锁的过程中,由于操作系统并不直接对接动态语言层,无法解析加锁方法或解锁方法,因此线程管理装置负责在操作系统创建的线程内解释并执行该方法。另外,线程对内存区域进行读写的过程中,由于操作系统并不直接对接动态语言层,无法解析内存区域读写方法,因此线程管理装置负责在操 作系统创建的线程内解释并执行该方法。
(2)采用同步声明实现线程同步
在本申请的另外一些实施例中,可对方法或动态语言程序语句进行声明,以表明被声明的方法或动态语言程序语句需要同步互斥执行,即,一个线程执行该方法或动态语言程序语句时,其他线程不能执行相同的方法或动态语言程序语句。
以JavaScript虚拟机举例来说,如果线程1和其他线程(如线程2)均需要执行方法a,并且在线程1和线程2调用方法a的语句中,该方法a均被声明为进行同步互斥,这样,线程1和线程2不能同时执行方法a,只能在一个线程执行完成方法a后,另一个线程才能执行方法a。具体实现过程可如图6所示,可包括:
在图6的S601中:JavaScript虚拟机接收到线程1调用方法a的指令;
在图6的S602中:JavaScript虚拟机判断当前是否有其他线程正在执行该方法,若有,则转入图6中的S603,否则转入图6中的S604;
在图6的S603中:JavaScript虚拟机暂不执行线程1中的方法a,当其他线程(比如线程2)执行完成方法a后,转入图6中的S604:
在图6的S604中:JavaScript虚拟机执行线程1中的方法a。
再举例来说,如果线程1和其他线程(如线程2)均需要执行某语句(如对某全局变量进行赋值的操作),并且该语句被声明为进行同步互斥,这样,线程1和线程2不能同时执行该语句(比如不能同时对该全局变量进行赋值),只能在一个线程执行完成该语句后,另一个线程才能执行该语句。具体实现过程可如图7所示,可包括:
在图7的S701中:JavaScript虚拟机接收到线程1执行该语句的指令;
在图7的S702中:JavaScript虚拟机判断当前是否有其他线程正在执行该语句,若有,则转入图7中的703,否则转入图7的S704;
在图7的S703中:JavaScript虚拟机暂不执行线程1中的该语句,当其他线程(比如线程2)执行完成该语句后,转入图7的S704:
在图7的S704中:JavaScript虚拟机执行线程1中的该语句。
声明的方式可以是显式声明也可以是隐式声明。其中,在一些例子中,如果在调用该方法的语句之前和之后增加同步关键字进行声明,则称为显式声明,同样,如果在该动态语言程序语句之前和之后增加同步关键字进行声明,也称为显式声明。在另一些例子中,如果在定义或描述方法的程序代码中,将该方法放入同步关键字的范围之内,这种声明方式称为隐式声明,线程管理装置在通过调用操作系统提供的接口执行该方法时, 操作系统可根据该方法的程序代码中的同步关键字,对该方法的执行过程采用同步互斥操作;同样,如果将该动态语言程序语句放入同步关键字的范围之内,则也称为隐式声明。
(三)对象同步
在多线程系统中,多个线程同时运行时可能需要对同一对象进行读写操作,在多个线程同时对同一个对象进行写入的情况下,由于CPU时间调度上的问题,写入数据会被多次的覆盖,导致错误发生。此时需要使用同步互斥技术,保证数据在任何时刻,最多有一个线程对特定的JavaScript对象进行读写。
本申请实施例中,线程管理装置对语言层提供了对象同步的能力。可预先指定对某个或某些对象的读写进行同步互斥。采用互斥对象机制实现对象同步,只有获得同步锁的线程才有读写指定的对象的权限。
该同步锁的创建过程与前述实施例类似,可通过调用动态语言层提供的用于创建同步锁的接口,使线程管理装置通过操作系统提供的接口,执行操作系统中的创建同步锁的方法,创建得到同步锁。
同步锁中可包括如下属性和方法:
锁标识属性:该属性值用于唯一标识同步锁,一个同步锁与一个内存区域(这里是一个对象对应的内存区域)对应,不同的对象对应不同的同步锁;
加锁方法:获得该线程对象的线程可执行该方法,执行该方法可对锁标识属性的属性值所指示的对象(或该对象所在的内存区域)进行加锁,被加锁的对象只能被获得该线程对象的线程进行读写操作,其他线程不允许对该对象进行读写操作;
解锁方法:获得该线程对象的线程可执行该方法,执行该方法可对锁标识属性的属性值所指示的对象进行解锁。
本申请实施例中,可以采用以下方式指定某个或某些对象采用同步互斥操作:
方法1:对象可以被划分为不同类型,本申请实施例中,提供了ConcurrentArray、ConcurrentSet和ConcurrentMap三种常用对象。该三种对象在内部虚拟机实现上已经保证线程间安全,该三种对象的操作不需要开发者在JavaScript层使用同步关键字同步;
方法2:可使用同步关键字对需要采用同步互斥操作的对象进行声明。
对象的布局由线程管理装置规定。对象的布局可以理解为对象在内存中的存储结构。比如,以对象为JavaScript对象为例,如图8a所示,JavaScript对象的布局中可包括对象头部分和对象实例部分,对象头部分中可存储有对象名称等信息,对象实例部分中可存 储对象中的方法使用的变量等。
本申请实施例中,JavaScript对象的布局头中设置有用于标识同步锁状态的信息域,该信息域可以是一个或多个比特,如图8b所示。该信息域的不同取值可标识该JavaScript对象的锁状态。例如,如果该信息域的取值为0时,表示该JavaScript对象未加锁,该信息域的取值为1时,表示该JavaScript对象已经加锁。
参见图9所示,当线程管理装置在接收到线程1对某对象进行读写的指令时(图9中的S901),可判断该对象是否是被指定为进行同步互斥的对象(图9中的S902),若是,则请求获取同步锁(图9中的S903),若获取到同步锁,则将该同步锁给线程1,线程1收到同步锁后,将对该对象加锁,比如设置该对象的布局中的锁状态标志位取值为1,并对该对象进行读写操作,并在读写完成后,对该对象进行解锁,比如设置该对象的布局中的锁状态标志位取值为0(图9中的S904),若未获取到同步锁,则说明当前该对象正在被读写,此种情况下,线程管理装置等待该对象读写完成后,将同步锁给线程1,线程1获取到同步锁之后的操作同前所述(图9中的S905);若该对象未被指定为进行同步互斥,则对该对象进行读写操作(图9中的S906)。
需要说明的是,线程对对象进行加锁或解锁的过程中,由于操作系统并不直接对接动态语言层,无法解析加锁方法或解锁方法,因此线程管理装置负责在操作系统创建的线程内解释并执行该方法。另外,线程对对象进行读写的过程中,由于操作系统并不直接对接动态语言层,无法解析内存区域读写方法,因此线程管理装置负责在操作系统创建的线程内解释并执行该方法。
基于相同的技术构思,本申请实施例还提供了一种线程管理装置,该线程管理装置具体可以是前述实施例中的动态语言虚拟机,更具体地,可以是JavaScript虚拟机。
参见图10,为本申请实施例提供的线程管理装置的结构示意图,如图所示,该装置可包括:接收模块1001、管理模块1002,其中:
接收模块1001,用于接收动态语言层发送的创建线程对象指令,所述创建线程对象指令中包括用于创建线程对象的接口,所述线程对象属于指定的应用程序;
管理模块1102,用于根据所述用于创建线程对象的接口,调用操作系统中所述接口对应的对象所提供的用于创建线程对象的方法,创建得到对应的线程对象;并启动所述线程对象,得到对应的线程。
可选地,所述应用程序中对应多个线程,所述多个线程中的每个线程拥有各自独立的栈以及私有数据区,所述多个线程共享一个堆;其中,一个线程的栈是该线程所占用 的内存空间,所述堆是所述应用程序的所有线程共享的内存空间。
可选地,所述接收模块还用于:接收动态语言层发送的对全局数据进行读写的指令;管理模块1002可还用于:请求获得用于对所述全局变量进行读写的第一同步锁,所述第一同步锁为全局对象;调用获得到的第一同步锁的加锁方法对所述全局变量进行加锁,对所述全局变量进行读写,并在读写完成后调用所述第一同步锁的解锁方法对所述全局变量进行解锁。
可选地,接收模块1001还用于:接收动态语言层发送的执行指定对象提供的指定方法的指令。管理模块1002还用于:确定所述指定方法是否被声明为进行同步互斥;若是,则请求获得所述指定方法对应的第二同步锁,所述第二同步锁为全局对象;调用获得到的所述第二同步锁的加锁方法对所述指定方法进行加锁,执行所述指定方法,并在执行完成后调用所述第二同步锁的解锁方法对所述指定方法进行解锁。
可选地,接收模块1001还用于:接收用于对目标对象进行读写的指令.管理模块1002还用于:若所述目标对象所属的类型为指定类型或被声明为进行同步互斥,则请求获得对所述目标对象进行读写的第三同步锁,所述第三同步锁为全局对象;调用获得到的所述第三同步锁的加锁方法对所述目标对象进行加锁,对所述目标对象进行读写,并在读写完成后调用所述第三同步锁的解锁方法对所述目标对象进行解锁。
可选地,管理模块1002具体用于:获取所述目标对象所在的内存区域中用于存储对象头信息的内存区域中的对象状态标志位,若所述对象状态标志位的取值表明所述目标对象未被加锁,则获得用于对所述目标对象进行读写的第三同步锁。可选地,管理模块1002具体用于:对所述目标对象进行加锁时,设置所述状态标志位的取值以表明所述目标对象已被加锁.。可选地,管理模块1002具体用于:对所述目标对象进行解锁时,设置所述状态标志位的取值以表明所述目标对象未被加锁。
可选地,所述管理模块还用于:将创建得到的线程对象所对应的句柄提供给所述动态语言层。
可选地,管理模块1002具体用于:接收动态语言层发送的启动所述线程对象的指令,所述启动线程对象的指令中包括所述线程对象对应的句柄;根据所述启动线程对象的指令,通过操作系统调用所述句柄对应的线程对象提供的启动方法,所述启动方法用于根据所述线程对象的运行属性的属性值,执行该属性值所指示的方法。
可选地,接收模块1001还用于:接收动态语言层发送的等待线程结束的指令,所述等待线程结束的指令中包括所述线程对象所对应的句柄。管理模块1002还用于:根据接 收到的等待线程结束的指令,通过操作系统调用所述句柄对应的线程对象提供的用于等待线程结束的方法来等待线程运行结束。
可选地,接收模块1001还用于:接收动态语言层发送的停止线程的指令,所述停止线程的指令中包括所述线程对象所对应的句柄。管理模块1002还用于:根据接收到的停止线程的指令,通过操作系统调用所述句柄对应的线程对象提供的用于停止线程的方法来停止线程运行。
基于相同的技术构思,本申请实施例还提供了一个或多个计算机可读介质,所述可读介质上存储有指令,所述指令被一个或多个处理器执行时,使得通信设备执行前述实施例描述的线程管理方法。
基于相同的技术构思,本申请实施例还提供了一种装置1100,该装置1100可实现前述实施例描述的流程。
图11示例性地示出了根据各种实施例的示例装置1100,装置1100可包括一个或多个处理器1102,系统控制逻辑1101耦合于至少一个处理器1102,非易失性存储器(non-volatile memory,NMV)/存储器1104耦合于系统控制逻辑1101,网络接口1106耦合于系统控制逻辑1101。
处理器1102可包括一个或多个单核处理器或多核处理器。处理器1102可包括任何一般用途处理器或专用处理器(如图像处理器、应用处理器基带处理器等)的组合。
一个实施例中的系统控制逻辑1101,可包括任何适当的接口控制器,以提供到处理器1102中的至少一个的任何合适的接口,和/或提供到与系统控制逻辑1101通信的任何合适的设备或组件的任何合适的接口。
一个实施例中的系统控制逻辑1101,可包括一个或多个内存控制器,以提供到系统内存1103的接口。系统内存1103用来加载以及存储数据和/或指令。例如,对应装置1100,在一个实施例中,系统内存1103可包括任何合适的易失性存储器。
NVM/存储器1104可包括一个或多个有形的非暂时的计算机可读介质,用于存储数据和/或指令。例如,NVM/存储器1104可包括任何合适的非易失性存储装置,如一个或多个硬盘(hard disk device,HDD),一个或多个光盘(compact disk,CD),和/或一个或多个数字通用盘(digital versatile disk,DVD)。
NVM/存储器1104可包括存储资源,该存储资源物理上是该系统所安装的或者可以被访问的设备的一部分,但不一定是设备的一部分。例如,NVM/存储器1104可经由网络接口1106被网络访问。
系统内存1103以及NVM/存储器1104可分别包括临时的或持久的指令1110的副本。指令1110可包括当由处理器1102中的至少一个执行时导致装置1100实现图3至图7、图9描述的方法之一或组合的指令。各实施例中,指令1110或硬件、固件,和/或软件组件可另外地/可替换地被置于系统控制逻辑1101,网络接口1106和/或处理器1102。
网络接口1106可包括一个接收器来为装置1100提供无线接口来与一个或多个网络和/或任何合适的设备进行通信。网络接口1106可包括任何合适的硬件和/或固件。网络接口1106可包括多个天线来提供多输入多输出无线接口。在一个实施例中,网络接口1106可包括一个网络适配器、一个无线网络适配器、一个电话调制解调器,和/或无线调制解调器。
在一个实施例中,处理器1102中的至少一个可以与用于系统控制逻辑的一个或多个控制器的逻辑一起封装。在一个实施例中,处理器中的至少一个可以与用于系统控制逻辑的一个或多个控制器的逻辑一起封装以形成系统级封装。在一个实施例中,处理器中的至少一个可以与用于系统控制逻辑的一个或多个控制器的逻辑集成在相同的管芯上。在一个实施例中,处理器中的至少一个可以与用于系统控制逻辑的一个或多个控制器的逻辑集成在相同的管芯上以形成系统芯片。
装置1100可进一步包括输入/输出装置1105。输入/输出装置1105可包括用户接口旨在使用户与装置1100进行交互,可包括外围组件接口,其被设计为使得外围组件能够与系统交互,和/或,可包括传感器,旨在确定环境条件和/或有关装置1100的位置信息。

Claims (24)

  1. 一种线程管理方法,其特征在于,包括:
    接收动态语言层发送的创建线程对象指令,所述创建线程对象指令中包括用于创建线程对象的接口,所述线程对象对应指定的应用程序;
    根据所述用于创建线程对象的接口,调用操作系统中所述接口对应的对象所提供的用于创建线程对象的方法,创建得到对应的线程对象;
    启动所述线程对象,得到对应的线程。
  2. 如权利要求1所述的方法,其特征在于,所述应用程序中对应多个线程,所述多个线程中的每个线程拥有各自独立的栈以及私有数据区,所述多个线程共享一个堆;其中,一个线程的栈为该线程所占用的内存空间,所述堆为所述应用程序的所有线程共享的内存空间。
  3. 如权利要求1所述的方法,其特征在于,还包括:
    接收动态语言层发送的对全局数据进行读写的指令;
    请求获得用于对所述全局变量进行读写的第一同步锁,所述第一同步锁为全局对象;
    调用获得到的第一同步锁的加锁方法对所述全局变量进行加锁,对所述全局变量进行读写,并在读写完成后调用所述第一同步锁的解锁方法对所述全局变量进行解锁。
  4. 如权利要求1所述的方法,其特征在于,还包括:
    接收动态语言层发送的执行指定对象提供的指定方法的指令;
    确定所述指定方法是否被声明为进行同步互斥;
    若是,则请求获得所述指定方法对应的第二同步锁,所述第二同步锁为全局对象;调用获得到的所述第二同步锁的加锁方法对所述指定方法进行加锁,执行所述指定方法,并在执行完成后调用所述第二同步锁的解锁方法对所述指定方法进行解锁。
  5. 如权利要求1所述的方法,其特征在于,还包括:
    接收用于对目标对象进行读写的指令;
    若所述目标对象所属的类型为指定类型或被声明为进行同步互斥,则请求获得对所述目标对象进行读写的第三同步锁,所述第三同步锁为全局对象;
    调用获得到的所述第三同步锁的加锁方法对所述目标对象进行加锁,对所述目标对象进行读写,并在读写完成后调用所述第三同步锁的解锁方法对所述目标对象进行解锁。
  6. 如权利要求5所述的方法,其特征在于,请求获得对所述目标对象进行读写的第三同步锁,包括:
    获取所述目标对象所在的内存区域中用于存储对象头信息的内存区域中的对象状态标志位,若所述对象状态标志位的取值表明所述目标对象未被加锁,则获得用于对所述目标对象进行读写的第三同步锁;
    对所述目标对象进行加锁,包括:
    设置所述状态标志位的取值以表明所述目标对象已被加锁;
    对所述目标对象进行解锁,包括:
    设置所述状态标志位的取值以表明所述目标对象未被加锁。
  7. 如权利要求1至6中任一项所述的方法,其特征在于,还包括:
    将创建得到的线程对象所对应的句柄提供给所述动态语言层。
  8. 如权利要求1至6中任一项所述的方法,其特征在于,启动所述线程对象,包括:
    接收动态语言层发送的启动所述线程对象的指令,所述启动线程对象的指令中包括所述线程对象对应的句柄;
    根据所述启动线程对象的指令,通过操作系统调用所述句柄对应的线程对象提供的启动方法,所述启动方法用于根据所述线程对象的运行属性的属性值,执行该属性值所指示的方法。
  9. 如权利要求1所述的方法,其特征在于,还包括:
    接收动态语言层发送的等待线程结束的指令,所述等待线程结束的指令中包括所述线程对象所对应的句柄;
    根据接收到的等待线程结束的指令,通过操作系统调用所述句柄对应的线程对象提供的用于等待线程结束的方法来等待线程运行结束。
  10. 如权利要求1所述的方法,其特征在于,还包括:
    接收动态语言层发送的停止线程的指令,所述停止线程的指令中包括所述线程对象所对应的句柄;
    根据接收到的停止线程的指令,通过操作系统调用所述句柄对应的线程对象提供的用于停止线程的方法来停止线程运行。
  11. 如权利要求1至6中任一项所述的方法,其特征在于,所述接收、所述调用和所述启动的操作的执行主体为JavaScript虚拟机,所述动态语言层为JavaScript语言层。
  12. 一种线程管理装置,其特征在于,包括:
    接收模块,用于接收动态语言层发送的创建线程对象指令,所述创建线程对象指令中包括用于创建线程对象的接口,所述线程对象属于指定的应用程序;
    管理模块,用于根据所述用于创建线程对象的接口,调用操作系统中所述接口对应的对象所提供的用于创建线程对象的方法,创建得到对应的线程对象;并启动所述线程对象,得到对应的线程。
  13. 如权利要求12所述的装置,其特征在于,所述应用程序中对应多个线程,所述多个线程中的每个线程拥有各自独立的栈以及私有数据区,所述多个线程共享一个堆;其中,一个线程的栈为该线程所占用的内存空间,所述堆为所述应用程序的所有线程共享的内存空间。
  14. 如权利要求12所述的装置,其特征在于,所述接收模块还用于:接收动态语言层发送的对全局数据进行读写的指令;
    所述管理模块还用于:请求获得用于对所述全局变量进行读写的第一同步锁,所述第一同步锁为全局对象;调用获得到的第一同步锁的加锁方法对所述全局变量进行加锁,对所述全局变量进行读写,并在读写完成后调用所述第一同步锁的解锁方法对所述全局变量进行解锁。
  15. 如权利要求12所述的装置,其特征在于,所述接收模块还用于:接收动态语言层发送的执行指定对象提供的指定方法的指令;
    所述管理模块还用于:确定所述指定方法是否被声明为进行同步互斥;若是,则请求获得所述指定方法对应的第二同步锁,所述第二同步锁为全局对象;调用获得到的所述第二同步锁的加锁方法对所述指定方法进行加锁,执行所述指定方法,并在执行完成后调用所述第二同步锁的解锁方法对所述指定方法进行解锁。
  16. 如权利要求12所述的装置,其特征在于,所述接收模块还用于:接收用于对目标对象进行读写的指令;
    所述管理模块还用于:若所述目标对象所属的类型为指定类型或被声明为进行同步互斥,则请求获得对所述目标对象进行读写的第三同步锁,所述第三同步锁为全局对象;调用获得到的所述第三同步锁的加锁方法对所述目标对象进行加锁,对所述目标对象进行读写,并在读写完成后调用所述第三同步锁的解锁方法对所述目标对象进行解锁。
  17. 如权利要求16所述的装置,其特征在于,所述管理模块具体用于:获取所述目标对象所在的内存区域中用于存储对象头信息的内存区域中的对象状态标志位,若所述对象状态标志位的取值表明所述目标对象未被加锁,则获得用于对所述目标对象进行读 写的第三同步锁;
    所述管理模块具体用于:对所述目标对象进行加锁时,设置所述状态标志位的取值以表明所述目标对象已被加锁;
    所述管理模块具体用于:对所述目标对象进行解锁时,设置所述状态标志位的取值以表明所述目标对象未被加锁。
  18. 如权利要求12至17中任一项所述的装置,其特征在于,所述管理模块还用于:将创建得到的线程对象所对应的句柄提供给所述动态语言层。
  19. 如权利要求12至17中任一项所述的装置,其特征在于,所述管理模块具体用于:接收动态语言层发送的启动所述线程对象的指令,所述启动线程对象的指令中包括所述线程对象对应的句柄;根据所述启动线程对象的指令,通过操作系统调用所述句柄对应的线程对象提供的启动方法,所述启动方法用于根据所述线程对象的运行属性的属性值,执行该属性值所指示的方法。
  20. 如权利要求12所述的装置,其特征在于,所述接收模块还用于:接收动态语言层发送的等待线程结束的指令,所述等待线程结束的指令中包括所述线程对象所对应的句柄;
    所述管理模块还用于:根据接收到的等待线程结束的指令,通过操作系统调用所述句柄对应的线程对象提供的用于等待线程结束的方法来等待线程运行结束。
  21. 如权利要求12所述的装置,其特征在于,所述接收模块还用于:接收动态语言层发送的停止线程的指令,所述停止线程的指令中包括所述线程对象所对应的句柄;
    所述管理模块还用于:根据接收到的停止线程的指令,通过操作系统调用所述句柄对应的线程对象提供的用于停止线程的方法来停止线程运行。
  22. 如权利要求12至17中任一项所述的装置,其特征在于,所述装置为JavaScript虚拟机,所述动态语言层为JavaScript语言层。
  23. 一个或多个计算机可读介质,所述可读介质上存储有指令,所述指令被一个或多个处理器执行时,使得通信设备执行如权利要求1-11中任一项所述的方法。
  24. 一种线程管理装置,包括:
    一个或多个处理器;以及
    一个或多个计算机可读介质,所述可读介质上存储有指令,所述指令被所述一个或多个处理器执行时,使得所述装置执行如权利要求1-11中任一项所述的方法。
PCT/CN2018/072039 2017-01-23 2018-01-10 一种线程管理方法及装置 WO2018133713A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201710062788.2 2017-01-23
CN201710062788.2A CN108345452B (zh) 2017-01-23 2017-01-23 一种线程管理方法及装置

Publications (1)

Publication Number Publication Date
WO2018133713A1 true WO2018133713A1 (zh) 2018-07-26

Family

ID=62908294

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2018/072039 WO2018133713A1 (zh) 2017-01-23 2018-01-10 一种线程管理方法及装置

Country Status (3)

Country Link
CN (1) CN108345452B (zh)
TW (1) TW201828047A (zh)
WO (1) WO2018133713A1 (zh)

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109614220B (zh) * 2018-10-26 2020-06-30 阿里巴巴集团控股有限公司 一种多核系统处理器和数据更新方法
CN110465938B (zh) * 2019-06-28 2022-11-25 炬星科技(深圳)有限公司 状态约束图的读写控制方法、设备及存储介质
CN113110928B (zh) * 2021-04-21 2024-09-17 宏晶微电子科技股份有限公司 多线程启动方法及多线程启动系统
CN116700965B (zh) * 2023-05-30 2024-06-21 北京瑞泰兴成工程技术有限公司 一种基于分布式的数据采集方法及系统

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101421711A (zh) * 2006-04-13 2009-04-29 微软公司 用于资源受限设备的虚拟执行系统
CN103744723A (zh) * 2014-01-24 2014-04-23 深圳联友科技有限公司 一种线程池的管理方法和管理系统
CN104850460A (zh) * 2015-06-02 2015-08-19 上海斐讯数据通信技术有限公司 一种服务程序线程管理方法
CN105373414A (zh) * 2014-08-26 2016-03-02 龙芯中科技术有限公司 支持MIPS平台的Java虚拟机实现方法及装置

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1266590C (zh) * 2002-12-31 2006-07-26 上海科泰世纪科技有限公司 面向构件基于系统内核的进程池/线程池管理方法
US9395957B2 (en) * 2010-12-22 2016-07-19 Microsoft Technology Licensing, Llc Agile communication operator
CN102103526A (zh) * 2011-02-14 2011-06-22 博视联(苏州)信息科技有限公司 服务端和客户端间通过服务管理进行进程间通信的方法及系统

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101421711A (zh) * 2006-04-13 2009-04-29 微软公司 用于资源受限设备的虚拟执行系统
CN103744723A (zh) * 2014-01-24 2014-04-23 深圳联友科技有限公司 一种线程池的管理方法和管理系统
CN105373414A (zh) * 2014-08-26 2016-03-02 龙芯中科技术有限公司 支持MIPS平台的Java虚拟机实现方法及装置
CN104850460A (zh) * 2015-06-02 2015-08-19 上海斐讯数据通信技术有限公司 一种服务程序线程管理方法

Also Published As

Publication number Publication date
CN108345452A (zh) 2018-07-31
CN108345452B (zh) 2021-07-23
TW201828047A (zh) 2018-08-01

Similar Documents

Publication Publication Date Title
Bonachea et al. GASNet Specification, v1. 8.1
US9176713B2 (en) Method, apparatus and program storage device that provides a user mode device interface
WO2018133713A1 (zh) 一种线程管理方法及装置
CN110389829B (zh) 多租户环境中扩展对象的分类与分发
CN106663024B (zh) 变量句柄
US9069907B2 (en) Abstracting special file interfaces to concurrently support multiple operating system levels
US8375175B2 (en) Fast and efficient reacquisition of locks for transactional memory systems
US8327374B1 (en) Framework for executing multiple threads and sharing resources in a multithreaded computer programming environment
CN111857993B (zh) 一种内核态调用用户态函数的方法
JPH1115793A (ja) 資源の保全性を保護する方法
RU2746155C2 (ru) Уничтожение объекта на основе последовательности выполняемых действий
US20220261489A1 (en) Capability management method and computer device
CN112363779A (zh) 一种动态链接程序的安全控制方法
KR20080005522A (ko) 애플리케이션 프레임워크 페이즈화 모델
US20240061938A1 (en) Patching enclaves with shared resources
US10394610B2 (en) Managing split packages in a module system
US8291377B2 (en) External configuration of processing content for script
US9418175B2 (en) Enumeration of a concurrent data structure
Mutia Inter-Process Communication Mechanism in Monolithic Kernel and Microkernel
JP4738548B2 (ja) エミュレートされた処理環境でメモリ・アクセスを管理する方法、システム、およびそのためのコンピュータ・プログラム
CN117009109A (zh) 进程间数据共享方法、装置及电子设备
CN117033028A (zh) 进程间数据共享方法、装置及电子设备
US20070240122A1 (en) Method, system and program storage device for providing request trace data in a user mode device interface
CN117076154A (zh) 进程间数据共享方法、装置及电子设备

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 18741857

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 18741857

Country of ref document: EP

Kind code of ref document: A1