Write threadsafe code

A well-behaved C UDR must be threadsafe.

During execution, an SQL request might travel around the different VP classes. For example, a query starts in the CPU VP, but it might migrate to a user-defined VP to execute a UDR that was registered for that VP class. In turn, the UDR might fetch a smart large object, which would cause the thread to migrate to the AIO VP.

Migrating a thread to a different VP means that the database server must preserve the state of the thread before it migrates the thread. When a client application connects to the database server, the database server creates a thread-control block (TCB) to store thread-state information needed when a thread switches VPs. The TCB includes the following thread-state information:
  • Contents of the VP system registers
  • Program counter, which contains the address of the next instruction to execute.
  • Stack pointer, which points to private memory, called a thread stack

    For more information about use of the thread stack by a UDR, see Manage stack space.

Tip: For more information about the structure and use of the thread-control block, see your Informix® Administrator's Guide.

When a thread migrates from one VP to another, it releases its original VP so this VP can execute other threads. The benefit of releasing the CPU VP outweighs the overhead involved in saving the thread state. Therefore, a C UDR must be able to continue execution without loss of information when it migrates to a different VP.

For a C UDR to successfully migrate among VPs, its code must be threadsafe; that is, it must have the following attributes:
  • Does not perform any dynamic memory allocation with operating-system calls
  • Does not modify global or static data
  • Does not modify other global process-state information
Tip: A parallelizable UDR has additional coding restrictions. For more information, see Creating parallelizable UDRs.