• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * mbox.h
3  *
4  * Simple thread mailbox interface
5  */
6 
7 #ifndef _MBOX_H
8 #define _MBOX_H
9 
10 #include "thread.h"
11 
12 /*
13  * If a mailbox is allocated statically (as a struct mailbox), this
14  * is the number of slots it gets.
15  */
16 #define MAILBOX_STATIC_SIZE	512
17 
18 struct mailbox {
19     struct semaphore prod_sem;	/* Producer semaphore (empty slots) */
20     struct semaphore cons_sem;	/* Consumer semaphore (data slots) */
21     struct semaphore head_sem;	/* Head pointer semaphore */
22     struct semaphore tail_sem;	/* Tail pointer semaphore */
23     void **wrap;		/* Where pointers wrap */
24     void **head;		/* Head pointer */
25     void **tail;		/* Tail pointer */
26 
27     void *data[MAILBOX_STATIC_SIZE]; /* Data array */
28 };
29 
30 /* The number of bytes for an mailbox of size s */
31 #define MBOX_BYTES(s) (sizeof(struct mailbox) + \
32 		       ((s)-MAILBOX_STATIC_SIZE)*sizeof(void *))
33 
34 void mbox_init(struct mailbox *mbox, size_t size);
35 int mbox_post(struct mailbox *mbox, void *msg, mstime_t timeout);
36 mstime_t mbox_fetch(struct mailbox *mbox, void **msg, mstime_t timeout);
37 
38 /*
39  * This marks a mailbox object as unusable; it will remain unusable
40  * until sem_init() is called on it again.  This DOES NOT clear the
41  * list of blocked processes on this mailbox!
42  *
43  * It is also possible to mark the mailbox invalid by zeroing its
44  * memory structure.
45  */
mbox_set_invalid(struct mailbox * mbox)46 static inline void mbox_set_invalid(struct mailbox *mbox)
47 {
48     if (!!mbox)
49 	sem_set_invalid(&mbox->prod_sem);
50 }
51 
52 /*
53  * Ask if a mailbox object has been initialized.
54  */
mbox_is_valid(struct mailbox * mbox)55 static inline bool mbox_is_valid(struct mailbox *mbox)
56 {
57     return ((!!mbox) && sem_is_valid(&mbox->prod_sem));
58 }
59 
60 #endif /* _MBOX_H */
61