Contexts 3 5 1 – Fast Window Switcher Software

broken image


In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point. This allows multiple processes to share a single central processing unit (CPU), and is an essential feature of a multitasking operating system.

Contexts 3 5 3 – Fast Window Switcher Software Context Switching leads to an overhead cost because of TLB flushes, sharing the cache between multiple tasks, running the task scheduler etc. Context switching between two threads of the same process is faster than between two different processes as threads have the same virtual memory maps.

  1. Hide windows program completely through a hotkey or as an icon in system tray. Hide some less used icons from tray. Arrange the windows on taskbar based on your choice. Automatically maximize all new windows or only new IE windows. CD to MP3 Freeware 5.1 Download now. A free and easy-to-use CD ripper software allows you to rip audio CDs.
  2. Audio Switcher is a 100% free, and Open Source application for Windows® Vista, 7, and 8. Audio Switcher makes switching between sound devices trivial. No longer do you have to go into Control Panel or the Windows® Sound options, instead there is an easy to access icon, or even hotkeys. The beautiful part of Audio Switcher is it's simplicity.
  3. And, since Windows 3.1 itself was basically a DOS application, you can install Windows 3.1 in DOSBox and run old 16-bit Windows 3.1 applications, too. Use 32-Bit Windows for 16-bit Software. 16-bit programs no longer function on 64-bit versions of Windows.

The precise meaning of the phrase 'context switch' varies. In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed. A context switch can also occur as the result of an interrupt, such as when a task needs to access disk storage, freeing up CPU time for other tasks. Some operating systems also require a context switch to move between user mode and kernel mode tasks. The process of context switching can have a negative impact on system performance.[1]: 28 

Cost[edit]

