Added a spin_on() macro
authorBarret Rhoden <brho@cs.berkeley.edu>
Wed, 28 Apr 2010 02:30:53 +0000 (19:30 -0700)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 3 Nov 2011 00:35:45 +0000 (17:35 -0700)
It will spin while the condition is true, like while (1), but will
eventually time out based on a number of spins, print a warning, and
proceed.  Useful for places you want to wait, but don't want to
deadlock.

kern/include/atomic.h

index 3825236..3dc56c0 100644 (file)
@@ -28,6 +28,18 @@ static inline void write_sequnlock(seqlock_t *lock);
 static inline seq_ctr_t read_seqbegin(seqlock_t *lock);
 static inline bool read_seqretry(seqlock_t *lock, seq_ctr_t ctr);
 
+#define MAX_SPINS 1000000000
+
+/* Will spin for a little while, but not deadlock if it never happens */
+#define spin_on(x)                                                             \
+       for (int i = 0; (x); i++) {                                                \
+               cpu_relax();                                                           \
+               if (i == MAX_SPINS) {                                                  \
+                       printk("Probably timed out/failed.\n");                            \
+                       break;                                                             \
+               }                                                                      \
+       }
+
 /*********************** Checklist stuff **********************/
 typedef struct checklist_mask {
        // only need an uint8_t, but we need the bits[] to be word aligned