Making your code thread-safe

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.

Protecting data

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:

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.).

Tools for protecting data

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.