Context switches are usually computationally intensive, and much of the design of operating systems is to optimize the use of context switches. Switching from one process to another requires a certain amount of time for doing the administration – saving and loading registers and memory maps, updating various tables and lists, etc. What is actually involved in a context switch depends on the architectures, operating systems, and the number of resources shared (threads that belong to the same process share many resources compared to unrelated non-cooperating processes). For example, in the Linux kernel, context switching involves switching registers, stack pointer (it's typical stack-pointer register), program counter, flushing the translation lookaside buffer (TLB) and loading the page table of the next process to run (unless the old process shares the memory with the new).[2][3] Furthermore, analogous context switching happens between user threads, notably green threads, and is often very lightweight, saving and restoring minimal context. In extreme cases, such as switching between goroutines in Go, a context switch is equivalent to a coroutine yield, which is only marginally more expensive than a subroutine call.

Switching cases[edit]

There are three potential triggers for a context switch:

Multitasking[edit]

Most commonly, within some scheduling scheme, one process must be switched out of the CPU so another process can run. This context switch can be triggered by the process making itself unrunnable, such as by waiting for an I/O or synchronization operation to complete. On a pre-emptive multitasking system, the scheduler may also switch out processes that are still runnable. To prevent other processes from being starved of CPU time, pre-emptive schedulers often configure a timer interrupt to fire when a process exceeds its time slice. This interrupt ensures that the scheduler will gain control to perform a context switch.

Interrupt handling[edit]

Modern architectures are interrupt driven. This means that if the CPU requests data from a disk, for example, it does not need to busy-wait until the read is over; it can issue the request (to the I/O device) and continue with some other task. When the read is over, the CPU can be interrupted (by a hardware in this case, which sends interrupt request to PIC) and presented with the read. For interrupts, a program called an interrupt handler is installed, and it is the interrupt handler that handles the interrupt from the disk.

When an interrupt occurs, the hardware automatically switches a part of the context (at least enough to allow the handler to return to the interrupted code). The handler may save additional context, depending on details of the particular hardware and software designs. Often only a minimal part of the context is changed in order to minimize the amount of time spent handling the interrupt. The kernel does not spawn or schedule a special process to handle interrupts, but instead the handler executes in the (often partial) context established at the beginning of interrupt handling. Once interrupt servicing is complete, the context in effect before the interrupt occurred is restored so that the interrupted process can resume execution in its proper state.

User and kernel mode switching[edit]

When the system transitions between user mode and kernel mode, a context switch is not necessary; a mode transition is not by itself a context switch. However, depending on the operating system, a context switch may also take place at this time.

Steps[edit]

In a switch, the state of the process currently executing must be saved somehow, so that when it is rescheduled, this state can be restored.

The process state includes all the registers that the process may be using, especially the program counter, plus any other operating system specific data that may be necessary. This is usually stored in a data structure called a process control block (PCB) or switchframe.

The PCB might be stored on a per-process stack in kernel memory (as opposed to the user-mode call stack), or there may be some specific operating system-defined data structure for this information. A handle to the PCB is added to a queue of processes that are ready to run, often called the ready queue.

Since the operating system has effectively suspended the execution of one process, it can then switch context by choosing a process from the ready queue and restoring its PCB. In doing so, the program counter from the PCB is loaded, and thus execution can continue in the chosen process. Process and thread priority can influence which process is chosen from the ready queue (i.e., it may be a priority queue).

Example[edit]

Considering a general arithmetic addition operation A = B+1. The instruction is stored in the instruction register and the program counter is incremented. A and B are read from memory and are stored in registers R1, R2 respectively. In this case, B+1 is calculated and written in R1 as the final answer. This operation as there are sequential reads and writes and there's no waits for function calls used, hence no context switch/wait takes place in this case.

However, certain special instructions require system calls that require context switch to wait/sleep processes. A system call handler is used for context switch to kernel mode. A display(data x) function may require data x from the Disk and a device driver in kernel mode, hence the display() function goes to sleep and waits on the READ operation to get the value of x from the disk, causing the program to wait and a wait for function call to the released setting the current statement to go to sleep and wait for the syscall to wake it up. To maintain concurrency however the program needs to re-execute the new value and the sleeping process together again.

Performance[edit]

Context switching itself has a cost in performance, due to running the task scheduler, TLB flushes, and indirectly due to sharing the CPU cache between multiple tasks.[4] Switching between threads of a single process can be faster than between two separate processes, because threads share the same virtual memory maps, so a TLB flush is not necessary.[5]

The time to switch between two separate processes is called the process switching latency.The time to switch between two threads of the same process is called the thread switching latency.The time from when a hardware interrupt is generated to when the interrupt is serviced is called the interrupt latency.

Switching between two processes in a single address space operating system can be faster than switching between two processes in an operating system with private per-process address spaces.[6]

Hardware vs. software[edit]

Context switching can be performed primarily by software or hardware. Some processors, like the Intel 80386 and its successors,[7] have hardware support for context switches, by making use of a special data segment designated the task state segment (TSS). A task switch can be explicitly triggered with a CALL or JMP instruction targeted at a TSS descriptor in the global descriptor table. It can occur implicitly when an interrupt or exception is triggered if there's a task gate in the interrupt descriptor table (IDT). When a task switch occurs the CPU can automatically load the new state from the TSS.

As with other tasks performed in hardware, one would expect this to be rather fast; however, mainstream operating systems, including Windows and Linux,[8] do not use this feature. This is mainly due to two reasons:

  • Hardware context switching does not save all the registers (only general-purpose registers, not floating point registers — although the TS bit is automatically turned on in the CR0control register, resulting in a fault when executing floating-point instructions and giving the OS the opportunity to save and restore the floating-point state as needed).
  • Associated performance issues, e.g., software context switching can be selective and store only those registers that need storing, whereas hardware context switching stores nearly all registers whether they are required or not.

See also[edit]

References[edit]

  1. ^Tanenbaum, Andrew S.; Bos, Herbert (March 20, 2014). Modern Operating Systems (4th ed.). Pearson. ISBN978-0133591620.
  2. ^IA-64 Linux Kernel: Design and Implementation, 4.7 Switching Address Spaces
  3. ^Operating Systems, 5.6 The Context Switch, p. 118
  4. ^Chuanpeng Li; Chen Ding; Kai Shen. 'Quantifying The Cost of Context Switch'(PDF).Cite journal requires |journal= (help)
  5. ^Ulrich Drepper (9 October 2014). 'Memory part 3: Virtual Memory'. LWN.net.
  6. ^D.L. Sims.'Multiple and single address spaces: towards a middle ground'.1993.doi:10.1109/IWOOOS.1993.324906
  7. ^'Context Switch definition'. Linfo.org. Archived from the original on 2010-02-18. Retrieved 2013-09-08.
  8. ^Bovet, Daniel Pierre; Cesati, Marco (2006). Understanding the Linux Kernel, Third Edition. O'Reilly Media. p. 104. ISBN978-0-596-00565-8. Retrieved 2009-11-23.

External links[edit]

  • Context Switching at OSDev.org
  • Context Switch Definition by The Linux Information Project (LINFO)
  • Context Switches from the Microsoft Developer Network (MSDN)
  • General Architecture and Design -Interrupt Handling at FreeBSD.org
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Context_switch&oldid=1044651221'

OpenGL Context Creation is the part of initialization that creates a fully realized OpenGL implementation. You need to go through this process to use OpenGL.

A Note on Platforms

Because OpenGL doesn't exist until you create an OpenGL Context, OpenGL context creation is not governed by the OpenGL Specification. It is instead governed by platform-specific APIs. The following discussion will cover Windows-based initialization. GLX has its initialization functions as well; some of them have analogs in Windows, and some do not. Many of the Windows-specific initialization functions have the 'wgl' prefix affixed to them.

This also assumes you know how to handle the Win32 API at some basic level of competence. You should know what a window handle (HWND) and a device context (DC) are, as well as how to create them. Focus 1 9 14 16. This is not a tutorial on how to create a Window.

Simple Context Creation

This section covers the basics of context creation.

The Window Itself

Software

When you create your HWND, you need to make sure that it has the CS_OWNDC set for its style.

Pixel Format

Each window in MS Windows has a Device Context (DC) associated with it. This object can store something called a Pixel Format. This is a generic structure that describes the properties of the default framebuffer that the OpenGL context you want to create should have.

Setting up the pixel format is non-intuitive. The way you create a pixel format is that you fill out a struct that describes the features you want. Then you give that struct to a function that will return a number that represents the closest match that it can find in the list of supported pixel formats. You then set this number to be the pixel format of the DC.

The struct described above is the PIXELFORMATDESCRIPTOR. A good way to set this up is as follows:

As you can see, many of the fields in the struct are set to 0. Leave them that way. The ones we need to be concerned about, the ones you might want to use, are labled above with comments. There are more flags than are specified in this pixel format; more information on them can be found in the Windows SDK documentation. These will do for now.

Now that we have a PIXELFORMATDESCRIPTOR, we need to convert this into a pixel format number. We do this with the function ChoosePixelFormat. This function takes a device context and PFD struct and returns a pixel format number. If it returns 0, then it could not find a pixel format that matches the description, or the PFD was not filled out correctly.

Once you have the pixel format number, you can set it into the DC with SetPixelFormat. This function takes the DC, the pixel format number, and a PFD struct pointer. Don't get excited about being able to supply the PFD struct; it doesn't read any important information out of it to set the pixel format into the context.

Create the Context

Once you have set pixel format in the DC, creating the context is easy. You call wglCreateContext. This function takes the DC as a parameter and returns a handle to the the OpenGL context (of type HGLRC, for handle to GL Rendering Context).

Before you can use OpenGL, the context you created must be made current. This is done with the wglMakeCurrent function. This takes a DC and the HGLRC context. If there is already a current context, then this function will cause the old context to be replaced with the new. OpenGL functions after this will refer to state in the new context, not the old one. If you pass NULL for the context, then the old one is removed and OpenGL functions will fail (or crash) as though you had never made a context current.

The current context is thread-specific; each thread can have a different context current, and it's dangerous to have the same context current in multiple threads.

Delete the Context

Window

When you create your HWND, you need to make sure that it has the CS_OWNDC set for its style.

Pixel Format

Each window in MS Windows has a Device Context (DC) associated with it. This object can store something called a Pixel Format. This is a generic structure that describes the properties of the default framebuffer that the OpenGL context you want to create should have.

Setting up the pixel format is non-intuitive. The way you create a pixel format is that you fill out a struct that describes the features you want. Then you give that struct to a function that will return a number that represents the closest match that it can find in the list of supported pixel formats. You then set this number to be the pixel format of the DC.

The struct described above is the PIXELFORMATDESCRIPTOR. A good way to set this up is as follows:

As you can see, many of the fields in the struct are set to 0. Leave them that way. The ones we need to be concerned about, the ones you might want to use, are labled above with comments. There are more flags than are specified in this pixel format; more information on them can be found in the Windows SDK documentation. These will do for now.

Now that we have a PIXELFORMATDESCRIPTOR, we need to convert this into a pixel format number. We do this with the function ChoosePixelFormat. This function takes a device context and PFD struct and returns a pixel format number. If it returns 0, then it could not find a pixel format that matches the description, or the PFD was not filled out correctly.

Once you have the pixel format number, you can set it into the DC with SetPixelFormat. This function takes the DC, the pixel format number, and a PFD struct pointer. Don't get excited about being able to supply the PFD struct; it doesn't read any important information out of it to set the pixel format into the context.

Create the Context

Once you have set pixel format in the DC, creating the context is easy. You call wglCreateContext. This function takes the DC as a parameter and returns a handle to the the OpenGL context (of type HGLRC, for handle to GL Rendering Context).

Before you can use OpenGL, the context you created must be made current. This is done with the wglMakeCurrent function. This takes a DC and the HGLRC context. If there is already a current context, then this function will cause the old context to be replaced with the new. OpenGL functions after this will refer to state in the new context, not the old one. If you pass NULL for the context, then the old one is removed and OpenGL functions will fail (or crash) as though you had never made a context current.

The current context is thread-specific; each thread can have a different context current, and it's dangerous to have the same context current in multiple threads.

Delete the Context

Technically not part of creation, but you should know how to delete a context.

The first step is always to make sure that the context you want to delete is not current. Call wglMakeCurrent with NULL for the context.

Now that the context is not current, you can call wglDeleteContext on it.

Proper Context Creation

Unless you are making a very simple application, you should not use the above simple context creation steps. There are a number of WGL extensions that give you greater power and flexibility in creating contexts. But to get access to those extensions, you have to make context creation a bit more complex.

Create a False Context

The key problem is this: the function you use to get WGL extensions is, itself, an OpenGL extension. Thus like any OpenGL function, it requires an OpenGL context to call it. So in order to get the functions we need to create a context, we have to.. create a context. Fortunately, this context does not need to be our final context. All we need to do is create a dummy context to get function pointers, then use those functions directly.

Warning: Unfortunately, Windows does not allow the user to change the pixel format of a window. You get to set it exactly once. Therefore, if you want to use a different pixel format from the one your fake context used (for sRGB or multisample framebuffers, or just different bit-depths of buffers), you must destroy the window entirely and recreate it after we are finished with the dummy context.

A good pixel format to choose for the dummy context is a simple 32-bit RGBA color buffer, with a 24-bit depth buffer and 8-bit stencil, as we did in the above sample PFD. This will usually get a hardware accelerated pixel format.

So, this step means going through the above code to create a context. Make it current as well.

Get WGL Extensions

If you are using an extension loading library, now is the time to call whatever function is required to have it load function pointers of interest. If you are not using an extension loading library, then you will need to do this manually.

There are quite a few extensions of interest for doing advanced context creation. Most of them revolve around pixel format creation, with one notable exception.

Pixel Format Extensions

The PFD struct is a nice way to describe your needs to the OpenGL implementation. But it does have one major flaw; it isn't extensible. Therefore, there is the WGL_ARB_pixel_format extension. This extension defines a new mechanism for getting a pixel format number, one based on providing a list of attributes and values.

To use this, the extension must be defined. Much like WGL_ARB_extensions_string, this one has been around for a long time, and even old implementations will provide it. So if you've gotten this far, it's a good bet that WGL_ARB_pixel_format is implemented too.

There are several new functions in this extension, but the one we are interested in is this one:

wglChoosePixelFormatARB is analogous to ChoosePixelFormat. Instead of taking a fixed PFD struct, it takes a list of attributes and values. Many of these attributes have direct analogs to PFD struct fields, but some of them are new. Also, unlike ChoosePixelFormat, this function can return multiple formats that fill the requested parameters. The order of these is in order from best fits to worst, though what constitutes 'best' is implementation-defined.

In any case, the way it works is fairly simple. Calibre 4 6 0 4. piAttribIList is a list of integer attributes. Every two elements in the list is an attribute/value pair. The attribute '0' represents the end of the list, and it doesn't need a value after it. You can pass NULL if you wish; this function will act as if you passed an empty list.

Similarly, pfAttribFList is a list of floating-point attributes. Every two elements in the list is an attribute/value pair. How do you put the attributes (which are integers) in a float list? Very carefully. You need to static-cast them (if you're using C++) or do other trickery to make C keep the bit-pattern between the integer and float form the same.

The nMaxFormats is the maximum number of formats that will be stored in piFormats. Therefore, piFormats should be a list of at least that many entries. The nNumFormats is a return value, informing you how many entries were stored in the list.

If this function returns FALSE (not GL_FALSE, but the Windows FALSE. Both are just 0, though), then the code failed to find an appropriate pixel format. Despite not finding a pixel format, the piFormats list is left in an undefined state (translation: the implementation is free to change stuff in it even if it failed). If the return value is not FALSE, the function worked and you have pixel format numbers.

Here is an example of this function that should produce a near-equivalent list of pixel formats as our above code:

There are a number of extensions that have added new attributes for this function. The important ones that you might want to use are:

  • WGL_ARB_pixel_format_float: Allows for floating-point framebuffers.
  • WGL_ARB_framebuffer_sRGB: Allows for color buffers to be in sRGB format.
  • WGL_ARB_multisample: Allows for multisampled framebuffers.

Once you have a pixel format number, you can set it just like any pixel format with SetPixelFormat.

Create Context with Attributes

OpenGL 3.0 and above created a deprecation and removal model for getting rid of old, legacy functionality. However, it also created a bit of a problem. In previous OpenGL versions, the new version was a strict superset of the old. Therefore, if you wanted a 1.5 context and got a 2.0 context, that was fine; you just got extra functionality you didn't use. Once the possibility of removing old functionality came into being, that was no longer viable.

Thus, the extension WGL_ARB_create_context was made. It exposes a new function to replace wglCreateContext. Much like wglChoosePixelFormatARB, it adds an extensibility mechanism to the system that makes it possible to extend the options for context creation.

If the fake context does not expose this extension, then you cannot use this section. You must use wglCreateContext as normal.

Iterm 2 git. If it does advertise this extension, then there are a number of features that we can access that would normally not be available:

  • Ensure getting an OpenGL 3.0 or greater context.
  • Creating an OpenGL 3.2 or greater core context, without the compatibility features.
  • Creating a context without a window, for off-screen rendering. This may not actually work.
Legacy Note: Implementations that support GL 3.0 or 3.1, but not 3.2 used a slightly different scheme for context creation than those that support 3.2. Pre 3.2 implementations were required to ask for a GL 3.0 or greater context in order to get one; thus, you had to use the new creation API to get a higher GL version. 3.2 and above do not; they can get backwards-compatible profiles of 3.0 or greater versions (assuming the implementation supports them). Thus, the best way to ensure that you get 3.0 or above is to ask for it with this extension. As more drivers implement GL 3.2, this will become less of an issue.
You can tell the difference by checking the extensions. If WGL_ARB_create_context_profile is defined, then it uses the above method. If it is not, then the only way to get a GL 3.0 or greater context is to use wglCreateContext directly.

The signature for wglCreateContextAttribsARB is as follows:

HGLRC wglCreateContextAttribsARB(HDC hDC, HGLRC hshareContext, const int *attribList);

The attribList works similarly to the one in wglChoosePixelFormatARB. It is a series of attribute/value pairs, with a 0 attribute signaling the end of the list.

You can ask for a specific version of OpenGL by using the two attributes WGL_CONTEXT_MAJOR_VERSION_ARB and WGL_CONTEXT_MINOR_VERSION_ARB. How this is resolved is complicated.

Contexts 3 5 1 – Fast Window Switcher Software Reviews

There are a number of rules that define what version you get back when you ask for a specific version. The rules are complicated, but boil down to two things:

  1. It will always return an OpenGL version equal to or greater than the one you ask for.
  2. It will never return an OpenGL version and profile that does not implement core features that the version you ask for implements.

If the extension WGL_ARB_create_context_profile is defined, then you can also use the WGL_CONTEXT_PROFILE_MASK_ARB to select a core profile (WGL_CONTEXT_CORE_PROFILE_BIT_ARB) or a compatibility profile (WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB). Note that these are bits, so you could ask for both (but it would simply return a compatibility one). The details of what this means merit a longer discussion.

You can also pass a number of flags with the WGL_CONTEXT_FLAGS_ARB. With these, you can ask for a forward compatible context (WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB) and/or a debug context (WGL_CONTEXT_DEBUG_BIT_ARB). A debug context will often implement ARB_debug_output for enhanced error message testing. A forward compatible context must fully remove deprecated features in the version that it returns; you should never actually use this.

The hshareContext is a special field. If you have two GL contexts, and you want them to share objects, then you can use the function wglShareLists. But you have to do this before you create objects in either context. wglCreateContextAttribsARB incorporates this functionality directly into context creation.

Sample Code: Create Render Context, Check GL_VERSION

Contexts 3 5 1 – Fast Window Switcher Software Download

Heres a working program which creates a render context and shows the version number in a messagebox, then shuts down the program:

See Also

References

Contexts 3 5 1 – Fast Window Switcher Software Update

Retrieved from 'http://www.khronos.org/opengl/wiki_opengl/index.php?title=Creating_an_OpenGL_Context_(WGL)&oldid=14723'




broken image