pidentd  3.0.19
About: implementation of the RFC1413 identification server (more or less complete rewrite compared to version 2)
  Fossies Dox: pidentd-3.0.19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

buffer.c
Go to the documentation of this file.
1 /*
2 ** buffer.c - FIFO buffer management routines.
3 **
4 ** Copyright (c) 1997-1998 Peter Eriksson <pen@lysator.liu.se>
5 **
6 ** This program is free software; you can redistribute it and/or
7 ** modify it as you wish - as long as you don't claim that you wrote
8 ** it.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 */
14 
15 #include "config.h"
16 
17 #include <stdlib.h>
18 
19 #include "pidentd.h"
20 
21 
22 
23 int
24 buffer_init(buffer_t *bp, int bsize)
25 {
26  bp->buf = s_malloc(sizeof(void *) * bsize);
27 
28  bp->bsize = bsize;
29  bp->occupied = 0;
30  bp->nextin = 0;
31  bp->nextout = 0;
32 
33  pthread_mutex_init(&bp->mtx, NULL);
34  pthread_cond_init(&bp->more, NULL);
35  pthread_cond_init(&bp->less, NULL);
36 
37  return 0;
38 }
39 
40 
41 
42 void
43 buffer_put(buffer_t *bp, void *item)
44 {
45  pthread_mutex_lock(&bp->mtx);
46 
47  while (bp->occupied >= bp->bsize)
48  pthread_cond_wait(&bp->less, &bp->mtx);
49 
50  bp->buf[bp->nextin++] = item;
51 
52  bp->nextin %= bp->bsize;
53  bp->occupied++;
54 
57 }
58 
59 
60 
61 void *
63 {
64  void *item;
65 
66  pthread_mutex_lock(&bp->mtx);
67  while (bp->occupied <= 0)
68  pthread_cond_wait(&bp->more, &bp->mtx);
69 
70  item = bp->buf[bp->nextout++];
71  bp->nextout %= bp->bsize;
72  bp->occupied--;
73 
76 
77  return item;
78 }
79 
80 
81 
82 void
84 {
88  s_free(bp->buf);
89 }
90 
buffer_get
void * buffer_get(buffer_t *bp)
Definition: buffer.c:62
s_free
void s_free(void *p)
Definition: safeio.c:153
pthread_mutex_init
int pthread_mutex_init(pthread_mutex_t *, pthread_mutexattr_t)
buffer_t
Definition: buffer.h:18
pthread_cond_init
int pthread_cond_init(pthread_cond_t *, pthread_condattr_t)
pidentd.h
pthread_mutex_destroy
int pthread_mutex_destroy(pthread_mutex_t *)
pthread_cond_destroy
int pthread_cond_destroy(pthread_cond_t *)
buffer_t::nextout
int nextout
Definition: buffer.h:24
buffer_init
int buffer_init(buffer_t *bp, int bsize)
Definition: buffer.c:24
buffer_t::occupied
int occupied
Definition: buffer.h:22
s_malloc
void * s_malloc(size_t size)
Definition: safeio.c:132
buffer_put
void buffer_put(buffer_t *bp, void *item)
Definition: buffer.c:43
pthread_cond_wait
int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *)
buffer_destroy
void buffer_destroy(buffer_t *bp)
Definition: buffer.c:83
buffer_t::mtx
pthread_mutex_t mtx
Definition: buffer.h:25
buffer_t::more
pthread_cond_t more
Definition: buffer.h:26
buffer_t::less
pthread_cond_t less
Definition: buffer.h:27
pthread_mutex_unlock
int pthread_mutex_unlock(pthread_mutex_t *)
pthread_mutex_lock
int pthread_mutex_lock(pthread_mutex_t *)
buffer_t::bsize
int bsize
Definition: buffer.h:21
pthread_cond_signal
int pthread_cond_signal(pthread_cond_t *)
buffer_t::buf
void ** buf
Definition: buffer.h:20
buffer_t::nextin
int nextin
Definition: buffer.h:23