-
Notifications
You must be signed in to change notification settings - Fork 6
/
syn.h
30 lines (22 loc) · 895 Bytes
/
syn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef _SYN_H_
#define _SYN_H_
#include "ws.h"
#ifndef _STRUCT_SBUF_T_
#define _STRUCT_SBUF_T_
/* safe buffer : cycle queue with synchronization */
typedef struct{
int *buf; /* buffer array */
int n; /* maximum number of slots */
int front; /* the index of first item */
int rear; /* the index of last item */
sem_t *mutex; /* protects access to buf exclusively */
sem_t *slots; /* counts available slots */
sem_t *items; /* counts available items */
}sbuf_t;
#endif
void P(sem_t *s); /* wait operation with semaphore s */
void V(sem_t *s); /* signal operation with semaphore s */
void sbuf_init(sbuf_t *sp, int n); /* init buffer */
void sbuf_insert(sbuf_t *sp, int item); /* insert item into buffer */
int sbuf_remove(sbuf_t *sp); /* remove an item from buffer */
#endif