insq(D3) insq(D3)
NAME
insq - insert a message into a queue
SYNOPSIS
#include <sys/stream.h>
#include <sys/ddi.h>
int insq(queue_t *q, mblk_t *emp, mblk_t *nmp);
Arguments
q Pointer to the queue containing message emp.
emp Pointer to the existing message before which the new
message is to be inserted.
nmp Pointer to the new message to be inserted.
DESCRIPTION
insq inserts a message into a queue. The message to be
inserted, nmp, is placed in the queue pointed to by q,
immediately before the message, emp. If emp is NULL, the new
message is placed at the end of the queue. All flow control
parameters are updated. The service procedure is scheduled to
run unless disabled by a previous call to noenable(D3).
Return Values
If nmp was successfully enqueued, insq returns 1. Otherwise,
insq returns 0.
USAGE
Messages are ordered in the queue based on their priority, as
described in srv(D2). If an attempt is made to insert a
message out of order in the queue, then nmp is not enqueued.
The insertion can fail if there is not enough memory to
allocate the accounting data structures used with messages
whose priority bands are greater than zero.
If emp is non-NULL, it must point to a message in the queue
pointed to by q, or a system panic could result.
Level
Base or Interrupt.
Synchronization Constraints
Does not sleep.
Copyright 1994 Novell, Inc. Page 1
insq(D3) insq(D3)
Driver-defined basic locks, read/write locks, and sleep locks
may be held across calls to this function.
The caller must have the stream frozen [see freezestr(D3)]
when calling this function.
Singlethreaded Example
This routine illustrates the use of insq to insert a message
into the middle of a queue. This routine can be used to strip
all the M_PROTO headers off all messages on a queue. We
traverse the list of messages on the queue, looking for
M_PROTO messages (line 9). When one is found, we remove it
from the queue using rmvq(D3) (line 10). If there is no data
portion to the message (line 11), we free the entire message
using freemsg(D3). Otherwise, for every M_PROTO message block
in the message, we strip the M_PROTO block off using
unlinkb(D3) (line 15) and free the message block using
freeb(D3). When the header has been stripped, the data
portion of the message is inserted back into the queue where
it was originally found (line 19).
1 void
2 striproto(q)
3 queue_t *q;
4 {
5 mblk_t *emp, *nmp, *mp;
6 mp = q->q_first;
7 while (mp) {
8 emp = mp->b_next;
9 if (mp->b_datap->db_type == M_PROTO) {
10 rmvq(q, mp);
11 if (msgdsize(mp) == 0) {
12 freemsg(mp);
13 } else {
14 while (mp->b_datap->db_type == M_PROTO) {
15 nmp = unlinkb(mp);
16 freeb(mp);
17 mp = nmp;
18 }
19 insq(q, emp, mp);
20 }
21 }
22 mp = emp;
23 }
24 }
Copyright 1994 Novell, Inc. Page 2
insq(D3) insq(D3)
Multithreaded Example
This routine illustrates the use of insq to insert a message
into the middle of a queue. This routine can be used to strip
all the M_PROTO headers off all messages on a queue. First,
we freeze the stream (line 7) so the state of the queue does
not change while we are searching it. Then we traverse the
list of messages on the queue, looking for M_PROTO messages
(line 11). When one is found, we remove it from the queue
using rmvq(D3) (line 12). If there is no data portion of the
message (line 13), we free the entire message using
freemsg(D3). Otherwise, for every M_PROTO message block in
the message, we strip the M_PROTO block off using unlinkb(D3)
(line 17) and free the message block using freeb(D3). When
the header has been stripped, the data portion of the message
is inserted back into the queue where it was originally found
(line 21). Finally, when we are done searching the queue,
we unfreeze the stream (line 26).
1 void
2 striproto(q)
3 queue_t *q;
4 {
5 mblk_t *emp, *nmp, *mp;
6 pl_t pl;
7 pl = freezestr(q);
8 strqget(q, QFIRST, 0, &mp);
9 while (mp) {
10 emp = mp->b_next;
11 if (mp->b_datap->db_type == M_PROTO) {
12 rmvq(q, mp);
13 if (msgdsize(mp) == 0) {
14 freemsg(mp);
15 } else {
16 while (mp->b_datap->db_type == M_PROTO) {
17 nmp = unlinkb(mp);
18 freeb(mp);
19 mp = nmp;
20 }
21 insq(q, emp, mp);
22 }
23 }
24 mp = emp;
25 }
26 unfreezestr(q, pl);
27 }
Copyright 1994 Novell, Inc. Page 3
insq(D3) insq(D3)
REFERENCES
freezestr(D3), getq(D3), putbq(D3), putq(D3), rmvq(D3),
srv(D2) unfreezestr(D3),
NOTICES
Portability
All processors
Applicability
ddi: 1, 2, 3, 4, 5, 5mp, 6, 6mp, 7, 7mp
Copyright 1994 Novell, Inc. Page 4