Project

General

Profile

Revision b781545c

IDb781545cbe86f50b4a5266115f2ec45f0d5f4316

Added by Georg Kunz over 8 years ago

[master] streamlined templated implementation of locks

View differences:

include/cttaslock.h
20 20
#ifndef CTTASLOCK_H_
21 21
#define CTTASLOCK_H_
22 22

  
23
#include "clock.h"
24

  
25 23
#ifdef ATOMIC_OPS_DEBUG
26 24
#include "catomicopsdebug.h"
27 25
#else
28 26
#include <atomic_ops.h>
29 27
#endif
30 28

  
31
class cTTASLock: public cLock
29
class cTTASLock
32 30
{
33 31
private:
34 32
    AO_t lck;
35 33

  
34
    static const AO_t unlocked = 0;
35
    static const AO_t locked = 1;
36

  
36 37
public:
37 38
    /**
38 39
     * Constructor.
39 40
     */
40
    cTTASLock();
41

  
42
    /**
43
     * Destructor.
44
     */
45
    virtual ~cTTASLock();
41
    cTTASLock()
42
    {
43
        AO_store_write(&lck, 0);
44
    }
46 45

  
47 46
    /*
48 47
     * Locks the lock
49 48
     */
50
    void lock();
49
    inline void lock()
50
    {
51
        //
52
        // very simply TTAS based spinlock. Does not scale with the number of CPUs
53
        // and should thus be replaced by more complex locks.
54
        //
55
        while (true)
56
        {
57
            while (AO_load_full(&lck) == locked)
58
            {
59
                __asm__ ("pause");
60
            }
61
            if (AO_compare_and_swap_full(&lck, unlocked, locked) != 0)
62
            {
63
                return;
64
            }
65
        }
66
    }
51 67

  
52 68
    /*
53 69
     * Unlocks the lock
54 70
     */
55
    void unlock();
71
    inline void unlock()
72
    {
73
        AO_store_full(&lck, unlocked);
74
    }
56 75

  
57 76
};
58 77

  

Also available in: Unified diff