Statistics
| Branch: | Revision:

root / src / sim / cttaslock.cc @ e26d3d25

History | View | Annotate | Download (1.22 KB)

1
//==========================================================================
2
//  CTTASLOCK.CC - part of
3
//
4
//                     Horizon/OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//==========================================================================
8

    
9
/*--------------------------------------------------------------*
10
 Copyright (C) 2011 Simon Tenbusch
11

12
 This file is distributed WITHOUT ANY WARRANTY. See the file
13
 `license' for details on this and other legal matters.
14
 *--------------------------------------------------------------*/
15

    
16
#include "simkerneldefs.h"
17
#include "cttaslock.h"
18

    
19
cTTASLock::cTTASLock()
20
{
21
    AO_store_write(&lck, 0);
22
}
23

    
24
cTTASLock::~cTTASLock()
25
{
26

    
27
}
28

    
29
static const AO_t unlocked = 0;
30
static const AO_t locked = 1;
31

    
32
void cTTASLock::lock()
33
{
34
    //
35
    // very simply TTAS based spinlock. Does not scale with the number of CPUs
36
    // and should thus be replaced by more complex locks.
37
    //
38
    while (true)
39
    {
40
        while (AO_load_full(&lck) == locked)
41
        {
42
            __asm__ ("pause");
43
        }
44
        if (AO_compare_and_swap_full(&lck, unlocked, locked) != 0)
45
        {
46
            return;
47
        }
48
    }
49
}
50

    
51
void cTTASLock::unlock()
52
{
53
    AO_store_full(&lck, unlocked);
54
}
55