Skip to content
Snippets Groups Projects
notify.c 1.47 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Keep notification lists so dependents can register to be
     * told when something changes.
     *
     * Stuart Levy, slevy@ncsa.uiuc.edu
     * National Center for Supercomputing Applications,
     * University of Illinois 2001.
    
     * This file is part of partiview, released under the
     * Illinois Open Source License; see the file LICENSE.partiview for details.
    
    slevy's avatar
    slevy committed
    #include <stdio.h>
    #include <stdlib.h>
    #include "shmem.h"
    #include "notify.h"
    
    void notify_add( Notify **slot, void (*notefunc)(Notify*, void*src, void*arg),
    				void *source, void *arg ) {
      Notify *not;
      NotifyNode *node;
      if(slot == NULL || notefunc == NULL) return;
      if((not = *slot) == NULL) {
        not = NewN( Notify, 1 );
        not->notifying = 0;
        not->first = NULL;
        *slot = not;
      }
      node = NewN( NotifyNode, 1 );
      node->notefunc = notefunc;
      node->source = source;
      node->arg = arg;
      node->next = not->first;
      not->first = node;
    }
    
    int notify_remove( Notify *not, NotifyFunc notefunc, void *source, void *arg ) {
      NotifyNode *n, **np;
      int count = 0;
    
    slevy's avatar
     
    slevy committed
      if(not == NULL) return 0;
    
    slevy's avatar
    slevy committed
      for(np = &not->first; (n = *np) != NULL; np = &(*np)->next) {
        if(n->notefunc == notefunc &&
    	    n->source == source &&
    	    n->arg == arg) {
    	*np = n->next;
    	Free( n );
    	count++;
        }
      }
      return count;
    }
    
    void notify_all( Notify *not ) {
      NotifyNode *n;
      if(not == NULL || not->notifying) return;
      not->notifying = 1;
      for(n = not->first; n != NULL; n = n->next) {
        (*n->notefunc)(not, n->source, n->arg);
      }
      not->notifying = 0;
    }