Navigation


Changeset 25:67ca08d5bc48 in freeDiameter for libfreeDiameter/fifo.c


Ignore:
Timestamp:
Oct 26, 2009, 4:00:49 PM (14 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Completed connection context files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libfreeDiameter/fifo.c

    r21 r25  
    150150        CHECK_POSIX(  pthread_mutex_lock( &q->mtx )  );
    151151       
    152         /* Ok, now invalidate the queue */
    153         q->eyec = 0xdead;
    154        
    155152        if ((q->count != 0) || (q->data != NULL)) {
    156153                TRACE_DEBUG(INFO, "The queue cannot be destroyed (%d, %p)", q->count, q->data);
     
    158155                return EINVAL;
    159156        }
     157       
     158        /* Ok, now invalidate the queue */
     159        q->eyec = 0xdead;
    160160       
    161161        while (q->thrs) {
     
    179179        free(q);
    180180        *queue = NULL;
     181       
     182        return 0;
     183}
     184
     185/* Move the content of old into new, and update loc_update atomically */
     186int fd_fifo_move ( struct fifo ** old, struct fifo * new, struct fifo ** loc_update )
     187{
     188        struct fifo * q;
     189        int loops = 0;
     190       
     191        TRACE_ENTRY("%p %p %p", old, new, loc_update);
     192        CHECK_PARAMS( old && CHECK_FIFO( *old ) && CHECK_FIFO( new ));
     193       
     194        q = *old;
     195        CHECK_PARAMS( ! q->data );
     196        if (new->high) {
     197                TODO("Implement support for thresholds in fd_fifo_move...");
     198        }
     199       
     200        /* Update loc_update */
     201        *old = NULL;
     202        if (loc_update)
     203                *loc_update = new;
     204       
     205        /* Lock the queues */
     206        CHECK_POSIX(  pthread_mutex_lock( &q->mtx )  );
     207        CHECK_POSIX(  pthread_mutex_lock( &new->mtx )  );
     208       
     209        /* Any waiting thread on the old queue returns an error */
     210        q->eyec = 0xdead;
     211        while (q->thrs) {
     212                CHECK_POSIX(  pthread_cond_signal(&q->cond)  );
     213                CHECK_POSIX(  pthread_mutex_unlock( &q->mtx ));
     214                pthread_yield();
     215                CHECK_POSIX(  pthread_mutex_lock( &q->mtx )  );
     216                ASSERT( ++loops < 10 ); /* detect infinite loops */
     217        }
     218       
     219        /* Move all data from old to new */
     220        fd_list_move_end( &new->list, &q->list );
     221        if (q->count && (!new->count)) {
     222                CHECK_POSIX(  pthread_cond_signal(&new->cond)  );
     223        }
     224        new->count += q->count;
     225       
     226        /* Destroy old */
     227        CHECK_POSIX(  pthread_mutex_unlock( &q->mtx )  );
     228        CHECK_POSIX(  pthread_cond_destroy( &q->cond )  );
     229        CHECK_POSIX(  pthread_mutex_destroy( &q->mtx )  );
     230        free(q);
     231       
     232        /* Unlock new, we're done */
     233        CHECK_POSIX(  pthread_mutex_unlock( &new->mtx )  );
    181234       
    182235        return 0;
Note: See TracChangeset for help on using the changeset viewer.