Chapter Contents

Previous

Next
atcoexit

atcoexit



Register Coprocess Cleanup Function


SYNOPSIS
DESCRIPTION
RETURN VALUE
PORTABILITY
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <coproc.h>

int atcoexit(void (*func)(void), coproc_t procid);


DESCRIPTION

The atcoexit function defines a function called during termination of a particular coprocess or of all coprocesses either as the result of a call to coexit or a return from the coprocess' initial function. The func argument should be a function with no arguments returning void . The procid argument specifies the ID of the coprocess whose termination is to be intercepted or 0 to intercept termination of all coprocesses.

atcoexit routines free resources associated with particular coprocesses. These can be library-managed resources, such as load modules or FILEs, because these normally are cleaned up only when the entire program terminates. atcoexit can be called any number of times, and the same routine can be registered more than once, in which case it is called once for each registration.

atcoexit cleanup routines are called in the opposite order of their registration, and they execute as part of the terminating coprocess. They are called after termination of the coprocess' active functions. (Thus, a cleanup routine cannot cause coprocess execution to resume by issuing longjmp .) A cleanup routine can call coexit , which has no effect other than possibly changing the information returned to the cocalling coprocess. In this case, no cleanup routine previously called is called again during termination of the same coprocess.

It is not possible to deregister a function once registered. However, when a load module containing a registered cleanup routine is unloaded using unloadm , the cleanup routine is deregistered automatically.

You can call atcoexit during the termination of a coprocess, but the corresponding function is not called during termination of this coprocess. (Normally, it is called during the termination of other coprocesses to which it applies.)

Note that atexit(func) is equivalent to atcoexit(func, coproc(MAIN)) .


RETURN VALUE

atcoexit returns 0 if successful or a nonzero value if unsuccessful.


PORTABILITY

atcoexit is not portable.


EXAMPLE

This example defines a routine coalloc that can be called to allocate memory belonging to a particular coprocess. An atcoexit cleanup routine is defined to free the memory allocated by each coprocess when it terminates.

#include <stdlib.h>
#include <coproc.h>

struct memelt {    /* header for each block allocated  */
   struct memelt *fwd;
   coproc_t owner;
   double pad [0]; /* force correct alignment          */
};

static int first = 1;
static struct memelt *queue;
static void cleanup(void);

void *coalloc(int amt)
{
   struct memelt *newmem;

   if (first){
      if (atcoexit(cleanup, 0))
         abort();
      else
         first = 0;
   }

   newmem = (struct memelt *) malloc(amt + sizeof(struct memelt));
   if (!newmem) return 0;
   newmem->owner = coproc(SELF);
   newmem->fwd = queue;
   queue = newmem;
   return ((char *) newmem) + sizeof(struct memelt);
}

void cleanup()
{
   coproc_t ending = coproc(SELF);
   struct memelt **prev, *cur;

   for (prev = &queue; cur = *prev;) {
      if (cur->owner == ending) {
         *prev = cur->fwd;
         free(cur);
      }
      else
         prev = &cur->fwd;
   }
}


RELATED FUNCTIONS

blkjmp , atexit


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.