Open Server uses it’s own implementation of threads with non-preemptive scheduling. EAServer uses native operating system threads with preemptive scheduling. Context switches were predictable in Open Server’s non-preemptive environment. EAServer does not support non-preemptive scheduling. As a result, context switches are unpredictable and managed by the operating system’s thread management facility. You must modify your Open Server application so that when a context switch does occur, your resources (variables, data, and so on) are maintained and not overwritten by another thread.
In a multi-threaded program, it is possible for multiple “threads” of control to be active concurrently. These threads can overwrite each other's data, resulting in unpredictable behavior such as race conditions. Any data shared across threads is vulnerable to race conditions:
Global variables – such as “errno” in C programs or user-defined global variables. Since any thread can access these global variables, one thread may inadvertently overwrite the value of a variable accessed by a different thread.
Static variables – a C function containing a static variable can be executed concurrently by multiple threads, resulting in unpredictable changes to the variable’s contents.
Automatic variables are created on the stack. Since each thread typically has its own stack, these variables are generally immune to context switches.
Consider the following code segment running in a traditional Open Server application:
static int x = 0; x += 100; if (x > 1000) x = 0;
A race condition cannot occur when this code runs because a context switch cannot occur with non-preemptive scheduling. However, when you move to EAServer, the result of the execution of this code is unpredictable.
You can protect the previous code with an Open Server mutex:
static int x = 0; srv_lockmutex(mutex); x += 100; if (x > 1000) x = 0; srv_unlockmutex(mutex);
Identify sections of code that need explicit protection and protect them with any applicable synchronization mechanism such as Open Server mutexes, and test your software (for deadlocks, etc.).
Solaris users may find tools in the SPARCworks/iMPact multithreaded development kit from SunSoft useful in analyzing your code to identify critical sections that need protection, and help test the software after protecting these sections. This kit contains tools such as LockLint, LoopTool, SPARCworks Debugger, and Thread Analyzer.
Copyright © 2005. Sybase Inc. All rights reserved. |
![]() |