Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

spinlock.h

Go to the documentation of this file.
00001 // See the end of this file for license information.
00002 
00003 #ifndef TORSION_SPINLOCK_H
00004 #define TORSION_SPINLOCK_H
00005 
00006 #include "asm.h"
00007 #include "critical.h"
00008 
00009 unsigned const cpu_count = 1;
00010 
00016 
00017 class Spinlock {
00018 protected:
00019   bool locked;
00020 
00021 public:
00022   inline void
00023   init() {
00024     locked = false;
00025   }
00026 
00028   inline void
00029   lock() {
00030     if (cpu_count >= 2) {
00031       enter_critical();
00032       while (atomic_test_and_set(locked) == true);
00033       leave_critical();
00034     }
00035   }
00036 
00039   inline bool
00040   try_lock() {
00041     if (cpu_count >= 2) {
00042       enter_critical();
00043       bool success = atomic_test_and_set(locked);
00044       leave_critical();
00045       return success;
00046     }
00047   }
00048 
00049   inline void
00050   unlock() {
00051     if (cpu_count >= 2) {
00052       enter_critical();
00053       locked = false;
00054       leave_critical();
00055     }
00056   }
00057 };
00058 
00059 #endif
00060 
00061 /* Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman
00062  *
00063  * This program is free software; you can redistribute it and/or modify it
00064  * under the terms of the GNU General Public License as published by the
00065  * Free Software Foundation; either version 2 of the License, or (at your
00066  * option) any later version.
00067  * 
00068  * This program is distributed in the hope that it will be useful, but
00069  * WITHOUT ANY WARRANTY; without even the implied warranty of
00070  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00071  * General Public License for more details (in the COPYING file).
00072  * 
00073  * You should have received a copy of the GNU General Public License along
00074  * with this program; if not, write to the Free Software Foundation, Inc.,
00075  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00076  */

Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman