npv:_reasonable_ "pedanticage" of the code
[nyanmp.git] / npv / pkt_q / public / code.frag.c
blobce11e63a4fe335d163d71304bf5aee09e5e88dd4
1 STATIC void lock(struct pkt_q_t *this)
3 int r;
5 r = pthread_mutex_lock(&this->mutex);
6 if (r != 0)
7 fatalx("%d:unable to lock packet queue\n", this, r);
9 STATIC void unlock(struct pkt_q_t *this)
11 int r;
13 r = pthread_mutex_unlock(&this->mutex);
14 if (r != 0)
15 fatalx("%d:unable to unlock packet queue\n", this, r);
17 /* actually rotate the pkt ref ptrs */
18 STATIC void deq(struct pkt_q_t *this)
20 if (this->n > 1) {
21 avcodec_pkt_ref_t *save;
23 save = this->q[0];
24 memmove(&this->q[0], &this->q[1], (this->n - 1)
25 * sizeof(this->q[0]));
26 this->q[this->n - 1] = save;
28 this->n--;
30 /* steal the pkt reference */
31 STATIC void enq(struct pkt_q_t *this, avcodec_pkt_ref_t *pr)
33 if (this->n == this->n_max)
34 grow(this);
35 avcodec_pkt_move_ref(this->q[this->n], pr);
36 ++this->n;
38 STATIC void unref_all(struct pkt_q_t *this)
40 u32 pr;
42 pr = 0;
43 loop {
44 if (pr == this->n)
45 break;
46 avcodec_pkt_unref(this->q[pr]);
47 ++pr;
49 this->n = 0;
51 STATIC struct pkt_q_t *new(u8 *msg_hdr)
53 int r;
54 struct pkt_q_t *this;
56 this = malloc(sizeof(*this));
57 if (this == 0)
58 fatal("unable to allocate memory for the %s packet queue\n", msg_hdr);
59 this->q = 0;
60 this->n = 0;
61 this->n_max = 0;
62 this->msg_hdr = strdup(msg_hdr);
63 r = pthread_mutex_init(&this->mutex, 0);
64 if (r != 0)
65 fatal("unable to init the mutex for the %s packet queue\n", msg_hdr);
66 return this;
68 STATIC bool has_eof(struct pkt_q_t *this)
70 if (this->n != 0) {
71 avcodec_pkt_ref_t *last;
73 last = this->q[this->n - 1];
74 if (last->data == 0 && last->sz == 0)
75 return true;
77 return false;