Protecting critical sections

To prevent Open Server from suspending a thread, you can temporarily raise the priority of the thread by calling srv_setpri. Server threads all start at the same priority level, which is represented by the SRV_C_DEFAULTPRI constant defined in ospublic.h. Thread priorities range from SRV_C_LOWPRIORITY to SRV_C_MAXPRIORITY, with SRV_C_DEFAULTPRI in the middle.

Open Server always resumes the executable thread that has the highest priority. If more than one executable thread has the same priority, Open Server resumes the one that became executable first. If you raise the priority of a thread above that of any other thread, Open Server continues to execute the thread until it is no longer executable or its priority is lowered, preventing other threads from executing.

While raising the priority of a thread is an effective way to guarantee that no other thread can interfere during a critical section, it can have a detrimental effect on concurrency. Raising the priority permits a single thread to take over the server. Even the threads that make up the Open Server runtime system are prevented from running if you raise the priority of a thread above SRV_C_DEFAULTPRI. To minimize the effects, delay raising the priority until absolutely necessary, and lower it again as soon as possible. Do not put unnecessary code inside the critical section.