MCPcopy Create free account
hub / github.com/F-Stack/f-stack / ng_dequeue

Function ng_dequeue

lib/ff_ng_base.c:1950–2013  ·  view source on GitHub ↗

* Taking into account the current state of the queue and node, possibly take * the next entry off the queue and return it. Return NULL if there was * nothing we could return, either because there really was nothing there, or * because the node was in a state where it cannot yet process the next item * on the queue. */

Source from the content-addressed store, hash-verified

1948 * on the queue.
1949 */
1950static __inline item_p
1951ng_dequeue(node_p node, int *rw)
1952{
1953 item_p item;
1954 struct ng_queue *ngq = &node->nd_input_queue;
1955
1956 /* This MUST be called with the mutex held. */
1957 mtx_assert(&ngq->q_mtx, MA_OWNED);
1958
1959 /* If there is nothing queued, then just return. */
1960 if (!QUEUE_ACTIVE(ngq)) {
1961 CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; "
1962 "queue flags 0x%lx", __func__,
1963 node->nd_ID, node, ngq->q_flags);
1964 return (NULL);
1965 }
1966
1967 /*
1968 * From here, we can assume there is a head item.
1969 * We need to find out what it is and if it can be dequeued, given
1970 * the current state of the node.
1971 */
1972 if (HEAD_IS_READER(ngq)) {
1973 while (1) {
1974 long t = ngq->q_flags;
1975 if (t & WRITER_ACTIVE) {
1976 /* There is writer, reader can't proceed. */
1977 CTR4(KTR_NET, "%20s: node [%x] (%p) queued "
1978 "reader can't proceed; queue flags 0x%lx",
1979 __func__, node->nd_ID, node, t);
1980 return (NULL);
1981 }
1982 if (atomic_cmpset_acq_int(&ngq->q_flags, t,
1983 t + READER_INCREMENT))
1984 break;
1985 cpu_spinwait();
1986 }
1987 /* We have got reader lock for the node. */
1988 *rw = NGQRW_R;
1989 } else if (atomic_cmpset_acq_int(&ngq->q_flags, OP_PENDING,
1990 OP_PENDING + WRITER_ACTIVE)) {
1991 /* We have got writer lock for the node. */
1992 *rw = NGQRW_W;
1993 } else {
1994 /* There is somebody other, writer can't proceed. */
1995 CTR4(KTR_NET, "%20s: node [%x] (%p) queued writer can't "
1996 "proceed; queue flags 0x%lx", __func__, node->nd_ID, node,
1997 ngq->q_flags);
1998 return (NULL);
1999 }
2000
2001 /*
2002 * Now we dequeue the request (whatever it may be) and correct the
2003 * pending flags and the next and last pointers.
2004 */
2005 item = STAILQ_FIRST(&ngq->queue);
2006 STAILQ_REMOVE_HEAD(&ngq->queue, el_next);
2007 if (STAILQ_EMPTY(&ngq->queue))

Callers 1

ngthreadFunction · 0.70

Calls 1

mtx_assertFunction · 0.85

Tested by

no test coverage detected