filehash/ffdb_pagepool.c
int
ffdb_pagepool_put_page (ffdb_pagepool_t* pgp, void* mem, 
			unsigned int flags)
{
  ffdb_bkt_t* bp;
  ffdb_bkt_waiter_t* sleeper = 0;

  FFDB_LOCK(pgp->lock);
#ifdef _FFDB_STATISTICS
  pgp->pageput++;
#endif
  
  bp = (ffdb_bkt_t *)((char *)mem - sizeof (ffdb_bkt_t));

  if (!FFDB_FLAG_ISSET(bp->flags, FFDB_PAGE_PINNED)) {
    fprintf (stderr, "ffdb_pagepool_put_page: page %d is not pinned.\n",
	     bp->pgno);
#ifdef _FFDB_STATISTICS
    ffdb_pagepool_stat (pgp);
#endif
    ffdb_dump_stack ();
    abort();
  }

  /**
   * Here is a scary thing, if the flags has DIRTY bit set,
   * but bp->flags does not have the DIRTY bit set,
   * how do we control multiple threads simultaneous writes
   * suggestion: always set flag when you create pages
   */
  if (FFDB_FLAG_ISSET(flags, FFDB_PAGE_DIRTY))
    FFDB_FLAG_SET(bp->flags, FFDB_PAGE_DIRTY);

  /**
   * Derefence the page
   */
  bp->ref--;
  /*
   * I am giving up the ownership
   */
  FFDB_THREAD_NULL(bp->owner);
  
  /** 
   * Now Unpin the page and wake up other threads waiting 
   */
  FFDB_FLAG_CLR(bp->flags, FFDB_PAGE_PINNED);

  if (bp->waiters > 0) {
    sleeper = FFDB_CIRCLEQ_LAST(&bp->wqh);
    sleeper->wakeup = 0xdeafbeaf;
  }

  FFDB_UNLOCK(pgp->lock);

  /* bp->waiters will not be changed until other threads are waken up */
#if 0
  fprintf (stderr, "Signaling page %d free \n", bp->pgno);
#endif
  if (sleeper)
    FFDB_COND_SIGNAL(sleeper->cv);
  return 0;
}
/**
 * Put a page back into the cache so that other threads can access this page
 *
 * 
 * @param pgp cache page pool pointer
 * @param mem user cached page memory will be returned
 * @param flags this flags can be FFDB_PAGE_DIRTY if this page has been modified
 * 
 * @return 0 if this page is successfully be put back into the cache. Otherwise
 * return -1 with an appropriate errno set
 */
Put a page back into the cache so that other threads can access this page

@param pgp cache page pool pointer
@param mem user cached page memory will be returned
@param flags this flags can be FFDB_PAGE_DIRTY if this page has been modified

@return 0 if this page is successfully be put back into the cache. Otherwise
return -1 with an appropriate errno set
Put a page back into the cache so that other threads can access this page @param pgp cache page pool pointer @param mem user cached page memory will be returned @param flags this flags can be FFDB_PAGE_DIRTY if this page has been modified @return 0 if this page is successfully be put back into the cache . Otherwise return - 1 with an appropriate errno set
int
ffdb_pagepool_put_page (ffdb_pagepool_t* pgp, void* mem, 
			unsigned int flags)
{
  ffdb_bkt_t* bp;
  ffdb_bkt_waiter_t* sleeper = 0;
  FFDB_LOCK(pgp->lock);
#ifdef _FFDB_STATISTICS
  pgp->pageput++;
#endif
  bp = (ffdb_bkt_t *)((char *)mem - sizeof (ffdb_bkt_t));
  if (!FFDB_FLAG_ISSET(bp->flags, FFDB_PAGE_PINNED)) {
    fprintf (stderr, "ffdb_pagepool_put_page: page %d is not pinned.\n",
	     bp->pgno);
#ifdef _FFDB_STATISTICS
    ffdb_pagepool_stat (pgp);
#endif
    ffdb_dump_stack ();
    abort();
  }
  if (FFDB_FLAG_ISSET(flags, FFDB_PAGE_DIRTY))
    FFDB_FLAG_SET(bp->flags, FFDB_PAGE_DIRTY);
  bp->ref--;
  FFDB_THREAD_NULL(bp->owner);
  FFDB_FLAG_CLR(bp->flags, FFDB_PAGE_PINNED);
  if (bp->waiters > 0) {
    sleeper = FFDB_CIRCLEQ_LAST(&bp->wqh);
    sleeper->wakeup = 0xdeafbeaf;
  }
  FFDB_UNLOCK(pgp->lock);
#if 0
  fprintf (stderr, "Signaling page %d free \n", bp->pgno);
#endif
  if (sleeper)
    FFDB_COND_SIGNAL(sleeper->cv);
  return 0;
}
int ffdb_pagepool_put_page ( ffdb_pagepool_t * pgp , void * mem , unsigned int flags ) { ffdb_bkt_t * bp ; ffdb_bkt_waiter_t * sleeper = 0 ; FFDB_LOCK ( pgp -> lock ) ; #ifdef _FFDB_STATISTICS pgp -> pageput ++ ; #endif bp = ( ffdb_bkt_t * ) ( ( char * ) mem - sizeof ( ffdb_bkt_t ) ) ; if ( ! FFDB_FLAG_ISSET ( bp -> flags , FFDB_PAGE_PINNED ) ) { fprintf ( stderr , " \n " , bp -> pgno ) ; #ifdef _FFDB_STATISTICS ffdb_pagepool_stat ( pgp ) ; #endif ffdb_dump_stack ( ) ; abort ( ) ; } if ( FFDB_FLAG_ISSET ( flags , FFDB_PAGE_DIRTY ) ) FFDB_FLAG_SET ( bp -> flags , FFDB_PAGE_DIRTY ) ; bp -> ref -- ; FFDB_THREAD_NULL ( bp -> owner ) ; FFDB_FLAG_CLR ( bp -> flags , FFDB_PAGE_PINNED ) ; if ( bp -> waiters > 0 ) { sleeper = FFDB_CIRCLEQ_LAST ( & bp -> wqh ) ; sleeper -> wakeup = 0xdeafbeaf ; } FFDB_UNLOCK ( pgp -> lock ) ; #if 0 fprintf ( stderr , " \n " , bp -> pgno ) ; #endif if ( sleeper ) FFDB_COND_SIGNAL ( sleeper -> cv ) ; return 0 ; }
Put a page back into the cache so that other threads can access this page
Put a page back into the cache so that other threads can access this page
/**
   * Here is a scary thing, if the flags has DIRTY bit set,
   * but bp->flags does not have the DIRTY bit set,
   * how do we control multiple threads simultaneous writes
   * suggestion: always set flag when you create pages
   */ /**
   * Derefence the page
   */ /*
   * I am giving up the ownership
   */ /** 
   * Now Unpin the page and wake up other threads waiting 
   */ /* bp->waiters will not be changed until other threads are waken up */