Static initializers for semaphores
authorBarret Rhoden <brho@cs.berkeley.edu>
Wed, 4 Feb 2015 17:10:00 +0000 (12:10 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Sun, 1 Mar 2015 14:36:01 +0000 (09:36 -0500)
Similar to the SPINLOCK_INITIALIZERS.  I thought it'd be more of a pain, but
since Linux did it, then so can we.

kern/include/kthread.h
kern/src/ktest/pb_ktests.c

index e55a956..c5d90a4 100644 (file)
@@ -55,6 +55,23 @@ struct semaphore {
 #endif
 };
 
+/* omitted elements (the sem debug stuff) are initialized to 0 */
+#define SEMAPHORE_INITIALIZER(name, n)                                         \
+{                                                                              \
+    .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
+       .nr_signals = (n),                                                         \
+    .lock       = SPINLOCK_INITIALIZER,                                        \
+    .irq_okay   = FALSE,                                                       \
+}
+
+#define SEMAPHORE_INITIALIZER_IRQSAVE(name, n)                                 \
+{                                                                              \
+    .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
+       .nr_signals = (n),                                                         \
+    .lock       = SPINLOCK_INITIALIZER_IRQSAVE,                                \
+    .irq_okay   = TRUE,                                                        \
+}
+
 struct cond_var {
        struct semaphore                        sem;
        spinlock_t                                      *lock;          /* usually points to internal_ */
@@ -122,11 +139,13 @@ void __reg_abortable_cv(struct cv_lookup_elm *cle, struct cond_var *cv);
 void dereg_abortable_cv(struct cv_lookup_elm *cle);
 bool should_abort(struct cv_lookup_elm *cle);
 
-/* qlocks are plan9's binary sempahore, which are wrappers around our sems */
+/* qlocks are plan9's binary sempahore, which are wrappers around our sems.
+ * Not sure if they'll need irqsave or normal sems. */
 typedef struct semaphore qlock_t;
 #define qlock_init(x) sem_init((x), 1)
 #define qlock(x) sem_down(x)
 #define qunlock(x) sem_up(x)
 #define canqlock(x) sem_trydown(x)
+#define QLOCK_INITIALIZER(name) SEMAPHORE_INITIALIZER(name, 1)
 
 #endif /* ROS_KERN_KTHREAD_H */
index 11d4dc0..ac1fe8b 100644 (file)
@@ -1337,8 +1337,7 @@ static void __test_up_sem(uint32_t srcid, long a0, long a1, long a2)
 // TODO: Add assertions.
 bool test_kthreads(void)
 {
-       struct semaphore sem;
-       sem_init(&sem, 1);              /* set to 1 to test the unwind */
+       struct semaphore sem = SEMAPHORE_INITIALIZER(sem, 1);
        printk("We're a kthread!  Stacktop is %p.  Testing suspend, etc...\n",
               get_stack_top());
        /* So we have something that will wake us up.  Routine messages won't get