informix
INFORMIX-ESQL/C Programmer's Manual
Using Informix Libraries

Creating a Dynamic Thread Library on UNIX Operating Systems

To create a dynamic thread library, you must define routines for every threaded operation that ESQL/C performs and you must register those functions with ESQL/C. The following list shows all of the functions that a multithreaded ESQL/C application requires and describes what each function must do.

Data Types

You can create typedefs for the data types in the preceding functions to the equivalent data types in your thread package, or you can use the appropriate data type from the thread package instead of the ifxOS_ version. The following list includes all the data types that the preceding functions use:

The following example uses the Solaris Kernel Threads package to demonstrate how to set up a dynamic-thread library. The first task is to define the 17 dynamic-thread functions that the shared and/or static library needs. In this example, the file is called dynthr.c:

Registering the Dynamic Thread Functions

Your ESQL/C application must use the ifxOS_set_thrfunc() function to register the dynamic thread functions with ESQL/C. The following declaration describes the ifxOS_set_thrfunc() function.

The first parameter, func, is a mint that indexes the function being registered. The second parameter is the name of the function that is being registered.

You must call ifxOS_set_thrfunc() once for each of the 17 ifxOS functions listed in Creating a Dynamic Thread Library on UNIX Operating Systems.

The ifxOS_set_thrfunc() function returns 0 if it successfully registers the function and -1 if it fails to register the function. For example, to register the user-defined function my_mutex_lock() as the ifxOS_th_mutex_lock routine, you use the following call:

TH_MUTEX_LOCK is defined in sqlhdr.h and tells the client to call my_mutex_lock() whenever it needs to lock a mutex.

The following list shows the indexes and the functions they register.

Index Function
TH_ONCE ifxOS_th_once
TH_MUTEXATTR_CREATE ifxOS_th_mutexattr_create()
TH_MUTEXATTR_SETKIND ifxOS_th_mutexattr_setkind_np()
TH_MUTEXATTR_DELETE ifxOS_th_mutexattr_delete()
TH_MUTEX_INIT ifxOS_th_mutex_init()
TH_MUTEX_DESTROY ifxOS_th_mutex_destroy()
TH_MUTEX_LOCK ifxOS_th_mutex_lock()
TH_MUTEX_UNLOCK ifxOS_th_mutex_unlock()
TH_MUTEX_TRYLOCK ifxOS_th_mutex_trylock()
TH_CONDATTR_CREATE ifxOS_th_condattr_create()
TH_CONDATTR_DELETE ifxOS_th_condattr_delete()
TH_COND_INIT ifxOS_th_cond_init()
TH_COND_DESTROY ifxOS_th_cond_destroy()
TH_COND_TIMEDWAIT ifxOS_th_cond_timedwait()
TH_KEYCREATE ifxOS_th_keycreate()
TH_GETSPECIFIC ifxOS_th_getspecific()
TH_SETSPECIFIC ifxOS_th_setspecific()

The following function, dynthr_init(), which is also defined in dynthr.c, registers the 17 functions defined in Creating a Dynamic Thread Library on UNIX Operating Systems. FUNCFAIL is defined to be -1.

Setting the $THREADLIB Environment Variable

The following C-shell command sets THREADLIB to specify a user-defined thread package:

Creating the Shared Library

You must compile dynthr.c into a shared or static library. The following example illustrates how to compile a shared or static library on a workstation running the Solaris operating system:

You can also use the $LD_LIBRARY_PATH environment variable:

To compile dynthr.c into a static library, perform the following tasks (on Solaris):

You must update your application, test.ec, to call the dynthr_init() routine first, or none of the thread functions will be registered.

Compiling with the -thread and -l Preprocessor Options

You must compile the application using the -thread and the -l preprocessor options. The -thread option indicates that you are linking thread-safe libraries instead of the default Informix shared libraries. The -l option allows you to specify system libraries that you want to link. Finally, you compile your application, link libdynthr.so and run it, as shown in the following example:


INFORMIX-ESQL/C Programmer's Manual, Version 9.21
Copyright © 1999, Informix Software, Inc. All rights reserved