Statistics
| Branch: | Revision:

root / src / sim / cttaslock.cc @ e26d3d25

History | View | Annotate | Download (1.22 KB)

1 93d0d4d3 Simon Tenbusch
//==========================================================================
2
//  CTTASLOCK.CC - part of
3
//
4
//                     Horizon/OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//==========================================================================
8
9
/*--------------------------------------------------------------*
10 e118c57b Simon Tenbusch
 Copyright (C) 2011 Simon Tenbusch
11 93d0d4d3 Simon Tenbusch

12 e118c57b Simon Tenbusch
 This file is distributed WITHOUT ANY WARRANTY. See the file
13
 `license' for details on this and other legal matters.
14 93d0d4d3 Simon Tenbusch
 *--------------------------------------------------------------*/
15
16
#include "simkerneldefs.h"
17
#include "cttaslock.h"
18
19 e118c57b Simon Tenbusch
cTTASLock::cTTASLock()
20
{
21
    AO_store_write(&lck, 0);
22 93d0d4d3 Simon Tenbusch
}
23
24 e118c57b Simon Tenbusch
cTTASLock::~cTTASLock()
25
{
26 93d0d4d3 Simon Tenbusch
27
}
28
29
static const AO_t unlocked = 0;
30 e118c57b Simon Tenbusch
static const AO_t locked = 1;
31 93d0d4d3 Simon Tenbusch
32
void cTTASLock::lock()
33
{
34 e118c57b Simon Tenbusch
    //
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 93d0d4d3 Simon Tenbusch
}
50
51
void cTTASLock::unlock()
52
{
53 e118c57b Simon Tenbusch
    AO_store_full(&lck, unlocked);
54 93d0d4d3 Simon Tenbusch
}