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:
ifxOS_th_mutex_t
This structure defines a mutex object: pthread_mutex_t in DCE and POSIX.
ifxOS_th_mutexattr_t
This structure defines a mutex attributes object called pthread_mutexattr_t in DCE and POSIX. If mutex attribute objects are unsupported in your thread package (for instance, Solaris Kernel Threads), you can assign them a data type of mint.
ifxOS_th_once_t
This structure allows client initialization operations to guarantee mutually exclusive access to the initialization routine, and to guarantee that each initialization is executed only once. This routine has the same functionality as the pthread_once_t structure in DCE and POSIX.
ifxOS_th_condattr_t
This structure defines an object that specifies the attributes of a condition variable: pthread_condattr_t in DCE and POSIX. If this object is unsupported in your thread package (for instance, Solaris Kernel Threads), you can assign it a data type of mint.
ifxOS_th_cond_t
This structure defines a condition variable called pthread_cond_t in DCE and POSIX.
ifxOS_th_timespec_t
This structure defines an absolute time at which the ifxOS_th_cond_timedwait() function times out if a condition variable has not been signaled or broadcast. This structure is timespec_t in DCE and POSIX.
ifxOS_th_key_t
This structure defines a thread-specific data key used in the ifxOS_th_keycreate(), ifxOS_th_setspecific() and ifxOS_getspecific() routines. This structure is pthread_key_t in DCE and POSIX.
ifxOS_th_addr_t
This structure defines an address that contains data to be associated with a thread-specific data key of type ifxOS_th_key_t. The ifxOS_th_addr_t structure is equivalent to pthread_addr_t in DCE. You can specify void * as an alternative that can be used for thread packages (such as POSIX) that do not define such a structure.
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:
/* Prototypes for the dynamic thread functions */
 
mint ifx_th_once(mutex_t *pblock, void (*pfn)(void), mint *init_data);
mint ifx_th_mutexattr_create(mint *mutex_attr);
mint ifx_th_mutexattr_setkind_np(mint *mutex_attr, mint kind);
mint ifx_th_mutexattr_delete(mint *mutex_attr);
mint ifx_th_mutex_init(mutex_t *mutexp, mint mutex_attr);
mint ifx_th_mutex_destroy(mutex_t *mutexp);
mint ifx_th_mutex_lock(mutex_t *mutexp);
mint ifx_th_mutex_trylock(mutex_t *mutexp);
mint ifx_th_mutex_unlock(mutex_t *mutexp);
mint ifx_th_condattr_create(mint *cond_attr);
mint ifx_th_cond_init(cond_t *condp, mint cond_attr);
mint ifx_th_condattr_delete(mint *cond_attr);
mint ifx_th_cond_destroy(cond_t *condp);
mint ifx_th_cond_timedwait(cond_t *sleep_cond, mutex_t *sleep_mutex, 
   timestruc_t *t);
mint ifx_th_keycreate(thread_key_t *allkey, void (*AllDestructor)
   (void *));
mint ifx_th_getspecific(thread_key_t key, void **tcb);
mint ifx_th_setspecific(thread_key_t key, void *tcb);
 
/*
 * The functions . . . *
 *                     */

mint ifx_th_once(mutex_t *pblock, void (*pfn)(void), mint *init_data)
{
   if (!*init_data)
   {
      mutex_lock(pblock);
      if (!*init_data)
      {  
         (*pfn)();
         *init_data = 1;
      }  
      mutex_unlock(pblock);
   }
   return(0);
}

/* Mutex attributes are not supported in solaris kernel threads *
* The functions must be defined anyway, to avoid accessing     *
* a NULL function pointer.                                     */
 
mint ifx_th_mutexattr_create(mint *mutex_attr)
{
   *mutex_attr = 0;
   return(0);
}
 
/* Mutex attributes are not supported in solaris kernel threads */
mint ifx_th_mutexattr_setkind_np(mint *mutex_attr, mint kind)
{
   *mutex_attr = 0;
   return(0);
}
 
/* Mutex attributes are not supported in solaris kernel threads */
mint ifx_th_mutexattr_delete(mint *mutex_attr)
{
   return(0);
}
 
mint ifx_th_mutex_init(mutex_t *mutexp, mint mutex_attr)
{
   return(mutex_init(mutexp, USYNC_THREAD, (void *)NULL));
}
 
mint ifx_th_mutex_destroy(mutex_t *mutexp)
{
   return(mutex_destroy(mutexp));
}
 
mint ifx_th_mutex_lock(mutex_t *mutexp)
{
   return(mutex_lock(mutexp));
}
/* Simulate mutex_trylock using mutex_lock */
mint ifx_th_mutex_trylock(mutex_t *mutexp)
{
   mint ret;
 
   ret = mutex_trylock(mutexp);
   if (ret == 0)
      return(1); /* as per the DCE call */
   if (ret == EBUSY)
      return(0); /* as per the DCE call */
   return(ret);
}
 
mint ifx_th_mutex_unlock(mutex_t *mutexp)
{
   return(mutex_unlock(mutexp));
}
 
/* Condition attributes are not supported in solaris kernel threads */
mint ifx_th_condattr_create(mint *cond_attr)
{
   *cond_attr = 0;
   return(0);
}

mint ifx_th_cond_init(cond_t *condp, mint cond_attr)
{
   return(cond_init(condp, USYNC_THREAD, (void *)NULL));
}
 
mint ifx_th_condattr_delete(int *cond_attr)
{
   return(0);
}
 
mint ifx_th_cond_destroy(cond_t *condp)
{
   return(cond_destroy(condp));
}
 
mint ifx_th_cond_timedwait(cond_t *sleep_cond, mutex_t 
   *sleep_mutex,timestruc_t
   *t)
{
   return(cond_timedwait(sleep_cond, sleep_mutex, t));
}
 
mint ifx_th_keycreate(thread_key_t *allkey, void (*AllDestructor)
   (void *))
{
   return(thr_keycreate(allkey, AllDestructor));
}
 
mint ifx_th_getspecific(thread_key_t key, void **tcb)
{
   return(thr_getspecific(key, tcb));
}
 
mint ifx_th_setspecific(thread_key_t key, void *tcb)
{
   return(thr_setspecific(key, tcb));