[syslinux:lwip] thread: allow marking invalid; allow for static mboxes

syslinux-bot for H. Peter Anvin hpa at zytor.com
Sat Dec 17 21:19:33 PST 2011


Commit-ID:  5b08704e284545c82b4c5d3ff3aaee815107f6ef
Gitweb:     http://www.syslinux.org/commit/5b08704e284545c82b4c5d3ff3aaee815107f6ef
Author:     H. Peter Anvin <hpa at zytor.com>
AuthorDate: Tue, 11 Oct 2011 20:22:08 -0700
Committer:  H. Peter Anvin <hpa at zytor.com>
CommitDate: Tue, 11 Oct 2011 20:22:08 -0700

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 at zytor.com>

---
 core/include/mbox.h           |   33 ++++++++++++++++++++++++++++++++-
 core/include/thread.h         |   21 +++++++++++++++++++++
 core/lwip/src/arch/sys_arch.c |    2 +-
 3 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/core/include/mbox.h b/core/include/mbox.h
index 9b927a0..3c35ce4 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 213fb44..85f2daf 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 b0bc0b6..65be387 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;
 


More information about the Syslinux-commits mailing list