summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2011-10-11 20:22:08 -0700
committerH. Peter Anvin <hpa@zytor.com>2011-10-11 20:22:08 -0700
commit5b08704e284545c82b4c5d3ff3aaee815107f6ef (patch)
treecf2c52b60cf9a78bd9f6a6aa0e17be3f56427810
parent8a761944b0f4cf5bf88c9dafa9d8711c115166c2 (diff)
downloadsyslinux-5b08704e284545c82b4c5d3ff3aaee815107f6ef.tar.gz
thread: allow marking invalid; allow for static mboxes
Add an operation to mark a semaphore or a mailbox invalid (need to be reinitialized); and to test whether or not they are initialized. Allow for static mboxes, that is, not using dynamic memory. Both these changes should allow for the use of non-dynamic allocations in lwIP 1.4 and higher. Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r--core/include/mbox.h33
-rw-r--r--core/include/thread.h21
-rw-r--r--core/lwip/src/arch/sys_arch.c2
3 files changed, 54 insertions, 2 deletions
diff --git a/core/include/mbox.h b/core/include/mbox.h
index 9b927a0b..3c35ce4e 100644
--- a/core/include/mbox.h
+++ b/core/include/mbox.h
@@ -9,6 +9,12 @@
#include "thread.h"
+/*
+ * If a mailbox is allocated statically (as a struct mailbox), this
+ * is the number of slots it gets.
+ */
+#define MAILBOX_STATIC_SIZE 512
+
struct mailbox {
struct semaphore prod_sem; /* Producer semaphore (empty slots) */
struct semaphore cons_sem; /* Consumer semaphore (data slots) */
@@ -18,11 +24,36 @@ struct mailbox {
void **head; /* Head pointer */
void **tail; /* Tail pointer */
- void *data[]; /* Data array */
+ void *data[MAILBOX_STATIC_SIZE]; /* Data array */
};
+/* The number of bytes for an mailbox of size s */
+#define MBOX_BYTES(s) (sizeof(struct mailbox) + \
+ ((s)-MAILBOX_STATIC_SIZE)*sizeof(void *))
+
void mbox_init(struct mailbox *mbox, size_t size);
int mbox_post(struct mailbox *mbox, void *msg, mstime_t timeout);
mstime_t mbox_fetch(struct mailbox *mbox, void **msg, mstime_t timeout);
+/*
+ * This marks a mailbox object as unusable; it will remain unusable
+ * until sem_init() is called on it again. This DOES NOT clear the
+ * list of blocked processes on this mailbox!
+ *
+ * It is also possible to mark the mailbox invalid by zeroing its
+ * memory structure.
+ */
+static inline void mbox_set_invalid(struct mailbox *mbox)
+{
+ sem_set_invalid(&mbox->prod_sem);
+}
+
+/*
+ * Ask if a mailbox object has been initialized.
+ */
+static inline bool mbox_is_valid(struct mailbox *mbox)
+{
+ return sem_is_valid(&mbox->prod_sem);
+}
+
#endif /* _MBOX_H */
diff --git a/core/include/thread.h b/core/include/thread.h
index 213fb441..85f2dafc 100644
--- a/core/include/thread.h
+++ b/core/include/thread.h
@@ -80,6 +80,27 @@ mstime_t sem_down(struct semaphore *, mstime_t);
void sem_up(struct semaphore *);
void sem_init(struct semaphore *, int);
+/*
+ * This marks a semaphore object as unusable; it will remain unusable
+ * until sem_init() is called on it again. This DOES NOT clear the
+ * list of blocked processes on this semaphore!
+ *
+ * It is also possible to mark the semaphore invalid by zeroing its
+ * memory structure.
+ */
+static inline void sem_set_invalid(struct semaphore *sem)
+{
+ sem->list.next = NULL;
+}
+
+/*
+ * Ask if a semaphore object has been initialized.
+ */
+static inline bool sem_is_valid(struct semaphore *sem)
+{
+ return !!sem->list.next;
+}
+
struct thread *start_thread(const char *name, size_t stack_size, int prio,
void (*start_func)(void *), void *func_arg);
void __exit_thread(void);
diff --git a/core/lwip/src/arch/sys_arch.c b/core/lwip/src/arch/sys_arch.c
index b0bc0b6f..65be3874 100644
--- a/core/lwip/src/arch/sys_arch.c
+++ b/core/lwip/src/arch/sys_arch.c
@@ -38,7 +38,7 @@ sys_mbox_t sys_mbox_new(int size)
{
struct mailbox *mbox;
- mbox = malloc(sizeof(struct mailbox) + size*sizeof(void *));
+ mbox = malloc(MBOX_BYTES(size));
if (!mbox)
return NULL;